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

GoStdlib includes absolute paths in the output #3994

Closed
AlessandroPatti opened this issue Jul 25, 2024 · 6 comments · Fixed by #4009
Closed

GoStdlib includes absolute paths in the output #3994

AlessandroPatti opened this issue Jul 25, 2024 · 6 comments · Fixed by #4009

Comments

@AlessandroPatti
Copy link
Contributor

What version of rules_go are you using?

0.48.0

What version of gazelle are you using?

N/A

What version of Bazel are you using?

7.2.1

Does this issue reproduce with the latest releases of all the above?

Yes

What operating system and processor architecture are you using?

Linux x86_64, but can repro on any platform

Any other potentially useful information about your toolchain?

Custom crosstool, using LLVM toolchain and custom sysroot. The crosstool setup find the sysroot and sets -I and -L to the correct relative paths

What did you do?

I am building a simple go binary, but the generated stdlib archives contain absolute paths. Looks like paths passed to the linker are expanded to absolute paths and stored in the object files. repro.zip contains a minimal repro:

$ unzip repro.zip
$ bazel build :example
$ strings $(find bazel-out/ -name cgo.a) | grep external/llvm

What did you expect to see?

Fully hermetic and reproducible builds

What did you see instead?

The full path in the output causes the output digest to change, reducing cache hits

@fmeum
Copy link
Member

fmeum commented Aug 2, 2024

The situation as I understand it:

  • go install runs the compilation in its own clean environment and thus doesn't permit us to pass in relative paths. Even worse: compiling with -x will lie to you about absolute paths as it rewrites them.
  • All linker flags are persisted in cgo comments verbatim in _cgo_gotypes.go.
  • Building the standard library doesn't actually produce any linker outputs, but the linker is run to produce objects passed in as cgo's -dynimport. Without proper linker flags, it may fail and then internal linking would no longer be possible.

The simplest fix I see would be to extend the existing filterbuildid tool to also remove all mentions of the execpath in _cgo_gotypes.go after the cgo command has completed.

@fmeum
Copy link
Member

fmeum commented Aug 2, 2024

@AlessandroPatti Could you test the fix in #4005?

@AlessandroPatti
Copy link
Contributor Author

strings $(find bazel-out/ -name cgo.a) | grep external/llvm

@fmeum Yes, it works on the repro! I'll try later on the original use case I had. It should also be applied to the stdliblist action?

@AlessandroPatti
Copy link
Contributor Author

AlessandroPatti commented Aug 2, 2024

@fmeum I also have an alternative solution here, which instead just defers the expansion of the abs flags. Seems the abs paths are embedded only if the CGO_LDFLAGS env variable contains them. Did not get around to add tests but I could confirm it works

@fmeum
Copy link
Member

fmeum commented Aug 2, 2024

I actually prefer your solution as the more principled one, so please send a PR if possible. A go_bazel_test that runs strings in a similar way as in your repro would be very welcome as well.

@AlessandroPatti
Copy link
Contributor Author

@fmeum Opened #4009

@fmeum fmeum closed this as completed in 64759ee Aug 7, 2024
tyler-french pushed a commit that referenced this issue Aug 30, 2024
**What type of PR is this?**

Bug fix

**What does this PR do? Why is it needed?**

It fixes reproducibility issues reported in
#3994 by lazy-expanding
paths to absolute paths

**Which issues(s) does this PR fix?**

Fixes #3994
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants