Skip to content

proposal: cmd/go: include the raw import path in the output of go list -json #64193

@bcmills

Description

@bcmills

Background

The ImportPath field in the output of go list -json is not quite what it says on the tin. Instead of a package import path, it contains an import path augmented with a suffix for uniqueness.

This has led to several bugs and some confusion in the past:

It also adds complexity in tools that deal with the output of go list. For example, golang.org/x/tools/go/packages renames the field to ID and parses out the package path in an ad-hoc manner:
https://cs.opensource.google/go/x/tools/+/master:go/packages/golist.go;l=544-549;drc=f60f2e6aa42c945111053771ea53938694791d83

Since the way one interacts with the go command is by passing it package import paths, the need for this kind of ad-hoc parsing requires that users building tools on top of cmd/go must implement their own ad-hoc parsing, with varying degrees of accuracy.

Unfortunately, too many tools already expect the ImportPath field to contain this extra information. We cannot feasibly rename ImportPath itself to be clearer.

Proposal

I propose that we add a new field to the Package struct format produced by go list, containing the original, unmodified path by which the package's source code was located (in GOROOT, GOPATH, or the module cache).

I suggest that we name the new field PackagePath, along the lines of the existing PkgPath field reported by x/tools/go/packages.

Specifically:

  • The path should not contain the suffix .test or _test unless that suffix is literally included in the directory or module path containing the package source code.
  • The path should not contain suffixes to denote test or PGO variants.
  • As a result, multiple packages reported by go list may share the same PackagePath. They can be distinguished using the (misnamed) ImportPath field, whose semantics remain unchanged.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions