bazel-contrib / bazel-contrib/rules_go

Calling go/packages.Load during Bazel build

Open
#1,996 7 comments 9 reactions 0 assignees View on GitHub
go question tools
Dominant language
Go
Stars
1.5k
Forks
760
Avg merge
1d 11h
Merged PRs (30d)
12

Description

I am writing a Go code generation tool that depends on [go/packages](https://godoc.org/golang.org/x/tools/go/packages#Load) for creating a [types.Package](https://godoc.org/go/types#Package) from a particular import path.

I've written a custom Bazel rule that depends on a target `go_library` label so that I can pass the Go library's `importpath` to the executable. But given the default behavior of `go/packages`, the `go list` driver is selected and Bazel fails to resolve the import path (as expected):
```
couldn't exec 'go [list -e -json -compiled -test=false -export=true -deps=true
-find=false -- <$importpath>]': exec: "go": executable file not found in
$PATH *exec.Error
```

I've noticed that the `go/packages.Load` API defines a [Config.Env](https://godoc.org/golang.org/x/tools/go/packages#Config) parameter to override the Go packages driver, and [looks for the GOPACKAGESDRIVER environment variable](https://github.com/golang/tools/blob/master/go/packages/external.go#L31), but I'm not sure what implementation I need to provide in order to resolve the necessary package information that `go/packages` expects.

I realize that there are a couple of open issues that may or may not be related, most notably https://github.com/bazelbuild/rules_go/issues/1644 and recent conversations in https://github.com/bazelbuild/rules_go/issues/512. This issue is more targeted at seeking guidance on how to move forward in the absence of a sophisticated, upstream solution like that mentioned in https://github.com/bazelbuild/rules_go/issues/1644.

Contributor guide

Open the contributing guide

Research direction

Start with the go/packages.Load API, Config.Env, and the GOPACKAGESDRIVER handling in go/packages/external.go. Review the referenced rules_go issues 1644 and 512 for existing approaches to resolving Go packages under Bazel. Done would require a documented or implemented way for a Bazel-built tool to supply the package information that go/packages expects.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.