From 8b102f25716490cac6e428519d42bc6f466779a2 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 22 Jun 2025 14:37:06 +0200 Subject: [PATCH] Stop using Cargo's OUT_DIR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (Note: this commit should technically have preceded the "Fix config paths for disjoint build-dirs and in-tree installs" one, to make that one easier to follow, but I wasn't 100% sure if this commit is right.) From https://doc.rust-lang.org/cargo/reference/environment-variables.html > OUT_DIR — If the package has a build script, this is set to the folder > where the build script should place its output. See below for more > information. (Only set during compilation.) so OUT_DIR is something like "target/debug/build/fish-41da27d587f48978". Whenever build.rs is re-run, we get a new one. I don't think we need this flexibility anywhere. It wouldn't protect concurrent "cargo test" from interfering with each other - that's handled by a file lock taken by Cargo. Use "target/" instead (or CMAKE_BINARY_DIR if set). Namespace the files better, so we don't create weird paths like target/test/complete_test/... target/fish_root/ --- build.rs | 8 +------- src/env/config_paths.rs | 2 +- src/path.rs | 2 +- src/tests/mod.rs | 7 +++++-- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/build.rs b/build.rs index 79b379ef1..931cd11ce 100644 --- a/build.rs +++ b/build.rs @@ -25,14 +25,8 @@ fn main() { .unwrap_or(canonicalize(MANIFEST_DIR).join("target")); // FISH_BUILD_DIR is set by CMake, if we are using it. - // OUT_DIR is set by Cargo when the build script is running (not compiling) - let default_build_dir = env::var("OUT_DIR").unwrap(); - let build_dir = option_env!("FISH_BUILD_DIR").unwrap_or(&default_build_dir); - let build_dir = canonicalize_str(build_dir); - rsconf::set_env_value("FISH_BUILD_DIR", &build_dir); - rsconf::set_env_value( - "FISH_BUILD_OUTPUT_DIR", + "FISH_BUILD_DIR", option_env!("FISH_BUILD_DIR").unwrap_or(cargo_target_dir.to_str().unwrap()), ); diff --git a/src/env/config_paths.rs b/src/env/config_paths.rs index 2c4490887..ad88fa7f2 100644 --- a/src/env/config_paths.rs +++ b/src/env/config_paths.rs @@ -32,7 +32,7 @@ // TODO: we should determine program_name from argv0 somewhere in this file // Detect if we're running right out of the CMAKE build directory - if exec_path.starts_with(env!("FISH_BUILD_OUTPUT_DIR")) { + if exec_path.starts_with(env!("FISH_BUILD_DIR")) { let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); FLOG!( config, diff --git a/src/path.rs b/src/path.rs index 210c7ffe5..add2b01a9 100644 --- a/src/path.rs +++ b/src/path.rs @@ -600,7 +600,7 @@ fn make_base_directory(xdg_var: &wstr, non_xdg_homepath: &wstr) -> BaseDirectory use std::path::PathBuf; let mut build_dir = PathBuf::from(env!("FISH_BUILD_DIR")); - build_dir.push("fish_root"); + build_dir.push("fish-test-home"); let err = match std::fs::create_dir_all(&build_dir) { Ok(_) => 0, diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 48250d77f..b750bebe8 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -42,6 +42,7 @@ pub mod prelude { use std::cell::RefCell; use std::env::set_current_dir; use std::ffi::CString; + use std::path::PathBuf; /// A wrapper around a Parser with some test helpers. pub struct TestParser { @@ -89,8 +90,10 @@ pub fn test_init() -> impl ScopeGuarding { DONE.get_or_init(|| { // If we are building with `cargo build` and have build w/ `cmake`, FISH_BUILD_DIR might // not yet exist. - std::fs::create_dir_all(env!("FISH_BUILD_DIR")).unwrap(); - set_current_dir(env!("FISH_BUILD_DIR")).unwrap(); + let mut test_dir = PathBuf::from(env!("FISH_BUILD_DIR")); + test_dir.push("fish-test"); + std::fs::create_dir_all(&test_dir).unwrap(); + set_current_dir(&test_dir).unwrap(); { let s = CString::new("").unwrap(); unsafe {