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
22 changes: 19 additions & 3 deletions src/workspace/parser/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub(super) fn to_targets(
script_path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("")
.expect("previously normalized")
);
targets.push(Target::custom_build_target(
&name,
Expand Down Expand Up @@ -906,7 +906,10 @@ fn validate_unique_names(targets: &[TomlTarget], target_kind: &str) -> CargoResu
fn validate_unique_build_scripts(scripts: &[String]) -> CargoResult<()> {
let mut seen = HashMap::default();
for script in scripts {
let stem = Path::new(script).file_stem().unwrap().to_str().unwrap();
let stem = Path::new(script)
.file_stem()
.and_then(|s| s.to_str())
.expect("previously normalized");
seen.entry(stem)
.or_insert_with(Vec::new)
.push(script.as_str());
Expand Down Expand Up @@ -1109,6 +1112,7 @@ pub fn normalize_build(
// Explicitly no build script.
Some(TomlPackageBuild::Auto(false)) => Ok(build.cloned()),
Some(TomlPackageBuild::SingleScript(build_file)) => {
validate_build_name(build_file)?;
let build_file = paths::normalize_path(Path::new(build_file));
let build = build_file.into_os_string().into_string().expect(
"`build_file` started as a String and `normalize_path` shouldn't have changed that",
Expand All @@ -1118,8 +1122,20 @@ pub fn normalize_build(
Some(TomlPackageBuild::Auto(true)) => {
Ok(Some(TomlPackageBuild::SingleScript(BUILD_RS.to_owned())))
}
Some(TomlPackageBuild::MultipleScript(_scripts)) => Ok(build.cloned()),
Some(TomlPackageBuild::MultipleScript(scripts)) => {
for script in scripts {
validate_build_name(script)?;
}
Ok(build.cloned())
}
}
}

fn validate_build_name(build: &str) -> CargoResult<()> {
if Path::new(build).file_stem().is_none() {
anyhow::bail!("invalid `package.build` file name");
}
Ok(())
}

fn name_or_panic(target: &TomlTarget) -> &str {
Expand Down
28 changes: 28 additions & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,34 @@ Caused by:
.run();
}

#[cargo_test]
fn build_script_empty() {
Comment thread
raushan728 marked this conversation as resolved.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2024"
build = ""
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
invalid `package.build` file name

"#]])
.run();
}

#[cargo_test]
fn links_duplicates() {
// this tests that the links_duplicates are caught at resolver time
Expand Down