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

Update cargo_build_script to always render custom runfiles dirs. #2891

Merged
merged 7 commits into from
Oct 3, 2024

Conversation

UebelAndre
Copy link
Collaborator

@UebelAndre UebelAndre commented Sep 20, 2024

After #2887 I started finding similar linker errors in environments that supported runfiles and symlinks but have yet to see this issue in environments without them. I believe my understanding of how the DefaultInfo.default_runfiles objects worked and in the previous change had attempted to rely on it for a runfiles directory. However, there is no guarantee a directory will be rendered and no way to force it to be rendered for actions either (bazelbuild/bazel#15486). The consequence is that creating a custom runfiles directory is no longer a contingency and explicitly creating it is the correct thing to do to ensure CARGO_MANIFEST_DIR linker inputs are always available to the consuming actions.

With the propagation of CARGO_MANIFEST_DIR (now an action output) into Rustc actions, a new flag is also introduced to prune unnecessary files out of the directory to reduce bloat and potential invalidation in downstream actions, --@rules_rust//cargo/settings:cargo_manifest_dir_filename_suffixes_to_retain. This flag accepts a list of path suffixes used to determine what (if anything) in CARGO_MANIFEST_DIR should be retained and passed to downstream actions.

Note that the failure mode this addresses is hard to observe as it requires a crate which defines a linker path to something in CARGO_MANIFEST_DIR and then for a clean build to be done with a change to a dependent of said crate. If the runfiles directory for the build script is never rendered then there will be no file to link into the crate.

@UebelAndre UebelAndre marked this pull request as ready for review September 20, 2024 05:46
@@ -61,7 +61,7 @@ fn main() {
.expect("Failed to create output directory");
std::fs::copy(src, &out_dest).unwrap_or_else(|e| {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If this is going to exist long term then perhaps this directory could have symlinks generated into it whenever possible and the files from DefaultInfo.default_runfiles.files can be tracked in addition to the output directory. This would probably match runfiles directories more directly but I'm concerned about how remote caches will handle a directory of symlinks so will save such an optimization for another time.

@UebelAndre
Copy link
Collaborator Author

After soaking this change, I'm finding it leads to additional issues within the CargoBuildScriptRunfilesDir action. The actions will error with:

Failed to copy file 'bazel-out/**/_bs-.exe -> bazel-out/**/_bs.cargo_runfiles\*\_bs-.exe`
Os { code: 5, kind: PermissionDenied, message: "Access is denied." }

The error is somewhat confusing but when I manually inspect the filesystem on the worker I see very strangely:

dir bazel-out\**\
Mode            LastWriteTime          Length Name
----            -------------          ------ ----
d----l     9/19/2024 11:00 PM                 _bs-.exe
d----      ...
d----      ...
...

It appears the way symlinks are created confuse Windows into believing these are directories. When I inspect the target of the symlink I can see it's to the original build script:

(Get-Item _bs-.exe).Target
C:\bzl\**\_bs_.exe

However, the script is not on the machine. This is extra strange to me because while investigating I'd explicitly added the real build script to the runfiles of the cargo_build_script_runfiles rule and confirmed it's in script[DefaultInfo].default_runfiles.files but the file is never pulled from the remote cache for this action.

diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl
index 28f91000..c6647ebe 100644
--- a/cargo/private/cargo_build_script.bzl
+++ b/cargo/private/cargo_build_script.bzl
@@ -48,7 +48,7 @@ def _cargo_build_script_runfiles_impl(ctx):

     # Tools are ommitted here because they should be within the `script`
     # attribute's runfiles.
-    runfiles = ctx.runfiles(files = ctx.files.data)
+    runfiles = ctx.runfiles(files = [script] + ctx.files.data)

     return [
         DefaultInfo(

This very much feels like a bug to me. If a depset of files is passed to an action, all files should be downloaded and made available for the action.

I'm not sure what to do with this change. It still feels more correct but I do not know how to handle this case and the failure mode of the CargoBuildScriptRunfilesDir action is a lot more frequent then the broken linking from what I've gathered so far.

@UebelAndre UebelAndre force-pushed the cargo_runfiles branch 5 times, most recently from 9146393 to d0e7593 Compare October 3, 2024 13:22
@UebelAndre
Copy link
Collaborator Author

I'm not sure what to do with this change. It still feels more correct but I do not know how to handle this case and the failure mode of the CargoBuildScriptRunfilesDir action is a lot more frequent then the broken linking from what I've gathered so far.

I've decided to introduce an incompatibility flag to enable this behavior by default and a new flag for pruning the generated CARGO_MANIFEST_DIR to try and reduce the negative impact of always passing the directory on to downstream actions. This feels like the right thing to do given the state of runfiles.

Copy link
Collaborator

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

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

This feels quite complicated, but I can see how we ended up here... Thanks for tracking it down!

cargo/cargo_build_script_runner/bin.rs Outdated Show resolved Hide resolved
@UebelAndre UebelAndre added this pull request to the merge queue Oct 3, 2024
@UebelAndre UebelAndre removed this pull request from the merge queue due to a manual request Oct 3, 2024
UebelAndre and others added 2 commits October 3, 2024 09:47
Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
@UebelAndre UebelAndre added this pull request to the merge queue Oct 3, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Oct 3, 2024
@UebelAndre UebelAndre added this pull request to the merge queue Oct 3, 2024
Merged via the queue into bazelbuild:main with commit 5cb6121 Oct 3, 2024
4 checks passed
zemnmez-renovate-bot added a commit to zemn-me/monorepo that referenced this pull request Oct 4, 2024
##### [`v0.52.0](https://github.com/bazelbuild/rules_rust/releases/tag/0.52.0)

### 0.52.0

#### Bzlmod

```python
bazel_dep(name = "rules_rust", version = "0.52.0")
```

#### WORKSPACE

```python
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_rust",
    integrity = "sha256-eTHntUQQe2ICm/L8cuefnXdSOtZQ1ELZPD/M6a1ewes=",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.52.0/rules_rust-v0.52.0.tar.gz"],
)
```

Additional documentation can be found at: https://bazelbuild.github.io/rules_rust/#setup

#### What's Changed

-   Allowlist more clang flags in bindgen by [@hlopko](https://github.com/hlopko) in bazelbuild/rules_rust#2902
-   Allow `+` in repo names by [@fmeum](https://github.com/fmeum) in bazelbuild/rules_rust#2908
-   Added utility for parsing action Args param files by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2897
-   Updated platform triple mapping to support T3 platforms without `std`. by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2899
-   Fix `override_targets` keys in docs by [@max-heller](https://github.com/max-heller) in bazelbuild/rules_rust#2905
-   Use absolute path with "-fsanitize-ignorelist" by [@vitalybuka](https://github.com/vitalybuka) in bazelbuild/rules_rust#2911
-   Fix platform validation for wasm32-wasip1 target by [@c16a](https://github.com/c16a) in bazelbuild/rules_rust#2894
-   Update `cargo_build_script` to always render custom runfiles dirs. by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2891
-   Check target tags before ignore tags for lint/fmt aspects by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2913
-   Get rolling releases CI green by [@illicitonion](https://github.com/illicitonion) in bazelbuild/rules_rust#2916
-   Avoid uses of shell to improve Windows tests by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2912
-   Prep release 0.52.0 by [@illicitonion](https://github.com/illicitonion) in bazelbuild/rules_rust#2910

#### New Contributors

-   [@fmeum](https://github.com/fmeum) made their first contribution in bazelbuild/rules_rust#2908
-   [@max-heller](https://github.com/max-heller) made their first contribution in bazelbuild/rules_rust#2905
-   [@vitalybuka](https://github.com/vitalybuka) made their first contribution in bazelbuild/rules_rust#2911
-   [@c16a](https://github.com/c16a) made their first contribution in bazelbuild/rules_rust#2894

**Full Changelog**: bazelbuild/rules_rust@0.51.0...0.52.0
github-merge-queue bot pushed a commit to zemn-me/monorepo that referenced this pull request Oct 4, 2024
##### [`v0.52.0](https://github.com/bazelbuild/rules_rust/releases/tag/0.52.0)

### 0.52.0

#### Bzlmod

```python
bazel_dep(name = "rules_rust", version = "0.52.0")
```

#### WORKSPACE

```python
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_rust",
    integrity = "sha256-eTHntUQQe2ICm/L8cuefnXdSOtZQ1ELZPD/M6a1ewes=",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.52.0/rules_rust-v0.52.0.tar.gz"],
)
```

Additional documentation can be found at: https://bazelbuild.github.io/rules_rust/#setup

#### What's Changed

-   Allowlist more clang flags in bindgen by [@hlopko](https://github.com/hlopko) in bazelbuild/rules_rust#2902
-   Allow `+` in repo names by [@fmeum](https://github.com/fmeum) in bazelbuild/rules_rust#2908
-   Added utility for parsing action Args param files by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2897
-   Updated platform triple mapping to support T3 platforms without `std`. by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2899
-   Fix `override_targets` keys in docs by [@max-heller](https://github.com/max-heller) in bazelbuild/rules_rust#2905
-   Use absolute path with "-fsanitize-ignorelist" by [@vitalybuka](https://github.com/vitalybuka) in bazelbuild/rules_rust#2911
-   Fix platform validation for wasm32-wasip1 target by [@c16a](https://github.com/c16a) in bazelbuild/rules_rust#2894
-   Update `cargo_build_script` to always render custom runfiles dirs. by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2891
-   Check target tags before ignore tags for lint/fmt aspects by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2913
-   Get rolling releases CI green by [@illicitonion](https://github.com/illicitonion) in bazelbuild/rules_rust#2916
-   Avoid uses of shell to improve Windows tests by [@UebelAndre](https://github.com/UebelAndre) in bazelbuild/rules_rust#2912
-   Prep release 0.52.0 by [@illicitonion](https://github.com/illicitonion) in bazelbuild/rules_rust#2910

#### New Contributors

-   [@fmeum](https://github.com/fmeum) made their first contribution in bazelbuild/rules_rust#2908
-   [@max-heller](https://github.com/max-heller) made their first contribution in bazelbuild/rules_rust#2905
-   [@vitalybuka](https://github.com/vitalybuka) made their first contribution in bazelbuild/rules_rust#2911
-   [@c16a](https://github.com/c16a) made their first contribution in bazelbuild/rules_rust#2894

**Full Changelog**: bazelbuild/rules_rust@0.51.0...0.52.0
github-merge-queue bot pushed a commit that referenced this pull request Oct 5, 2024
github-merge-queue bot pushed a commit that referenced this pull request Oct 5, 2024
After #2826 was merged, I
started seeing flaky builds on Windows related to build script
executables
(#2891 (comment)).
This appears to be related to
bazelbuild/bazel#21747 so to avoid the issue,
this change ensures that Windows build script executables are copied
instead of symlinked.
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.

2 participants