Skip to content

fix(test): make build work in Nix hermetic sandboxes (closes #1119) - #1120

Merged
robcohen merged 2 commits into
mainfrom
fix/nix-sandbox-test-shebang
May 15, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/nix-sandbox-test-shebang

Conversation

@robcohen

@robcohen robcohen commented May 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #1119.

Two Nix-sandbox / release-mode build issues, both surfaced by hermetic-sandbox testing.

1. /usr/bin/env shebang in test stub

crates/rustledger/tests/price_clobber_post_fetch.rs wrote a shell script with #!/usr/bin/env bash. Hermetic sandboxes that don't expose /usr/bin — Nix in particular — fail kernel shebang resolution: Command::spawn returns ENOENT, ExternalCommandSource::fetch_price returns Err, and run_with_external_command silently logs to stderr while returning Ok(()). Net effect: exit code 0 + empty stdout.

That single failure mode produced asymmetric behavior:

  • clobber_post_fetch_emits_duplicate_when_clobber_is_set — expects non-empty stdout. Fails on Nix (the symptom in [Bug]: Doesn't build on aarch64-linux when used with Nix #1119).
  • clobber_post_fetch_skips_when_response_date_matches_existing — expects zero price AAPL lines. Passes for the wrong reason (empty stdout matches its zero-count expectation).

Fix. Pass sh -c '<body>' directly via --source-cmd instead of writing an executable shell script. Resolves through $PATH (Nix stdenv provides sh); no /usr/bin/env, no chmod, no shebang.

2. #[should_panic] on debug_assert! paths in release builds

crates/rustledger-validate/src/lib.rs had two #[should_panic] tests targeting debug_assert! calls in ValidationSession::check_phase_ordering. debug_assert! is a no-op in release builds, so the tests fail under cargo test --release with "test did not panic as expected". Nix package builds (via crane) compile with --release, so they hit this.

Fix. Gate both tests on #[cfg(debug_assertions)]. The phase-ordering check still works correctly in release (it returns false and no-ops the misuse, as documented on ValidationSession); only the panic-assertion test is debug-only.

Deeper verification

In addition to running the failing test directly:

  • Reproduced the bug locally via unshare --user --map-root-user --mount sh -c "mount --bind /tmp /usr ..." — same panic, same stack trace as the issue report.
  • Verified the fix locally in the same simulated sandbox: all 3 price_clobber_post_fetch tests pass.
  • Audited for similar shebang fragility — bean_price_compat.rs uses the same pattern but skips entirely when bean-price isn't on PATH (which it isn't in the Nix sandbox), so it never reaches the stub-script code.
  • Ran nix build .#rustledger end-to-end — checkPhase passes, all tests + doctests green, binary builds and installs.

How to test

# Reproduces the original failure if /usr/bin/env is missing:
unshare --user --map-root-user --mount sh -c "mount --bind /tmp /usr && cargo test -p rustledger --test price_clobber_post_fetch --release"

# Verifies the release-mode test gating:
cargo test -p rustledger-validate --lib --release
cargo test -p rustledger-validate --lib  # also exercises the debug-only tests

# End-to-end Nix build:
nix build .#rustledger

Verification

  • cargo test --workspace --all-features clean (debug).
  • cargo test --workspace --all-features --release clean.
  • cargo clippy -p rustledger --tests -- -D warnings clean.
  • Mount-namespace simulation of Nix sandbox: all 3 price_clobber_post_fetch tests pass.
  • nix build .#rustledger --no-link --print-build-logs — full checkPhase passes, binary builds.

🤖 Generated with Claude Code

 #1119)

The integration test fixture in price_clobber_post_fetch.rs wrote an
executable shell script with a #!/usr/bin/env bash shebang. In
hermetic build sandboxes that don't expose /usr/bin (Nix in
particular), the kernel's shebang resolution returns ENOENT,
Command::spawn fails, ExternalCommandSource::fetch_price returns an
Err, and the failure path silently logs to stderr while returning
Ok(()) — so out.status.success() is true but stdout is empty.

The _emits_duplicate_when_clobber_is_set test failed loudly because
its expected output (a fresh price directive) was missing. The
_skips_when_response_date_matches_existing test "passed" for the
wrong reason: its expectation of zero output happened to match the
empty-stdout failure mode. Both tests were actually broken — only
one surfaced.

Fix: stop writing a shell script and instead pass sh -c '<body>'
directly via --source-cmd. Resolving sh through $PATH works on every
Unix the test suite targets, including Nix's stdenv. Eliminates the
need for chmod +x and removes the /usr/bin/env dependency.

Verified against the three tests in the file (release profile)
post-fix; all pass on x86_64-linux without /usr/bin/env reliance.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner May 15, 2026 17:22
Copilot AI review requested due to automatic review settings May 15, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a hermetic build failure in the price_clobber_post_fetch integration tests by avoiding a generated script with a /usr/bin/env bash shebang.

Changes:

  • Replaces the temporary executable shell script helper with an inline sh -c --source-cmd.
  • Removes chmod/shebang-related setup and narrows TempDir usage to the Linux-only cache test.
  • Updates comments to document the Nix-related failure mode.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented May 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 31.7ms ± 0.3ms 23.9 MB
beancount 863.6ms ± 9.9ms 50.3 MB
Comparison 27.2x faster 2.1x less

vs main branch (2026-05-15)

Metric main this PR Change
Time 40.3ms 31.7ms -21.3% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 786
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

Two #[should_panic] tests in rustledger-validate target the
debug_assert! calls in ValidationSession::check_phase_ordering.
Since debug_assert! is a no-op in release builds, the tests fail
under cargo test --release with note panic-as-expected — which is
exactly what Nix package builds via crane do.

Surfaced while verifying the #1119 fix with a real nix build: the
sandbox failed checkPhase on these two tests after the primary
/usr/bin/env-shebang fix made everything else pass.

The phase-ordering check still works correctly in release builds
(it no-ops and returns false instead of panicking) — documented on
ValidationSession. Only the panic-assertion test is debug-only.

Verified: 71 passes in debug (tests included), 69 in release (gated
out), nix build .#rustledger green end-to-end.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen robcohen changed the title fix(test): drop /usr/bin/env shebang in price_clobber_post_fetch fix(test): make build work in Nix hermetic sandboxes (closes #1119) May 15, 2026
@robcohen
robcohen merged commit fe260c5 into main May 15, 2026
33 checks passed
@robcohen
robcohen deleted the fix/nix-sandbox-test-shebang branch May 15, 2026 17:59
@robcohen robcohen mentioned this pull request May 15, 2026
9 tasks done
robcohen added a commit that referenced this pull request May 15, 2026
Bumps the workspace from 0.14.1 to 0.15.0. Minor-version bump per
SemVer because 0.14.2..0.15.0 ships PR #1116, which is a breaking
API change for downstream consumers:

* `rustledger_validate` — removed the `validate()`,
  `validate_with_options()`, `validate_with_today()`, and the two
  spanned variants. Replaced with `ValidationSession::new(opts).
  run_phase(_, Phase::Early, today).run_phase(_, Phase::Late, today).
  finalize()`.

* `rustledger_plugin_types` — `PluginOutput.directives:
  Vec<DirectiveWrapper>` replaced with `ops: Vec<PluginOp>` where
  `PluginOp { Keep(usize) | Modify(usize, DirectiveWrapper) |
  Insert(DirectiveWrapper) | Delete(usize) }`. The wrapper
  round-trip's span-fidelity loss is now eliminated by construction.

* `rustledger_plugin::NativePlugin` — new `is_synth()` trait method
  for self-classifying plugins as pre-booking synthesizers.

* `rustledger_loader` — new `PluginPass { PreBookingSynth |
  PostBooking | All }` enum; `run_plugins` takes a `pass` parameter.
  `run_booking` returns `(Vec<Spanned<Directive>>,
  Vec<Spanned<Directive>>)` partitioned into (booked, failed) instead
  of `Vec<usize>` of failed indices.

* `rustledger_booking` — `INTERPOLATED_MARKER` const removed.

Plus the v0.14.1..v0.15.0 fix train: pad multi-currency / future-date
regressions, duplicate-Open suppression across five plugins,
plugin-error source-location propagation, `--clobber` post-fetch
dedup hardening (#1120 / #1119) and the `cfg(debug_assertions)` gate
on the should-panic phase-ordering tests so Nix package builds via
`cargo test --release` pass.

# Surface coverage (per RELEASING.md)

* Workspace `Cargo.toml`: `[workspace.package].version` + 10
  `[workspace.dependencies]` path-pinned crates.
* 14 crate `Cargo.toml` files.
* `Cargo.lock` regenerated via `cargo check --workspace`.
* `packages/mcp-server/package.json`: `version` and the
  `@rustledger/wasm` dep pin.
* `packaging/rpm/rustledger.spec`: `Version`, `Source0` URL, and
  `%setup -n rustledger-X.Y.Z` directory. `BuildRequires: rust >=
  1.94` already matches the workspace MSRV — no change.

# Pre-flight smoke checks

* `cargo check --workspace --all-features --all-targets` — clean.
* `cd packages/mcp-server && npm ci && npm run build` — clean
  (temporarily reverted `@rustledger/wasm` to `^0.14.1` during the
  build since `^0.15.0` isn't on npm yet; restored to `^0.15.0`
  before committing).
* `cd crates/rustledger-wasm && wasm-pack build --target web
  --release` — clean.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.

[Bug]: Doesn't build on aarch64-linux when used with Nix

2 participants