golang / go

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

cmd/go: When `go list -m -json -versions` returns no versions, it does not include an `Origin` #66077

Open mondy opened 8 months ago

mondy commented 8 months ago

Go version

go version go1.22.0 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\myname\AppData\Local\go-build
set GOENV=C:\Users\myname\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\myname\go\pkg\mod
set GONOPROXY=github.com/myname/*
set GONOSUMDB=github.com/myname/*
set GOOS=windows
set GOPATH=C:\Users\myname\go
set GOPRIVATE=github.com/myname/*
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\myname\scoop\apps\go\current
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Users\myname\scoop\apps\go\current\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.0
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\myname\AppData\Local\Temp\go-build3669545043=/tmp/go-build -gno-record-gcc-switches

What did you do?

Execute go list -m -json -versions fyne.io/fyne/v2/cmd/fyne on go version 1.22.0 and 1.21.7.

What did you see happen?

Execute go list -m -json -versions fyne.io/fyne/v2/cmd/fyne. Output:

{
        "Path": "fyne.io/fyne/v2/cmd/fyne"
}

Origin field is not output.

Execute with go version 1.21.7.

{
        "Path": "fyne.io/fyne/v2/cmd/fyne",
        "Origin": {
                "VCS": "git",
                "URL": "https://github.com/fyne-io/fyne",
                "Subdir": "cmd/fyne",
                "TagPrefix": "cmd/fyne/",
                "TagSum": "t1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="
        }
}

Origin field is output.

What did you expect to see?

I would like to get the output of the Origin field in go version 1.22.0 as well.

seankhliao commented 8 months ago

the issue title says subdirectory, but the report doesn't mention anything about directories?

mondy commented 8 months ago

the issue title says subdirectory, but the report doesn't mention anything about directories?

fyne.io/fyne/v2/cmd/fyne is subdirectory.

seankhliao commented 8 months ago

cc @bcmills

bcmills commented 8 months ago

go list -m operates on modules, not packages. fyne.io/fyne/v2/cmd/fyne is a package with the module fyne.io/fyne/v2, not a module in its own right.

Normally, asking about a path that does not refer to a valid module results in an error, but in this case the error is suppressed by the -versions flag. go list -m -json -versions fyne.io/fyne/v2/cmd/fyne is successfully informing you that there are no known versions of that module in existence. It doesn't tell you an Origin for a lack of such versions, because it doesn't know how to compute origins for things that don't exist.

bcmills commented 8 months ago

Hmm, there might be an issue here, but it's subtle.

To give a bit more detail, https://go.dev/cl/543216 and https://go.dev/cl/543155 fixed some bugs in which cmd/go was reporting incorrect Origin metadata for mixed-origin data:

In this case, the mixed origin consists of:

  1. The information that https://github.com/fyne-io/fyne does not contain a cmd/fyne at its latest commit, and does not contain any tags that would correspond to a module in that subdirectory, as well as
  2. the information that both https://fyne.io/fyne/v2/cmd/fyne?go-get=1 and https://fyne.io/fyne/v2/cmd?go-get=1 return results without go-import metadata.

In theory, the Origin structure does have enough metadata to represent both of those facts, as shown by the Origin metadata returned for a query for @latest (instead of -versions):

$ GOPROXY=direct gotip list -m -e -json fyne.io/fyne/v2/cmd/fyne@latest
{
        "Path": "fyne.io/fyne/v2/cmd/fyne",
        "Version": "latest",
        "Error": {
                "Err": "module fyne.io/fyne/v2/cmd/fyne: no matching versions for query \"latest\""
        },
        "Origin": {
                "VCS": "git",
                "URL": "https://github.com/fyne-io/fyne",
                "Subdir": "cmd/fyne",
                "Hash": "e332a5e47813bff1af9631cbbfe84654c0143189",
                "TagPrefix": "cmd/fyne/",
                "TagSum": "t1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
                "Ref": "HEAD"
        }
}

(CC @matloob @samthanawalla @suzmue)

mondy commented 8 months ago

The reason I need this feature is related to #65350. The feature I really want is to get an installable version with go install.

Find the root directory by trimming Origin.Subdir from the Path of the retrieved JSON data. And Versions can be reliably obtained from the root directory. I need the Origin information for this.

If Origin was intentionally erased (if it was decided as a specification), it may not be necessary.