Return a hard error when custom build outside package

This commit is contained in:
Lin Yihai 2023-11-21 11:10:46 +08:00
parent 92ce5a2b27
commit edfbcf0a6c
2 changed files with 19 additions and 18 deletions

View File

@ -344,7 +344,7 @@ fn build_ar_list(
paths::normalize_path(&pkg.root().join(custome_build_path));
if !abs_custome_build_path.is_file() || !abs_custome_build_path.starts_with(pkg.root())
{
error_custom_build_file_not_in_package(pkg, &abs_custome_build_path, t, &ws)?
error_custom_build_file_not_in_package(pkg, &abs_custome_build_path, t)?;
}
}
}
@ -427,20 +427,16 @@ fn error_custom_build_file_not_in_package(
pkg: &Package,
path: &Path,
target: &Target,
ws: &Workspace<'_>,
) -> CargoResult<()> {
) -> CargoResult<Vec<ArchiveFile>> {
let tip = {
let description_name = target.description_named();
if path.is_file() {
format!("the source file of {:?} target `{}` doesn't appear to be a path inside of the package.\n\
format!("the source file of `{description_name}` doesn't appear to be a path inside of the package.\n\
It is at `{}`, whereas the root the package is `{}`.\n",
target.kind(), target.name(), path.display(), pkg.root().display()
path.display(), pkg.root().display()
)
} else {
format!(
"the source file of {:?} target `{}` doesn't appear to exist.\n",
target.kind(),
target.name()
)
format!("the source file of `{description_name}` doesn't appear to exist.\n",)
}
};
let msg = format!(
@ -449,7 +445,7 @@ fn error_custom_build_file_not_in_package(
Please update the `build` setting in the manifest at `{}` and point to a path inside the root of the package.",
tip, pkg.manifest_path().display()
);
ws.config().shell().error(&msg)
anyhow::bail!(msg)
}
/// Construct `Cargo.lock` for the package to be published.

View File

@ -3325,26 +3325,31 @@ fn build_script_outside_pkg_root() {
let mut expect_msg = String::from("\
warning: manifest has no documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
error: the source file of \"custom-build\" target `build-script-custom_build` doesn't appear to exist.
error: the source file of `build script` doesn't appear to exist.
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.
Please update the `build` setting in the manifest at `[CWD]/Cargo.toml` and point to a path inside the root of the package.
");
// custom_build.rs does not exist
p.cargo("package -l").with_stderr(&expect_msg).run();
p.cargo("package -l")
.with_status(101)
.with_stderr(&expect_msg)
.run();
// custom_build.rs outside the package root
let custom_build_root = p.root().parent().unwrap().join("t_custom_build");
let custom_build_root = paths::root().join("t_custom_build");
_ = fs::create_dir(&custom_build_root).unwrap();
_ = fs::write(&custom_build_root.join("custom_build.rs"), "fn main() {}");
expect_msg = format!(
"\
warning: manifest has no documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
error: the source file of \"custom-build\" target `build-script-custom_build` doesn't appear to be a path inside of the package.
error: the source file of `build script` doesn't appear to be a path inside of the package.
It is at `{}/t_custom_build/custom_build.rs`, whereas the root the package is `[CWD]`.
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.
Please update the `build` setting in the manifest at `[CWD]/Cargo.toml` and point to a path inside the root of the package.
", p.root().parent().unwrap().display());
p.cargo("package -l").with_stderr(&expect_msg).run();
_ = fs::remove_dir_all(&custom_build_root).unwrap();
", paths::root().display());
p.cargo("package -l")
.with_status(101)
.with_stderr(&expect_msg)
.run();
}