]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.18] cmd/go: make paths consistent between 'go work init' and...
authorBryan C. Mills <bcmills@google.com>
Thu, 3 Mar 2022 20:07:15 +0000 (15:07 -0500)
committerDmitri Shuralyov <dmitshur@golang.org>
Mon, 7 Mar 2022 16:34:36 +0000 (16:34 +0000)
Fixes #51448

Change-Id: I86719b55037c377eb82154e169d8a9bbae20b77c
Reviewed-on: https://go-review.googlesource.com/c/go/+/389854
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
(cherry picked from commit 87a345ca3849a68de13f29bdc7628f2943734946)
Reviewed-on: https://go-review.googlesource.com/c/go/+/389298
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>

src/cmd/go/internal/modload/modfile.go
src/cmd/go/internal/workcmd/use.go
src/cmd/go/testdata/script/work_init_path.txt [new file with mode: 0644]
src/cmd/go/testdata/script/work_use.txt
src/cmd/go/testdata/script/work_use_deleted.txt
src/cmd/go/testdata/script/work_use_dot.txt

index 627cf1dbc0642ee6056bdfac85a18940093a54c3..75c278a7dfdb5f39dc196f22dd14bf0b21d5c6c2 100644 (file)
@@ -802,7 +802,7 @@ var latestVersionIgnoringRetractionsCache par.Cache // path → queryLatestVersi
 // an absolute path or a relative path starting with a '.' or '..'
 // path component.
 func ToDirectoryPath(path string) string {
-       if modfile.IsDirectoryPath(path) {
+       if path == "." || modfile.IsDirectoryPath(path) {
                return path
        }
        // The path is not a relative path or an absolute path, so make it relative
index 1ee2d4e3c4d81567b7ce6bcb8ad3cdd8c97921a1..9e3bb4ae97859475e077c79ff840b50f0961316c 100644 (file)
@@ -186,5 +186,5 @@ func pathRel(workDir, dir string) (abs, canonical string) {
 
        // Normalize relative paths to use slashes, so that checked-in go.work
        // files with relative paths within the repo are platform-independent.
-       return abs, filepath.ToSlash(rel)
+       return abs, modload.ToDirectoryPath(rel)
 }
diff --git a/src/cmd/go/testdata/script/work_init_path.txt b/src/cmd/go/testdata/script/work_init_path.txt
new file mode 100644 (file)
index 0000000..e397788
--- /dev/null
@@ -0,0 +1,17 @@
+# Regression test for https://go.dev/issue/51448.
+# 'go work init . foo/bar' should produce a go.work file
+# with the same paths as 'go work init; go work use -r .'.
+
+go work init . foo/bar
+mv go.work go.work.init
+
+go work init
+go work use -r .
+cmp go.work go.work.init
+
+-- go.mod --
+module example
+go 1.18
+-- foo/bar/go.mod --
+module example
+go 1.18
index f5ea89c900e2fc160cf8f54f3a68b3dda4317512..12c8cecab74c7e245119ab9fda5a06d8568e93b0 100644 (file)
@@ -14,16 +14,16 @@ use (
 go 1.18
 
 use (
-       foo
-       foo/bar/baz
+       ./foo
+       ./foo/bar/baz
 )
 -- go.want_work_other --
 go 1.18
 
 use (
-       foo
-       foo/bar/baz
-       other
+       ./foo
+       ./foo/bar/baz
+       ./other
 )
 -- foo/go.mod --
 module foo
index 660eb56e2ddf7cbb14562a0e3bd34e3f3015a0a4..b379cbc09d982626cc70b0b1f48e02b60bc1c9d3 100644 (file)
@@ -6,13 +6,13 @@ go 1.18
 
 use (
        .
-       sub
-       sub/dir/deleted
+       ./sub
+       ./sub/dir/deleted
 )
 -- go.work.want --
 go 1.18
 
-use sub/dir
+use ./sub/dir
 -- sub/README.txt --
 A go.mod file has been deleted from this directory.
 In addition, the entire subdirectory sub/dir/deleted
index ccd83d6a61a6c32c5b497733ce5cbf19fa2bb9f7..8f210423ec2ad1f3fb579ccaaab23a90480a204a 100644 (file)
@@ -31,7 +31,7 @@ grep '^use ["]?'$PWD'["]?$' ../../go.work
 # resulting workspace would contain a duplicate module.
 cp ../../go.work.orig ../../go.work
 ! go work use $PWD .
-stderr '^go: already added "bar/baz" as "'$PWD'"$'
+stderr '^go: already added "\./bar/baz" as "'$PWD'"$'
 cmp ../../go.work ../../go.work.orig
 
 
@@ -43,7 +43,7 @@ go 1.18
 -- go.work.rel --
 go 1.18
 
-use bar/baz
+use ./bar/baz
 -- bar/baz/go.mod --
 module example/bar/baz
 go 1.18