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

Break reliance on GOROOT_FINAL #3984

Merged
merged 3 commits into from
Jul 16, 2024

Conversation

JacobOaks
Copy link
Contributor

What type of PR is this?

Bug fix

What does this PR do? Why is it needed?

Due to golang/go#62047, Go 1.23 won't support GOROOT_FINAL. This means that #971 will no longer fix #969,
meaning linked binaries will contain full GOROOT paths, making them non-reproducible.

Instead of using GOROOT_FINAL, this change directly sets GOROOT when invoking the linker, which will cause the linker to continue to write GOROOT into the binary, keeping builds consistent.

An existing test that asserts on paths containing GOROOT passes with this PR. This passes on Go 1.23rc1 as well (it fails without this PR).

I'm not a rules_go expert and I'm not married to this particular solution. If there's a clever way we can set -trimpath when invoking the compiler, that might be better - but we already use that flag to trim off the bazel sandbox I believe.

Which issues(s) does this PR fix?

Fixes #3983

Due to golang/go#62047, Go 1.23 won't support `GOROOT_FINAL`.
This means that bazel-contrib#971 will no longer fix bazel-contrib#969,
meaning linked binaries will contain full GOROOT paths, making them non-reproducible.

Instead of using `GOROOT_FINAL`, this change sets `GOROOT` when invoking the linker,
which will cause the linker to continue to write `GOROOT` into the binary,
keeping builds consistent.

This works on Go 1.23rc1 as well.

I'm not a rules_go expert and I'm not married to this particular solution,
I just wanted to bring attention to the issue.
If there's a clever way we can set `-trimpath` when invoking the compiler,
that might be better - but we already use that flag to trim off the bazel sandbox I believe.
Copy link

google-cla bot commented Jul 8, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@sluongng
Copy link
Contributor

sluongng commented Jul 9, 2024

I suspect this might require a minimum Go SDK version bump to 1.21, in rules_go setup as well as in the test.

Is it possible to make the behavior conditional based on the current SDK version?

@JacobOaks
Copy link
Contributor Author

Hey @sluongng - can you elaborate on why you suspect that? As far as I can tell, rules_go seems to be set up for 1.21 already.

@sluongng
Copy link
Contributor

It's from the issue you linked.

As of Go 1.21, in order to provide reproducible builds of the Go toolchain (golang/go#57120), cmd/dist started building releases with the -trimpath flag set, which incidentally caused GOROOT_FINAL to have no effect (golang/go#61921). (Development versions of toolchains are still built without -trimpath by default, so GOROOT_FINAL may affect those builds somewhat.)

So we could safely ignore this variable starting from 1.21.

However, some folks could be using rules_go with older Go SDK versions who wish to get the latest rules_go features still. It's best if we could avoid forcing folks to upgrade their Go SDK when upgrading rules_go by making rules_go code work with older versions as well as newer ones.

Effectively, I think you could undo your change in go/private/context.bzl and leave the variable as is.

In link.go, you could call a func manageGoRoot() (func()) func (return is the defer func) with 2 different implementations: one using

//go:build go1.21
// +build go1.21

and one using

//go:build !go1.21
// +build !go1.21

@sluongng
Copy link
Contributor

On second thought, if this is only a breaking behavior in 1.23, I think we should make it a 1.23 forward feature instead of 1.21. 🤔

@JacobOaks
Copy link
Contributor Author

JacobOaks commented Jul 12, 2024

Hey @sluongng - so I did try this solution on a couple pre-Go1.23 versions, and it worked on the most recent older versions too - but I agree it's probably a good idea to hide this behind 1.23+ just to be safe.

I tried to add build constraints, but the problem I ran into is that go_tool_binary targets (which the builder binary happens to be) do not support build constraints due to the way they invoke go build. So it seems like we have a couple options:

  • Change go_tool_binary to materialize a temporary go module
  • Switch off of something like runtime.Version(), which is probably quite brittle and undesirable, but seems to technically work?
  • Verify that this change works on all Go versions supported by rules_go and drop the version-switching idea

Let me know if you have thoughts/other ideas.

@sluongng
Copy link
Contributor

@sluongng
Copy link
Contributor

ah it's part of nogo which is built after a transition? 🤔

Hmm...

@JacobOaks
Copy link
Contributor Author

JacobOaks commented Jul 12, 2024

Yeah, I think nogo is a separate type of rule that does support build constraints.

I updated the PR to switch based off runtime.Version for now, which like I mentioned isn't quite as satisfying as build constraints would be, but does seem to technically work. Though I'm not familiar with rules_go enough to know if there are situations where the builder would have been built with a different version than is being used when link.go is invoking the linker.

Copy link
Member

@fmeum fmeum left a comment

Choose a reason for hiding this comment

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

@tyler-french Could you test this?

Copy link
Contributor

@tyler-french tyler-french left a comment

Choose a reason for hiding this comment

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

We've tested this at Uber

@fmeum fmeum merged commit 1a32e03 into bazel-contrib:master Jul 16, 2024
2 checks passed
cgrindel-self-hosted-renovate bot referenced this pull request in cgrindel/bazel-starlib Aug 30, 2024
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [io_bazel_rules_go](https://redirect.github.com/bazelbuild/rules_go) |
http_archive | minor | `v0.49.0` -> `v0.50.0` |

---

### Release Notes

<details>
<summary>bazelbuild/rules_go (io_bazel_rules_go)</summary>

###
[`v0.50.0`](https://redirect.github.com/bazelbuild/rules_go/releases/tag/v0.50.0)

[Compare
Source](https://redirect.github.com/bazelbuild/rules_go/compare/v0.49.0...v0.50.0)

#### `WORKSPACE` code

load("@&#8203;bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive")

    http_archive(
        name = "io_bazel_rules_go",
sha256 =
"67b4d1f517ba73e0a92eb2f57d821f2ddc21f5bc2bd7a231573f11bd8758192e",
        urls = [

"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.50.0/rules_go-v0.50.0.zip",

"https://github.com/bazelbuild/rules_go/releases/download/v0.50.0/rules_go-v0.50.0.zip",
        ],
    )

load("@&#8203;io_bazel_rules_go//go:deps.bzl", "go_register_toolchains",
"go_rules_dependencies")

    go_rules_dependencies()

    go_register_toolchains(version = "1.23.0")

#### What's Changed

- Break reliance on GOROOT_FINAL by
[@&#8203;JacobOaks](https://redirect.github.com/JacobOaks) in
[https://github.com/bazelbuild/rules_go/pull/3984](https://redirect.github.com/bazelbuild/rules_go/pull/3984)
- Migrate to macos_arm64 by
[@&#8203;meteorcloudy](https://redirect.github.com/meteorcloudy) in
[https://github.com/bazelbuild/rules_go/pull/3990](https://redirect.github.com/bazelbuild/rules_go/pull/3990)
- Support matching release candidate toolchain versions by
[@&#8203;JacobOaks](https://redirect.github.com/JacobOaks) in
[https://github.com/bazelbuild/rules_go/pull/3998](https://redirect.github.com/bazelbuild/rules_go/pull/3998)
- rm crosstool by
[@&#8203;sluongng](https://redirect.github.com/sluongng) in
[https://github.com/bazelbuild/rules_go/pull/3986](https://redirect.github.com/bazelbuild/rules_go/pull/3986)
- fix(timeout.go): remove redundant leaked go func in
RegisterTimeoutHandler by
[@&#8203;Roytangrb](https://redirect.github.com/Roytangrb) in
[https://github.com/bazelbuild/rules_go/pull/4004](https://redirect.github.com/bazelbuild/rules_go/pull/4004)
- Run nogo in a separate validation action by
[@&#8203;fmeum](https://redirect.github.com/fmeum) in
[https://github.com/bazelbuild/rules_go/pull/3995](https://redirect.github.com/bazelbuild/rules_go/pull/3995)

#### New Contributors

- [@&#8203;JacobOaks](https://redirect.github.com/JacobOaks) made their
first contribution in
[https://github.com/bazelbuild/rules_go/pull/3984](https://redirect.github.com/bazelbuild/rules_go/pull/3984)
- [@&#8203;Roytangrb](https://redirect.github.com/Roytangrb) made their
first contribution in
[https://github.com/bazelbuild/rules_go/pull/4004](https://redirect.github.com/bazelbuild/rules_go/pull/4004)

**Full Changelog**:
bazel-contrib/rules_go@release-0.49...release-0.50

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config
help](https://redirect.github.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://redirect.github.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC41OC4wIiwidXBkYXRlZEluVmVyIjoiMzguNTguMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: cgrindel-self-hosted-renovate[bot] <139595543+cgrindel-self-hosted-renovate[bot]@users.noreply.github.com>
renovate bot referenced this pull request in kreempuff/rules_unreal_engine Aug 30, 2024
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [io_bazel_rules_go](https://togithub.com/bazelbuild/rules_go) |
http_archive | minor | `v0.49.0` -> `v0.50.0` |

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>bazelbuild/rules_go (io_bazel_rules_go)</summary>

###
[`v0.50.0`](https://togithub.com/bazelbuild/rules_go/releases/tag/v0.50.0)

[Compare
Source](https://togithub.com/bazelbuild/rules_go/compare/v0.49.0...v0.50.0)

#### `WORKSPACE` code

load("@&#8203;bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive")

    http_archive(
        name = "io_bazel_rules_go",
sha256 =
"67b4d1f517ba73e0a92eb2f57d821f2ddc21f5bc2bd7a231573f11bd8758192e",
        urls = [

"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.50.0/rules_go-v0.50.0.zip",

"https://github.com/bazelbuild/rules_go/releases/download/v0.50.0/rules_go-v0.50.0.zip",
        ],
    )

load("@&#8203;io_bazel_rules_go//go:deps.bzl", "go_register_toolchains",
"go_rules_dependencies")

    go_rules_dependencies()

    go_register_toolchains(version = "1.23.0")

#### What's Changed

- Break reliance on GOROOT_FINAL by
[@&#8203;JacobOaks](https://togithub.com/JacobOaks) in
[https://github.com/bazelbuild/rules_go/pull/3984](https://togithub.com/bazelbuild/rules_go/pull/3984)
- Migrate to macos_arm64 by
[@&#8203;meteorcloudy](https://togithub.com/meteorcloudy) in
[https://github.com/bazelbuild/rules_go/pull/3990](https://togithub.com/bazelbuild/rules_go/pull/3990)
- Support matching release candidate toolchain versions by
[@&#8203;JacobOaks](https://togithub.com/JacobOaks) in
[https://github.com/bazelbuild/rules_go/pull/3998](https://togithub.com/bazelbuild/rules_go/pull/3998)
- rm crosstool by [@&#8203;sluongng](https://togithub.com/sluongng) in
[https://github.com/bazelbuild/rules_go/pull/3986](https://togithub.com/bazelbuild/rules_go/pull/3986)
- fix(timeout.go): remove redundant leaked go func in
RegisterTimeoutHandler by
[@&#8203;Roytangrb](https://togithub.com/Roytangrb) in
[https://github.com/bazelbuild/rules_go/pull/4004](https://togithub.com/bazelbuild/rules_go/pull/4004)
- Run nogo in a separate validation action by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[https://github.com/bazelbuild/rules_go/pull/3995](https://togithub.com/bazelbuild/rules_go/pull/3995)

#### New Contributors

- [@&#8203;JacobOaks](https://togithub.com/JacobOaks) made their first
contribution in
[https://github.com/bazelbuild/rules_go/pull/3984](https://togithub.com/bazelbuild/rules_go/pull/3984)
- [@&#8203;Roytangrb](https://togithub.com/Roytangrb) made their first
contribution in
[https://github.com/bazelbuild/rules_go/pull/4004](https://togithub.com/bazelbuild/rules_go/pull/4004)

**Full Changelog**:
bazel-contrib/rules_go@release-0.49...release-0.50

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/kreempuff/rules_unreal_engine).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC41Ni4wIiwidXBkYXRlZEluVmVyIjoiMzguNTYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
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.

Full GOROOT paths in Go 1.23 binaries Full stdlib file paths in binary
5 participants