Skip to content

Commit

Permalink
all: merge master (a297bfd) into gopls-release-branch.0.15
Browse files Browse the repository at this point in the history
For golang/go#64973

Conflicts:

- gopls/go.mod
- gopls/go.sum

Merge List:

+ 2024-02-06 a297bfd go/callgraph/rta: audit for types.Alias safety
+ 2024-02-06 08393e0 cmd/deadcode: audit for types.Alias safety
+ 2024-02-06 37586e4 internal/apidiff: audit for types.Alias safety
+ 2024-02-06 6d4ccf2 gopls/internal/cache: prime goimports cache asynchronously
+ 2024-02-06 8b6359d gopls/internal/cache: share goimports state for GOMODCACHE
+ 2024-02-06 2bb7f1c gopls/internal/test/integration/bench: improve completion benchmarks
+ 2024-02-06 f6dc1e9 internal/imports: merge init and newModuleResolver
+ 2024-02-06 4f6024e internal/gopathwalk: walk directories concurrently
+ 2024-02-06 8fcb5f0 gopls/internal/test/marker: skip on LUCI solaris builders
+ 2024-02-06 08bd728 gopls/internal/test/integration: add a test for an inner go.work file
+ 2024-02-06 aee2e76 gopls/internal/settings: deprecate "allowModfileModifications"
+ 2024-02-05 ddb71b0 gopls/internal/cache: remove findWorkspaceModFile
+ 2024-02-05 365517a gopls/internal/cache: detect and reinit on workspace vendor changes
+ 2024-02-05 0be034b internal/aliases: Adds an internal alias package.
+ 2024-02-05 8efa10c internal/imports: add a benchmark for the initial mod cache scan
+ 2024-02-05 df03d7d gopls/internal/golang: add missing check for nil funcBody
+ 2024-02-03 a1fbc78 gopls/internal/golang: move Go semantic tokens out of server
+ 2024-02-03 51e3724 gopls: remove unused parameters
+ 2024-02-03 d6bd2d7 gopls/internal/cache: add assertions for export data inconsistency
+ 2024-02-02 3403ce1 go/internal/gccgoimporter: recognize "any" as a builtin type
+ 2024-02-02 aecdb2d gopls/internal/cache: support workspace vendoring
+ 2024-02-02 e80085c gopls/internal/golang: downgrade bug.Reportf for missing builtin
+ 2024-02-02 9c43803 gopls/internal/cache: allow command-line-package >1 CompileGoFiles
+ 2024-02-02 efce0f5 gopls/internal/cache/metadata: assert graph is acyclic
+ 2024-02-02 c3f60b7 gopls/internal/golang: hover: show embedded fields
+ 2024-02-02 85146f5 gopls/internal/lsp/source: highlight returns correctly
+ 2024-02-02 e211e0f gopls: enable crashmonitor for unexpected crashes
+ 2024-02-01 d077888 gopls: update third party dependencies
+ 2024-02-01 2e15dc7 gopls/internal/test/marker: reject "module testdata"
+ 2024-02-01 c07f927 cmd/deadcode: enable crash reporting
+ 2024-02-01 44aed24 gopls/internal/analysis/undeclaredname: improve fix name
+ 2024-02-01 afe5265 go/packages: wait for sizes goroutine on all control paths
+ 2024-02-01 4250783 refactor/eg: don't use cgo in Test
+ 2024-02-01 1efbdde cmd/stringer, go/loader: use testenv.NeedsTool(t, "cgo")
+ 2024-01-31 5f90691 go/buildutil: permit comma delimiters in build tags
+ 2024-01-31 0c80ba3 internal/imports: remove the unused ProcessEnv.ModFile field
+ 2024-01-31 c046c5b gopls/internal/test/marker/testdata: move test to correct location
+ 2024-01-31 da7ed64 gopls/internal/cache/imports: simplify importsState invalidation
+ 2024-01-30 7ec8ac0 gopls/internal/golang: hover: show type's methods after doc comment
+ 2024-01-29 0e1f5be gopls: update x/telemetry dependency
+ 2024-01-29 341c043 gopls/internal/test/integration/bench: fix broken benchmarks
+ 2024-01-29 b048cf1 cmd/callgraph: unskip TestCallgraph on windows/arm64
+ 2024-01-29 14d7f7b gopls/internal/cache: memoize the view filter func
+ 2024-01-29 5e16437 gopls/internal/cache: skip TestZeroConfigAlgorithm on wasm
+ 2024-01-29 dd0f88f gopls/doc/emacs.md: explain how to organize imports automatically
+ 2024-01-25 3d49bd5 internal/lsp: publish protocol.UnmarshalJSON
+ 2024-01-24 35d8b1b gopls/internal/golang: don't bug.Report repaired ASTs in hover
+ 2024-01-24 8174727 gopls/internal/test: await quiescence in TestInconsistentVendoring
+ 2024-01-24 df2fa1e gopls/doc: s/source/golang/ in the SVG file
+ 2024-01-24 238800b gopls: manual tweaks after the lsp/ -> . rename
+ 2024-01-24 95c6ac4 gopls/internal/protocol/command: move from ../lsp
+ 2024-01-24 6d109d1 gopls/internal/protocol: move out of lsp/
+ 2024-01-24 f872b3d gopls/internal/cache: move out of lsp/
+ 2024-01-24 6823da4 gopls/internal/golang: move from lsp/source
+ 2024-01-23 129144e gopls/internal/lsprpc: move out of lsp/
+ 2024-01-23 eed1997 gopls: use relative watched file patterns if supported
+ 2024-01-23 fa791ac gopls/internal/progress: simplify API
+ 2024-01-23 816dcdf go/packages: add test for flake observed on Android
+ 2024-01-23 d5e76f1 gopls/internal/progress: move from lsp/
+ 2024-01-23 6fe9326 gopls/internal/telemetry/cmd/stacks: include client name in report
+ 2024-01-23 6a58b98 gopls/internal/lsp/lsprpc: radically reduce API
+ 2024-01-23 0ffb1d0 gopls/doc: add Helix editor documentation for gopls
+ 2024-01-23 c16e222 gopls/internal/test/integration: add regtest for hover crash
+ 2024-01-23 bd547e5 gopls/internal/lsp/cache: clean up public API
+ 2024-01-22 4c53267 gopls/internal/lsp/cache: don't report a bug for standalone test files
+ 2024-01-22 a49867f internal/imports: don't add imports that shadow predeclared names
+ 2024-01-22 e2ca594 gopls/internal/lsp/source: address problems detected by staticcheck
+ 2024-01-22 94d99e3 gopls/internal/test/marker: re-enable some tests
+ 2024-01-22 060c748 gopls: consolidate analyzers, eliminating "convenience" analyzers
+ 2024-01-22 7f6ec23 go/ssa: suppress go/packages-based test on android
+ 2024-01-22 a987ef7 go/packages: publish Driver{Request,Response}
+ 2024-01-19 e1555a3 go/ssa: add Function.DomPostorder
+ 2024-01-19 cd7b510 gopls/internal/lsp/cache: allow versions of form "go1.2.3"
+ 2024-01-19 d0930db gopls/internal/lsp/cache: fix bug reports from toGobDiagnostics
+ 2024-01-19 1871a2e gopls/internal/lsp/source: move Go CodeActions logic from server
+ 2024-01-19 afcd676 gopls/internal/lsp/source: show promoted methods in hover
+ 2024-01-19 525acd1 go/analysis/internal/analysisflags: add RelatedInformation JSON
+ 2024-01-19 c7ccb51 gopls/doc/design: rewrite the architecture overview
+ 2024-01-19 39a2545 gopls/internal/version: amend the year in a recent copyright header
+ 2024-01-19 186fcf3 gopls/internal/lsp/source: factor edit conversion utils
+ 2024-01-19 83c6c25 gopls/internal/server: analyze widest packages for open files
+ 2024-01-19 c467be3 gopls: add a main.version variable to override the version at linktime
+ 2024-01-19 6673e7a gopls/internal/lsp/source/completion: infer parameter types
+ 2024-01-18 3458e0c gopls/internal/lsp/source: fix Fix titles
+ 2024-01-18 72a36a7 gopls/internal/test/marker: fix test breakage due to semantic conflict
+ 2024-01-18 470afda gopls/internal/analysis/unusedparams: eliminate false positives
+ 2024-01-18 592d9e1 internal/lsp: convert refactor code actions to use codeAction/resolve
+ 2024-01-17 f2d3f78 gopls/internal/lsp/protocol: fix TestURIFromPath on windows
+ 2024-01-17 d2200d1 gopls/internal/test/marker: update skip comment for addgowork.txt
+ 2024-01-17 c0db45f gopls/internal/server: simplify DiagnoseFiles to avoid a race
+ 2024-01-17 0d1b478 gopls/internal/test/integration: fix flakiness of TestResolveImportCycle
+ 2024-01-17 d517112 gopls/internal/lsp/protocol: deliver log messages in order
+ 2024-01-12 9164f2a gopls/internal/test/marker: skip on solaris-amd64-oraclerel
+ 2024-01-12 dbc9d3e internal/analysisinternal: TypeExpr: don't forget Variadic
+ 2024-01-12 54cf5bc internal/debug: show new View information in the debug page
+ 2024-01-12 b37fde9 gopls: simplify gopls command configuration
+ 2024-01-12 6a0605d gopls/internal/test/marker: regenerate golden data
+ 2024-01-11 f572b7e gopls/internal/lsp/source/stub: trim version suffix from module path
+ 2024-01-11 0b1f1d4 gopls/internal/lsp/cache: (re-)ensure clean shutdown
+ 2024-01-10 706525d gopls/internal/lsp/source/completion: support postfix completion (iferr,
+ 2024-01-09 581c0b3 gopls/internal/lsp/source: add receiver name to stubbed methods
+ 2024-01-08 c95fa0f gopls/internal/test: skip marker tests on darwin builders if -short
+ 2024-01-08 25a0e9d go.mod: update golang.org/x dependencies
+ 2024-01-05 c9c95f9 internal/refactor/inline: improve a confusing error message
+ 2024-01-05 ba8672b go/analysis/passes/unusedresult: add functions from slices package

Change-Id: I428339a4a66ce849c3a3f8827da47125218bb121
  • Loading branch information
findleyr committed Feb 6, 2024
2 parents 50d32be + a297bfd commit 56dc868
Show file tree
Hide file tree
Showing 457 changed files with 9,643 additions and 5,818 deletions.
5 changes: 0 additions & 5 deletions cmd/callgraph/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"log"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

Expand All @@ -35,10 +34,6 @@ func init() {
}

func TestCallgraph(t *testing.T) {
if runtime.GOOS == "windows" && runtime.GOARCH == "arm64" {
t.Skipf("skipping due to suspected file corruption bug on windows/arm64 (https://go.dev/issue/50706)")
}

testenv.NeedsTool(t, "go")

gopath, err := filepath.Abs("testdata")
Expand Down
10 changes: 8 additions & 2 deletions cmd/deadcode/deadcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import (
"strings"
"text/template"

"golang.org/x/telemetry/counter"
"golang.org/x/telemetry/crashmonitor"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/rta"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/internal/aliases"
)

//go:embed doc.go
Expand Down Expand Up @@ -62,6 +65,9 @@ Flags:
}

func main() {
counter.Open() // Enable telemetry counter writing.
crashmonitor.Start() // Enable crash reporting watchdog.

log.SetPrefix("deadcode: ")
log.SetFlags(0) // no time prefix

Expand Down Expand Up @@ -380,10 +386,10 @@ func prettyName(fn *ssa.Function, qualified bool) string {
// method receiver?
if recv := fn.Signature.Recv(); recv != nil {
t := recv.Type()
if ptr, ok := t.(*types.Pointer); ok {
if ptr, ok := aliases.Unalias(t).(*types.Pointer); ok {
t = ptr.Elem()
}
buf.WriteString(t.(*types.Named).Obj().Name())
buf.WriteString(aliases.Unalias(t).(*types.Named).Obj().Name())
buf.WriteByte('.')
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/deadcode/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ With no flags, the command prints the name and location of each dead
function in the form of a typical compiler diagnostic, for example:
$ deadcode -f='{{range .Funcs}}{{println .Position}}{{end}}' -test ./gopls/...
gopls/internal/lsp/command.go:1206:6: unreachable func: openClientEditor
gopls/internal/protocol/command.go:1206:6: unreachable func: openClientEditor
gopls/internal/template/parse.go:414:18: unreachable func: Parsed.WriteNode
gopls/internal/template/parse.go:419:18: unreachable func: wrNode.writeNode
Expand Down
9 changes: 6 additions & 3 deletions cmd/stringer/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"bytes"
"flag"
"fmt"
"go/build"
"io"
"os"
"path"
Expand Down Expand Up @@ -50,6 +49,8 @@ func TestMain(m *testing.M) {
}

func TestEndToEnd(t *testing.T) {
testenv.NeedsTool(t, "go")

stringer := stringerPath(t)
// Read the testdata directory.
fd, err := os.Open("testdata")
Expand All @@ -76,8 +77,8 @@ func TestEndToEnd(t *testing.T) {
continue
}
t.Run(name, func(t *testing.T) {
if name == "cgo.go" && !build.Default.CgoEnabled {
t.Skipf("cgo is not enabled for %s", name)
if name == "cgo.go" {
testenv.NeedsTool(t, "cgo")
}
stringerCompileAndRun(t, t.TempDir(), stringer, typeName(name), name)
})
Expand Down Expand Up @@ -155,6 +156,8 @@ func TestTags(t *testing.T) {
// TestConstValueChange verifies that if a constant value changes and
// the stringer code is not regenerated, we'll get a compiler error.
func TestConstValueChange(t *testing.T) {
testenv.NeedsTool(t, "go")

stringer := stringerPath(t)
dir := t.TempDir()
source := filepath.Join(dir, "day.go")
Expand Down
2 changes: 1 addition & 1 deletion copyright/copyright.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func checkFile(toolsDir, filename string) (bool, error) {
return shouldAddCopyright, nil
}

// Copied from golang.org/x/tools/gopls/internal/lsp/source/util.go.
// Copied from golang.org/x/tools/gopls/internal/golang/util.go.
// Matches cgo generated comment as well as the proposed standard:
//
// https://golang.org/s/generatedcode
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ go 1.18
require (
github.com/yuin/goldmark v1.4.13
golang.org/x/mod v0.14.0
golang.org/x/net v0.19.0
golang.org/x/net v0.20.0
)

require golang.org/x/sync v0.5.0
require golang.org/x/sync v0.6.0

require (
golang.org/x/sys v0.16.0 // indirect
golang.org/x/telemetry v0.0.0-20240201224847-0a1d30dda509
)
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240201224847-0a1d30dda509 h1:Nr7eTQpQZ/ytesxDJpQgaf0t4sdLnnDtAbmtViTrSUo=
golang.org/x/telemetry v0.0.0-20240201224847-0a1d30dda509/go.mod h1:ZthVHHkOi8rlMEsfFr3Ie42Ym1NonbFNNRKW3ci0UrU=
17 changes: 8 additions & 9 deletions go/analysis/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ type Diagnostic struct {
// see https://pkg.go.dev/net/url#URL.ResolveReference.
URL string

// SuggestedFixes contains suggested fixes for a diagnostic
// which can be used to perform edits to a file that address
// the diagnostic.
//
// Diagnostics should not contain SuggestedFixes that overlap.
SuggestedFixes []SuggestedFix // optional
// SuggestedFixes is an optional list of fixes to address the
// problem described by the diagnostic, each one representing
// an alternative strategy; at most one may be applied.
SuggestedFixes []SuggestedFix

Related []RelatedInformation // optional
// Related contains optional secondary positions and messages
// related to the primary diagnostic.
Related []RelatedInformation
}

// RelatedInformation contains information related to a diagnostic.
Expand All @@ -55,8 +55,7 @@ type RelatedInformation struct {
// user can choose to apply to their code. Usually the SuggestedFix is
// meant to fix the issue flagged by the diagnostic.
//
// TextEdits for a SuggestedFix should not overlap,
// nor contain edits for other packages.
// The TextEdits must not overlap, nor contain edits for other packages.
type SuggestedFix struct {
// A description for this suggested fix to be shown to a user deciding
// whether to accept it.
Expand Down
33 changes: 25 additions & 8 deletions go/analysis/internal/analysisflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,24 @@ type JSONSuggestedFix struct {
Edits []JSONTextEdit `json:"edits"`
}

// A JSONDiagnostic can be used to encode and decode analysis.Diagnostics to and
// from JSON.
// TODO(matloob): Should the JSON diagnostics contain ranges?
// If so, how should they be formatted?
// A JSONDiagnostic describes the JSON schema of an analysis.Diagnostic.
//
// TODO(matloob): include End position if present.
type JSONDiagnostic struct {
Category string `json:"category,omitempty"`
Posn string `json:"posn"`
Message string `json:"message"`
SuggestedFixes []JSONSuggestedFix `json:"suggested_fixes,omitempty"`
Category string `json:"category,omitempty"`
Posn string `json:"posn"` // e.g. "file.go:line:column"
Message string `json:"message"`
SuggestedFixes []JSONSuggestedFix `json:"suggested_fixes,omitempty"`
Related []JSONRelatedInformation `json:"related,omitempty"`
}

// A JSONRelated describes a secondary position and message related to
// a primary diagnostic.
//
// TODO(adonovan): include End position if present.
type JSONRelatedInformation struct {
Posn string `json:"posn"` // e.g. "file.go:line:column"
Message string `json:"message"`
}

// Add adds the result of analysis 'name' on package 'id'.
Expand Down Expand Up @@ -401,11 +410,19 @@ func (tree JSONTree) Add(fset *token.FileSet, id, name string, diags []analysis.
Edits: edits,
})
}
var related []JSONRelatedInformation
for _, r := range f.Related {
related = append(related, JSONRelatedInformation{
Posn: fset.Position(r.Pos).String(),
Message: r.Message,
})
}
jdiag := JSONDiagnostic{
Category: f.Category,
Posn: fset.Position(f.Pos).String(),
Message: f.Message,
SuggestedFixes: fixes,
Related: related,
}
diagnostics = append(diagnostics, jdiag)
}
Expand Down
13 changes: 12 additions & 1 deletion go/analysis/passes/lostcancel/lostcancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,18 @@ func runFunc(pass *analysis.Pass, node ast.Node) {
if ret := lostCancelPath(pass, g, v, stmt, sig); ret != nil {
lineno := pass.Fset.Position(stmt.Pos()).Line
pass.ReportRangef(stmt, "the %s function is not used on all paths (possible context leak)", v.Name())
pass.ReportRangef(ret, "this return statement may be reached without using the %s var defined on line %d", v.Name(), lineno)

pos, end := ret.Pos(), ret.End()
// golang/go#64547: cfg.Block.Return may return a synthetic
// ReturnStmt that overflows the file.
if pass.Fset.File(pos) != pass.Fset.File(end) {
end = pos
}
pass.Report(analysis.Diagnostic{
Pos: pos,
End: end,
Message: fmt.Sprintf("this return statement may be reached without using the %s var defined on line %d", v.Name(), lineno),
})
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion go/analysis/passes/unusedresult/unusedresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,25 @@ func init() {
// List standard library functions here.
// The context.With{Cancel,Deadline,Timeout} entries are
// effectively redundant wrt the lostcancel analyzer.
funcs.Set("errors.New,fmt.Errorf,fmt.Sprintf,fmt.Sprint,sort.Reverse,context.WithValue,context.WithCancel,context.WithDeadline,context.WithTimeout")
funcs = stringSetFlag{
"context.WithCancel": true,
"context.WithDeadline": true,
"context.WithTimeout": true,
"context.WithValue": true,
"errors.New": true,
"fmt.Errorf": true,
"fmt.Sprint": true,
"fmt.Sprintf": true,
"slices.Clip": true,
"slices.Compact": true,
"slices.CompactFunc": true,
"slices.Delete": true,
"slices.DeleteFunc": true,
"slices.Grow": true,
"slices.Insert": true,
"slices.Replace": true,
"sort.Reverse": true,
}
Analyzer.Flags.Var(&funcs, "funcs",
"comma-separated list of functions whose results must be used")

Expand Down
42 changes: 31 additions & 11 deletions go/buildutil/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

package buildutil

// This logic was copied from stringsFlag from $GOROOT/src/cmd/go/build.go.
// This duplicated logic must be kept in sync with that from go build:
// $GOROOT/src/cmd/go/internal/work/build.go (tagsFlag.Set)
// $GOROOT/src/cmd/go/internal/base/flag.go (StringsFlag.Set)
// $GOROOT/src/cmd/internal/quoted/quoted.go (isSpaceByte, Split)

import "fmt"
import (
"fmt"
"strings"
)

const TagsFlagDoc = "a 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"

// TagsFlag is an implementation of the flag.Value and flag.Getter interfaces that parses
// a flag value in the same manner as go build's -tags flag and
// populates a []string slice.
// a flag value the same as go build's -tags flag and populates a []string slice.
//
// See $GOROOT/src/go/build/doc.go for description of build tags.
// See $GOROOT/src/cmd/go/doc.go for description of 'go build -tags' flag.
Expand All @@ -25,19 +30,32 @@ const TagsFlagDoc = "a list of `build tags` to consider satisfied during the bui
type TagsFlag []string

func (v *TagsFlag) Set(s string) error {
var err error
*v, err = splitQuotedFields(s)
if *v == nil {
*v = []string{}
// See $GOROOT/src/cmd/go/internal/work/build.go (tagsFlag.Set)
// 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, "'") {
var err error
*v, err = splitQuotedFields(s)
if *v == nil {
*v = []string{}
}
return err
}

// Starting in Go 1.13, the -tags flag is a comma-separated list of build tags.
*v = []string{}
for _, s := range strings.Split(s, ",") {
if s != "" {
*v = append(*v, s)
}
}
return err
return nil
}

func (v *TagsFlag) Get() interface{} { return *v }

func splitQuotedFields(s string) ([]string, error) {
// Split fields allowing '' or "" around elements.
// Quotes further inside the string do not count.
// See $GOROOT/src/cmd/internal/quoted/quoted.go (Split)
// This must remain in sync with that logic.
var f []string
for len(s) > 0 {
for len(s) > 0 && isSpaceByte(s[0]) {
Expand Down Expand Up @@ -76,5 +94,7 @@ func (v *TagsFlag) String() string {
}

func isSpaceByte(c byte) bool {
// See $GOROOT/src/cmd/internal/quoted/quoted.go (isSpaceByte, Split)
// This list must remain in sync with that.
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
}
Loading

0 comments on commit 56dc868

Please sign in to comment.