Skip to content

Append path into trimpath if option already exists#2994

Merged
robfig merged 2 commits intobazel-contrib:masterfrom
Reflejo:fz/fixtrimpath
Nov 30, 2021
Merged

Append path into trimpath if option already exists#2994
robfig merged 2 commits intobazel-contrib:masterfrom
Reflejo:fz/fixtrimpath

Conversation

@Reflejo
Copy link
Copy Markdown
Contributor

@Reflejo Reflejo commented Oct 31, 2021

What type of PR is this?

Bug fix

What does this PR do? Why is it needed?

Appends paths to trimpath as opposed to override potential used-defined flags.

Which issues(s) does this PR fix?

User defined trimpath through gc_goopts are being overriden since a second -trimpath is being set. This change appends the path using : which has been supported by go for a while (see https://go-review.googlesource.com/c/go/+/173344/)

@google-cla google-cla bot added the cla: yes label Oct 31, 2021
@achew22
Copy link
Copy Markdown
Member

achew22 commented Oct 31, 2021

As a rule of thumb, changes to the rules_go compiler infra that aren't accompanied by a regression test are rarely merged. Changes like this are difficult to understand without the context provided by understanding what the current behavior is and what the authors proposed new behavior is. Would you be willing to put a test together to go with this change so we can better understand the problem and, as an added bonus, prevent regressions in the future?

@Reflejo
Copy link
Copy Markdown
Contributor Author

Reflejo commented Oct 31, 2021

I don't mind writing the tests provided that the maintainers are onboard with the change. That was my plan; the template mentions to submit the PR to discuss test strategies, so that's what I did

@Reflejo
Copy link
Copy Markdown
Contributor Author

Reflejo commented Oct 31, 2021

Ok I added a test case that shows the issue. The test will fail with all_test.go:22: plugin.Open("plugin"): plugin was built with a different version of package go_plugin_with_proto_library/validate without this change.

This is because the plugin and the host are creating the pb.go file on different build folders which then is included into their binaries. This doesn't work because go plugins are very finicky about dependencies matching exactly and the filepath difference will break it, hence why trimpath is needed.

@Reflejo
Copy link
Copy Markdown
Contributor Author

Reflejo commented Nov 4, 2021

@achew22 thoughts?

@robfig
Copy link
Copy Markdown
Contributor

robfig commented Nov 30, 2021

This change looks good to me, and the multi-element trimpath commit was included in Go 1.13 which is old enough that we don't need to worry. Merging, thank you!

@robfig robfig merged commit c59f544 into bazel-contrib:master Nov 30, 2021
@RaduBerinde
Copy link
Copy Markdown

RaduBerinde commented Jan 14, 2022

@RaduBerinde
Copy link
Copy Markdown

Also note that later on, we call absArgs with -trimpath which assumes the argument is a single path. It will not work correctly with the more complex syntax.

https://github.com/bazelbuild/rules_go/blob/ba7bdfd6b5118d135ae3f7984c94103510bf4167/go/tools/builders/compilepkg.go#L476

@jscissr jscissr mentioned this pull request Aug 27, 2025
fmeum pushed a commit that referenced this pull request Aug 27, 2025
**What type of PR is this?**

Bug fix

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

There were multiple problems with trim paths:

- The trim path was inconsistent between non-cgo and cgo. non-cgo
trimmed the working directory, and cgo trimmed the parent of the working
directory.
- For non-cgo, paths in external repos are not trimmed when
--experimental_sibling_repository_layout is enabled.
- For cgo, the name of the working directory is included in the output,
but this name is not consistent between local and remote builds,
breaking reproducibility.

These are fixed here by creating the trim path in the same way for both
non-cgo and cgo, and trimming the working directory as well as replacing
the parent of the working directory with `..`. This means that the
resulting paths are relative to the working directory.

Additionally, this fixes the existing feature to extend an existing
trimpath. It used the wrong separator (`:` instead of `;`), and it
applied `abs()` to the entire trimpath argument instead of the
individual paths inside it. (See
#2994 (comment)
and
#2994 (comment))

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

Fixes: #4434
Fixes: #4161
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants