aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2018-10-10 10:03:27 -0400
committerBryan C. Mills <bcmills@google.com>2018-11-27 06:27:52 +0000
commit440368da526b69fe9a500e29ce9cd84aa7cc6c35 (patch)
treeb418eb98cbf3fcedeba8f56458a3e7359500e91e /src
parent979d9027aecf265214a6dcc27fe037bfd70355f2 (diff)
downloadgo-440368da526b69fe9a500e29ce9cd84aa7cc6c35.tar.xz
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 <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/go/internal/imports/scan.go10
-rw-r--r--src/cmd/go/testdata/script/mod_symlink.txt23
2 files changed, 33 insertions, 0 deletions
diff --git a/src/cmd/go/internal/imports/scan.go b/src/cmd/go/internal/imports/scan.go
index d944e95724..966a38cfef 100644
--- a/src/cmd/go/internal/imports/scan.go
+++ b/src/cmd/go/internal/imports/scan.go
@@ -22,6 +22,16 @@ func ScanDir(dir string, tags map[string]bool) ([]string, []string, error) {
var files []string
for _, info := range infos {
name := info.Name()
+
+ // If the directory entry is a symlink, stat it to obtain the info for the
+ // link target instead of the link itself.
+ if info.Mode()&os.ModeSymlink != 0 {
+ info, err = os.Stat(name)
+ if err != nil {
+ continue // Ignore broken symlinks.
+ }
+ }
+
if info.Mode().IsRegular() && !strings.HasPrefix(name, "_") && strings.HasSuffix(name, ".go") && MatchFile(name, tags) {
files = append(files, filepath.Join(dir, name))
}
diff --git a/src/cmd/go/testdata/script/mod_symlink.txt b/src/cmd/go/testdata/script/mod_symlink.txt
new file mode 100644
index 0000000000..61da3cc355
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_symlink.txt
@@ -0,0 +1,23 @@
+env GO111MODULE=on
+[!symlink] skip
+
+# 'go list' should resolve modules of imported packages.
+go list -deps -f '{{.Module}}'
+stdout golang.org/x/text
+
+# They should continue to resolve if the importing file is a symlink.
+mkdir links
+cd links
+symlink go.mod -> ../go.mod
+symlink issue.go -> ../issue.go
+
+go list -deps -f '{{.Module}}'
+stdout golang.org/x/text
+
+-- go.mod --
+module golang.org/issue/28107
+
+-- issue.go --
+package issue
+
+import _ "golang.org/x/text/language"