x/tools/go/packages: 标准库和命令行模块的 Package.Module 未设置,

mspsb9vt  于 3个月前  发布在  Go
关注(0)|答案(5)|浏览(47)

gopackages命令(如结尾所示进行了修补)显示了每个包的模块:

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"

但是对于标准包和工具,它什么也不显示:

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"

这似乎是一个bug。即使"go list"没有报告标准包的Module信息,go/packages也应该合成一些内容。无论如何,它都应该记录实际执行的操作。

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
nr7wwzry

nr7wwzry1#

https://go.dev/cl/565477提到了这个问题:go/packages/gopackages: display module

lc8prwob

lc8prwob2#

https://go.dev/cl/565475提到了这个问题:gopls/internal/cache: fix two bugs related to workspace packages

ffvjumwh

ffvjumwh3#

https://go.dev/cl/565457提到了这个问题:[gopls-release-branch.0.15] gopls/internal/cache: fix two bugs related to workspace packages

llycmphe

llycmphe4#

相关:#61174 (评论)

yfjy0ee7

yfjy0ee75#

https://go.dev/cl/588141提到了这个问题:go/packages: document fields that are part of JSON schema

相关问题