Skip to content

Fix unresolved ${out_dir} token in dep env files - #4124

Merged
UebelAndre merged 1 commit into
bazelbuild:mainfrom
basvandijk:fix-dep-env-out-dir-redaction
Jul 5, 2026
Merged

Fix unresolved ${out_dir} token in dep env files#4124
UebelAndre merged 1 commit into
bazelbuild:mainfrom
basvandijk:fix-dep-env-out-dir-redaction

Conversation

@basvandijk

Copy link
Copy Markdown
Contributor

The bug

Since the OUT_DIR sanitization work (#4050 / #4011, first released in 0.71.x), outputs_to_dep_env redacts the producer's out_dir to the generic ${out_dir} substitution token, the same way outputs_to_env does.

That redaction is only correct for _bs.env files, which are consumed by the target that directly owns the build script (where process_wrapper's --out-dir resolves the token to the right directory). Dep env (DEP_*) files, however, are consumed by downstream crates' build scripts: their runner only substitutes ${pwd}, and their own out_dir points to a different directory. The token is therefore left unresolved, or would resolve to the wrong directory.

Real-world failure: libssh2-sys's build script fails to find zlib.h because libz-sys's DEP_Z_INCLUDE contains a literal ${out_dir} path component. Found while upgrading rules_rust to 0.71.3 in dfinity/ic (see dfinity/ic#10632, where this fix is currently carried as a patch).

The fix

Only substitute the exec root in dep env files and keep the real out_dir path. That path is valid for consumers: the producer's out_dir is a declared input of downstream build script actions.

Tests

  • Unit test out_dir_in_dep_env_value_is_not_redacted_to_substitution_token in cargo/private/cargo_build_script_runner/lib.rs.
  • End-to-end regression test //cargo/tests/dep_env:build_read_out_dir mirroring the libz-sys → libssh2-sys scenario: a producer build script advertises cargo:include=$OUT_DIR/include and the consumer build script asserts DEP_Z_INCLUDE points at an existing directory. Fails without the fix, passes with it.

Assisted-by: GitHub Copilot

`outputs_to_dep_env` redacted the producer's out_dir to the generic
`${out_dir}` substitution token, the same way `outputs_to_env` does.
But dep env (`DEP_*`) files are consumed by *downstream* crates' build
scripts, whose runner only substitutes `${pwd}` and whose own out_dir
points to a different directory, so the token was left unresolved (or
would resolve to the wrong directory). For example, libssh2-sys failed
to find zlib.h from libz-sys's DEP_Z_INCLUDE.

Only substitute the exec root in dep env files and keep the real
out_dir path, which is a declared input of downstream build script
actions.

Assisted-by: GitHub Copilot
@basvandijk
basvandijk marked this pull request as ready for review July 2, 2026 13:54
@UebelAndre
UebelAndre self-requested a review July 2, 2026 14:06
@basvandijk
basvandijk marked this pull request as draft July 2, 2026 14:32
@basvandijk

basvandijk commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Converting to draft since I see some tests failing on dfinity/ic#10632 which are likely due to this. Will have to debug this further ...

EDIT: False alarm. The bug was caused by something else and fixed in dfinity/ic@082d8ae.

@basvandijk
basvandijk marked this pull request as ready for review July 2, 2026 14:48

@UebelAndre UebelAndre left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you!

@UebelAndre
UebelAndre enabled auto-merge July 5, 2026 14:18
@UebelAndre
UebelAndre added this pull request to the merge queue Jul 5, 2026
Merged via the queue into bazelbuild:main with commit dedf053 Jul 5, 2026
3 checks passed
pull Bot pushed a commit to mikeyhodl/ic that referenced this pull request Jul 27, 2026
…ity#10900)

_Requested by **Nicolas Mattia** · [Slack
thread](https://dfinity.slack.com/archives/CL7Q2RXUM/p1784901121254099)_

## What

Upgrades the `rules_rust` Bazel module pin in `bazel/rust.MODULE.bazel`
from the `0.71.3` release tarball to a `codeload.github.com` snapshot of
the latest commit on `bazelbuild/rules_rust`'s default branch, matching
the `archive_override` + codeload pattern already used for other Bazel
modules in this repo (e.g. `rules_motoko`, `rules_foreign_cc` in the
root `MODULE.bazel`).

- **Before:**
`https://github.com/bazelbuild/rules_rust/releases/download/0.71.3/rules_rust-0.71.3.tar.gz`
(release 0.71.3)
- **After:**
`https://codeload.github.com/bazelbuild/rules_rust/tar.gz/5f04c8c14c2ffacfe10ad14e55298be21454be04`
(commit
[`5f04c8c1`](bazelbuild/rules_rust@5f04c8c),
latest on `main` as of 2026-07-24: "Add
`cargo_build_script.use_cc_toolchain` (dfinity#4161)")
- `strip_prefix` updated to
`rules_rust-5f04c8c14c2ffacfe10ad14e55298be21454be04` and the
`integrity` hash recomputed for the new tarball (sha256 of the codeload
archive fetched from an unrestricted network, converted to the
`sha256-<base64>` SRI form to match the field format already used by
this override).

## Patches

- **Removed** `bazel/rules_rust_dep_env.patch`: this worked around
`DEP_*` cross-crate env vars being redacted to an unresolved
`${out_dir}` token in build-script dep-env files (e.g. `libssh2-sys`
failing to find `zlib.h` via `libz-sys`'s `DEP_Z_INCLUDE`). This is now
fixed upstream by bazelbuild/rules_rust#4124 ("Fix unresolved
`${out_dir}` token in dep env files"), which is included as of the new
pinned commit — confirmed by diffing
`cargo/private/cargo_build_script_runner/lib.rs` at the new commit
against the patch: the upstream code now uses `redact_exec_root` instead
of full `redact_paths` in `outputs_to_dep_env`, matching the patch's
intent, and even includes the same libssh2-sys/libz-sys regression test.
The patch no longer applies (`git apply --check` fails cleanly against
the new file content) and its reference was removed from
`rust.MODULE.bazel`.
- **Kept** `bazel/rules_rust_strip_level.patch` unchanged: it configures
per-compilation-mode strip levels, which the native `rust.toolchain`
tag's flat `strip_level` `string_dict` still cannot express
(bazelbuild/rules_rust#3730 is still open upstream — traced the full
call chain through `rust/extensions.bzl` →
`rust/private/repositories.bzl` → `rust/private/toolchain.bzl`'s
`_rust_toolchain_impl`, which still requires a per-compilation-mode dict
at the rule level that the bzlmod extension tag cannot produce).
Verified the patch still applies cleanly (`git apply --check`, 3-line
offset, no fuzz) against `rust/extensions.bzl` fetched at the new target
commit.
- No other patches reference `rules_rust` in this repo.
- No `MODULE.bazel.lock` file exists in this repo, so there is nothing
to regenerate there.

## Validation

What I was able to run in my sandbox:
- `buildifier -mode=check bazel/rust.MODULE.bazel` — clean, no
reformatting needed (exit 0).
- `buildifier -lint=warn bazel/rust.MODULE.bazel` — only pre-existing
warnings unrelated to this change (canonical-repository `@@` warnings
elsewhere in the file, far from the diff).
- Fetched the exact files both patches touch at the exact target commit
(`rust/extensions.bzl`,
`cargo/private/cargo_build_script_runner/lib.rs`) and ran `git apply
--check` against them locally, as described above.
- The `sha256`/`integrity` value was computed from a real fetch of the
codeload tarball for the target commit, done outside my sandbox (my
sandbox's network egress blocks `codeload.github.com` for any repo other
than `dfinity/ic`), then converted to SRI format and round-tripped back
to hex to confirm the conversion was correct.

What I could **not** run in my sandbox, and why:
- `bazel build` / `bazel test` / `bazel fetch` for this change
specifically: my sandbox's network egress blocks (a)
`codeload.github.com` tarball fetches for non-`dfinity` repos, (b) the
`ghcr.io`/`pkg-containers.githubusercontent.com` blob storage needed to
pull the pinned `ic-dev` dev-container image, and (c)
`releases.bazel.build` binary downloads needed to get a matching Bazel
9.2.0 outside the container. All three are independent, hard
network-egress restrictions in that environment, not fixable from within
it.

Given that, **this PR's actual build/test signal will need to come from
CI** once it runs here. Please treat a green CI run (in particular
anything that exercises `@rules_rust` toolchains/`cargo_build_script`)
as the real confirmation that the new pin, hash, and patch set are
correct before merging this out of draft.

## Testing

- [ ] CI build/test is green (this is the validation this sandbox could
not produce locally — see above)
- [ ] Confirm no other in-repo patches or docs reference
`bazel/rules_rust_dep_env.patch`


---
_Generated by [Claude
Code](https://claude.ai/code/session_01WYQSaxC3QcWKWQi7rDHe27)_

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: IDX GitHub Automation <infra+github-automation@dfinity.org>
Co-authored-by: Nicolas Mattia <nicolas.mattia@dfinity.org>
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