Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(embeded): Don't pollute the scripts dir with target/ #12282

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,21 @@ 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 {
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..]);
Comment on lines +392 to +393
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not a blocker)
Maybe we don't need this intermediate directory as the short hash is short enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not about the length of the hash but the potential for the number of children of the parent directory. I can't remember if this is true for all platforms but I think its at least on Windows, you can get some pretty bad performance.

In the prior version, I had a third level instead. Here I'm hoping we can get away with just two.


self.config().home().join(rel_path)
} else {
Filesystem::new(self.root().join("target"))
}
}

/// Returns the root `[replace]` section of this workspace.
Expand Down
27 changes: 26 additions & 1 deletion tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}