diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index e8034bf5d17a2e..1f2ee638fd9557 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -1235,6 +1235,8 @@ // This flag is mainly for tools that understand Go version dependencies. // Users should prefer 'go get toolchain@version'. // +// The -droptoolchain flag removes the toolchain directive from the go.mod file. +// // The -exclude=path@version and -dropexclude=path@version flags // add and drop an exclusion for the given module path and version. // Note that -exclude=path@version is a no-op if that exclusion already exists. @@ -1264,7 +1266,7 @@ // // The -godebug, -dropgodebug, -require, -droprequire, -exclude, -dropexclude, // -replace, -dropreplace, -retract, -dropretract, -tool, -droptool, -ignore, -// and -dropignore editing flags may be repeated, and the changes are applied +// -dropignore, and -droptoolchain editing flags may be repeated, and the changes are applied // in the order given. // // The -print flag prints the final go.mod in its text format instead of @@ -1806,7 +1808,7 @@ // To view the current telemetry mode, run "go telemetry". // To disable telemetry uploading, but keep local data collection, run // "go telemetry local". -// To enable both collection and uploading, run “go telemetry on”. +// To enable both collection and uploading, run "go telemetry on". // To disable both collection and uploading, run "go telemetry off". // // The current telemetry mode is also available as the value of the diff --git a/src/cmd/go/internal/modcmd/edit.go b/src/cmd/go/internal/modcmd/edit.go index f73269378a11e1..a1356bba4c6684 100644 --- a/src/cmd/go/internal/modcmd/edit.go +++ b/src/cmd/go/internal/modcmd/edit.go @@ -66,6 +66,8 @@ The -toolchain=version flag sets the Go toolchain to use. This flag is mainly for tools that understand Go version dependencies. Users should prefer 'go get toolchain@version'. +The -droptoolchain flag removes the toolchain directive from the go.mod file. + The -exclude=path@version and -dropexclude=path@version flags add and drop an exclusion for the given module path and version. Note that -exclude=path@version is a no-op if that exclusion already exists. @@ -95,7 +97,7 @@ for the given path. The -godebug, -dropgodebug, -require, -droprequire, -exclude, -dropexclude, -replace, -dropreplace, -retract, -dropretract, -tool, -droptool, -ignore, -and -dropignore editing flags may be repeated, and the changes are applied +-dropignore, and -droptoolchain editing flags may be repeated, and the changes are applied in the order given. The -print flag prints the final go.mod in its text format instead of @@ -169,13 +171,14 @@ See https://golang.org/ref/mod#go-mod-edit for more about 'go mod edit'. } var ( - editFmt = cmdEdit.Flag.Bool("fmt", false, "") - editGo = cmdEdit.Flag.String("go", "", "") - editToolchain = cmdEdit.Flag.String("toolchain", "", "") - editJSON = cmdEdit.Flag.Bool("json", false, "") - editPrint = cmdEdit.Flag.Bool("print", false, "") - editModule = cmdEdit.Flag.String("module", "", "") - edits []func(*modfile.File) // edits specified in flags + editFmt = cmdEdit.Flag.Bool("fmt", false, "") + editGo = cmdEdit.Flag.String("go", "", "") + editToolchain = cmdEdit.Flag.String("toolchain", "", "") + editJSON = cmdEdit.Flag.Bool("json", false, "") + editPrint = cmdEdit.Flag.Bool("print", false, "") + editModule = cmdEdit.Flag.String("module", "", "") + editDropToolchain = cmdEdit.Flag.Bool("droptoolchain", false, "drop the toolchain directive") + edits []func(*modfile.File) // edits specified in flags ) type flagFunc func(string) @@ -213,6 +216,7 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) { *editJSON || *editPrint || *editFmt || + *editDropToolchain || len(edits) > 0 if !anyFlags { @@ -223,6 +227,10 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) { base.Fatalf("go: cannot use both -json and -print") } + if *editToolchain != "" && *editDropToolchain { + base.Fatalf("go: -toolchain and -droptoolchain are mutually exclusive") + } + if len(args) > 1 { base.Fatalf("go: too many arguments") } @@ -278,6 +286,9 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) { base.Fatalf("go: internal error: %v", err) } } + if *editDropToolchain { + modFile.DropToolchainStmt() + } if len(edits) > 0 { for _, edit := range edits { diff --git a/src/cmd/go/testdata/script/mod_edit_droptoolchain.txt b/src/cmd/go/testdata/script/mod_edit_droptoolchain.txt new file mode 100644 index 00000000000000..e635349551f602 --- /dev/null +++ b/src/cmd/go/testdata/script/mod_edit_droptoolchain.txt @@ -0,0 +1,35 @@ +# Test for go mod edit -droptoolchain + +# Setup +env GO111MODULE=on +env GOFLAGS=-mod=mod + +# Create a new module +! exists go.mod +go mod init example.com/test +exists go.mod + +# Check that the toolchain directive is not present initially +! grep toolchain go.mod + +# Add a toolchain directive +go mod edit -toolchain=go1.21 +grep 'toolchain go1.21' go.mod + +# Remove the toolchain directive using -droptoolchain +go mod edit -droptoolchain +! grep toolchain go.mod + +# Add a toolchain directive again +go mod edit -toolchain=go1.22 +grep 'toolchain go1.22' go.mod + +# Make sure that -toolchain=none still works as before +go mod edit -toolchain=none +! grep toolchain go.mod + +# Add a toolchain directive again and use -droptoolchain with an argument (should be ignored) +go mod edit -toolchain=go1.23 +grep 'toolchain go1.23' go.mod +go mod edit -droptoolchain +! grep toolchain go.mod \ No newline at end of file