golang / go

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

x/tools/go/packages: Package.Module is unset for std and cmd modules #65816

Open adonovan opened 9 months ago

adonovan commented 9 months ago

The gopackages command (patched as shown at the end) displays the module of each package:

xtools$ gopackages  golang.org/x/telemetry
Go package "golang.org/x/telemetry":
    module golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78
    package telemetry
    has no exported type info
    file /Users/adonovan/go/pkg/mod/golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78/doc.go
    file /Users/adonovan/go/pkg/mod/golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78/mode.go
    file /Users/adonovan/go/pkg/mod/golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78/types.go
    import "golang.org/x/telemetry/internal/telemetry"

xtools$ gopackages ./gopls
Go command "golang.org/x/tools/gopls":
    module golang.org/x/tools/gopls@
    package main
    has no exported type info
    file /Users/adonovan/w/xtools/gopls/main.go
    import "context"
    import "golang.org/x/tools/gopls/internal/cmd"
    import "golang.org/x/tools/gopls/internal/hooks"
    import "golang.org/x/tools/gopls/internal/telemetry"
    import "golang.org/x/tools/gopls/internal/version"
    import "golang.org/x/tools/internal/tool"
    import "os"

But for standard packages and tools, it shows nothing:

xtools$ gopackages errors
Go package "errors":
    package errors
    has no exported type info
    file /Users/adonovan/w/goroot/src/errors/errors.go
    file /Users/adonovan/w/goroot/src/errors/join.go
    file /Users/adonovan/w/goroot/src/errors/wrap.go
    import "internal/reflectlite"
    import "unsafe"

xtools$ gopackages cmd/link
Go command "cmd/link":
    package main
    has no exported type info
    file /Users/adonovan/w/goroot/src/cmd/link/doc.go
    file /Users/adonovan/w/goroot/src/cmd/link/main.go
    import "cmd/internal/sys"
    import "cmd/link/internal/amd64"
    import "cmd/link/internal/arm"
    import "cmd/link/internal/arm64"
    import "cmd/link/internal/ld"
    import "cmd/link/internal/loong64"
    import "cmd/link/internal/mips"
    import "cmd/link/internal/mips64"
    import "cmd/link/internal/ppc64"
    import "cmd/link/internal/riscv64"
    import "cmd/link/internal/s390x"
    import "cmd/link/internal/wasm"
    import "cmd/link/internal/x86"
    import "fmt"
    import "internal/buildcfg"
    import "os"

This seems like a bug. Even if "go list" doesn't report Module information for standard packages, go/packages should probably synthesize something. Either way, it should document what it actually does.

diff --git a/go/packages/gopackages/main.go b/go/packages/gopackages/main.go
index bf0b5043fc..706f13a99a 100644
--- a/go/packages/gopackages/main.go
+++ b/go/packages/gopackages/main.go
@@ -104,6 +104,7 @@ func (app *application) Run(ctx context.Context, args ...string) error {
        default:
                return tool.CommandLineErrorf("invalid mode: %s", app.Mode)
        }
+       cfg.Mode |= packages.NeedModule

        lpkgs, err := packages.Load(cfg, args...)
        if err != nil {
@@ -162,6 +163,9 @@ func (app *application) print(lpkg *packages.Package) {
                kind += "package"
        }
        fmt.Printf("Go %s %q:\n", kind, lpkg.ID) // unique ID
+       if mod := lpkg.Module; mod != nil {
+               fmt.Printf("\tmodule %s@%s\n", mod.Path, mod.Version)
+       }
        fmt.Printf("\tpackage %s\n", lpkg.Name)

        // characterize type info
gopherbot commented 9 months ago

Change https://go.dev/cl/565477 mentions this issue: go/packages/gopackages: display module

gopherbot commented 9 months ago

Change https://go.dev/cl/565475 mentions this issue: gopls/internal/cache: fix two bugs related to workspace packages

gopherbot commented 9 months ago

Change https://go.dev/cl/565457 mentions this issue: [gopls-release-branch.0.15] gopls/internal/cache: fix two bugs related to workspace packages

dominikh commented 6 months ago

Related: https://github.com/golang/go/issues/61174#issuecomment-1622471317

gopherbot commented 6 months ago

Change https://go.dev/cl/588141 mentions this issue: go/packages: document fields that are part of JSON schema