Skip to content

Commit 440368d

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/imports: resolve symlinks in ScanDir
We were using the mode reported by ReadDir to decide whether each entry is a file, but in the case of symlinks that isn't sufficient: a symlink could point to either a file or a directory, and if it is a file we should treat it as such. Fixes #28107 Change-Id: Icf6e495dce427a7b1124c9cc9f085e40a215c169 Reviewed-on: https://go-review.googlesource.com/c/141097 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 979d902 commit 440368d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/cmd/go/internal/imports/scan.go

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ func ScanDir(dir string, tags map[string]bool) ([]string, []string, error) {
2222
var files []string
2323
for _, info := range infos {
2424
name := info.Name()
25+
26+
// If the directory entry is a symlink, stat it to obtain the info for the
27+
// link target instead of the link itself.
28+
if info.Mode()&os.ModeSymlink != 0 {
29+
info, err = os.Stat(name)
30+
if err != nil {
31+
continue // Ignore broken symlinks.
32+
}
33+
}
34+
2535
if info.Mode().IsRegular() && !strings.HasPrefix(name, "_") && strings.HasSuffix(name, ".go") && MatchFile(name, tags) {
2636
files = append(files, filepath.Join(dir, name))
2737
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
env GO111MODULE=on
2+
[!symlink] skip
3+
4+
# 'go list' should resolve modules of imported packages.
5+
go list -deps -f '{{.Module}}'
6+
stdout golang.org/x/text
7+
8+
# They should continue to resolve if the importing file is a symlink.
9+
mkdir links
10+
cd links
11+
symlink go.mod -> ../go.mod
12+
symlink issue.go -> ../issue.go
13+
14+
go list -deps -f '{{.Module}}'
15+
stdout golang.org/x/text
16+
17+
-- go.mod --
18+
module golang.org/issue/28107
19+
20+
-- issue.go --
21+
package issue
22+
23+
import _ "golang.org/x/text/language"

0 commit comments

Comments
 (0)