Skip to content

Commit f36c7c5

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: traverse module-root symlinks in Walk calls
fsys.Walk, like filepath.Walk, avoids traversing symlinks. Also like filepath.Walk, it follows a symlink at the root if the root path ends in a file separator (consistent with POSIX pathname resolution¹). If the user's working directory is within a repository stored in (and symlinked to) a different filesystem path, we want to follow the symlink instead of treating the module as completely empty. ¹https://pubs.opengroup.org/onlinepubs/9699919799.2013edition/basedefs/V1_chap04.html#tag_04_12 Fixes #50807. Updates #57754. Change-Id: Idaf6168dfffafe879e05b4ded5fda287fcd3eeec Reviewed-on: https://go-review.googlesource.com/c/go/+/463179 Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Russ Cox <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent 5f00ce8 commit f36c7c5

File tree

8 files changed

+83
-25
lines changed

8 files changed

+83
-25
lines changed

src/cmd/go/internal/fsys/fsys.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ func parentIsOverlayFile(name string) (string, bool) {
295295
// return an error.
296296
var errNotDir = errors.New("not a directory")
297297

298+
func nonFileInOverlayError(overlayPath string) error {
299+
return fmt.Errorf("replacement path %q is a directory, not a file", overlayPath)
300+
}
301+
298302
// readDir reads a dir on disk, returning an error that is errNotDir if the dir is not a directory.
299303
// Unfortunately, the error returned by os.ReadDir if dir is not a directory
300304
// can vary depending on the OS (Linux, Mac, Windows return ENOTDIR; BSD returns EINVAL).
@@ -354,18 +358,21 @@ func ReadDir(dir string) ([]fs.FileInfo, error) {
354358
case to.isDeleted():
355359
delete(files, name)
356360
default:
357-
// This is a regular file.
358-
f, err := os.Lstat(to.actualFilePath)
361+
// To keep the data model simple, if the overlay contains a symlink we
362+
// always stat through it (using Stat, not Lstat). That way we don't need
363+
// to worry about the interaction between Lstat and directories: if a
364+
// symlink in the overlay points to a directory, we reject it like an
365+
// ordinary directory.
366+
fi, err := os.Stat(to.actualFilePath)
359367
if err != nil {
360368
files[name] = missingFile(name)
361369
continue
362-
} else if f.IsDir() {
363-
return nil, fmt.Errorf("for overlay of %q to %q: overlay Replace entries can't point to directories",
364-
filepath.Join(dir, name), to.actualFilePath)
370+
} else if fi.IsDir() {
371+
return nil, &fs.PathError{Op: "Stat", Path: filepath.Join(dir, name), Err: nonFileInOverlayError(to.actualFilePath)}
365372
}
366373
// Add a fileinfo for the overlaid file, so that it has
367374
// the original file's name, but the overlaid file's metadata.
368-
files[name] = fakeFile{name, f}
375+
files[name] = fakeFile{name, fi}
369376
}
370377
}
371378
sortedFiles := diskfis[:0]
@@ -541,14 +548,22 @@ func overlayStat(path string, osStat func(string) (fs.FileInfo, error), opName s
541548

542549
switch {
543550
case node.isDeleted():
544-
return nil, &fs.PathError{Op: "lstat", Path: cpath, Err: fs.ErrNotExist}
551+
return nil, &fs.PathError{Op: opName, Path: cpath, Err: fs.ErrNotExist}
545552
case node.isDir():
546553
return fakeDir(filepath.Base(path)), nil
547554
default:
548-
fi, err := osStat(node.actualFilePath)
555+
// To keep the data model simple, if the overlay contains a symlink we
556+
// always stat through it (using Stat, not Lstat). That way we don't need to
557+
// worry about the interaction between Lstat and directories: if a symlink
558+
// in the overlay points to a directory, we reject it like an ordinary
559+
// directory.
560+
fi, err := os.Stat(node.actualFilePath)
549561
if err != nil {
550562
return nil, err
551563
}
564+
if fi.IsDir() {
565+
return nil, &fs.PathError{Op: opName, Path: cpath, Err: nonFileInOverlayError(node.actualFilePath)}
566+
}
552567
return fakeFile{name: filepath.Base(path), real: fi}, nil
553568
}
554569
}

src/cmd/go/internal/fsys/fsys_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ func TestWalkSymlink(t *testing.T) {
827827
testenv.MustHaveSymlink(t)
828828

829829
initOverlay(t, `{
830-
"Replace": {"overlay_symlink": "symlink"}
830+
"Replace": {"overlay_symlink/file": "symlink/file"}
831831
}
832832
-- dir/file --`)
833833

@@ -841,18 +841,23 @@ func TestWalkSymlink(t *testing.T) {
841841
dir string
842842
wantFiles []string
843843
}{
844-
{"control", "dir", []string{"dir", "dir" + string(filepath.Separator) + "file"}},
844+
{"control", "dir", []string{"dir", filepath.Join("dir", "file")}},
845845
// ensure Walk doesn't walk into the directory pointed to by the symlink
846846
// (because it's supposed to use Lstat instead of Stat).
847847
{"symlink_to_dir", "symlink", []string{"symlink"}},
848-
{"overlay_to_symlink_to_dir", "overlay_symlink", []string{"overlay_symlink"}},
848+
{"overlay_to_symlink_to_dir", "overlay_symlink", []string{"overlay_symlink", filepath.Join("overlay_symlink", "file")}},
849+
850+
// However, adding filepath.Separator should cause the link to be resolved.
851+
{"symlink_with_slash", "symlink" + string(filepath.Separator), []string{"symlink" + string(filepath.Separator), filepath.Join("symlink", "file")}},
852+
{"overlay_to_symlink_to_dir", "overlay_symlink" + string(filepath.Separator), []string{"overlay_symlink" + string(filepath.Separator), filepath.Join("overlay_symlink", "file")}},
849853
}
850854

851855
for _, tc := range testCases {
852856
t.Run(tc.name, func(t *testing.T) {
853857
var got []string
854858

855859
err := Walk(tc.dir, func(path string, info fs.FileInfo, err error) error {
860+
t.Logf("walk %q", path)
856861
got = append(got, path)
857862
if err != nil {
858863
t.Errorf("walkfn: got non nil err argument: %v, want nil err argument", err)

src/cmd/go/internal/modindex/scan.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import (
2323
// moduleWalkErr returns filepath.SkipDir if the directory isn't relevant
2424
// when indexing a module or generating a filehash, ErrNotIndexed,
2525
// if the module shouldn't be indexed, and nil otherwise.
26-
func moduleWalkErr(modroot string, path string, info fs.FileInfo, err error) error {
26+
func moduleWalkErr(root string, path string, info fs.FileInfo, err error) error {
2727
if err != nil {
2828
return ErrNotIndexed
2929
}
3030
// stop at module boundaries
31-
if info.IsDir() && path != modroot {
31+
if info.IsDir() && path != root {
3232
if fi, err := fsys.Stat(filepath.Join(path, "go.mod")); err == nil && !fi.IsDir() {
3333
return filepath.SkipDir
3434
}
@@ -52,18 +52,23 @@ func moduleWalkErr(modroot string, path string, info fs.FileInfo, err error) err
5252
func indexModule(modroot string) ([]byte, error) {
5353
fsys.Trace("indexModule", modroot)
5454
var packages []*rawPackage
55-
err := fsys.Walk(modroot, func(path string, info fs.FileInfo, err error) error {
56-
if err := moduleWalkErr(modroot, path, info, err); err != nil {
55+
56+
// If the root itself is a symlink to a directory,
57+
// we want to follow it (see https://go.dev/issue/50807).
58+
// Add a trailing separator to force that to happen.
59+
root := str.WithFilePathSeparator(modroot)
60+
err := fsys.Walk(root, func(path string, info fs.FileInfo, err error) error {
61+
if err := moduleWalkErr(root, path, info, err); err != nil {
5762
return err
5863
}
5964

6065
if !info.IsDir() {
6166
return nil
6267
}
63-
if !str.HasFilePathPrefix(path, modroot) {
68+
if !strings.HasPrefix(path, root) {
6469
panic(fmt.Errorf("path %v in walk doesn't have modroot %v as prefix", path, modroot))
6570
}
66-
rel := str.TrimFilePathPrefix(path, modroot)
71+
rel := path[len(root):]
6772
packages = append(packages, importRaw(modroot, rel))
6873
return nil
6974
})

src/cmd/go/internal/modload/search.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"cmd/go/internal/modindex"
2424
"cmd/go/internal/par"
2525
"cmd/go/internal/search"
26+
"cmd/go/internal/str"
2627
"cmd/go/internal/trace"
2728
"cmd/internal/pkgpattern"
2829

@@ -77,7 +78,10 @@ func matchPackages(ctx context.Context, m *search.Match, tags map[string]bool, f
7778
_, span := trace.StartSpan(ctx, "walkPkgs "+root)
7879
defer span.Done()
7980

80-
root = filepath.Clean(root)
81+
// If the root itself is a symlink to a directory,
82+
// we want to follow it (see https://go.dev/issue/50807).
83+
// Add a trailing separator to force that to happen.
84+
root = str.WithFilePathSeparator(filepath.Clean(root))
8185
err := fsys.Walk(root, func(pkgDir string, fi fs.FileInfo, err error) error {
8286
if err != nil {
8387
m.AddError(err)
@@ -100,9 +104,7 @@ func matchPackages(ctx context.Context, m *search.Match, tags map[string]bool, f
100104
}
101105
}
102106

103-
rel := strings.TrimPrefix(filepath.ToSlash(pkgDir[len(root):]), "/")
104-
name := path.Join(importPathRoot, rel)
105-
107+
name := path.Join(importPathRoot, filepath.ToSlash(pkgDir[len(root):]))
106108
if !treeCanMatch(name) {
107109
want = false
108110
}

src/cmd/go/internal/search/search.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,16 @@ func (m *Match) MatchPackages() {
125125
if (m.pattern == "std" || m.pattern == "cmd") && src != cfg.GOROOTsrc {
126126
continue
127127
}
128+
129+
// If the root itself is a symlink to a directory,
130+
// we want to follow it (see https://go.dev/issue/50807).
131+
// Add a trailing separator to force that to happen.
128132
src = str.WithFilePathSeparator(filepath.Clean(src))
129133
root := src
130134
if m.pattern == "cmd" {
131135
root += "cmd" + string(filepath.Separator)
132136
}
137+
133138
err := fsys.Walk(root, func(path string, fi fs.FileInfo, err error) error {
134139
if err != nil {
135140
return err // Likely a permission error, which could interfere with matching.
@@ -269,6 +274,10 @@ func (m *Match) MatchDirs(modRoots []string) {
269274
}
270275
}
271276

277+
// If dir is actually a symlink to a directory,
278+
// we want to follow it (see https://go.dev/issue/50807).
279+
// Add a trailing separator to force that to happen.
280+
dir = str.WithFilePathSeparator(dir)
272281
err := fsys.Walk(dir, func(path string, fi fs.FileInfo, err error) error {
273282
if err != nil {
274283
return err // Likely a permission error, which could interfere with matching.

src/cmd/go/internal/workcmd/use.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ func runUse(ctx context.Context, cmd *base.Command, args []string) {
131131
}
132132

133133
// Add or remove entries for any subdirectories that still exist.
134-
fsys.Walk(useDir, func(path string, info fs.FileInfo, err error) error {
134+
// If the root itself is a symlink to a directory,
135+
// we want to follow it (see https://go.dev/issue/50807).
136+
// Add a trailing separator to force that to happen.
137+
fsys.Walk(str.WithFilePathSeparator(useDir), func(path string, info fs.FileInfo, err error) error {
135138
if err != nil {
136139
return err
137140
}

src/cmd/go/testdata/script/list_goroot_symlink.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ stdout '^encoding/binary: '$WORK${/}lib${/}goroot${/}src${/}encoding${/}binary'$
5555
# So we check such a pattern to confirm that it works and reports a path relative
5656
# to $GOROOT/src (and not the symlink target).
5757

58-
# BUG(#50807): This should report encoding/binary, not "matched no packages".
5958
exec $WORK/lib/goroot/bin/go list -f '{{.ImportPath}}: {{.Dir}}' .../binary
60-
! stdout .
61-
stderr '^go: warning: "\.\.\./binary" matched no packages$'
59+
stdout '^encoding/binary: '$WORK${/}lib${/}goroot${/}src${/}encoding${/}binary'$'
60+
! stderr .
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[!symlink] skip
2+
3+
symlink $WORK/gopath/src/sym -> $WORK/gopath/src/tree
4+
symlink $WORK/gopath/src/tree/squirrel -> $WORK/gopath/src/dir2 # this symlink should not be followed
5+
cd sym
6+
go list ./...
7+
cmp stdout $WORK/gopath/src/want_list.txt
8+
-- tree/go.mod --
9+
module example.com/tree
10+
11+
go 1.20
12+
-- tree/tree.go --
13+
package tree
14+
-- tree/branch/branch.go --
15+
package branch
16+
-- dir2/squirrel.go --
17+
package squirrel
18+
-- want_list.txt --
19+
example.com/tree
20+
example.com/tree/branch

0 commit comments

Comments
 (0)