Skip to content

Commit aca7b08

Browse files
committed
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
1 parent 282a3e7 commit aca7b08

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/cargo/core/workspace.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,17 @@ impl<'cfg> Workspace<'cfg> {
385385
}
386386

387387
fn default_target_dir(&self) -> Filesystem {
388-
Filesystem::new(self.root().join("target"))
388+
if self.root_maybe().is_embedded() {
389+
let hash = crate::util::hex::short_hash(&self.root_manifest().to_string_lossy());
390+
let mut rel_path = PathBuf::new();
391+
rel_path.push("target");
392+
rel_path.push(&hash[0..2]);
393+
rel_path.push(&hash[2..]);
394+
395+
self.config().home().join(rel_path)
396+
} else {
397+
Filesystem::new(self.root().join("target"))
398+
}
389399
}
390400

391401
/// Returns the root `[replace]` section of this workspace.

tests/testsuite/script.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ args: []
3535
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
3636
[COMPILING] echo v0.0.0 ([ROOT]/foo)
3737
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
38-
[RUNNING] `[..]debug/echo[EXE]`
38+
[RUNNING] `[..]/debug/echo[EXE]`
3939
",
4040
)
4141
.run();
@@ -537,3 +537,28 @@ fn main() {
537537
)
538538
.run();
539539
}
540+
541+
#[cargo_test]
542+
fn implicit_target_dir() {
543+
let script = ECHO_SCRIPT;
544+
let p = cargo_test_support::project()
545+
.file("script.rs", script)
546+
.build();
547+
548+
p.cargo("-Zscript script.rs")
549+
.masquerade_as_nightly_cargo(&["script"])
550+
.with_stdout(
551+
r#"bin: [ROOT]/home/.cargo/target/[..]/debug/script[EXE]
552+
args: []
553+
"#,
554+
)
555+
.with_stderr(
556+
"\
557+
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
558+
[COMPILING] script v0.0.0 ([ROOT]/foo)
559+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
560+
[RUNNING] `[ROOT]/home/.cargo/target/[..]/debug/script[EXE]`
561+
",
562+
)
563+
.run();
564+
}

0 commit comments

Comments
 (0)