Skip to content

Commit edfbcf0

Browse files
committed
Return a hard error when custom build outside package
1 parent 92ce5a2 commit edfbcf0

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn build_ar_list(
344344
paths::normalize_path(&pkg.root().join(custome_build_path));
345345
if !abs_custome_build_path.is_file() || !abs_custome_build_path.starts_with(pkg.root())
346346
{
347-
error_custom_build_file_not_in_package(pkg, &abs_custome_build_path, t, &ws)?
347+
error_custom_build_file_not_in_package(pkg, &abs_custome_build_path, t)?;
348348
}
349349
}
350350
}
@@ -427,20 +427,16 @@ fn error_custom_build_file_not_in_package(
427427
pkg: &Package,
428428
path: &Path,
429429
target: &Target,
430-
ws: &Workspace<'_>,
431-
) -> CargoResult<()> {
430+
) -> CargoResult<Vec<ArchiveFile>> {
432431
let tip = {
432+
let description_name = target.description_named();
433433
if path.is_file() {
434-
format!("the source file of {:?} target `{}` doesn't appear to be a path inside of the package.\n\
434+
format!("the source file of `{description_name}` doesn't appear to be a path inside of the package.\n\
435435
It is at `{}`, whereas the root the package is `{}`.\n",
436-
target.kind(), target.name(), path.display(), pkg.root().display()
436+
path.display(), pkg.root().display()
437437
)
438438
} else {
439-
format!(
440-
"the source file of {:?} target `{}` doesn't appear to exist.\n",
441-
target.kind(),
442-
target.name()
443-
)
439+
format!("the source file of `{description_name}` doesn't appear to exist.\n",)
444440
}
445441
};
446442
let msg = format!(
@@ -449,7 +445,7 @@ fn error_custom_build_file_not_in_package(
449445
Please update the `build` setting in the manifest at `{}` and point to a path inside the root of the package.",
450446
tip, pkg.manifest_path().display()
451447
);
452-
ws.config().shell().error(&msg)
448+
anyhow::bail!(msg)
453449
}
454450

455451
/// Construct `Cargo.lock` for the package to be published.

tests/testsuite/package.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,26 +3325,31 @@ fn build_script_outside_pkg_root() {
33253325
let mut expect_msg = String::from("\
33263326
warning: manifest has no documentation, homepage or repository.
33273327
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
3328-
error: the source file of \"custom-build\" target `build-script-custom_build` doesn't appear to exist.
3328+
error: the source file of `build script` doesn't appear to exist.
33293329
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.
33303330
Please update the `build` setting in the manifest at `[CWD]/Cargo.toml` and point to a path inside the root of the package.
33313331
");
33323332
// custom_build.rs does not exist
3333-
p.cargo("package -l").with_stderr(&expect_msg).run();
3333+
p.cargo("package -l")
3334+
.with_status(101)
3335+
.with_stderr(&expect_msg)
3336+
.run();
33343337

33353338
// custom_build.rs outside the package root
3336-
let custom_build_root = p.root().parent().unwrap().join("t_custom_build");
3339+
let custom_build_root = paths::root().join("t_custom_build");
33373340
_ = fs::create_dir(&custom_build_root).unwrap();
33383341
_ = fs::write(&custom_build_root.join("custom_build.rs"), "fn main() {}");
33393342
expect_msg = format!(
33403343
"\
33413344
warning: manifest has no documentation, homepage or repository.
33423345
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
3343-
error: the source file of \"custom-build\" target `build-script-custom_build` doesn't appear to be a path inside of the package.
3346+
error: the source file of `build script` doesn't appear to be a path inside of the package.
33443347
It is at `{}/t_custom_build/custom_build.rs`, whereas the root the package is `[CWD]`.
33453348
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.
33463349
Please update the `build` setting in the manifest at `[CWD]/Cargo.toml` and point to a path inside the root of the package.
3347-
", p.root().parent().unwrap().display());
3348-
p.cargo("package -l").with_stderr(&expect_msg).run();
3349-
_ = fs::remove_dir_all(&custom_build_root).unwrap();
3350+
", paths::root().display());
3351+
p.cargo("package -l")
3352+
.with_status(101)
3353+
.with_stderr(&expect_msg)
3354+
.run();
33503355
}

0 commit comments

Comments
 (0)