]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: change -tags to a comma-separated list
authorRuss Cox <rsc@golang.org>
Tue, 23 Apr 2019 16:19:57 +0000 (12:19 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 24 Apr 2019 14:25:19 +0000 (14:25 +0000)
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 <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
src/cmd/go/alldocs.go
src/cmd/go/go_test.go
src/cmd/go/internal/work/build.go
src/cmd/go/testdata/script/mod_build_tags.txt

index 6e4d77d5f6efe4fed4cdcdbf78511d084c53246c..650a81ddff7a7041906404d73e8f4b5ab19931ec 100644 (file)
 //             install and load all packages from dir instead of the usual locations.
 //             For example, when building with a non-standard configuration,
 //             use -pkgdir to keep generated packages in a separate location.
-//     -tags 'tag list'
-//             a space-separated list of build tags to consider satisfied during the
+//     -tags tag,list
+//             a comma-separated list of build tags to consider satisfied during the
 //             build. For more information about build tags, see the description of
 //             build constraints in the documentation for the go/build package.
+//             (Earlier versions of Go used a space-separated list, and that form
+//             is deprecated but still recognized.)
 //     -trimpath
 //             remove all file system paths from the resulting executable.
 //             Instead of absolute file system paths, the recorded file names
index 5ec02d8e493d9ab7493b0a6ae82af70c867c2c50..337dfd7ca850d0696be4cc09b1270e81b46b6a47 100644 (file)
@@ -4644,7 +4644,7 @@ func TestBuildTagsNoComma(t *testing.T) {
        tg.makeTempdir()
        tg.setenv("GOPATH", tg.path("go"))
        tg.run("build", "-tags", "tag1 tag2", "math")
-       tg.runFail("build", "-tags", "tag1,tag2", "math")
+       tg.runFail("build", "-tags", "tag1,tag2 tag3", "math")
        tg.grepBoth("space-separated list contains comma", "-tags with a comma-separated list didn't error")
 }
 
index 355c1477f5d0bb0d4c0b7935c99a17ae1c7bf65c..9c03f0818de37c88b61e0614d04daa42aabb2414 100644 (file)
@@ -104,10 +104,12 @@ and test commands:
                install and load all packages from dir instead of the usual locations.
                For example, when building with a non-standard configuration,
                use -pkgdir to keep generated packages in a separate location.
-       -tags 'tag list'
-               a space-separated list of build tags to consider satisfied during the
+       -tags tag,list
+               a comma-separated list of build tags to consider satisfied during the
                build. For more information about build tags, see the description of
                build constraints in the documentation for the go/build package.
+               (Earlier versions of Go used a space-separated list, and that form
+               is deprecated but still recognized.)
        -trimpath
                remove all file system paths from the resulting executable.
                Instead of absolute file system paths, the recorded file names
@@ -233,7 +235,7 @@ func AddBuildFlags(cmd *base.Command) {
        cmd.Flag.StringVar(&cfg.BuildPkgdir, "pkgdir", "", "")
        cmd.Flag.BoolVar(&cfg.BuildRace, "race", false, "")
        cmd.Flag.BoolVar(&cfg.BuildMSan, "msan", false, "")
-       cmd.Flag.Var((*base.StringsFlag)(&cfg.BuildContext.BuildTags), "tags", "")
+       cmd.Flag.Var((*tagsFlag)(&cfg.BuildContext.BuildTags), "tags", "")
        cmd.Flag.Var((*base.StringsFlag)(&cfg.BuildToolexec), "toolexec", "")
        cmd.Flag.BoolVar(&cfg.BuildTrimpath, "trimpath", false, "")
        cmd.Flag.BoolVar(&cfg.BuildWork, "work", false, "")
@@ -242,6 +244,29 @@ func AddBuildFlags(cmd *base.Command) {
        cmd.Flag.StringVar(&cfg.DebugActiongraph, "debug-actiongraph", "", "")
 }
 
+// tagsFlag is the implementation of the -tags flag.
+type tagsFlag []string
+
+func (v *tagsFlag) Set(s string) error {
+       // For compatibility with Go 1.12 and earlier, allow "-tags='a b c'" or even just "-tags='a'".
+       if strings.Contains(s, " ") || strings.Contains(s, "'") {
+               return (*base.StringsFlag)(v).Set(s)
+       }
+
+       // Split on commas, ignore empty strings.
+       *v = []string{}
+       for _, s := range strings.Split(s, ",") {
+               if s != "" {
+                       *v = append(*v, s)
+               }
+       }
+       return nil
+}
+
+func (v *tagsFlag) String() string {
+       return "<TagsFlag>"
+}
+
 // fileExtSplit expects a filename and returns the name
 // and ext (without the dot). If the file has no
 // extension, ext will be empty.
index 1347eaacbf78a98946afe87a0897632fe2c8cf6b..ae1d605e1f2f1227bbdea8c70c55f69941b6074f 100644 (file)
@@ -16,6 +16,9 @@ stdout '\[y\.go\]'
 go list -f {{.GoFiles}} -tags 'tag1 tag2'
 stdout '\[x\.go y\.go\]'
 
+go list -f {{.GoFiles}} -tags tag1,tag2 # commas allowed as of Go 1.13
+stdout '\[x\.go y\.go\]'
+
 -- x/go.mod --
 module x