Skip to content

Commit 144c96f

Browse files
samthanawallagopherbot
authored andcommitted
cmd/go: set GoVersion for files on the command line with vet
For: #65612 Fixes: #66092 For now, we will align the behavior such that vet and the compiler agree that gover.Local() will be used for command-line-files. We expect to change this to set the goversion as the containing module's go version. Change-Id: If7f2ea3a82e8e876716f18dacc021026de175a93 Reviewed-on: https://go-review.googlesource.com/c/go/+/593156 Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Sam Thanawalla <[email protected]>
1 parent 22cf1e7 commit 144c96f

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

src/cmd/go/internal/work/exec.go

+1
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,7 @@ func buildVetConfig(a *Action, srcfiles []string) {
11801180
PackageFile: make(map[string]string),
11811181
Standard: make(map[string]bool),
11821182
}
1183+
vcfg.GoVersion = "go" + gover.Local()
11831184
if a.Package.Module != nil {
11841185
v := a.Package.Module.GoVersion
11851186
if v == "" {

src/cmd/go/internal/work/gc.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,18 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
6666

6767
pkgpath := pkgPath(a)
6868
defaultGcFlags := []string{"-p", pkgpath}
69+
vers := gover.Local()
6970
if p.Module != nil {
7071
v := p.Module.GoVersion
7172
if v == "" {
7273
v = gover.DefaultGoModVersion
7374
}
75+
// TODO(samthanawalla): Investigate when allowedVersion is not true.
7476
if allowedVersion(v) {
75-
defaultGcFlags = append(defaultGcFlags, "-lang=go"+gover.Lang(v))
77+
vers = v
7678
}
7779
}
80+
defaultGcFlags = append(defaultGcFlags, "-lang=go"+gover.Lang(vers))
7881
if p.Standard {
7982
defaultGcFlags = append(defaultGcFlags, "-std")
8083
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# go.dev/issue/66092
2+
# This test ensures that files listed on the commandline will pass
3+
# the language version to the compiler.
4+
# All compilations should specify some -lang.
5+
6+
go build -n x.go
7+
stderr '-lang=go1\.[0-9]+'
8+
9+
-- x.go --
10+
package main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# go.dev/issue/65612
2+
# go vet should set the GoVersion for command line files.
3+
4+
env TESTGO_VERSION=go1.22.1
5+
env TESTGO_VERSION_SWITCH=switch
6+
7+
go vet -n -json example.com/m
8+
stderr '"GoVersion": "go1.22.0"'
9+
10+
# A command line file should use the local go version.
11+
go vet -n -json main.go
12+
stderr '"GoVersion": "go1.22.1"'
13+
14+
# In workspace mode, the command line file version should use go.work version.
15+
cp go.work.orig go.work
16+
go vet -n -json example.com/m
17+
stderr '"GoVersion": "go1.22.0'
18+
19+
go vet -n -json main.go
20+
stderr '"GoVersion": "go1.22.2'
21+
22+
# Without go.mod or go.work, the command line file version should use local go version .
23+
env TESTGO_VERSION=go1.22.3
24+
rm go.mod
25+
rm go.work
26+
27+
! go vet -n -json example.com/m
28+
29+
go vet -n -json main.go
30+
stderr '"GoVersion": "go1.22.3"'
31+
32+
-- go.mod --
33+
module example.com/m
34+
35+
go 1.22.0
36+
37+
-- go.work.orig --
38+
go 1.22.2
39+
40+
use .
41+
42+
-- main.go --
43+
package main

0 commit comments

Comments
 (0)