From 282a3e73244e6115353ecb7a0922d44bc72a8443 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 16 Jun 2023 21:17:54 -0500 Subject: [PATCH 1/2] refactor(core): Split out target-dir defaulting --- src/cargo/core/workspace.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 7f7114131fe..4a53e4d211e 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -381,7 +381,11 @@ impl<'cfg> Workspace<'cfg> { pub fn target_dir(&self) -> Filesystem { self.target_dir .clone() - .unwrap_or_else(|| Filesystem::new(self.root().join("target"))) + .unwrap_or_else(|| self.default_target_dir()) + } + + fn default_target_dir(&self) -> Filesystem { + Filesystem::new(self.root().join("target")) } /// Returns the root `[replace]` section of this workspace. From aca7b084055281cd9a0ccf588c9417eab609b5c6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 16 Jun 2023 21:37:55 -0500 Subject: [PATCH 2/2] fix(embedded): Keep target dir in cargo home This was broken in #12268 when we stopped using an intermediate `Cargo.toml` file. Unlike pre-#12268, - We are hashing the path, rather than the content, with the assumption that people change content more frequently than the path - We are using a simpler hash than `blake3` in the hopes that we can get away with it Unlike the Pre-RFC demo - We are not forcing a single target dir for all scripts in the hopes that we get #5931 --- src/cargo/core/workspace.rs | 12 +++++++++++- tests/testsuite/script.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 4a53e4d211e..c13163af8b9 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -385,7 +385,17 @@ impl<'cfg> Workspace<'cfg> { } fn default_target_dir(&self) -> Filesystem { - Filesystem::new(self.root().join("target")) + if self.root_maybe().is_embedded() { + let hash = crate::util::hex::short_hash(&self.root_manifest().to_string_lossy()); + let mut rel_path = PathBuf::new(); + rel_path.push("target"); + rel_path.push(&hash[0..2]); + rel_path.push(&hash[2..]); + + self.config().home().join(rel_path) + } else { + Filesystem::new(self.root().join("target")) + } } /// Returns the root `[replace]` section of this workspace. diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index d4f343be644..0b3ace7c503 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -35,7 +35,7 @@ args: [] [WARNING] `package.edition` is unspecifiead, defaulting to `2021` [COMPILING] echo v0.0.0 ([ROOT]/foo) [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s -[RUNNING] `[..]debug/echo[EXE]` +[RUNNING] `[..]/debug/echo[EXE]` ", ) .run(); @@ -537,3 +537,28 @@ fn main() { ) .run(); } + +#[cargo_test] +fn implicit_target_dir() { + let script = ECHO_SCRIPT; + let p = cargo_test_support::project() + .file("script.rs", script) + .build(); + + p.cargo("-Zscript script.rs") + .masquerade_as_nightly_cargo(&["script"]) + .with_stdout( + r#"bin: [ROOT]/home/.cargo/target/[..]/debug/script[EXE] +args: [] +"#, + ) + .with_stderr( + "\ +[WARNING] `package.edition` is unspecifiead, defaulting to `2021` +[COMPILING] script v0.0.0 ([ROOT]/foo) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s +[RUNNING] `[ROOT]/home/.cargo/target/[..]/debug/script[EXE]` +", + ) + .run(); +}