]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: clean paths before using them form index functions
authorMichael Matloob <matloob@golang.org>
Wed, 8 Jun 2022 18:26:44 +0000 (14:26 -0400)
committerMichael Matloob <matloob@golang.org>
Wed, 8 Jun 2022 21:00:49 +0000 (21:00 +0000)
We use str.TrimFilePathPrefix to trim the module root prefix and get the
relative path of each package in the module when scanning the module
and in the RelPath function.  Make sure to clean the path before
indexing and in RelPath to ensure that each path starts with that
prefix, because walk will clean the root path before joining each
subdirectory path to it.

Change-Id: I1dc1eddbd42030eb6d5d8e76a8675f94216447c3
Reviewed-on: https://go-review.googlesource.com/c/go/+/411118
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/modindex/read.go
src/cmd/go/testdata/script/list_replace_absolute_windows.txt [new file with mode: 0644]

index daa85762be00cc3430979614670d03f52125dc77..6ec3a6b3af6e1b42268bc4317ecdb8609a7ba42e 100644 (file)
@@ -112,6 +112,7 @@ func Get(modroot string) (*ModuleIndex, error) {
        if modroot == "" {
                panic("modindex.Get called with empty modroot")
        }
+       modroot = filepath.Clean(modroot)
        isModCache := str.HasFilePathPrefix(modroot, cfg.GOMODCACHE)
        return openIndex(modroot, isModCache)
 }
@@ -217,7 +218,7 @@ func (mi *ModuleIndex) Packages() []string {
 
 // RelPath returns the path relative to the module's root.
 func (mi *ModuleIndex) RelPath(path string) string {
-       return str.TrimFilePathPrefix(path, mi.modroot)
+       return str.TrimFilePathPrefix(filepath.Clean(path), mi.modroot) // mi.modroot is already clean
 }
 
 // ImportPackage is the equivalent of build.Import given the information in ModuleIndex.
diff --git a/src/cmd/go/testdata/script/list_replace_absolute_windows.txt b/src/cmd/go/testdata/script/list_replace_absolute_windows.txt
new file mode 100644 (file)
index 0000000..6f5d737
--- /dev/null
@@ -0,0 +1,37 @@
+# Test a replacement with an absolute path (so the path isn't
+# cleaned by having filepath.Abs called on it). This checks
+# whether the modindex logic cleans the modroot path before using
+# it.
+
+[!windows] [short] skip
+
+go run print_go_mod.go # use this program to write a go.mod with an absolute path
+cp stdout go.mod
+
+go list -modfile=go.mod all
+-- print_go_mod.go --
+//go:build ignore
+package main
+
+import (
+    "fmt"
+    "os"
+)
+
+func main() {
+    work := os.Getenv("WORK")
+fmt.Printf(`module example.com/mod
+
+require b.com v0.0.0
+
+replace b.com => %s\gopath\src/modb
+`,  work)
+}
+-- a.go --
+package a
+
+import _ "b.com/b"
+-- modb/go.mod --
+module b.com
+-- modb/b/b.go --
+package b