Skip to content
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
21 changes: 1 addition & 20 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use cargo_util_schemas::manifest::PackageName;

use crate::util::frontmatter::FrontmatterError;
use crate::util::frontmatter::ScriptSource;
use crate::util::restricted_names;

pub(super) fn expand_manifest(content: &str) -> Result<String, FrontmatterError> {
let source = ScriptSource::parse(content)?;
Expand Down Expand Up @@ -57,25 +56,7 @@ pub fn sanitize_name(name: &str) -> String {
'-'
};

let mut name = PackageName::sanitize(name, placeholder).into_inner();

loop {
if restricted_names::is_keyword(&name) {
name.push(placeholder);
} else if restricted_names::is_conflicting_artifact_name(&name) {
// Being an embedded manifest, we always assume it is a `[[bin]]`
name.push(placeholder);
} else if name == "test" {
name.push(placeholder);
} else if restricted_names::is_windows_reserved(&name) {
// Go ahead and be consistent across platforms
name.push(placeholder);
} else {
break;
}
}

name
PackageName::sanitize(name, placeholder).into_inner()
}

#[cfg(test)]
Expand Down
83 changes: 75 additions & 8 deletions tests/testsuite/script/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,25 +674,92 @@ args: []
}

#[cargo_test(nightly, reason = "-Zscript is unstable")]
fn test_name_is_deps_dir_implicit() {
#[cfg(not(windows))]
fn test_name_is_windows_reserved_name() {
let script = ECHO_SCRIPT;
let p = cargo_test_support::project()
.file("deps.rs", script)
.build();
let p = cargo_test_support::project().file("con", script).build();

p.cargo("-Zscript -v deps.rs")
p.cargo("-Zscript -v ./con")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout_data(str![[r#"
current_exe: [ROOT]/home/.cargo/build/[HASH]/target/debug/deps-[EXE]
current_exe: [ROOT]/home/.cargo/build/[HASH]/target/debug/con[EXE]
arg0: [..]
args: []

"#]])
.with_stderr_data(str![[r#"
[WARNING] `package.edition` is unspecified, defaulting to `2024`
[COMPILING] deps- v0.0.0 ([ROOT]/foo/deps.rs)
[COMPILING] con v0.0.0 ([ROOT]/foo/con)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `[ROOT]/home/.cargo/build/[HASH]/target/debug/deps-[EXE]`
[RUNNING] `[ROOT]/home/.cargo/build/[HASH]/target/debug/con[EXE]`

"#]])
.run();
}

#[cargo_test(nightly, reason = "-Zscript is unstable")]
fn test_name_is_sysroot_package_name() {
let script = ECHO_SCRIPT;
let p = cargo_test_support::project().file("test", script).build();

p.cargo("-Zscript -v ./test")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout_data(str![[r#"
current_exe: [ROOT]/home/.cargo/build/[HASH]/target/debug/test[EXE]
arg0: [..]
args: []

"#]])
.with_stderr_data(str![[r#"
[WARNING] `package.edition` is unspecified, defaulting to `2024`
[COMPILING] test v0.0.0 ([ROOT]/foo/test)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `[ROOT]/home/.cargo/build/[HASH]/target/debug/test[EXE]`

"#]])
.run();
}

#[cargo_test(nightly, reason = "-Zscript is unstable")]
fn test_name_is_keyword() {
let script = ECHO_SCRIPT;
let p = cargo_test_support::project().file("self", script).build();

p.cargo("-Zscript -v ./self")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout_data(str![[r#"
current_exe: [ROOT]/home/.cargo/build/[HASH]/target/debug/self[EXE]
arg0: [..]
args: []

"#]])
.with_stderr_data(str![[r#"
[WARNING] `package.edition` is unspecified, defaulting to `2024`
[COMPILING] self v0.0.0 ([ROOT]/foo/self)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `[ROOT]/home/.cargo/build/[HASH]/target/debug/self[EXE]`

"#]])
.run();
}

#[cargo_test(nightly, reason = "-Zscript is unstable")]
fn test_name_is_deps_dir_implicit() {
let script = ECHO_SCRIPT;
let p = cargo_test_support::project()
.file("deps.rs", script)
.build();

p.cargo("-Zscript -v deps.rs")
.masquerade_as_nightly_cargo(&["script"])
.with_status(101)
.with_stdout_data(str![""])
.with_stderr_data(str![[r#"
[WARNING] `package.edition` is unspecified, defaulting to `2024`
[ERROR] failed to parse manifest at `[ROOT]/foo/deps.rs`

Caused by:
the binary target name `deps` is forbidden, it conflicts with cargo's build directory names

"#]])
.run();
Expand Down