Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: go list -json -m does not provide Replace.Path relative to current directory when using workspaces #54263

Open
mlaventure opened this issue Aug 4, 2022 · 3 comments
Labels
GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@mlaventure
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.18.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mlaventure/.cache/go-build"
GOENV="/home/mlaventure/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mlaventure/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mlaventure/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/mlaventure/tmp/go/workspace/fubar/go.mod"
GOWORK="/home/mlaventure/tmp/go/workspace/go.work"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build500860194=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Given a monorepo setup as follow:

~/tmp/go $ tree
.
├── foo
│   ├── foo.go
│   └── go.mod
└── workspace
    ├── fubar
    │   ├── go.mod
    │   └── main.go
    └── go.work

3 directories, 5 files

With the following go.work:

go 1.18

use ./fubar

replace github.com/foo/foo => ../foo

When getting the list of replaced modules, the .Replace.Path entry is given relative to the go.work file instead of relative to the current folder the command was invoked from. The command ran was the following:

~/tmp/go/workspace/fubar$ go list -json -m all

What did you expect to see?

Path being appropriately set to reflect the current directory

{
        "Path": "github.com/foo/foo",
        "Version": "v0.0.0-00010101000000-000000000000",
        "Replace": {
                "Path": "../../foo",
                "Dir": "/home/mlaventure/tmp/go/foo",
                "GoMod": "/home/mlaventure/tmp/go/foo/go.mod",
                "GoVersion": "1.18"
        },
        "Indirect": true,
        "Dir": "/home/mlaventure/tmp/go/foo",
        "GoMod": "/home/mlaventure/tmp/go/foo/go.mod",
        "GoVersion": "1.18"
}

What did you see instead?

{                                                 
        "Path": "github.com/foo/foo",                                                                                                                                                                                                                                                                                                                      
        "Version": "v0.0.0-00010101000000-000000000000",
        "Replace": {
                "Path": "../foo",
                "Dir": "/home/mlaventure/tmp/go/foo",
                "GoMod": "/home/mlaventure/tmp/go/foo/go.mod",
                "GoVersion": "1.18"
        },
        "Indirect": true,
        "Dir": "/home/mlaventure/tmp/go/foo",
        "GoMod": "/home/mlaventure/tmp/go/foo/go.mod",
        "GoVersion": "1.18"
}
@dmitshur dmitshur added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. GoCommand cmd/go labels Aug 4, 2022
@dmitshur dmitshur added this to the Backlog milestone Aug 4, 2022
@dmitshur
Copy link
Contributor

dmitshur commented Aug 4, 2022

CC @bcmills, @matloob.

@bcmills
Copy link
Contributor

bcmills commented Oct 12, 2023

The Path reported by go list should be relative to the root of the workspace or module, not relative to the directory in which the go command is invoked. As far as I can tell this is working as designed, and as documented in https://go.dev/ref/mod#go-mod-file-replace.

@mlaventure, did you see documented that led you to believe that the field had a different meaning?

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 12, 2023
@mlaventure
Copy link
Author

The Path reported by go list should be relative to the root of the workspace or module, not relative to the directory in which the go command is invoked. As far as I can tell this is working as designed, and as documented in https://go.dev/ref/mod#go-mod-file-replace.

@mlaventure, did you see documented that led you to believe that the field had a different meaning?

In this case, the current folder is a go module. I was expecting to get the path relative to the go.mod found in the current directory.

I should have use module instead of directory in my descriptions.

The fact that the entry is relative to the go.work and not the go.mod upon which go list is being involved leads to an unexpected/invalid value. In other words, trying to access Replace.Path from the location of Replace.Dir gives an invalid path. (e.g. /home/mlaventure/tmp/go/foo/../foo instead of /home/mlaventure/tmp/go/foo/../../foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants