// 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
// 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)
}
--- /dev/null
+# 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
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
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
# 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
-- go.work.rel --
go 1.18
-use bar/baz
+use ./bar/baz
-- bar/baz/go.mod --
module example/bar/baz
go 1.18