mirror of
https://github.com/storytold/craft-anything.git
synced 2026-11-19 14:00:52 +01:00
Find a Rust build's program under Cargo's target directory (R5.7)
A project whose Cargo settings move the build output (build.target-dir in .cargo/config.toml, or CARGO_TARGET_DIR) built, but Play and the screenshot/self-test checks only looked in app/target, so they could not start the program. built_program now looks under CARGO_TARGET_DIR, then the closest project or ancestor .cargo setting, then the default target/ as a fallback, and starts the first target directory that has the named program. The new AI regression uses a project with target-dir = "out" and fails before the fix; engine tests cover the environment path (relative and absolute), a project setting, an ancestor setting, the environment coming first, and the default fallback.
This commit is contained in:
1 parent
174ddf0c86
commit
efff13a28d
3 files changed
+136
-16
No files matched your search
@@ -262,6 +262,30 @@ fn built_program_finds_the_cargo_output_only() {
|
||||
assert_eq!(built_program(d.path()), Some(debug.join(exe("snake"))));
|
||||
}
|
||||
|
||||
/// R5.7: a project that moves Cargo's output writes its program where the config says, not in
|
||||
/// `target/`; Play and the checks must look there.
|
||||
#[test]
|
||||
fn built_program_follows_cargos_moved_target_directory() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let app = d.path();
|
||||
std::fs::create_dir_all(app.join(".cargo")).unwrap();
|
||||
std::fs::write(app.join(".cargo/config.toml"), "[build]\ntarget-dir = \"out\"\n").unwrap();
|
||||
std::fs::write(app.join("Cargo.toml"), "[package]\nname = \"probe\"\n").unwrap();
|
||||
std::fs::create_dir_all(app.join("src")).unwrap();
|
||||
std::fs::write(app.join("src/main.rs"), "fn main() {}\n").unwrap();
|
||||
let debug = app.join("out").join("debug");
|
||||
std::fs::create_dir_all(&debug).unwrap();
|
||||
let exe = if cfg!(windows) { "probe.exe" } else { "probe" };
|
||||
let program = debug.join(exe);
|
||||
std::fs::write(&program, "x").unwrap();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
}
|
||||
assert_eq!(built_program(app), Some(program));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn play_starts_the_window_app_of_a_workspace_not_its_newest_program() {
|
||||
// Play started the CLI a workspace also built (it was newer), which printed help and quit
|
||||
|
||||
+111
-16
@@ -20,29 +20,83 @@ pub fn site_dir(app: &Path) -> Option<PathBuf> {
|
||||
/// The program a Rust build in `app` made, so a finished game or app can be started. A workspace
|
||||
/// often builds several (the app, its CLI, tools), so the project's own Cargo.toml files decide:
|
||||
/// its `default-run`, then a program whose package uses a window or game library, then the root
|
||||
/// package's; the newest build of that program wins (release or debug). Only when the manifests
|
||||
/// name no programs is it the newest executable. Build scripts and dependencies live in
|
||||
/// subfolders and are skipped, and so are stray copies (`app.running-123.exe`) of a declared one.
|
||||
/// shortcut: assumes the default target dir; read `cargo metadata` if a project ever moves it.
|
||||
/// package's; the newest build of that program wins (release or debug) in the first target
|
||||
/// directory that has it. Only when the manifests name no programs is it the newest executable.
|
||||
/// Build scripts and dependencies live in subfolders and are skipped, and so are stray copies
|
||||
/// (`app.running-123.exe`) of a declared one.
|
||||
pub fn built_program(app: &Path) -> Option<PathBuf> {
|
||||
if !app.join("Cargo.toml").is_file() {
|
||||
return None;
|
||||
}
|
||||
let built: Vec<PathBuf> = ["release", "debug"]
|
||||
.iter()
|
||||
.filter_map(|d| std::fs::read_dir(app.join("target").join(d)).ok())
|
||||
.flatten()
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.is_file() && is_program(p))
|
||||
.collect();
|
||||
let newest = |ps: &mut dyn Iterator<Item = &PathBuf>| ps.max_by_key(|p| p.metadata().and_then(|m| m.modified()).ok()).cloned();
|
||||
let stem = |p: &Path| p.file_stem().and_then(|s| s.to_str()).map(str::to_string).unwrap_or_default();
|
||||
let targets = cargo_target_dirs(app, std::env::var_os("CARGO_TARGET_DIR").as_deref());
|
||||
let built = |target: &Path| -> Vec<PathBuf> {
|
||||
["release", "debug"]
|
||||
.iter()
|
||||
.filter_map(|d| std::fs::read_dir(target.join(d)).ok())
|
||||
.flatten()
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.is_file() && is_program(p))
|
||||
.collect()
|
||||
};
|
||||
let newest = |mut programs: Vec<PathBuf>, name: Option<&str>| -> Option<PathBuf> {
|
||||
if let Some(name) = name {
|
||||
programs.retain(|p| p.file_stem().and_then(|s| s.to_str()) == Some(name));
|
||||
}
|
||||
programs.into_iter().max_by_key(|p| p.metadata().and_then(|m| m.modified()).ok())
|
||||
};
|
||||
let wanted = cargo_programs(app);
|
||||
if wanted.is_empty() {
|
||||
return newest(&mut built.iter());
|
||||
return targets.iter().find_map(|target| newest(built(target), None));
|
||||
}
|
||||
wanted.iter().find_map(|name| newest(&mut built.iter().filter(|p| stem(p) == *name)))
|
||||
wanted.iter().find_map(|name| targets.iter().find_map(|target| newest(built(target), Some(name.as_str()))))
|
||||
}
|
||||
|
||||
/// The directories Cargo may have written the build to, best first: `CARGO_TARGET_DIR`, the
|
||||
/// closest `.cargo/config.toml` (or `.cargo/config`) with `build.target-dir`, then `app/target`.
|
||||
/// A project's setting is relative to the folder holding `.cargo`; the environment's is relative
|
||||
/// to `app`, where Cargo runs. The default stays last, so a program built before the setting
|
||||
/// moved is still found. Reading the settings starts no program, so Play and the checks may call
|
||||
/// this on the UI thread.
|
||||
fn cargo_target_dirs(app: &Path, env: Option<&std::ffi::OsStr>) -> Vec<PathBuf> {
|
||||
let mut dirs = Vec::new();
|
||||
if let Some(dir) = env.filter(|dir| !dir.is_empty()) {
|
||||
let dir = resolved(app, PathBuf::from(dir));
|
||||
if !dirs.contains(&dir) {
|
||||
dirs.push(dir);
|
||||
}
|
||||
}
|
||||
let mut here = Some(app);
|
||||
for _ in 0..32 {
|
||||
let Some(dir) = here else { break };
|
||||
if let Some(target) = cargo_config_target(&dir.join(".cargo")) {
|
||||
let target = resolved(dir, target);
|
||||
if !dirs.contains(&target) {
|
||||
dirs.push(target);
|
||||
}
|
||||
break;
|
||||
}
|
||||
here = dir.parent();
|
||||
}
|
||||
let default = app.join("target");
|
||||
if !dirs.contains(&default) {
|
||||
dirs.push(default);
|
||||
}
|
||||
dirs
|
||||
}
|
||||
|
||||
/// A relative Cargo target dir from `base`, or the absolute one as it is.
|
||||
fn resolved(base: &Path, dir: PathBuf) -> PathBuf {
|
||||
if dir.is_absolute() { dir } else { base.join(dir) }
|
||||
}
|
||||
|
||||
/// `build.target-dir` from a folder's Cargo config, when it sets one.
|
||||
fn cargo_config_target(cargo_dir: &Path) -> Option<PathBuf> {
|
||||
["config.toml", "config"].iter().find_map(|name| {
|
||||
let text = std::fs::read_to_string(cargo_dir.join(name)).ok()?;
|
||||
let config: toml::Table = text.parse().ok()?;
|
||||
config.get("build")?.get("target-dir")?.as_str().map(PathBuf::from)
|
||||
})
|
||||
}
|
||||
|
||||
/// Libraries that put a window on screen: a package using one makes the program to start.
|
||||
@@ -304,3 +358,44 @@ mod launch_tests {
|
||||
assert_eq!(launch(relative, None), Some((cwd.join(binary), vec![root])));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod target_dir_tests {
|
||||
use super::*;
|
||||
use std::ffi::OsStr;
|
||||
|
||||
fn config(dir: &Path, body: &str) {
|
||||
std::fs::create_dir_all(dir.join(".cargo")).unwrap();
|
||||
std::fs::write(dir.join(".cargo/config.toml"), body).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn target_dirs_follow_the_environment_then_the_project_setting() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let app = d.path().join("app");
|
||||
std::fs::create_dir_all(&app).unwrap();
|
||||
assert_eq!(cargo_target_dirs(&app, None), vec![app.join("target")], "the default");
|
||||
let env = OsStr::new("env-out");
|
||||
assert_eq!(cargo_target_dirs(&app, Some(env)), vec![app.join("env-out"), app.join("target")], "the environment wins");
|
||||
let absolute = d.path().join("absolute");
|
||||
assert_eq!(cargo_target_dirs(&app, Some(absolute.as_os_str())), vec![absolute, app.join("target")], "an absolute environment path stays as it is");
|
||||
config(&app, "[build]\ntarget-dir = \"out\"\n");
|
||||
assert_eq!(cargo_target_dirs(&app, None), vec![app.join("out"), app.join("target")], "the project's setting");
|
||||
assert_eq!(
|
||||
cargo_target_dirs(&app, Some(env)),
|
||||
vec![app.join("env-out"), app.join("out"), app.join("target")],
|
||||
"the environment comes before the project's setting"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn target_dirs_read_an_ancestor_setting_when_the_closer_one_has_none() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let workspace = d.path().join("workspace");
|
||||
let app = workspace.join("app");
|
||||
std::fs::create_dir_all(&app).unwrap();
|
||||
config(&app, "[build]\nother = 1\n");
|
||||
config(&workspace, "[build]\ntarget-dir = \"out\"\n");
|
||||
assert_eq!(cargo_target_dirs(&app, None), vec![workspace.join("out"), app.join("target")], "the closest setting wins");
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,7 @@ coverage and secrets passed. The companion CLI was built with locked dependencie
|
||||
| Failed external programs | A program that prints more than its output cap keeps the start for parsers and up to the last 16 KiB for the error. The error names the last error, the excerpt shows the end (a long final line keeps its end), and says when the output was cut; adb, Frida, build checks, screenshots and SSH use the kept end. | Process regressions for 4 MiB of stderr and stdout ending in REAL-ERROR, a truncated crash whose error sits above its stack trace, and a long final line; the existing cap, crash and timeout tests. |
|
||||
| New-project toolchains | New Python, Go, .NET, Maven, Gradle and Make projects use the same build catalogue as existing projects (`engine/src/toolchains.toml`). A Python project without a root entrypoint can start its one installed `[project.scripts]` console wrapper from its virtual environment. Shared launch recipes let screenshots, self-tests and Play start Python `main.py`, `app.py` or `__main__.py`, a single Python package with a `<name>/__main__.py` as `python3 -m <name>` in a project with a Python marker, Go with a root `main.go` or a single `cmd/<name>/main.go`, one root `.csproj`, and the single runnable jar (its manifest names a `Main-Class`) a Maven (`target/*.jar`) or Gradle (`build/libs/*.jar`) build left, with `java -jar`. Maven builds with `package -DskipTests`, so that jar is this build's. Python uses a project-local virtual environment when present, resolves its executable to an absolute path even for relative project folders, and forces fresh syntax compilation; .NET passes app options after `--`. Manifest markers take priority over standalone Python files. Runtime window-capture requests normalize relative project and runner paths to absolute paths. Godot and Electron receive an absolute project path when launched from a relative workspace. Existing packaged programs, Rust, Electron and Godot keep launch priority. Multiple `.csproj`, package or runnable-jar matches or a capped scan do not choose a project. Other layouts still need a packaged program or another launch recipe | Catalogue and launch regressions cover argv boundaries, ambiguous entrypoints, scan limits and Windows/local interpreter selection, including relative project folders and unchanged argument boundaries. Capture-request tests cover relative placements and PATH results, local interpreters and optional UI-tree output without opening a window. An owned interpreter stand-in runs the full new-project build/screenshot path, rejects failed builds, and verifies a failed screenshot cannot reuse an old PNG. An opt-in real Python 3.15.2 fixture on macOS passes build/screenshot checks, ignores invalid virtualenv code, and rejects a syntax error even when its mtime matches the cached version. Without `compileall -f` that same syntax-error probe incorrectly exits zero. Real Go/.NET/Maven/Gradle runs, Windows and GUI apps have not been verified for this change; `java -jar` on a real jar has not been run |
|
||||
| Relative project folders | Engine build arguments, option runs (`--screenshot`, `--self-test`), console run checks, Play and pins resolve their program, working directory and option/output paths to absolute paths before starting a child in `app/`. | Engine and AI regressions cover a relative workspace with a built program: Play and a pinned copy start and exit, an option run saves its file, a console run check compares its output, and Unity build arguments stay absolute; existing absolute-path controls remain. |
|
||||
| Cargo target directories | A Rust build's program is looked for under `CARGO_TARGET_DIR`, then the closest project or ancestor `.cargo/config.toml`/`.cargo` `build.target-dir`, then the default `target/`; Play, screenshot and self-test checks use it. | An AI regression with `.cargo/config.toml` `target-dir = "out"` fails before the fix; engine tests cover the environment, an absolute environment path, a project setting, an ancestor setting when the closer one has none, and the default fallback. No live build with a custom target dir was run. |
|
||||
| Keep non-UTF-8 Godot paths from opening a different project | Decline a Godot launch when its project path cannot be represented as UTF-8 instead of silently replacing bytes in the selected path. | Regression fixtures cover two distinct Unix project directories whose paths collapse under lossy conversion, plus the valid Unicode project. |
|
||||
| Cargo paths in Rust binaries | A versioned directory contributes dependency evidence only for a valid crate name and complete semantic version. Prerelease and build identifiers remain available. | Mixed registry-style path evidence with invalid names, invalid versions and a versioned dependency. |
|
||||
| Mac call observation | `apple_calls_observe` launches an executable under LLDB, records named function hits and reports a debugger refusal as an error with remediation. | Opt-in engine regression on arm64 macOS 27.0.1 with Apple LLDB 2100.0.16.4 recorded `alpha` and `beta` in order and returned `ProgramFailed` for a non-executable input (#1844). |
|
||||
|
||||
Reference in new issue
Block a user