Fix Rust formatting when //:format is called in different subdirectory #327
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We have been seeing problems when the Rust formatter is called from a subdirectory and not the workspace root directory.
The root cause is that rustfmt appears to look for the file list based on the BUILD_WORKING_DIRECTORY instead of
BUILD_WORKSPACE_DIRECTORY or the current working directory of format.sh
This patch fixes the issue by unsetting the BUILD_WORKING_DIRECTORY if format.sh has BUILD_WORKSAPCE_DIRECTORY is set.
This has been locally tested:
before:
Running in a subdirectory, e.g.
b
after:
As far as I can tell this only impacts rustfmt. We have most other formatters enabled and not seen similar problems.
The root cause is that the upstream_wrapper in rules_rust changes the working directory if the environment variable is set. See https://github.com/bazelbuild/rules_rust/blob/main/tools/upstream_wrapper/src/main.rs
Changes are visible to end-users: yes
Test plan
I have not been able to find automated test cases in rules_lint. I am happy to add to any test case, but I would need a pointer.
Manual testing: