[go] cmd/go: stamp VCS info when building .go files directly

3 views
Skip to first unread message

Quan Tong (Gerrit)

unread,
Nov 12, 2023, 8:43:33 PM11/12/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Quan Tong has uploaded this change for review.

View Change

cmd/go: stamp VCS info when building .go files directly

The existing implementation only stamps VCS info
if the requested package is within a module in the workspace.

Fixes #51279

Change-Id: I014713138cec4cb8d1cbe7488c35389c630b011f
---
M src/cmd/go/internal/load/pkg.go
M src/cmd/go/testdata/script/build_buildvcs_auto.txt
2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 1549800..1a96b4f 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -2451,9 +2451,6 @@
setPkgErrorf("error obtaining VCS status: %v\n\tUse -buildvcs=false to disable VCS stamping.", err)
}

- var repoDir string
- var vcsCmd *vcs.Cmd
- var err error
const allowNesting = true

wantVCS := false
@@ -2467,6 +2464,11 @@
panic(fmt.Sprintf("unexpected value for cfg.BuildBuildvcs: %q", cfg.BuildBuildvcs))
}

+ repoDir, vcsCmd, err := vcs.FromDir(base.Cwd(), "", allowNesting)
+ if err != nil && !errors.Is(err, os.ErrNotExist) {
+ setVCSError(err)
+ return
+ }
if wantVCS && p.Module != nil && p.Module.Version == "" && !p.Standard {
if p.Module.Path == "bootstrap" && cfg.GOROOT == os.Getenv("GOROOT_BOOTSTRAP") {
// During bootstrapping, the bootstrap toolchain is built in module
@@ -2474,11 +2476,6 @@
// (so the bootstrap toolchain packages don't even appear to be in GOROOT).
goto omitVCS
}
- repoDir, vcsCmd, err = vcs.FromDir(base.Cwd(), "", allowNesting)
- if err != nil && !errors.Is(err, os.ErrNotExist) {
- setVCSError(err)
- return
- }
if !str.HasFilePathPrefix(p.Module.Dir, repoDir) &&
!str.HasFilePathPrefix(repoDir, p.Module.Dir) {
// The module containing the main package does not overlap with the
@@ -2494,6 +2491,19 @@
goto omitVCS
}
}
+
+ modRepoDir, _, err := vcs.FromDir(p.Module.Dir, "", allowNesting)
+ if err != nil {
+ setVCSError(err)
+ return
+ }
+ if modRepoDir != repoDir {
+ if cfg.BuildBuildvcs != "auto" {
+ setVCSError(fmt.Errorf("main module is in repository %q but current directory is in repository %q", modRepoDir, repoDir))
+ return
+ }
+ goto omitVCS
+ }
}
if repoDir != "" && vcsCmd.Status != nil {
// Check that the current directory, package, and module are in the same
@@ -2512,18 +2522,6 @@
}
goto omitVCS
}
- modRepoDir, _, err := vcs.FromDir(p.Module.Dir, "", allowNesting)
- if err != nil {
- setVCSError(err)
- return
- }
- if modRepoDir != repoDir {
- if cfg.BuildBuildvcs != "auto" {
- setVCSError(fmt.Errorf("main module is in repository %q but current directory is in repository %q", modRepoDir, repoDir))
- return
- }
- goto omitVCS
- }

st, err := vcsStatusCache.Do(repoDir, func() (vcs.Status, error) {
return vcsCmd.Status(vcsCmd, repoDir)
diff --git a/src/cmd/go/testdata/script/build_buildvcs_auto.txt b/src/cmd/go/testdata/script/build_buildvcs_auto.txt
index cfd5d82..2b49496 100644
--- a/src/cmd/go/testdata/script/build_buildvcs_auto.txt
+++ b/src/cmd/go/testdata/script/build_buildvcs_auto.txt
@@ -29,6 +29,12 @@
stdout '^\tbuild\tvcs=git$'
stdout '^\tbuild\tvcs.modified=false$'

+go build -o example.exe example.go
+go version -m example.exe
+stdout '^\tbuild\tvcs=git$'
+stdout '^\tbuild\tvcs.revision=[a-f\d]{40}'
+stdout '^\tbuild\tvcs.time=.+'
+stdout '^\tbuild\tvcs.modified=true$'

# Building a binary from a different (nested) VCS repo should not stamp VCS
# info. It should be an error if VCS stamps are requested explicitly with

To view, visit change 541777. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I014713138cec4cb8d1cbe7488c35389c630b011f
Gerrit-Change-Number: 541777
Gerrit-PatchSet: 1
Gerrit-Owner: Quan Tong <quant...@gmail.com>

Vegard Stikbakke (Gerrit)

unread,
Jul 2, 2024, 2:18:45 AM (yesterday) Jul 2
to Quan Tong, goph...@pubsubhelper.golang.org, Michael Matloob, golang-co...@googlegroups.com
Attention needed from Bryan Mills and Michael Matloob

Vegard Stikbakke voted and added 1 comment

Votes added by Vegard Stikbakke

Code-Review+1

1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Vegard Stikbakke . resolved

Great patch! Would love for this to get in, we often build using `go ... cmd/<binary>/main.go` which means we need to inject the Git has differently

Open in Gerrit

Related details

Attention is currently required from:
  • Bryan Mills
  • Michael Matloob
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I014713138cec4cb8d1cbe7488c35389c630b011f
Gerrit-Change-Number: 541777
Gerrit-PatchSet: 1
Gerrit-Owner: Quan Tong <quant...@gmail.com>
Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Reviewer: Vegard Stikbakke <vegard.s...@gmail.com>
Gerrit-Attention: Bryan Mills <bcm...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Tue, 02 Jul 2024 06:18:35 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Michael Matloob (Gerrit)

unread,
Jul 2, 2024, 12:32:48 PM (yesterday) Jul 2
to Quan Tong, goph...@pubsubhelper.golang.org, Vegard Stikbakke, golang-co...@googlegroups.com
Attention needed from Bryan Mills and Quan Tong

Michael Matloob added 2 comments

File src/cmd/go/internal/load/pkg.go
Line 2467, Patchset 1 (Latest): repoDir, vcsCmd, err := vcs.FromDir(base.Cwd(), "", allowNesting)
Michael Matloob . unresolved

Moving this out of the `if` means that we ignore `wantVCS`, `p.Standard`, etc. which is not correct.

I think what we might want to do is to keep this in the next if block but change its condition to `wantVCS && !p.Standard`. And then add a further nested if for the case where `p.Module != nil`.

For the case where `p.Module == nil` we should check that the file itself is contained in the work module or workspace. See bcmills's issue comment: https://github.com/golang/go/issues/51279#issuecomment-1048196896

Line 2500, Patchset 1 (Latest): if modRepoDir != repoDir {
if cfg.BuildBuildvcs != "auto" {

setVCSError(fmt.Errorf("main module is in repository %q but current directory is in repository %q", modRepoDir, repoDir))
return
}
goto omitVCS
}
Michael Matloob . unresolved

we shouldn't be doing this check if repoDir is empty. i think it still belongs in the next if.

Perhaps we should keep this code in the next if but add a check that `p.Module` is not nil

Open in Gerrit

Related details

Attention is currently required from:
  • Bryan Mills
  • Quan Tong
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I014713138cec4cb8d1cbe7488c35389c630b011f
    Gerrit-Change-Number: 541777
    Gerrit-PatchSet: 1
    Gerrit-Owner: Quan Tong <quant...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Reviewer: Vegard Stikbakke <vegard.s...@gmail.com>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Attention: Quan Tong <quant...@gmail.com>
    Gerrit-Comment-Date: Tue, 02 Jul 2024 16:32:43 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Vegard Stikbakke (Gerrit)

    unread,
    3:54 AM (17 hours ago) 3:54 AM
    to Quan Tong, goph...@pubsubhelper.golang.org, Michael Matloob, golang-co...@googlegroups.com
    Attention needed from Bryan Mills, Michael Matloob and Quan Tong

    Vegard Stikbakke added 2 comments

    File src/cmd/go/internal/load/pkg.go
    Line 2467, Patchset 1 (Latest): repoDir, vcsCmd, err := vcs.FromDir(base.Cwd(), "", allowNesting)
    Michael Matloob . unresolved

    Moving this out of the `if` means that we ignore `wantVCS`, `p.Standard`, etc. which is not correct.

    I think what we might want to do is to keep this in the next if block but change its condition to `wantVCS && !p.Standard`. And then add a further nested if for the case where `p.Module != nil`.

    For the case where `p.Module == nil` we should check that the file itself is contained in the work module or workspace. See bcmills's issue comment: https://github.com/golang/go/issues/51279#issuecomment-1048196896

    Vegard Stikbakke

    Are there any tests we can add to `build_buildvcs_auto.txt` to show this isn't correct?

    File src/cmd/go/testdata/script/build_buildvcs_auto.txt
    Line 32, Patchset 1 (Latest):go build -o example.exe example.go
    Vegard Stikbakke . unresolved

    Should this block have a matching comment section above it? The comment above does seem to apply only to the `go build .` case ("on a package ... by default").

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Bryan Mills
    • Michael Matloob
    • Quan Tong
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I014713138cec4cb8d1cbe7488c35389c630b011f
    Gerrit-Change-Number: 541777
    Gerrit-PatchSet: 1
    Gerrit-Owner: Quan Tong <quant...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Reviewer: Vegard Stikbakke <vegard.s...@gmail.com>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Attention: Quan Tong <quant...@gmail.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Wed, 03 Jul 2024 07:54:19 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Michael Matloob <mat...@golang.org>
    unsatisfied_requirement
    open
    diffy

    Michael Matloob (Gerrit)

    unread,
    3:53 PM (5 hours ago) 3:53 PM
    to Quan Tong, goph...@pubsubhelper.golang.org, Vegard Stikbakke, golang-co...@googlegroups.com
    Attention needed from Bryan Mills and Quan Tong

    Michael Matloob voted Commit-Queue+1

    Commit-Queue+1
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Bryan Mills
    • Quan Tong
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I014713138cec4cb8d1cbe7488c35389c630b011f
    Gerrit-Change-Number: 541777
    Gerrit-PatchSet: 1
    Gerrit-Owner: Quan Tong <quant...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Reviewer: Vegard Stikbakke <vegard.s...@gmail.com>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Attention: Quan Tong <quant...@gmail.com>
    Gerrit-Comment-Date: Wed, 03 Jul 2024 19:53:20 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    open
    diffy

    Michael Matloob (Gerrit)

    unread,
    4:12 PM (5 hours ago) 4:12 PM
    to Quan Tong, goph...@pubsubhelper.golang.org, Go LUCI, Vegard Stikbakke, golang-co...@googlegroups.com
    Attention needed from Bryan Mills, Michael Matloob, Quan Tong and Vegard Stikbakke

    Michael Matloob voted and added 1 comment

    Votes added by Michael Matloob

    Commit-Queue+1

    1 comment

    File src/cmd/go/internal/load/pkg.go
    Line 2467, Patchset 1 (Latest): repoDir, vcsCmd, err := vcs.FromDir(base.Cwd(), "", allowNesting)
    Michael Matloob . unresolved

    Moving this out of the `if` means that we ignore `wantVCS`, `p.Standard`, etc. which is not correct.

    I think what we might want to do is to keep this in the next if block but change its condition to `wantVCS && !p.Standard`. And then add a further nested if for the case where `p.Module != nil`.

    For the case where `p.Module == nil` we should check that the file itself is contained in the work module or workspace. See bcmills's issue comment: https://github.com/golang/go/issues/51279#issuecomment-1048196896

    Vegard Stikbakke

    Are there any tests we can add to `build_buildvcs_auto.txt` to show this isn't correct?

    Michael Matloob

    It looks like tests are already failing because we're triggering vcs stamping when we shouldn't be.

    But here, simple test that sets -buildvcs=false should fail. For example adding the following after line 31 will fail:

        # With -buildvcs=false, 'go build' should not stamp
    # VCS metadata.

    go build -buildvcs=false -o example.exe .
    go version -m example.exe
    ! stdout '^\tbuild\tvcs=git$'
    ! stdout '^\tbuild\tvcs.modified=true$'

    p.Standard is a bit harder to test: we can't simply build a standard library package because the standard library might not be in VCS. We might have to make a fake GOROOT, but I haven't tried that.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Bryan Mills
    • Michael Matloob
    • Quan Tong
    • Vegard Stikbakke
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I014713138cec4cb8d1cbe7488c35389c630b011f
    Gerrit-Change-Number: 541777
    Gerrit-PatchSet: 1
    Gerrit-Owner: Quan Tong <quant...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Reviewer: Vegard Stikbakke <vegard.s...@gmail.com>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Attention: Vegard Stikbakke <vegard.s...@gmail.com>
    Gerrit-Attention: Quan Tong <quant...@gmail.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Wed, 03 Jul 2024 20:12:13 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Vegard Stikbakke <vegard.s...@gmail.com>
    Comment-In-Reply-To: Michael Matloob <mat...@golang.org>
    unsatisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages