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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ENABLE_BAZEL_PACKAGES_LOAD_HACK=true for poor Bazel users #40

Merged
merged 4 commits into from Feb 20, 2023

Conversation

slimsag
Copy link
Member

@slimsag slimsag commented Feb 16, 2023

Hello /Sourcegraph employee hat on/ 馃憢 On behalf of Sourcegraph, we'd like to contribute these changes back to autogold that enable users of the Bazel build systems to make use of autogold despite a bug/disagreement in Go/Bazel (see below.) We agree to the project terms & license these changes under the open source LICENSE, as we don't want the annoyance of maintaining a fork of this code on our side long term.


Context: Bazel build systems try to keep hermeticity by setting PATH="." - but Go does not like this as it is a security concern; almost all Go tooling relies on golang.org/x/tools/go/packages.Load which behind the scenes must invoke go and uses the secure version of x/sys/execabs, but ultimately this means Go tools like autogold cannot be run in Bazel:

golang/go#57304

Autogold relies on packages.Load in order to determine the Go package name / path when writing out a Go AST representation of the value passed in; but the issue above means autogold cannot be used with Bazel without removing "." from your PATH, which Bazel claims breaks hermeticity (one of the whole reasons people use Bazel.)

For Bazel users, we allow them to set ENABLE_BAZEL_HACKS=true which causes autogold to guess/infer package names and paths using stack trace information and import paths. This is not perfect, it doesn't respect packages whose import paths donot match their defined package foo statement for example - but it's sufficient to enable autogold to be used in Bazel build environments where the above Go/Bazel bug is found.

  • By selecting this checkbox, I agree to license my contributions to this project under the license(s) described in the LICENSE file, and I have the right to do so or have received permission to do so by an employer or client I am producing work for whom has this right.

Bazel build systems try to keep hermeticity by setting PATH="." - but Go does not like this as
it is a security concern; almost all Go tooling relies on golang.org/x/tools/go/packages.Load
which behind the scenes must invoke `go` and uses the secure version of x/sys/execabs, but
ultimately this means Go tools like autogold cannot be run in Bazel:

golang/go#57304

Autogold relies on `packages.Load` in order to determine the Go package name / path when writing
out a Go AST representation of the value passed in; but the issue above means autogold cannot be
used with Bazel without removing "." from your PATH, which Bazel claims breaks hermeticity (one
of the whole reasons people use Bazel.)

For Bazel users, we allow them to set ENABLE_BAZEL_HACKS=true which causes autogold to guess/infer
package names and paths using stack trace information and import paths. This is not perfect, it
doesn't respect packages whose import paths donot match their defined `package foo` statement for
example - but it's sufficient to enable autogold to be used in Bazel build environments where the
above Go/Bazel bug is found.

Signed-off-by: Stephen Gutekanst <stephen@sourcegraph.com>
@codecov-commenter
Copy link

codecov-commenter commented Feb 16, 2023

Codecov Report

Merging #40 (4c87f57) into main (bae639c) will decrease coverage by 2.72%.
The diff coverage is 5.00%.

馃摚 This organization is not using Codecov鈥檚 GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main      #40      +/-   ##
==========================================
- Coverage   35.26%   32.54%   -2.72%     
==========================================
  Files           5        6       +1     
  Lines         346      381      +35     
==========================================
+ Hits          122      124       +2     
- Misses        198      229      +31     
- Partials       26       28       +2     
Impacted Files Coverage 螖
diff.go 64.00% <0.00%> (-5.57%) 猬囷笍
expect.go 36.41% <0.00%> (-0.38%) 猬囷笍
bazel.go 6.45% <6.45%> (酶)

馃摚 We鈥檙e building smart automated test selection to slash your CI/CD build times. Learn more

Signed-off-by: Stephen Gutekanst <stephen@sourcegraph.com>
Signed-off-by: Stephen Gutekanst <stephen@sourcegraph.com>
@jhchabran
Copy link
Contributor

I confirm it's working after testing things on our side. The thing we're wondering about is that if we should name that variable BAZEL_TEST=1 which would by default enable this behaviour. That being said, the fact that this would be implicit (because Bazel sets that variable to 1 on its own) might be a problem.

Another solution would be to rename it to ENABLE_AUTOGOLD_BAZEL_HACKS so anyone reading the Buildfile would understand which package is affected by the env var.

@slimsag
Copy link
Member Author

slimsag commented Feb 17, 2023

Gotcha, cool, I'll get this merged soon - as for env var naming, I consider the following in naming this:

  1. Ultimately, this is a bug in Go packages.Load or in Bazel - not autogold; merging these hacks here really isn't correct but I'm doing so to be kind to our bazel users :) these hacks have drawbacks though, the correct fix is in packages.Load or Bazel ultimately. One should know if they are using hacks, and so it shouldn't be enabled by default / implicitly.
  2. Given that pretty much every Go tool out there uses packages.Load, it seems likely others will run into this with other non-autogold tooling; in such a case it'd be nice to not have to specify one env var for each tool you use, if possible.
  3. It should convey it is Bazel-specific (contains BAZEL)

Thus I come up with:

  • Must contain HACK
  • Must contain BAZEL
  • Should not contain AUTOGOLD
  • May contain GO, PACKAGES, LOAD

I can see how ENABLE_BAZEL_HACKS might appear as a statement about my perception of Bazel though ;) How's ENABLE_BAZEL_PACKAGES_LOAD_HACK instead in all seriousness?

Separately, if we do want a different name (or on-by-default behavior) in sourcegraph we're totally free to fork (really, I don't mind that at all so if that makes sense for us there that works for me.)

@davejrt
Copy link

davejrt commented Feb 17, 2023

This totally makes sense. I don't believe we'll have any issue with using ENABLE_BAZEL_PACKAGES_LOAD_HACK as the name as we can document this reflecting your thoughts above to ensure it's well communicated this issue is further upstream.

@jhchabran
Copy link
Contributor

I'm totally fine with ENABLE_BAZEL_PACKAGES_LOAD_HACK, it conveys enough context, that's perfect.

Ultimately, this is a bug in Go packages.Load or in Bazel - not autogold;

We all agree on that. We're seeing conflicting design goals here and it's great that we can compensate that on autogold's side.

Ultimately, this is a bug in Go packages.Load or in Bazel

Yes, execabs clashes with how Bazel expects things to be, but I have very little hope that this would be fixed soon, it's pretty niche.

Given that pretty much every Go tool out there uses packages.Load, it seems likely others will run into this with other non-autogold tooling; in such a case it'd be nice to not have to specify one env var for each tool you use, if possible.

Makes sense, thanks.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
@slimsag slimsag changed the title Add ENABLE_BAZEL_HACKS=true for poor Bazel users Add ENABLE_BAZEL_PACKAGES_LOAD_HACK=true for poor Bazel users Feb 20, 2023
@slimsag slimsag merged commit f2fed4f into hexops:main Feb 20, 2023
@slimsag slimsag deleted the bazel branch February 20, 2023 18:31
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.

None yet

4 participants