Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions crates/perry/tests/functional_batch2_regressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,56 @@
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::sync::Once;

fn perry_bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_perry"))
}

fn workspace_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../..")
.canonicalize()
.expect("canonicalize workspace root")
}

fn target_debug_dir() -> PathBuf {
std::env::var_os("CARGO_TARGET_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| workspace_root().join("target"))
.join("debug")
}
Comment on lines +35 to +40

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Resolve CARGO_TARGET_DIR to an absolute path before exporting PERRY_RUNTIME_DIR.

On Line 36 to Line 39, a relative CARGO_TARGET_DIR is preserved as relative. On Line 93, perry compile runs with current_dir(dir) (temp fixture), so PERRY_RUNTIME_DIR=target/debug can resolve to the wrong directory and fail runtime archive lookup.

Suggested fix
 fn target_debug_dir() -> PathBuf {
     std::env::var_os("CARGO_TARGET_DIR")
         .map(PathBuf::from)
+        .map(|p| if p.is_absolute() { p } else { workspace_root().join(p) })
         .unwrap_or_else(|| workspace_root().join("target"))
         .join("debug")
 }

Also applies to: 93-93

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry/tests/functional_batch2_regressions.rs` around lines 35 - 40,
The `target_debug_dir()` function preserves relative paths from the
`CARGO_TARGET_DIR` environment variable as relative, which causes incorrect
resolution when `perry compile` runs with a different current directory at line
93. Convert the `CARGO_TARGET_DIR` value to an absolute path before joining it
with "debug" by using canonicalize or a similar method to ensure the path always
resolves to the correct location regardless of the current working directory
context.


/// `PERRY_NO_AUTO_OPTIMIZE=1` (below) skips the per-app runtime rebuild and
/// links the prebuilt `libperry_runtime.a`. Under `cargo test` the staticlib
/// crate-type is not produced as a side effect of building the test binary
/// (only the rlib is), so build it explicitly once and point the compiler at
/// `target/debug` via `PERRY_RUNTIME_DIR`. Mirrors `module_import_forms.rs`.
fn ensure_runtime_archive() {
static BUILD_RUNTIME: Once = Once::new();
BUILD_RUNTIME.call_once(|| {
let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
let build = Command::new(cargo)
.current_dir(workspace_root())
.arg("build")
.arg("-p")
.arg("perry-runtime")
.output()
.expect("run cargo build -p perry-runtime");
assert!(
build.status.success(),
"cargo build -p perry-runtime failed\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&build.stdout),
String::from_utf8_lossy(&build.stderr)
);
});
}

fn runtime_dir() -> PathBuf {
ensure_runtime_archive();
target_debug_dir()
}

/// Write `files` (relative path -> contents) into `dir`, compile `entry`
/// with `--no-cache`, run it, and return stdout. Asserts compile + run succeed.
fn compile_and_run(dir: &Path, files: &[(&str, &str)], entry: &str) -> String {
Expand All @@ -45,6 +90,7 @@ fn compile_and_run(dir: &Path, files: &[(&str, &str)], entry: &str) -> String {
.arg("-o")
.arg(&output)
.env("PERRY_NO_AUTO_OPTIMIZE", "1")
.env("PERRY_RUNTIME_DIR", runtime_dir())
.output()
.expect("run perry compile");
assert!(
Expand Down
Loading