Skip to content

Commit d584d2b

Browse files
committed
cmd/go: fix version stamping for v2 modules and subdirectories
We were not passing the module path to newCodeRepo which caused it to incorrectly parse the major version. This allowed v0 and v1 modules to work because an empty major version is allowed in that case. Additionally we need to pass the root module path to derive the correct tag for subdirectories. Fixes: #72877 Fixes: #71738 Change-Id: Id792923f426858513972e713623270edbc76c545 Reviewed-on: https://go-review.googlesource.com/c/go/+/661875 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent a08d2db commit d584d2b

File tree

5 files changed

+110
-17
lines changed

5 files changed

+110
-17
lines changed

src/cmd/go/internal/load/pkg.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -2577,7 +2577,16 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
25772577
}
25782578
appendSetting("vcs.modified", strconv.FormatBool(st.Uncommitted))
25792579
// Determine the correct version of this module at the current revision and update the build metadata accordingly.
2580-
repo := modfetch.LookupLocal(ctx, repoDir)
2580+
rootModPath := goModPath(repoDir)
2581+
// If no root module is found, skip embedding VCS data since we cannot determine the module path of the root.
2582+
if rootModPath == "" {
2583+
goto omitVCS
2584+
}
2585+
codeRoot, _, ok := module.SplitPathVersion(rootModPath)
2586+
if !ok {
2587+
goto omitVCS
2588+
}
2589+
repo := modfetch.LookupLocal(ctx, codeRoot, p.Module.Path, repoDir)
25812590
revInfo, err := repo.Stat(ctx, st.Revision)
25822591
if err != nil {
25832592
goto omitVCS

src/cmd/go/internal/modfetch/repo.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -222,23 +222,27 @@ func Lookup(ctx context.Context, proxy, path string) Repo {
222222

223223
var lookupLocalCache par.Cache[string, Repo] // path, Repo
224224

225-
// LookupLocal will only use local VCS information to fetch the Repo.
226-
func LookupLocal(ctx context.Context, path string) Repo {
225+
// LookupLocal returns a Repo that accesses local VCS information.
226+
//
227+
// codeRoot is the module path of the root module in the repository.
228+
// path is the module path of the module being looked up.
229+
// dir is the file system path of the repository containing the module.
230+
func LookupLocal(ctx context.Context, codeRoot string, path string, dir string) Repo {
227231
if traceRepo {
228232
defer logCall("LookupLocal(%q)", path)()
229233
}
230234

231235
return lookupLocalCache.Do(path, func() Repo {
232236
return newCachingRepo(ctx, path, func(ctx context.Context) (Repo, error) {
233-
repoDir, vcsCmd, err := vcs.FromDir(path, "", true)
237+
repoDir, vcsCmd, err := vcs.FromDir(dir, "", true)
234238
if err != nil {
235239
return nil, err
236240
}
237241
code, err := lookupCodeRepo(ctx, &vcs.RepoRoot{Repo: repoDir, Root: repoDir, VCS: vcsCmd}, true)
238242
if err != nil {
239243
return nil, err
240244
}
241-
r, err := newCodeRepo(code, repoDir, path)
245+
r, err := newCodeRepo(code, codeRoot, path)
242246
if err == nil && traceRepo {
243247
r = newLoggingRepo(r)
244248
}

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

+77-5
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ exec git branch -m main
3434
# Use a 0.0.0 pseudo-version when no tags are present.
3535
go build
3636
go version -m example$GOEXE
37-
stdout '\s+mod\s+example\s+v0.0.0-20220719150700-b52f952448d2\s+'
37+
stdout '\s+mod\s+example\s+v0.0.0-20220719150700-e7537ba8fd6d\s+'
3838
rm example$GOEXE
3939

4040
# Use a 0.0.0 pseudo-version if the current tag is not a valid semantic version.
4141
exec git tag 1.0.1
4242
go build
4343
go version -m example$GOEXE
44-
stdout '\s+mod\s+example\s+v0.0.0-20220719150700-b52f952448d2\s+'
44+
stdout '\s+mod\s+example\s+v0.0.0-20220719150700-e7537ba8fd6d\s+'
4545
rm example$GOEXE
4646

4747
# Use the current tag which has a valid semantic version to stamp the version.
@@ -79,14 +79,14 @@ exec git commit -m 'commit 3'
7979
# Use a pseudo-version when current commit doesn't match a tagged version.
8080
go build
8181
go version -m example$GOEXE
82-
stdout '\s+mod\s+example\s+v1.0.3-0.20220719150702-deaeab06f7fe\s+'
82+
stdout '\s+mod\s+example\s+v1.0.3-0.20220719150702-b0226f18a7ae\s+'
8383
rm example$GOEXE
8484

8585
# Use pseudo+dirty when uncommitted changes are present.
8686
mv README2 README3
8787
go build
8888
go version -m example$GOEXE
89-
stdout '\s+mod\s+example\s+v1.0.3-0.20220719150702-deaeab06f7fe\+dirty\s+'
89+
stdout '\s+mod\s+example\s+v1.0.3-0.20220719150702-b0226f18a7ae\+dirty\s+'
9090
rm example$GOEXE
9191

9292
# Make sure we always use the previously tagged version to generate the pseudo-version at a untagged revision.
@@ -105,7 +105,7 @@ exec git tag v1.0.4
105105
exec git checkout ':/commit 4'
106106
go build
107107
go version -m example$GOEXE
108-
stdout '\s+mod\s+example\s+v1.0.3-0.20220719150703-2e239bf29c13\s+'
108+
stdout '\s+mod\s+example\s+v1.0.3-0.20220719150703-2ebc76937b49\s+'
109109
rm example$GOEXE
110110

111111
# Create +incompatible module
@@ -121,6 +121,67 @@ go version -m example$GOEXE
121121
stdout '\s+mod\s+example\s+v2.0.0\+incompatible.dirty\s+'
122122
rm example$GOEXE
123123

124+
# Make sure v2 works as expected.
125+
exec git checkout v1.0.4
126+
go mod edit -module example/v2
127+
exec git add .
128+
exec git commit -m 'commit 7'
129+
exec git tag v2.1.1
130+
go build
131+
go version -m example$GOEXE
132+
stdout '\s+mod\s+example/v2\s+v2.1.1\s+'
133+
rm example$GOEXE
134+
135+
# v2+dirty
136+
mv README5 README6
137+
go build
138+
go version -m example$GOEXE
139+
stdout '\s+mod\s+example/v2\s+v2.1.1\+dirty\s+'
140+
rm example$GOEXE
141+
142+
# v2+pseudo
143+
exec git add .
144+
exec git commit -m 'commit 8'
145+
go build
146+
go version -m example$GOEXE
147+
stdout '\s+mod\s+example/v2\s+v2.1.2-0.20220719150704-0ebeb94ecde2\s+'
148+
rm example$GOEXE
149+
150+
# v2+pseudo+dirty
151+
mv README6 README7
152+
go build
153+
go version -m example$GOEXE
154+
stdout '\s+mod\s+example/v2\s+v2.1.2-0.20220719150704-0ebeb94ecde2\+dirty\s+'
155+
rm example$GOEXE
156+
157+
# modules in subdirectories should be stamped with the correct tag
158+
exec git add .
159+
cd subdir
160+
exec git commit -m 'commit 9'
161+
go build
162+
go version -m subdir$GOEXE
163+
# missing tag creates a pseudo version with v2.0.0
164+
stdout '\s+mod\s+example/subdir/v2\s+v2.0.0-20220719150704-fbef6799938f\s+'
165+
rm subdir$GOEXE
166+
# tag with subdir
167+
exec git tag subdir/v2.1.0
168+
go build
169+
go version -m subdir$GOEXE
170+
stdout '\s+mod\s+example/subdir/v2\s+v2.1.0\s+'
171+
# v2+dirty
172+
mv ../README7 README8
173+
go build
174+
go version -m subdir$GOEXE
175+
stdout '\s+mod\s+example/subdir/v2\s+v2.1.0\+dirty\s+'
176+
rm subdir$GOEXE
177+
178+
# modules in a subdirectory without a go.mod in the root should result in (devel)
179+
rm ../go.mod
180+
go build
181+
go version -m subdir$GOEXE
182+
stdout '\s+mod\s+example/subdir/v2\s+\(devel\)\s+'
183+
rm subdir$GOEXE
184+
124185
-- $WORK/repo/go.mod --
125186
module example
126187

@@ -133,6 +194,17 @@ func main() {
133194
-- $WORK/copy/README --
134195
hello
135196

197+
-- $WORK/repo/subdir/go.mod --
198+
module example/subdir/v2
199+
200+
go 1.18
201+
202+
-- $WORK/repo/subdir/main.go --
203+
package main
204+
205+
func main() {
206+
}
207+
136208
-- $WORK/home/gopher/.gitconfig --
137209
[user]
138210
name = Go Gopher

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ stdout '^\tbuild\tvcs.modified=true$'
4444
cd ..
4545

4646
# Revision and commit time are tagged for repositories with commits.
47-
exec bzr add a README
47+
exec bzr add a README go.mod
4848
exec bzr commit -m 'initial commit'
4949
cd a
5050
go install
@@ -61,7 +61,7 @@ cd ..
6161
cp README README2
6262
exec bzr add a README2
6363
exec bzr commit -m 'second commit'
64-
exec bzr tag v1.2.3
64+
exec bzr tag a/v1.2.3
6565
cd a
6666
go install
6767
go version -m $GOBIN/a$GOEXE
@@ -114,6 +114,10 @@ exit 1
114114
-- repo/README --
115115
Far out in the uncharted backwaters of the unfashionable end of the western
116116
spiral arm of the Galaxy lies a small, unregarded yellow sun.
117+
-- repo/go.mod --
118+
module example.com
119+
120+
go 1.18
117121
-- repo/a/go.mod --
118122
module example.com/a
119123

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ go version -m $GOBIN/a$GOEXE
4141
stdout '^\tbuild\tvcs.revision=0000000000000000000000000000000000000000$'
4242
stdout '^\tbuild\tvcs.time=1970-01-01T00:00:00Z$'
4343
stdout '^\tbuild\tvcs.modified=true$'
44-
stdout '\s+mod\s+example.com/a\s+v0.0.0-19700101000000-000000000000\+dirty'
44+
stdout '\s+mod\s+example.com/a\s\(devel\)\s+'
4545
cd ..
4646

4747
# Revision and commit time are tagged for repositories with commits.
48-
exec hg add a README
48+
exec hg add a README go.mod
4949
exec hg commit -m 'initial commit' --user test-user --date '2024-07-31T01:21:27+00:00'
50-
exec hg tag v1.2.3
50+
exec hg tag a/v1.2.3
5151
# Switch back to the tagged branch.
5252
# Tagging a commit causes a new commit to be created. (See https://repo.mercurial-scm.org/hg/help/revsets)
5353
exec hg update '.~1'
5454
cd a
5555
go install
5656
go version -m $GOBIN/a$GOEXE
57-
stdout '^\tbuild\tvcs.revision=71eaed52daeaafea83cb604f75b0a0336ef2c345$'
57+
stdout '^\tbuild\tvcs.revision=eae91df98b5dd3c4451accf64c683ddc3edff6a9$'
5858
stdout '^\tbuild\tvcs.time=2024-07-31T01:21:27Z$'
5959
stdout '^\tbuild\tvcs.modified=false$'
6060
stdout '\s+mod\s+example.com/a\s+v1.2.3\s+'
@@ -73,7 +73,7 @@ exec hg status
7373
stdout '^.+'
7474
go install
7575
go version -m $GOBIN/a$GOEXE
76-
stdout '^\tbuild\tvcs.revision=71eaed52daeaafea83cb604f75b0a0336ef2c345$'
76+
stdout '^\tbuild\tvcs.revision=eae91df98b5dd3c4451accf64c683ddc3edff6a9$'
7777
stdout '^\tbuild\tvcs.time=2024-07-31T01:21:27Z$'
7878
stdout '^\tbuild\tvcs.modified=false$'
7979
stdout '\s+mod\s+example.com/a\s+v1.2.3\s+'
@@ -112,6 +112,10 @@ exit 1
112112
-- repo/README --
113113
Far out in the uncharted backwaters of the unfashionable end of the western
114114
spiral arm of the Galaxy lies a small, unregarded yellow sun.
115+
-- repo/go.mod --
116+
module example.com
117+
118+
go 1.18
115119
-- repo/a/go.mod --
116120
module example.com/a
117121

0 commit comments

Comments
 (0)