Skip to content

Commit e693ddf

Browse files
cmd/go: mod edit: add -droptoolchain flag
1 parent da9c5b1 commit e693ddf

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

src/cmd/go/alldocs.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/modcmd/edit.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ The -toolchain=version flag sets the Go toolchain to use.
6666
This flag is mainly for tools that understand Go version dependencies.
6767
Users should prefer 'go get toolchain@version'.
6868
69+
The -droptoolchain flag removes the toolchain directive from the go.mod file.
70+
6971
The -exclude=path@version and -dropexclude=path@version flags
7072
add and drop an exclusion for the given module path and version.
7173
Note that -exclude=path@version is a no-op if that exclusion already exists.
@@ -95,7 +97,7 @@ for the given path.
9597
9698
The -godebug, -dropgodebug, -require, -droprequire, -exclude, -dropexclude,
9799
-replace, -dropreplace, -retract, -dropretract, -tool, -droptool, -ignore,
98-
and -dropignore editing flags may be repeated, and the changes are applied
100+
-dropignore, and -droptoolchain editing flags may be repeated, and the changes are applied
99101
in the order given.
100102
101103
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'.
169171
}
170172

171173
var (
172-
editFmt = cmdEdit.Flag.Bool("fmt", false, "")
173-
editGo = cmdEdit.Flag.String("go", "", "")
174-
editToolchain = cmdEdit.Flag.String("toolchain", "", "")
175-
editJSON = cmdEdit.Flag.Bool("json", false, "")
176-
editPrint = cmdEdit.Flag.Bool("print", false, "")
177-
editModule = cmdEdit.Flag.String("module", "", "")
178-
edits []func(*modfile.File) // edits specified in flags
174+
editFmt = cmdEdit.Flag.Bool("fmt", false, "")
175+
editGo = cmdEdit.Flag.String("go", "", "")
176+
editToolchain = cmdEdit.Flag.String("toolchain", "", "")
177+
editJSON = cmdEdit.Flag.Bool("json", false, "")
178+
editPrint = cmdEdit.Flag.Bool("print", false, "")
179+
editModule = cmdEdit.Flag.String("module", "", "")
180+
editDropToolchain = cmdEdit.Flag.Bool("droptoolchain", false, "drop the toolchain directive")
181+
edits []func(*modfile.File) // edits specified in flags
179182
)
180183

181184
type flagFunc func(string)
@@ -213,6 +216,7 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) {
213216
*editJSON ||
214217
*editPrint ||
215218
*editFmt ||
219+
*editDropToolchain ||
216220
len(edits) > 0
217221

218222
if !anyFlags {
@@ -223,6 +227,10 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) {
223227
base.Fatalf("go: cannot use both -json and -print")
224228
}
225229

230+
if *editToolchain != "" && *editDropToolchain {
231+
base.Fatalf("go: -toolchain and -droptoolchain are mutually exclusive")
232+
}
233+
226234
if len(args) > 1 {
227235
base.Fatalf("go: too many arguments")
228236
}
@@ -278,6 +286,9 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) {
278286
base.Fatalf("go: internal error: %v", err)
279287
}
280288
}
289+
if *editDropToolchain {
290+
modFile.DropToolchainStmt()
291+
}
281292

282293
if len(edits) > 0 {
283294
for _, edit := range edits {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Test for go mod edit -droptoolchain
2+
3+
# Setup
4+
env GO111MODULE=on
5+
env GOFLAGS=-mod=mod
6+
7+
# Create a new module
8+
! exists go.mod
9+
go mod init example.com/test
10+
exists go.mod
11+
12+
# Check that the toolchain directive is not present initially
13+
! grep toolchain go.mod
14+
15+
# Add a toolchain directive
16+
go mod edit -toolchain=go1.21
17+
grep 'toolchain go1.21' go.mod
18+
19+
# Remove the toolchain directive using -droptoolchain
20+
go mod edit -droptoolchain
21+
! grep toolchain go.mod
22+
23+
# Add a toolchain directive again
24+
go mod edit -toolchain=go1.22
25+
grep 'toolchain go1.22' go.mod
26+
27+
# Make sure that -toolchain=none still works as before
28+
go mod edit -toolchain=none
29+
! grep toolchain go.mod
30+
31+
# Add a toolchain directive again and use -droptoolchain with an argument (should be ignored)
32+
go mod edit -toolchain=go1.23
33+
grep 'toolchain go1.23' go.mod
34+
go mod edit -droptoolchain
35+
! grep toolchain go.mod

0 commit comments

Comments
 (0)