Skip to content

Commit a86ea80

Browse files
committed
cmd/go/internal/tool: set Internal.ExeName on tool's package
While the cached name of an executable is set based on the base name of the package path, the executable produced as the output of link doesn't have ExeName set on it and is just called a.out (with a .exe suffix on Windows). Set ExeName so that the first time the binary is run, from the directory link is run in, it has the right name for ps. For #48429 Change-Id: Ic049304ec6fd5b23c2f5aaaf91aa58d79fe5a7ba Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/630695 Reviewed-by: Conrad Irwin <[email protected]> Reviewed-by: Hongxiang Jiang <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent a3a31ec commit a86ea80

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/cmd/go/internal/tool/tool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func buildAndRunModtool(ctx context.Context, tool string, args []string) {
287287
pkgOpts := load.PackageOpts{MainOnly: true}
288288
p := load.PackagesAndErrors(ctx, pkgOpts, []string{tool})[0]
289289
p.Internal.OmitDebug = true
290+
p.Internal.ExeName = path.Base(p.ImportPath)
290291

291292
a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p)
292293
a1.CacheExecutable = true
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[short] skip 'runs go build'
2+
3+
# First run: executable for bar is not cached.
4+
# Make sure it's not called a.out
5+
go tool bar
6+
stdout 'my name is: bar'$GOEXE
7+
! stdout 'a.out'
8+
9+
# Second run: executable is cached. Make sure it
10+
# has the right name.
11+
go tool bar
12+
stdout 'my name is: bar'$GOEXE
13+
! stdout 'a.out'
14+
15+
-- go.mod --
16+
module example.com/foo
17+
18+
go 1.24
19+
20+
tool example.com/foo/bar
21+
-- bar/bar.go --
22+
package main
23+
24+
import (
25+
"fmt"
26+
"os"
27+
"path/filepath"
28+
)
29+
30+
func main() {
31+
fmt.Println("my name is:", filepath.Base(os.Args[0]))
32+
}

0 commit comments

Comments
 (0)