cmd/go: make paths consistent between 'go work init' and 'go work use'

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>
This commit is contained in:
Bryan C. Mills 2022-03-03 15:07:15 -05:00 committed by Bryan Mills
parent 9d34fc5108
commit 87a345ca38
6 changed files with 29 additions and 12 deletions

View 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

View 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)
}

View File

@ -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

View 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

View 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

View 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