]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: set go.work path using GOWORK, and remove -workfile flag
authorMichael Matloob <matloob@golang.org>
Tue, 15 Feb 2022 19:27:13 +0000 (14:27 -0500)
committerMichael Matloob <matloob@golang.org>
Wed, 16 Feb 2022 15:58:47 +0000 (15:58 +0000)
This change removes the -workfile flag and allows the go.work file path
to be set using GOWORK (which was previously read-only). This removes
the potential discrepancy and confusion between the flag and environment
variable.

GOWORK will still return the actual path of the go.work file found if it
is set to '' or 'auto'. GOWORK will return 'off' if it is set to 'off'.

For #45713
Fixes #51171

Change-Id: I72eed65d47c63c81433f2b54158d514daeaa1ab3
Reviewed-on: https://go-review.googlesource.com/c/go/+/385995
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>
27 files changed:
doc/go1.18.html
src/cmd/go/alldocs.go
src/cmd/go/internal/base/flag.go
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/envcmd/env.go
src/cmd/go/internal/help/helpdoc.go
src/cmd/go/internal/list/list.go
src/cmd/go/internal/modcmd/download.go
src/cmd/go/internal/modcmd/graph.go
src/cmd/go/internal/modcmd/verify.go
src/cmd/go/internal/modcmd/why.go
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/run/run.go
src/cmd/go/internal/test/testflag.go
src/cmd/go/internal/work/build.go
src/cmd/go/internal/workcmd/edit.go
src/cmd/go/internal/workcmd/init.go
src/cmd/go/internal/workcmd/sync.go
src/cmd/go/internal/workcmd/use.go
src/cmd/go/testdata/script/work.txt
src/cmd/go/testdata/script/work_edit.txt
src/cmd/go/testdata/script/work_env.txt
src/cmd/go/testdata/script/work_gowork.txt [new file with mode: 0644]
src/cmd/go/testdata/script/work_init_gowork.txt [moved from src/cmd/go/testdata/script/work_init_workfile.txt with 52% similarity]
src/cmd/go/testdata/script/work_nowork.txt
src/cmd/go/testdata/script/work_workfile.txt [deleted file]
src/internal/cfg/cfg.go

index 243df2b7d4bb0e328274310717b3b1f99d1f9b98..c75bfe9e5dd17fb179262788ab7b4d34a71b762f 100644 (file)
@@ -345,8 +345,8 @@ Do not send CLs removing the interior tags from such phrases.
 <p><!-- https://golang.org/issue/45713 -->
   The <code>go</code> command now supports a "Workspace" mode. If a
   <code>go.work</code> file is found in the working directory or a
-  parent directory, or one is specified using the <code>-workfile</code>
-  flag, it will put the <code>go</code> command into workspace mode.
+  parent directory, or one is specified using the <code>GOWORK</code>
+  environment variable, it will put the <code>go</code> command into workspace mode.
   In workspace mode, the <code>go.work</code> file will be used to
   determine the set of main modules used as the roots for module
   resolution, instead of using the normally-found <code>go.mod</code>
index 13a3f00d6fca30c2e6b892940da91683d8fc05e1..63e7900e02f473877912fdf4c3aa02c928d78554 100644 (file)
 //             directory, but it is not accessed. When -modfile is specified, an
 //             alternate go.sum file is also used: its path is derived from the
 //             -modfile flag by trimming the ".mod" extension and appending ".sum".
-//     -workfile file
-//             in module aware mode, use the given go.work file as a workspace file.
-//             By default or when -workfile is "auto", the go command searches for a
-//             file named go.work in the current directory and then containing directories
-//             until one is found. If a valid go.work file is found, the modules
-//             specified will collectively be used as the main modules. If -workfile
-//             is "off", or a go.work file is not found in "auto" mode, workspace
-//             mode is disabled.
 //     -overlay file
 //             read a JSON config file that provides an overlay for build operations.
 //             The file is a JSON struct with a single field, named 'Replace', that
 //     GOVCS
 //             Lists version control commands that may be used with matching servers.
 //             See 'go help vcs'.
+//     GOWORK
+//             In module aware mode, use the given go.work file as a workspace file.
+//             By default or when GOWORK is "auto", the go command searches for a
+//             file named go.work in the current directory and then containing directories
+//             until one is found. If a valid go.work file is found, the modules
+//             specified will collectively be used as the main modules. If GOWORK
+//             is "off", or a go.work file is not found in "auto" mode, workspace
+//             mode is disabled.
 //
 // Environment variables for use with cgo:
 //
index 2c72c7e562b7333be43813553321469a555b44a0..120420a1265598f9a6e56084abe314cbc512eebc 100644 (file)
@@ -62,13 +62,6 @@ func AddModFlag(flags *flag.FlagSet) {
        flags.Var(explicitStringFlag{value: &cfg.BuildMod, explicit: &cfg.BuildModExplicit}, "mod", "")
 }
 
-// AddWorkfileFlag adds the workfile flag to the flag set. It enables workspace
-// mode for commands that support it by resetting the cfg.WorkFile variable
-// to "" (equivalent to auto) rather than off.
-func AddWorkfileFlag(flags *flag.FlagSet) {
-       flags.Var(explicitStringFlag{value: &cfg.WorkFile, explicit: &cfg.WorkFileExplicit}, "workfile", "")
-}
-
 // AddModCommonFlags adds the module-related flags common to build commands
 // and 'go mod' subcommands.
 func AddModCommonFlags(flags *flag.FlagSet) {
index 7f68d7bb62839edb9a6d535773e299e49e138181..deab3dddd0dbeaa94e136422d750a8a42bd2c03e 100644 (file)
@@ -49,10 +49,8 @@ var (
        BuildWork              bool // -work flag
        BuildX                 bool // -x flag
 
-       ModCacheRW       bool   // -modcacherw flag
-       ModFile          string // -modfile flag
-       WorkFile         string // -workfile flag
-       WorkFileExplicit bool   // whether -workfile was set explicitly
+       ModCacheRW bool   // -modcacherw flag
+       ModFile    string // -modfile flag
 
        CmdName string // "build", "install", "list", "mod tidy", etc.
 
index e56dd8223f0519e90e9c89cd8e576d5f227becc5..c1adf8cef4ca1b386042ff17c0da3d9245c9cfb8 100644 (file)
@@ -154,6 +154,10 @@ func ExtraEnvVars() []cfg.EnvVar {
        }
        modload.InitWorkfile()
        gowork := modload.WorkFilePath()
+       // As a special case, if a user set off explicitly, report that in GOWORK.
+       if cfg.Getenv("GOWORK") == "off" {
+               gowork = "off"
+       }
        return []cfg.EnvVar{
                {Name: "GOMOD", Value: gomod},
                {Name: "GOWORK", Value: gowork},
index d1eaad1c126716aed21222f160bb1cd0ed6b5caf..28ddaac8f172f62a4fe8368524c57c4946bb1d7b 100644 (file)
@@ -545,6 +545,14 @@ General-purpose environment variables:
        GOVCS
                Lists version control commands that may be used with matching servers.
                See 'go help vcs'.
+       GOWORK
+               In module aware mode, use the given go.work file as a workspace file.
+               By default or when GOWORK is "auto", the go command searches for a
+               file named go.work in the current directory and then containing directories
+               until one is found. If a valid go.work file is found, the modules
+               specified will collectively be used as the main modules. If GOWORK
+               is "off", or a go.work file is not found in "auto" mode, workspace
+               mode is disabled.
 
 Environment variables for use with cgo:
 
index d9a7078ccf29f7aa16d1a9d10e3bf24d486f4495..8be921193525cf78c6cd1296f62d13badf1ee432 100644 (file)
@@ -316,7 +316,6 @@ For more about modules, see https://golang.org/ref/mod.
 func init() {
        CmdList.Run = runList // break init cycle
        work.AddBuildFlags(CmdList, work.DefaultBuildFlags)
-       base.AddWorkfileFlag(&CmdList.Flag)
 }
 
 var (
index 6b8a010fd9d71ca248b91a2ccd7375e3305f432a..5bc6cbc4bb54124547d61dc14dd566f9afb019eb 100644 (file)
@@ -70,7 +70,6 @@ func init() {
        // TODO(jayconrod): https://golang.org/issue/35849 Apply -x to other 'go mod' commands.
        cmdDownload.Flag.BoolVar(&cfg.BuildX, "x", false, "")
        base.AddModCommonFlags(&cmdDownload.Flag)
-       base.AddWorkfileFlag(&cmdDownload.Flag)
 }
 
 type moduleJSON struct {
index 9b6aa1fb14dddc5ade58f888a5bf01f2ba7ed140..9568c6574044399c6813383625bc932f8eaebfdd 100644 (file)
@@ -42,7 +42,6 @@ var (
 func init() {
        cmdGraph.Flag.Var(&graphGo, "go", "")
        base.AddModCommonFlags(&cmdGraph.Flag)
-       base.AddWorkfileFlag(&cmdGraph.Flag)
 }
 
 func runGraph(ctx context.Context, cmd *base.Command, args []string) {
index 3f0c005d5d91482c87d764f155168ed3a579151c..459bf5d070060fa372bf2291699943b06e122ebe 100644 (file)
@@ -39,7 +39,6 @@ See https://golang.org/ref/mod#go-mod-verify for more about 'go mod verify'.
 
 func init() {
        base.AddModCommonFlags(&cmdVerify.Flag)
-       base.AddWorkfileFlag(&cmdVerify.Flag)
 }
 
 func runVerify(ctx context.Context, cmd *base.Command, args []string) {
index d8355cca957a460788b1f7f146679c79bdfe169c..2d3f1eb05bcd56065230ce381e8c062546181260 100644 (file)
@@ -59,7 +59,6 @@ var (
 func init() {
        cmdWhy.Run = runWhy // break init cycle
        base.AddModCommonFlags(&cmdWhy.Flag)
-       base.AddWorkfileFlag(&cmdWhy.Flag)
 }
 
 func runWhy(ctx context.Context, cmd *base.Command, args []string) {
index 523be8c473f97dc3ade1c1aef43af0c2fef24f46..a07066696e8ab7bd4d971ae9950c0843dc3da8c4 100644 (file)
@@ -288,16 +288,16 @@ func BinDir() string {
 // operate in workspace mode. It should not be called by other commands,
 // for example 'go mod tidy', that don't operate in workspace mode.
 func InitWorkfile() {
-       switch cfg.WorkFile {
+       switch gowork := cfg.Getenv("GOWORK"); gowork {
        case "off":
                workFilePath = ""
        case "", "auto":
                workFilePath = findWorkspaceFile(base.Cwd())
        default:
-               if !filepath.IsAbs(cfg.WorkFile) {
-                       base.Fatalf("the path provided to -workfile must be an absolute path")
+               if !filepath.IsAbs(gowork) {
+                       base.Fatalf("the path provided to GOWORK must be an absolute path")
                }
-               workFilePath = cfg.WorkFile
+               workFilePath = gowork
        }
 }
 
@@ -1109,7 +1109,7 @@ func setDefaultBuildMod() {
                if inWorkspaceMode() && cfg.BuildMod != "readonly" {
                        base.Fatalf("go: -mod may only be set to readonly when in workspace mode, but it is set to %q"+
                                "\n\tRemove the -mod flag to use the default readonly value,"+
-                               "\n\tor set -workfile=off to disable workspace mode.", cfg.BuildMod)
+                               "\n\tor set GOWORK=off to disable workspace mode.", cfg.BuildMod)
                }
                // Don't override an explicit '-mod=' argument.
                return
index c4b70b64fe651b6bed5a3b8b5bcc8a6d0d33c0a1..00a3e4b332f781c2d1f1171da7e93bdfb71fef7a 100644 (file)
@@ -65,7 +65,6 @@ func init() {
        CmdRun.Run = runRun // break init loop
 
        work.AddBuildFlags(CmdRun, work.DefaultBuildFlags)
-       base.AddWorkfileFlag(&CmdRun.Flag)
        CmdRun.Flag.Var((*base.StringsFlag)(&work.ExecCmd), "exec", "")
 }
 
index b9d1ec91ff7552aba0e1667d96f16d55349931a5..c046caca25d598bd22e78a8c6b051d217da680ae 100644 (file)
@@ -28,7 +28,6 @@ import (
 
 func init() {
        work.AddBuildFlags(CmdTest, work.OmitVFlag)
-       base.AddWorkfileFlag(&CmdTest.Flag)
 
        cf := CmdTest.Flag
        cf.BoolVar(&testC, "c", false, "")
index 1c278d3d992838549ea170ada4b53ef581244171..0b5848a77da8edcc059b0c9420c6fa6d14e3a568 100644 (file)
@@ -130,14 +130,6 @@ and test commands:
                directory, but it is not accessed. When -modfile is specified, an
                alternate go.sum file is also used: its path is derived from the
                -modfile flag by trimming the ".mod" extension and appending ".sum".
-       -workfile file
-               in module aware mode, use the given go.work file as a workspace file.
-               By default or when -workfile is "auto", the go command searches for a
-               file named go.work in the current directory and then containing directories
-               until one is found. If a valid go.work file is found, the modules
-               specified will collectively be used as the main modules. If -workfile
-               is "off", or a go.work file is not found in "auto" mode, workspace
-               mode is disabled.
        -overlay file
                read a JSON config file that provides an overlay for build operations.
                The file is a JSON struct with a single field, named 'Replace', that
@@ -217,7 +209,6 @@ func init() {
 
        AddBuildFlags(CmdBuild, DefaultBuildFlags)
        AddBuildFlags(CmdInstall, DefaultBuildFlags)
-       base.AddWorkfileFlag(&CmdBuild.Flag)
 }
 
 // Note that flags consulted by other parts of the code
index e7b1b1327163a315e2385a991e317b78ab60d900..05f4f3dddfc49fb0cd35fd7d21a18be876bd7b4a 100644 (file)
@@ -110,8 +110,6 @@ func init() {
        cmdEdit.Flag.Var(flagFunc(flagEditworkDropUse), "dropuse", "")
        cmdEdit.Flag.Var(flagFunc(flagEditworkReplace), "replace", "")
        cmdEdit.Flag.Var(flagFunc(flagEditworkDropReplace), "dropreplace", "")
-
-       base.AddWorkfileFlag(&cmdEdit.Flag)
 }
 
 func runEditwork(ctx context.Context, cmd *base.Command, args []string) {
@@ -137,7 +135,7 @@ func runEditwork(ctx context.Context, cmd *base.Command, args []string) {
        }
 
        if gowork == "" {
-               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using -workfile flag)")
+               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
        }
 
        anyFlags :=
index aa3126319a483dc79d7cc454b9e5fd1b09c40359..63bee6e4f50be99778b61ddb1630f8642435b030 100644 (file)
@@ -33,7 +33,6 @@ current go version will also be listed in the go.work file.
 
 func init() {
        base.AddModCommonFlags(&cmdInit.Flag)
-       base.AddWorkfileFlag(&cmdInit.Flag)
 }
 
 func runInit(ctx context.Context, cmd *base.Command, args []string) {
@@ -41,11 +40,6 @@ func runInit(ctx context.Context, cmd *base.Command, args []string) {
 
        modload.ForceUseModules = true
 
-       // TODO(matloob): support using the -workfile path
-       // To do that properly, we'll have to make the module directories
-       // make dirs relative to workFile path before adding the paths to
-       // the directory entries
-
        workFile := modload.WorkFilePath()
        if workFile == "" {
                workFile = filepath.Join(base.Cwd(), "go.work")
index 948fc5d370d6daf6eabd764c24a86cb6332947d9..b0f61c5fa247c04e203347536b305a61ed0b41a8 100644 (file)
@@ -39,14 +39,13 @@ that in each workspace module.
 
 func init() {
        base.AddModCommonFlags(&cmdSync.Flag)
-       base.AddWorkfileFlag(&cmdSync.Flag)
 }
 
 func runSync(ctx context.Context, cmd *base.Command, args []string) {
        modload.ForceUseModules = true
        modload.InitWorkfile()
        if modload.WorkFilePath() == "" {
-               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using -workfile flag)")
+               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
        }
 
        workGraph := modload.LoadModGraph(ctx, "")
index 3d003b78ebf82dd7bfef1f6bebac39f304504e41..1ee2d4e3c4d81567b7ce6bcb8ad3cdd8c97921a1 100644 (file)
@@ -42,7 +42,6 @@ func init() {
        cmdUse.Run = runUse // break init cycle
 
        base.AddModCommonFlags(&cmdUse.Flag)
-       base.AddWorkfileFlag(&cmdUse.Flag)
 }
 
 func runUse(ctx context.Context, cmd *base.Command, args []string) {
@@ -53,7 +52,7 @@ func runUse(ctx context.Context, cmd *base.Command, args []string) {
        gowork = modload.WorkFilePath()
 
        if gowork == "" {
-               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using -workfile flag)")
+               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
        }
        workFile, err := modload.ReadWorkFile(gowork)
        if err != nil {
index cbb3746a69a9e91fcf2d8abac56c790a583a02dd..a10bf5a1c355dfb5227713be4cb91cf5126cbdda 100644 (file)
@@ -32,7 +32,9 @@ stdout 'example.com/b'
 go list -mod=readonly all
 ! go list -mod=mod all
 stderr '^go: -mod may only be set to readonly when in workspace mode'
-go list -mod=mod -workfile=off all
+env GOWORK=off
+go list -mod=mod all
+env GOWORK=
 
 # Test that duplicates in the use list return an error
 cp go.work go.work.backup
@@ -53,7 +55,9 @@ go run example.com/d
 # This exercises the code that determines which module command-line-arguments
 # belongs to.
 go list ./b/main.go
-go build -n -workfile=off -o foo foo.go
+env GOWORK=off
+go build -n -o foo foo.go
+env GOWORK=
 go build -n -o foo foo.go
 
 -- go.work.dup --
index fd04bbda6e8ffba80a4d748cc82ce446f8cdd6f5..71959ca0dd7e17eb651942afd1de98e08d256de6 100644 (file)
@@ -30,7 +30,8 @@ cmp stdout go.work.want_print
 go work edit -json -go 1.19 -use b -dropuse c -replace 'x.1@v1.4.0 = ../z' -dropreplace x.1 -dropreplace x.1@v1.3.0
 cmp stdout go.work.want_json
 
-go work edit -print -fmt -workfile $GOPATH/src/unformatted
+env GOWORK=$GOPATH/src/unformatted
+go work edit -print -fmt
 cmp stdout formatted
 
 -- m/go.mod --
index ec3d3be3ed7b885e633c53978b81132c41195b22..511bb4e2cb62ea9abb7b995cf2516babe02fa38c 100644 (file)
@@ -13,6 +13,10 @@ cd src
 go env GOWORK
 stdout 'go.work'
 
+env GOWORK='off'
+go env GOWORK
+stdout 'off'
+
 ! go env -w GOWORK=off
 stderr '^go: GOWORK cannot be modified$'
 
diff --git a/src/cmd/go/testdata/script/work_gowork.txt b/src/cmd/go/testdata/script/work_gowork.txt
new file mode 100644 (file)
index 0000000..1cfbf0c
--- /dev/null
@@ -0,0 +1,24 @@
+env GOWORK=stop.work
+! go list a # require absolute path
+! stderr panic
+env GOWORK=doesnotexist
+! go list a
+! stderr panic
+
+env GOWORK=$GOPATH/src/stop.work
+go list -n a
+go build -n a
+go test -n a
+
+-- stop.work --
+go 1.18
+
+use ./a
+-- a/a.go --
+package a
+-- a/a_test.go --
+package a
+-- a/go.mod --
+module a
+
+go 1.18
\ No newline at end of file
similarity index 52%
rename from src/cmd/go/testdata/script/work_init_workfile.txt
rename to src/cmd/go/testdata/script/work_init_gowork.txt
index e6f56716f95532d66a9d0759bc0da0a7c978ec21..55ac99b8c009dc8479215c96447c190018d53a4d 100644 (file)
@@ -1,11 +1,15 @@
-# Test that the workfile flag is used by go work init.
+# Test that the GOWORK environment variable flag is used by go work init.
 
+! exists go.work
 go work init
 exists go.work
 
-go work init -workfile=$GOPATH/src/foo/foo.work
+env GOWORK=$GOPATH/src/foo/foo.work
+! exists foo/foo.work
+go work init
 exists foo/foo.work
 
+env GOWORK=
 cd foo/bar
 ! go work init
 stderr 'already exists'
index b0320cbccb4968371c7f2cd17ef8b71d10d393d1..b4c9b1d9cf3dc927bd8bbfbf3d92a21353e103e2 100644 (file)
@@ -1,17 +1,17 @@
 ! go work use
-stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using GOWORK environment variable\)$'
 
 ! go work use .
-stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using GOWORK environment variable\)$'
 
 ! go work edit
-stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using GOWORK environment variable\)$'
 
 ! go work edit -go=1.18
-stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using GOWORK environment variable\)$'
 
 ! go work sync
-stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using GOWORK environment variable\)$'
 
 -- go.mod --
 module example
diff --git a/src/cmd/go/testdata/script/work_workfile.txt b/src/cmd/go/testdata/script/work_workfile.txt
deleted file mode 100644 (file)
index b629181..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-! go list -workfile=stop.work a # require absolute path
-! stderr panic
-! go list -workfile=doesnotexist a
-! stderr panic
-
-go list -n -workfile=$GOPATH/src/stop.work a
-go build -n -workfile=$GOPATH/src/stop.work a
-go test -n -workfile=$GOPATH/src/stop.work a
-
--- stop.work --
-go 1.18
-
-use ./a
--- a/a.go --
-package a
--- a/a_test.go --
-package a
--- a/go.mod --
-module a
-
-go 1.18
\ No newline at end of file
index 4cb3fbd4f342a4615a8483f352b45974b3a4ec79..78664d7a96ddab14d3baa2cb5b1422c9edcdf535 100644 (file)
@@ -62,6 +62,7 @@ const KnownEnv = `
        GOTOOLDIR
        GOVCS
        GOWASM
+       GOWORK
        GO_EXTLINK_ENABLED
        PKG_CONFIG
 `