Skip to content

Commit 80e7832

Browse files
committed
cmd/go: change -tags to a comma-separated list
Using commas makes it possible to put multiple tags into GOFLAGS. The space-separated form is still recognized and will be maintained. Alleviates #26849 somewhat. Fixes #18800 (again). Change-Id: I6f4cf28ea31e53e21ccbdad6ef1a0aee63b007d7 Reviewed-on: https://go-review.googlesource.com/c/go/+/173438 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent 7469564 commit 80e7832

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
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/go_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4644,7 +4644,7 @@ func TestBuildTagsNoComma(t *testing.T) {
46444644
tg.makeTempdir()
46454645
tg.setenv("GOPATH", tg.path("go"))
46464646
tg.run("build", "-tags", "tag1 tag2", "math")
4647-
tg.runFail("build", "-tags", "tag1,tag2", "math")
4647+
tg.runFail("build", "-tags", "tag1,tag2 tag3", "math")
46484648
tg.grepBoth("space-separated list contains comma", "-tags with a comma-separated list didn't error")
46494649
}
46504650

src/cmd/go/internal/work/build.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ and test commands:
104104
install and load all packages from dir instead of the usual locations.
105105
For example, when building with a non-standard configuration,
106106
use -pkgdir to keep generated packages in a separate location.
107-
-tags 'tag list'
108-
a space-separated list of build tags to consider satisfied during the
107+
-tags tag,list
108+
a comma-separated list of build tags to consider satisfied during the
109109
build. For more information about build tags, see the description of
110110
build constraints in the documentation for the go/build package.
111+
(Earlier versions of Go used a space-separated list, and that form
112+
is deprecated but still recognized.)
111113
-trimpath
112114
remove all file system paths from the resulting executable.
113115
Instead of absolute file system paths, the recorded file names
@@ -233,7 +235,7 @@ func AddBuildFlags(cmd *base.Command) {
233235
cmd.Flag.StringVar(&cfg.BuildPkgdir, "pkgdir", "", "")
234236
cmd.Flag.BoolVar(&cfg.BuildRace, "race", false, "")
235237
cmd.Flag.BoolVar(&cfg.BuildMSan, "msan", false, "")
236-
cmd.Flag.Var((*base.StringsFlag)(&cfg.BuildContext.BuildTags), "tags", "")
238+
cmd.Flag.Var((*tagsFlag)(&cfg.BuildContext.BuildTags), "tags", "")
237239
cmd.Flag.Var((*base.StringsFlag)(&cfg.BuildToolexec), "toolexec", "")
238240
cmd.Flag.BoolVar(&cfg.BuildTrimpath, "trimpath", false, "")
239241
cmd.Flag.BoolVar(&cfg.BuildWork, "work", false, "")
@@ -242,6 +244,29 @@ func AddBuildFlags(cmd *base.Command) {
242244
cmd.Flag.StringVar(&cfg.DebugActiongraph, "debug-actiongraph", "", "")
243245
}
244246

247+
// tagsFlag is the implementation of the -tags flag.
248+
type tagsFlag []string
249+
250+
func (v *tagsFlag) Set(s string) error {
251+
// For compatibility with Go 1.12 and earlier, allow "-tags='a b c'" or even just "-tags='a'".
252+
if strings.Contains(s, " ") || strings.Contains(s, "'") {
253+
return (*base.StringsFlag)(v).Set(s)
254+
}
255+
256+
// Split on commas, ignore empty strings.
257+
*v = []string{}
258+
for _, s := range strings.Split(s, ",") {
259+
if s != "" {
260+
*v = append(*v, s)
261+
}
262+
}
263+
return nil
264+
}
265+
266+
func (v *tagsFlag) String() string {
267+
return "<TagsFlag>"
268+
}
269+
245270
// fileExtSplit expects a filename and returns the name
246271
// and ext (without the dot). If the file has no
247272
// extension, ext will be empty.

src/cmd/go/testdata/script/mod_build_tags.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ stdout '\[y\.go\]'
1616
go list -f {{.GoFiles}} -tags 'tag1 tag2'
1717
stdout '\[x\.go y\.go\]'
1818

19+
go list -f {{.GoFiles}} -tags tag1,tag2 # commas allowed as of Go 1.13
20+
stdout '\[x\.go y\.go\]'
21+
1922
-- x/go.mod --
2023
module x
2124

0 commit comments

Comments
 (0)