Compare commits

...

2 Commits

Author SHA1 Message Date
NARITA fcde8a618e
Merge e693ddf520 into 8552bcf7c2 2025-06-18 15:32:52 -04:00
Rikiya Narita e693ddf520 cmd/go: mod edit: add -droptoolchain flag 2025-05-19 21:56:33 +09:00
3 changed files with 58 additions and 10 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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