]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: have go work init use the -workfile flag
authorMichael Matloob <matloob@golang.org>
Mon, 14 Feb 2022 17:05:46 +0000 (12:05 -0500)
committerMichael Matloob <matloob@golang.org>
Mon, 14 Feb 2022 19:02:39 +0000 (19:02 +0000)
Change-Id: Idb4795bde699c919222953ec33fa1083798b2000
Reviewed-on: https://go-review.googlesource.com/c/go/+/385654
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
src/cmd/go/internal/workcmd/init.go
src/cmd/go/testdata/script/work_init_workfile.txt [new file with mode: 0644]

index cefecee8329dea1ec8f8ec56c8eb66942107bbb2..aa3126319a483dc79d7cc454b9e5fd1b09c40359 100644 (file)
@@ -46,7 +46,10 @@ func runInit(ctx context.Context, cmd *base.Command, args []string) {
        // make dirs relative to workFile path before adding the paths to
        // the directory entries
 
-       workFile := filepath.Join(base.Cwd(), "go.work")
+       workFile := modload.WorkFilePath()
+       if workFile == "" {
+               workFile = filepath.Join(base.Cwd(), "go.work")
+       }
 
        modload.CreateWorkFile(ctx, workFile, args)
 }
diff --git a/src/cmd/go/testdata/script/work_init_workfile.txt b/src/cmd/go/testdata/script/work_init_workfile.txt
new file mode 100644 (file)
index 0000000..e6f5671
--- /dev/null
@@ -0,0 +1,15 @@
+# Test that the workfile flag is used by go work init.
+
+go work init
+exists go.work
+
+go work init -workfile=$GOPATH/src/foo/foo.work
+exists foo/foo.work
+
+cd foo/bar
+! go work init
+stderr 'already exists'
+
+# Create directories to make go.work files in.
+-- foo/dummy.txt --
+-- foo/bar/dummy.txt --