Skip to content

x/tools/go/packages: relative "files=" query path are not relative to Config.Dir #65965

@fho

Description

@fho

Go version

go version go1.22.0 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN='/home/fho/go/bin/'
GOCACHE='/home/fho/.cache/go-build'
GOENV='/home/fho/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/fho/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/fho/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/tmp/repro/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3159040424=/tmp/go-build -gno-record-gcc-switches'

What did you do?

packages.Load accepts file paths via the file= pattern.
The *packages.Config parameter has a Dir field that is documented as:

Dir is the directory in which to run the build system's query tool
that provides information about the packages.
If Dir is empty, the tool is run in the current directory.

Therefore I expect that relative file query paths are relative to cfg.Dir.

I wrote a small tool to reproduce the issue.
The first command-line argument is used as cfg.Dir parameter, the second command line argument is passed as file= query pattern.

Tool + package to test the query can be found in: https://github.com/fho/golist_rel_file_query

What did you see happen?

When a relative path is specified as file= pattern, the path is made absolute to the current working directory instead of to cfg.Dir.

The go list query results in 0 packages:

$ pwd
/tmp/repro

$ tree
.
├── golist.go
├── go.mod
├── go.sum
├── oneprinter
│   └── main.go
└── util
    └── util.go
$ go run golist.go /tmp/repro/oneprinter/ main.go
packages.Load returned 0 packages

What did you expect to see?

When a relative path is specified as file= pattern, the path is made absolute to the cfg.Dir field.

Same results for running in the directory /tmp/repro:

go run golist.go /tmp/repro/oneprinter/ main.go

and running in the directory /tmp/repro/oneprinter


$ go run ../golist.go /tmp/repro/oneprinter/ main.go
packages.Load returned 1 packages
{"ID":"repro/oneprinter","Name":"main","PkgPath":"repro/oneprinter","GoFiles":["/tmp/repro/oneprinter/main.go"],"Imports":{"fmt":"fmt","repro/util":"repro/util"}}

This might be the cause for: #58726
Thanks to @barash-simplesurance for discovering the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions