golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.2k stars 17.7k forks source link

cmd/go: go tool doesn't pass command name as os.Args[0] when flags are present #70509

Open seankhliao opened 6 hours ago

seankhliao commented 6 hours ago

Go version

go version devel go1.24-efe0a86551 2024-11-21 20:50:15 +0000 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='0'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v3'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GODEBUG=''
GOENV='/home/user/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS='-trimpath "-ldflags=-s -w" -vet=all'
GOGCCFLAGS='-fPIC -m64 -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3521496734=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/user/tmp/testrepo0896/go.mod'
GOMODCACHE='/home/user/.data/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/.data/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/user/sdk/gotip'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/user/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='path'
GOTOOLDIR='/home/user/sdk/gotip/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.24-efe0a86551 2024-11-21 20:50:15 +0000'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

With a basic tool like: https://go.dev/play/p/0MpgRS3gwFS Invoking it results in:

$ go tool mytool
[/home/user/.cache/go-build/05/05c95035c59ab02d5d47f8b72f30bf94aafdbe7a97a027a1a3ac48961b3a0fda-d/mytool]

$ go tool mytool -hello world
[-hello world]

What did you see happen?

The tool doesn't receive its own name in os.Args[0], messing up argument / flag parsing.

What did you expect to see?

the usual os.Args

gabyhelp commented 6 hours ago

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

seankhliao commented 6 hours ago

cc @matloob @samthanawalla @ConradIrwin

gopherbot commented 9 minutes ago

Change https://go.dev/cl/631075 mentions this issue: go/cmd: pass os.Args[0] to mod tools

ConradIrwin commented 8 minutes ago

Thanks for testing this out @seankhliao! - sent a fix, and please let me know if you run into anything else with this feature.