Skip to content

Commit

Permalink
go/packages: add Target field and NeedTarget LoadMode bit
Browse files Browse the repository at this point in the history
Adding the NeedTarget LoadMode bit will populate the Target field on a
packages.Package with the Target from the driver. The Target field is
populated with the install path to the .a file for libraries and the
executable file for main packages.

In module mode there usually isn't an install target for .a files
(before Go 1.20, .a files for std were installed in $GOROOT/pkg, but Go
1.20 stopped installing them, though this behavior can be changed with
the installgoroot GODEBUG setting).  That means the target is primarily
useful for main packages or in GOPATH mode.

There isn't much additional work that needs to be done on the go command
side to determine the target, if there will be one, once the name and
files of the package are determined. One alternative to adding a
NeedTarget bit would then be to reuse one of the previously existing
bits to produce Target.  But neither NeedFiles or NeedName's meanings
are a good fit for Target so this CL adds a new bit.

Fixes golang/go#38445

Change-Id: I718ec22ff1f82a672449c586b007398714b8c10e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/635778
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
matloob committed Dec 16, 2024
1 parent c73f4c8 commit bf42cd7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
5 changes: 5 additions & 0 deletions go/packages/golist.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ type jsonPackage struct {
ImportPath string
Dir string
Name string
Target string
Export string
GoFiles []string
CompiledGoFiles []string
Expand Down Expand Up @@ -506,6 +507,7 @@ func (state *golistState) createDriverResponse(words ...string) (*DriverResponse
Name: p.Name,
ID: p.ImportPath,
Dir: p.Dir,
Target: p.Target,
GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles),
CompiledGoFiles: absJoin(p.Dir, p.CompiledGoFiles),
OtherFiles: absJoin(p.Dir, otherFiles(p)...),
Expand Down Expand Up @@ -811,6 +813,9 @@ func jsonFlag(cfg *Config, goVersion int) string {
if cfg.Mode&NeedEmbedPatterns != 0 {
addFields("EmbedPatterns")
}
if cfg.Mode&NeedTarget != 0 {
addFields("Target")
}
return "-json=" + strings.Join(fields, ",")
}

Expand Down
1 change: 1 addition & 0 deletions go/packages/loadmode_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var modes = [...]struct {
{NeedModule, "NeedModule"},
{NeedEmbedFiles, "NeedEmbedFiles"},
{NeedEmbedPatterns, "NeedEmbedPatterns"},
{NeedTarget, "NeedTarget"},
}

func (mode LoadMode) String() string {
Expand Down
7 changes: 7 additions & 0 deletions go/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ const (
// NeedEmbedPatterns adds EmbedPatterns.
NeedEmbedPatterns

// NeedTarget adds Target.
NeedTarget

// Be sure to update loadmode_string.go when adding new items!
)

Expand Down Expand Up @@ -479,6 +482,10 @@ type Package struct {
// information for the package as provided by the build system.
ExportFile string

// Target is the absolute install path of the .a file, for libraries,
// and of the executable file, for binaries.
Target string

// Imports maps import paths appearing in the package's Go source files
// to corresponding loaded Packages.
Imports map[string]*Package
Expand Down
55 changes: 51 additions & 4 deletions go/packages/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2322,8 +2322,8 @@ func TestLoadModeStrings(t *testing.T) {
"(NeedName|NeedExportFile)",
},
{
packages.NeedForTest | packages.NeedEmbedFiles | packages.NeedEmbedPatterns,
"(NeedForTest|NeedEmbedFiles|NeedEmbedPatterns)",
packages.NeedForTest | packages.NeedTarget | packages.NeedEmbedFiles | packages.NeedEmbedPatterns,
"(NeedForTest|NeedEmbedFiles|NeedEmbedPatterns|NeedTarget)",
},
{
packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedDeps | packages.NeedExportFile | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedTypesSizes,
Expand All @@ -2334,8 +2334,8 @@ func TestLoadModeStrings(t *testing.T) {
"(NeedName|NeedModule)",
},
{
packages.NeedName | 0x10000, // off the end (future use)
"(NeedName|0x10000)",
packages.NeedName | 0x100000, // off the end (future use)
"(NeedName|0x100000)",
},
{
packages.NeedName | 0x400, // needInternalDepsErrors
Expand Down Expand Up @@ -3339,6 +3339,53 @@ func Foo() int { return a.Foo() }
t.Logf("Packages: %+v", pkgs)
}

// TestTarget tests the new field added as part of golang/go#38445.
// The test uses GOPATH mode because non-main packages don't usually
// have install targets in module mode.
func TestTarget(t *testing.T) {
testenv.NeedsGoPackages(t)

dir := writeTree(t, `
-- gopath/src/a/a.go --
package a
func Foo() {}
-- gopath/src/b/b.go --
package main
import "a"
func main() {
a.Foo()
}
`)
gopath := filepath.Join(dir, "gopath")

pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedTarget,
Env: []string{"GOPATH=" + gopath, "GO111MODULE=off"},
}, filepath.Join(gopath, "src", "..."))
if err != nil {
t.Fatal(err)
}
var goexe string
if runtime.GOOS == "windows" {
goexe = ".exe"
}
want := map[string]string{
"a": filepath.Join(gopath, "pkg", runtime.GOOS+"_"+runtime.GOARCH, "a.a"),
"b": filepath.Join(gopath, "bin", "b"+goexe),
}
got := make(map[string]string)
packages.Visit(pkgs, nil, func(pkg *packages.Package) {
got[pkg.PkgPath] = pkg.Target
})
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Load returned mismatching Target fields (pkgpath->target -want +got):\n%s", diff)
}
t.Logf("Packages: %+v", pkgs)
}

// TestMainPackagePathInModeTypes tests (*types.Package).Path() for
// main packages in mode NeedTypes, a regression test for #70742, a
// bug in cmd/compile's export data that caused them to appear as
Expand Down

0 comments on commit bf42cd7

Please sign in to comment.