Skip to content

cmd/go: mod edit: add -droptoolchain flag #73776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 19 additions & 8 deletions src/cmd/go/internal/modcmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -213,6 +216,7 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) {
*editJSON ||
*editPrint ||
*editFmt ||
*editDropToolchain ||
len(edits) > 0

if !anyFlags {
Expand All @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions src/cmd/go/testdata/script/mod_edit_droptoolchain.txt
Original file line number Diff line number Diff line change
@@ -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