mirror of
https://github.com/hanzoai/visor.git
synced 2026-09-24 17:04:14 +00:00
nogo: set std language version from std go.mod
This change is a no-op, as we already used the toolchain language version. PiperOrigin-RevId: 926779620
This commit is contained in:
committed by
gVisor bot
parent
c6a17552a2
commit
82e3294b81
@@ -182,6 +182,11 @@ def go_context(ctx, goos = None, goarch = None):
|
||||
if go_ctx.mode.msan:
|
||||
nogo_args = nogo_args + ["-msan"]
|
||||
|
||||
stdlib_mods = [f for f in go_ctx.sdk.srcs.to_list() if f.path.endswith("/src/go.mod")]
|
||||
if len(stdlib_mods) != 1:
|
||||
fail("expect exactly one stdlib src/go.mod in %s, found %s" % (go_ctx.sdk.srcs, stdlib_mods))
|
||||
stdlib_mod = stdlib_mods[0]
|
||||
|
||||
return struct(
|
||||
env = dict(go_ctx.env, CGO_ENABLED = "0"),
|
||||
go = go_ctx.go,
|
||||
@@ -192,6 +197,7 @@ def go_context(ctx, goos = None, goarch = None):
|
||||
nogo_args = nogo_args,
|
||||
runfiles = depset([go_ctx.go] + go_ctx.sdk.srcs.to_list() + go_ctx.sdk.tools.to_list() + go_ctx.stdlib.libs.to_list()),
|
||||
stdlib_srcs = go_ctx.sdk.srcs,
|
||||
stdlib_mod = stdlib_mod,
|
||||
)
|
||||
|
||||
def select_goarch():
|
||||
|
||||
@@ -18,6 +18,7 @@ package cli
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io"
|
||||
@@ -512,6 +513,32 @@ func (r *Render) Execute(ctx context.Context, fs *flag.FlagSet, args ...any) sub
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
// goVersionRe matches a `go VERSION` line in go.mod, capturing VERSION.
|
||||
var goVersionRe = regexp.MustCompile(`^go\s+(\S+)`)
|
||||
|
||||
// resolveGOVERSION sets flags.GOVERSION from flags.GOVERSIONModFile, if necessary.
|
||||
func resolveGOVERSION() error {
|
||||
if flags.GOVERSIONModFile == "" {
|
||||
// Nothing to do.
|
||||
return nil
|
||||
}
|
||||
if flags.GOVERSION != "" {
|
||||
return errors.New("only one of -GOVERSION or -GOVERSION-mod-file may be set")
|
||||
}
|
||||
|
||||
b, err := os.ReadFile(flags.GOVERSIONModFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := goVersionRe.FindStringSubmatch(string(b))
|
||||
if len(m) != 2 {
|
||||
return fmt.Errorf("go line not found in go.mod:\n%s", string(b))
|
||||
}
|
||||
flags.GOVERSION = m[1]
|
||||
return nil
|
||||
}
|
||||
|
||||
// Main is the main entrypoint.
|
||||
func Main() {
|
||||
subcommands.Register(&Check{}, "")
|
||||
@@ -521,5 +548,8 @@ func Main() {
|
||||
subcommands.Register(subcommands.HelpCommand(), "")
|
||||
subcommands.Register(subcommands.FlagsCommand(), "")
|
||||
flag.CommandLine.Parse(os.Args[1:])
|
||||
if err := resolveGOVERSION(); err != nil {
|
||||
failure("error resolving GOVERSION: %v", err)
|
||||
}
|
||||
os.Exit(int(subcommands.Execute(context.Background())))
|
||||
}
|
||||
|
||||
+6
-1
@@ -69,6 +69,9 @@ def _nogo_stdlib_impl(ctx):
|
||||
# Build the configuration for the stdlib.
|
||||
go_ctx, args, inputs, raw_findings = _nogo_config(ctx, deps = [])
|
||||
|
||||
# GOVERSION of std is set by the std go.mod.
|
||||
args.append("-GOVERSION-mod-file=%s" % go_ctx.stdlib_mod)
|
||||
|
||||
# Build the analyzer command.
|
||||
facts_file = ctx.actions.declare_file(ctx.label.name + ".facts")
|
||||
findings_file = ctx.actions.declare_file(ctx.label.name + ".raw_findings")
|
||||
@@ -170,7 +173,6 @@ def _nogo_config(ctx, deps):
|
||||
"-go=%s" % go_ctx.go.path,
|
||||
"-GOOS=%s" % go_ctx.goos,
|
||||
"-GOARCH=%s" % go_ctx.goarch,
|
||||
"-GOVERSION=%s" % go_ctx.lang_version,
|
||||
"-tags=%s" % (",".join(go_ctx.gotags)),
|
||||
]
|
||||
inputs = []
|
||||
@@ -214,6 +216,9 @@ def _nogo_package_config(ctx, deps, importpath = None, target = None):
|
||||
# Returns (go_ctx, args, inputs, raw_findings).
|
||||
go_ctx, args, inputs, raw_findings = _nogo_config(ctx, deps)
|
||||
|
||||
# GOVERSION of packages are set by the project language version.
|
||||
args.append("-GOVERSION=%s" % go_ctx.lang_version)
|
||||
|
||||
# Add the module itself, for the type sanity check. This applies only to
|
||||
# the libraries, and not binaries or tests.
|
||||
binaries = []
|
||||
|
||||
@@ -41,6 +41,12 @@ var (
|
||||
// GOVERSION defines the Go version for analysis.
|
||||
GOVERSION string
|
||||
|
||||
// GOVERSIONModFile is the path to a go.mod to extract the Go version from.
|
||||
//
|
||||
// Only one of GOVERSION or GOVERSIONModFile may be set. cli.Main will
|
||||
// set GOVERSION from this mod file.
|
||||
GOVERSIONModFile string
|
||||
|
||||
// BuildTags defines the set of build tags for analysis. Note that
|
||||
// while this may also be a StringList, it is kept as an explicit
|
||||
// comma-separated list in order to build the standard flag.
|
||||
@@ -71,6 +77,7 @@ func init() {
|
||||
flag.StringVar(&GOOS, "GOOS", runtime.GOOS, "GOOS for analysis")
|
||||
flag.StringVar(&GOARCH, "GOARCH", runtime.GOARCH, "GOARCH for analysis")
|
||||
flag.StringVar(&GOVERSION, "GOVERSION", "", "GOVERSION for analysis")
|
||||
flag.StringVar(&GOVERSIONModFile, "GOVERSION-mod-file", "", "Path to go.mod containing the GOVERSION for analysis")
|
||||
flag.StringVar(&BuildTags, "tags", "", "comma-separated build tags")
|
||||
flag.BoolVar(&Race, "race", false, "-race build")
|
||||
flag.BoolVar(&MSAN, "msan", false, "-msan build")
|
||||
|
||||
Reference in New Issue
Block a user