Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support gcexportdata export files for loading packages instead of golang.org/x/tools/go/packages.Load #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Strum355
Copy link
Contributor

@Strum355 Strum355 commented Feb 10, 2024

Adds support for recreating packages.Package and packages.TypeInfo data from gcexportdata, which is data about the public API of a compiled package (what types does it export, what packages are indirectly exposed by it etc).

Both the gcexportdata for the sum of the transitive closures of package dependenices we are generating mocks from , as well as the source files of the packages of which we are generating mocks from are required as new inputs.

This is as an alternative to packages.Load, which is not available (or cheaply available) in certain environments (this in particular was spurred by a Bazel environment, but the idea likely extends to others like Buck{,2}).

@Strum355 Strum355 changed the title WIP: support gcexportdata export files for loading packages instead of golang.org/x/tools/go/packages.Load support gcexportdata export files for loading packages instead of golang.org/x/tools/go/packages.Load Feb 12, 2024
Copy link

sonarcloud bot commented Feb 12, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@@ -67,7 +71,7 @@ func parseFlags() (*generation.Options, error) {
app := kingpin.New(consts.Name, consts.Description).Version(consts.Version)
app.UsageWriter(os.Stdout)

app.Arg("path", "The import paths used to search for eligible interfaces").Required().StringsVar(&opts.PackageOptions[0].ImportPaths)
app.Arg("path", "The import paths used to search for eligible interfaces").StringsVar(&opts.PackageOptions[0].ImportPaths)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be covered later by https://github.com/derision-test/go-mockgen/pull/50/files#diff-bec68f6a8e7a9ffdba1264a1140ed04a177aade22c6e516c9d47cd36cd6e1656R334-R336.
It has to be non-required so that --manifest-dir can be passed in isolation

Strum355 added a commit to sourcegraph/sourcegraph-public-snapshot that referenced this pull request Feb 16, 2024
Adds a new:
- gazelle generator
- rule + rule targets + catchall target
for generating go-mockgen mocks & testing for their being up-to-date.

Each go_mockgen macro invocation adds targets for generating mocks, copying to the source tree, as well as testing whether the current source tree mocks are up-to-date.

How to use this: `bazel run //dev:go_mockgen` for the catch-all, or `bazel run //some/target:generate_mocks` for an individual package, and `bazel test //some/target:generate_mocks_tests` to test for up-to-date-ness. There is no catch-all for testing

This currently uses a fork of go-mockgen, with an open PR for upstream here: derision-test/go-mockgen#50.

Closes https://github.com/sourcegraph/sourcegraph/issues/60099

## Test plan

Extensive testing during development, including the following cases:
- Deleting a generated file and its entry in a go_library/go_test `srcs` attribute list and then re-running `sg bazel configure`
- Adding a non-existent output directory to mockgen.test.yaml and running the bash one-liner emitted to prepare the workspace for rerunning `sg bazel configure`

The existing config tests a lot of existing paths anyway (creating mocks for a 3rd party library's interface, entries for a given output file in >1 config file etc)
@@ -126,6 +131,10 @@ func parseManifest() ([]*generation.Options, error) {
opts.ForTest = true
}

if len(opts.Paths) > 0 && len(opts.Archives) > 0 {
return nil, fmt.Errorf("multiple import paths and archives are mutually exclusive")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("multiple import paths and archives are mutually exclusive")
return nil, fmt.Errorf("import paths and archives are mutually exclusive")

Is this more correct? They're both >0, not >1.

@@ -139,11 +148,15 @@ func parseManifest() ([]*generation.Options, error) {

var packageOptions []generation.PackageOptions
if len(opts.Sources) > 0 {
if len(opts.Paths) > 0 || len(opts.Interfaces) > 0 {
if len(opts.Paths) > 0 || len(opts.Interfaces) > 0 || opts.Path != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? I thought we rolled in the single path into the slice somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants