Skip to content

Commit 81a7392

Browse files
committed
Auto merge of #12282 - epage:target, r=weihanglo
fix(embeded): Don't pollute the scripts dir with `target/` ### What does this PR try to resolve? This PR is part of #12207. This specific behavior 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 ### How should we test and review this PR? A new test was added specifically to show the target dir behavior, rather than overloading an existing test or making all tests sensitive to changes in this behavior. ### Additional information In the future, we might want to resolve symlinks before we get to this point
2 parents 424362a + aca7b08 commit 81a7392

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/cargo/core/workspace.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,21 @@ impl<'cfg> Workspace<'cfg> {
381381
pub fn target_dir(&self) -> Filesystem {
382382
self.target_dir
383383
.clone()
384-
.unwrap_or_else(|| Filesystem::new(self.root().join("target")))
384+
.unwrap_or_else(|| self.default_target_dir())
385+
}
386+
387+
fn default_target_dir(&self) -> Filesystem {
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+
}
385399
}
386400

387401
/// 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)