]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: don't print cached output for non-build list commands
authorMichael Matloob <matloob@golang.org>
Fri, 21 Oct 2022 18:28:46 +0000 (14:28 -0400)
committerMichael Matloob <matloob@golang.org>
Fri, 21 Oct 2022 19:37:23 +0000 (19:37 +0000)
If a user is running a go list command that wouldn't trigger a build
(for example if -export was passed), don't print the cached stdout
outputs for previous builds of the artifacts.

Fixes #56375

Change-Id: I1d3e6c01d0eb3dada941bb2783ce2ac69aa3d5d2
Reviewed-on: https://go-review.googlesource.com/c/go/+/444836
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/work/buildid.go
src/cmd/go/internal/work/exec.go
src/cmd/go/testdata/script/list_compiler_output.txt [new file with mode: 0644]

index 72e0885922410a7e3e54453ebe5ab0eecbec0f7a..e79eb4c66a1d67133b6bd13a329496ad19d7c976 100644 (file)
@@ -397,7 +397,7 @@ func (b *Builder) fileHash(file string) string {
 // during a's work. The caller should defer b.flushOutput(a), to make sure
 // that flushOutput is eventually called regardless of whether the action
 // succeeds. The flushOutput call must happen after updateBuildID.
-func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string) bool {
+func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string, printOutput bool) bool {
        // The second half of the build ID here is a placeholder for the content hash.
        // It's important that the overall buildID be unlikely verging on impossible
        // to appear in the output by chance, but that should be taken care of by
@@ -462,9 +462,11 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string)
                                        // Best effort attempt to display output from the compile and link steps.
                                        // If it doesn't work, it doesn't work: reusing the cached binary is more
                                        // important than reprinting diagnostic information.
-                                       if c := cache.Default(); c != nil {
-                                               showStdout(b, c, a.actionID, "stdout")      // compile output
-                                               showStdout(b, c, a.actionID, "link-stdout") // link output
+                                       if printOutput {
+                                               if c := cache.Default(); c != nil {
+                                                       showStdout(b, c, a.actionID, "stdout")      // compile output
+                                                       showStdout(b, c, a.actionID, "link-stdout") // link output
+                                               }
                                        }
 
                                        // Poison a.Target to catch uses later in the build.
@@ -490,9 +492,11 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string)
                // Best effort attempt to display output from the compile and link steps.
                // If it doesn't work, it doesn't work: reusing the test result is more
                // important than reprinting diagnostic information.
-               if c := cache.Default(); c != nil {
-                       showStdout(b, c, a.Deps[0].actionID, "stdout")      // compile output
-                       showStdout(b, c, a.Deps[0].actionID, "link-stdout") // link output
+               if printOutput {
+                       if c := cache.Default(); c != nil {
+                               showStdout(b, c, a.Deps[0].actionID, "stdout")      // compile output
+                               showStdout(b, c, a.Deps[0].actionID, "link-stdout") // link output
+                       }
                }
 
                // Poison a.Target to catch uses later in the build.
@@ -536,19 +540,20 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string)
                if !cfg.BuildA {
                        if file, _, err := c.GetFile(actionHash); err == nil {
                                if buildID, err := buildid.ReadFile(file); err == nil {
-                                       if err := showStdout(b, c, a.actionID, "stdout"); err == nil {
-                                               a.built = file
-                                               a.Target = "DO NOT USE - using cache"
-                                               a.buildID = buildID
-                                               if a.json != nil {
-                                                       a.json.BuildID = a.buildID
-                                               }
-                                               if p := a.Package; p != nil {
-                                                       // Clearer than explaining that something else is stale.
-                                                       p.StaleReason = "not installed but available in build cache"
-                                               }
-                                               return true
+                                       if printOutput {
+                                               showStdout(b, c, a.actionID, "stdout")
                                        }
+                                       a.built = file
+                                       a.Target = "DO NOT USE - using cache"
+                                       a.buildID = buildID
+                                       if a.json != nil {
+                                               a.json.BuildID = a.buildID
+                                       }
+                                       if p := a.Package; p != nil {
+                                               // Clearer than explaining that something else is stale.
+                                               p.StaleReason = "not installed but available in build cache"
+                                       }
+                                       return true
                                }
                        }
                }
index 9e5b1eaca99e17748b17728e4413f76bb5412056..fb1a9bbc14e21db8231aeeea4cb19a7f19487559 100644 (file)
@@ -479,7 +479,7 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
                bit(needCompiledGoFiles, b.NeedCompiledGoFiles)
 
        if !p.BinaryOnly {
-               if b.useCache(a, b.buildActionID(a), p.Target) {
+               if b.useCache(a, b.buildActionID(a), p.Target, need&needBuild != 0) {
                        // We found the main output in the cache.
                        // If we don't need any other outputs, we can stop.
                        // Otherwise, we need to write files to a.Objdir (needVet, needCgoHdr).
@@ -1384,7 +1384,7 @@ func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
 // link is the action for linking a single command.
 // Note that any new influence on this logic must be reported in b.linkActionID above as well.
 func (b *Builder) link(ctx context.Context, a *Action) (err error) {
-       if b.useCache(a, b.linkActionID(a), a.Package.Target) || b.IsCmdList {
+       if b.useCache(a, b.linkActionID(a), a.Package.Target, !b.IsCmdList) || b.IsCmdList {
                return nil
        }
        defer b.flushOutput(a)
@@ -1626,7 +1626,7 @@ func (b *Builder) linkSharedActionID(a *Action) cache.ActionID {
 }
 
 func (b *Builder) linkShared(ctx context.Context, a *Action) (err error) {
-       if b.useCache(a, b.linkSharedActionID(a), a.Target) || b.IsCmdList {
+       if b.useCache(a, b.linkSharedActionID(a), a.Target, !b.IsCmdList) || b.IsCmdList {
                return nil
        }
        defer b.flushOutput(a)
diff --git a/src/cmd/go/testdata/script/list_compiler_output.txt b/src/cmd/go/testdata/script/list_compiler_output.txt
new file mode 100644 (file)
index 0000000..5230bab
--- /dev/null
@@ -0,0 +1,16 @@
+[short] skip
+
+go install -gcflags=-m .
+stderr 'can inline main'
+go list -gcflags=-m -f '{{.Stale}}' .
+stdout 'false'
+! stderr 'can inline main'
+
+-- go.mod --
+module example.com/foo
+
+go 1.20
+-- main.go --
+package main
+
+func main() {}