Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path/filepath: WalkDir does not treat "." as equivalent to os.Getwd() #50804

Closed
bcmills opened this issue Jan 25, 2022 · 3 comments
Closed

path/filepath: WalkDir does not treat "." as equivalent to os.Getwd() #50804

bcmills opened this issue Jan 25, 2022 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Unfortunate
Milestone

Comments

@bcmills
Copy link
Contributor

bcmills commented Jan 25, 2022

What version of Go are you using (go version)?

go1.18-16d6a5233a

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

linux/amd64 via play.golang.org.

What did you do?

(https://go.dev/play/p/OLEED5lI02l?v=gotip)

With PWD set to a path ending in a symlink to the actual working directory, compare the behavior of filepath.WalkDir with "." and with the directory reported by os.Getwd().

What did you expect to see?

Equivalent behavior for "." and os.Getwd(). Specifically, I expected filepath.WalkDir to report the current working directory as a symlink in both cases.

What did you see instead?

os.Chdir accepts the symlink as valid and changes to the underlying directory. With a matching PWD environment variable, os.Getwd() reports the symlink as the current working directory (as intended since #8400).

filepath.WalkDir(cwd, …) treats the current directory as the symlink itself, and does not list its contents.

However, filepath.WalkDir(".", …) treats "." as the underlying directory and walks its contents.

@bcmills
Copy link
Contributor Author

bcmills commented Jan 25, 2022

This may be as close to “working as intended” as is feasible given #8400. The equivalent C program reports "." as a non-link, although on Linux getcwd also reports the current working directory as a non-link.

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
  size_t n = PATH_MAX;
  char *buf = malloc(n);
  char *cwd = NULL;
  while (!(cwd = getcwd(buf, n))) {
    n *= 2;
    buf = realloc(buf, n);
  }
  printf("cwd is %s\n", cwd);
  free(buf);

  struct stat statbuf;
  memset(&statbuf, 0, sizeof(statbuf));
	if (lstat(".", &statbuf) != 0) {
    perror(NULL);
    return 1;
  }

  if (S_ISLNK(statbuf.st_mode)) {
    printf(". is a link\n");
  } else {
    printf(". is not a link\n");
  }

  if (fflush(stdout) != 0) {
    perror(NULL);
    return 1;
  }

  return 0;
}
~/tmp/link$ gcc main.c && ./a.out
cwd is /usr/local/google/home/bcmills/tmp/dir
. is not a link

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 25, 2022
@mknyszek mknyszek added this to the Go1.18 milestone Jan 25, 2022
@bcmills bcmills modified the milestones: Go1.18, Backlog Jan 25, 2022
@bcmills
Copy link
Contributor Author

bcmills commented Jan 26, 2022

I don't think this can be fixed without breaking compatibility, and — given that it can be worked around with a wrapper that reimplements the first layer of the walk — I don't think it's worth breaking compatibility to fix.

@bcmills bcmills closed this as completed Jan 26, 2022
@bcmills bcmills reopened this Jun 2, 2022
@bcmills bcmills closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2022
@bcmills
Copy link
Contributor Author

bcmills commented Jan 12, 2023

This is closely related to #26033.

@golang golang locked and limited conversation to collaborators Jan 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Unfortunate
Projects
None yet
Development

No branches or pull requests

4 participants
@mknyszek @bcmills @gopherbot and others