Skip to content

Commit 87a345c

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 9d34fc5 commit 87a345c

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

src/cmd/go/internal/modload/modfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ var latestVersionIgnoringRetractionsCache par.Cache // path → queryLatestVersi
802802
// an absolute path or a relative path starting with a '.' or '..'
803803
// path component.
804804
func ToDirectoryPath(path string) string {
805-
if modfile.IsDirectoryPath(path) {
805+
if path == "." || modfile.IsDirectoryPath(path) {
806806
return path
807807
}
808808
// The path is not a relative path or an absolute path, so make it relative

src/cmd/go/internal/workcmd/use.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,5 @@ func pathRel(workDir, dir string) (abs, canonical string) {
186186

187187
// Normalize relative paths to use slashes, so that checked-in go.work
188188
// files with relative paths within the repo are platform-independent.
189-
return abs, filepath.ToSlash(rel)
189+
return abs, modload.ToDirectoryPath(rel)
190190
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Regression test for https://go.dev/issue/51448.
2+
# 'go work init . foo/bar' should produce a go.work file
3+
# with the same paths as 'go work init; go work use -r .'.
4+
5+
go work init . foo/bar
6+
mv go.work go.work.init
7+
8+
go work init
9+
go work use -r .
10+
cmp go.work go.work.init
11+
12+
-- go.mod --
13+
module example
14+
go 1.18
15+
-- foo/bar/go.mod --
16+
module example
17+
go 1.18

src/cmd/go/testdata/script/work_use.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ use (
1414
go 1.18
1515

1616
use (
17-
foo
18-
foo/bar/baz
17+
./foo
18+
./foo/bar/baz
1919
)
2020
-- go.want_work_other --
2121
go 1.18
2222

2323
use (
24-
foo
25-
foo/bar/baz
26-
other
24+
./foo
25+
./foo/bar/baz
26+
./other
2727
)
2828
-- foo/go.mod --
2929
module foo

src/cmd/go/testdata/script/work_use_deleted.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ go 1.18
66

77
use (
88
.
9-
sub
10-
sub/dir/deleted
9+
./sub
10+
./sub/dir/deleted
1111
)
1212
-- go.work.want --
1313
go 1.18
1414

15-
use sub/dir
15+
use ./sub/dir
1616
-- sub/README.txt --
1717
A go.mod file has been deleted from this directory.
1818
In addition, the entire subdirectory sub/dir/deleted

src/cmd/go/testdata/script/work_use_dot.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ grep '^use ["]?'$PWD'["]?$' ../../go.work
3131
# resulting workspace would contain a duplicate module.
3232
cp ../../go.work.orig ../../go.work
3333
! go work use $PWD .
34-
stderr '^go: already added "bar/baz" as "'$PWD'"$'
34+
stderr '^go: already added "\./bar/baz" as "'$PWD'"$'
3535
cmp ../../go.work ../../go.work.orig
3636

3737

@@ -43,7 +43,7 @@ go 1.18
4343
-- go.work.rel --
4444
go 1.18
4545

46-
use bar/baz
46+
use ./bar/baz
4747
-- bar/baz/go.mod --
4848
module example/bar/baz
4949
go 1.18

0 commit comments

Comments
 (0)