Skip to content

Commit

Permalink
Change the logging level and other improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Nov 20, 2023
1 parent ac1e66d commit 92ce5a2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 49 deletions.
36 changes: 21 additions & 15 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,9 @@ fn build_ar_list(
if let Some(custome_build_path) = t.src_path().path() {
let abs_custome_build_path =
paths::normalize_path(&pkg.root().join(custome_build_path));
if abs_custome_build_path.is_file() {
if !abs_custome_build_path
.ancestors()
.any(|ancestor| ancestor == pkg.root())
{
warn_custom_build_file_not_in_package(pkg, &abs_custome_build_path, t, &ws)?
}
} else {
warn_on_nonexistent_file(&pkg, &custome_build_path, "build", &ws)?
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)?
}
}
}
Expand Down Expand Up @@ -429,21 +423,33 @@ fn warn_on_nonexistent_file(
))
}

fn warn_custom_build_file_not_in_package(
fn error_custom_build_file_not_in_package(
pkg: &Package,
path: &Path,
target: &Target,
ws: &Workspace<'_>,
) -> CargoResult<()> {
let tip = {
if path.is_file() {
format!("the source file of {:?} target `{}` 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()
)
} else {
format!(
"the source file of {:?} target `{}` doesn't appear to exist.\n",
target.kind(),
target.name()
)
}
};
let msg = format!(
"the source file of {:?} target `{}` doesn't appear to be a path inside of the package.\n\
It is at {}, whereas the root the package is {}.\n\
"{}\
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.\n\
Please update the `build` setting in the manifest at `{}` and point to a path inside the root of the package.",
target.kind(), target.name(), path.display(), pkg.root().display(), pkg.manifest_path().display()
tip, pkg.manifest_path().display()
);

ws.config().shell().warn(&msg)
ws.config().shell().error(&msg)
}

/// Construct `Cargo.lock` for the package to be published.
Expand Down
58 changes: 24 additions & 34 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3306,55 +3306,45 @@ fn init_and_add_inner_target(p: ProjectBuilder) -> ProjectBuilder {
}

#[cargo_test]
fn custom_build_warning() {
fn build_script_outside_pkg_root() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
license = "MIT"
description = "foo"
authors = []
build = "../t_custom_build/custom_build.rs"
"#,
[package]
name = "foo"
version = "0.0.1"
license = "MIT"
description = "foo"
authors = []
build = "../t_custom_build/custom_build.rs"
"#,
)
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("package -l")
.with_stderr(format!(
"\
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.
warning: build `{}/../t_custom_build/custom_build.rs` does not appear to exist.
Please update the build setting in the manifest at `{}/Cargo.toml`
This may become a hard error in the future.
",
p.root().display(),
p.root().display()
))
.run();
error: the source file of \"custom-build\" target `build-script-custom_build` 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();

// crate custom_build.rs outside the package root
// custom_build.rs outside the package root
let custom_build_root = p.root().parent().unwrap().join("t_custom_build");
_ = fs::create_dir(&custom_build_root).unwrap();
_ = fs::write(&custom_build_root.join("custom_build.rs"), "fn main() {}");

p.cargo("package -l")
.with_stderr(format!(
"\
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.
warning: the source file of \"custom-build\" target `build-script-custom_build` 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 {}.
error: the source file of \"custom-build\" target `build-script-custom_build` 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 `{}/Cargo.toml` and point to a path inside the root of the package.
",
p.root().parent().unwrap().display(),
p.root().display(),
p.root().display()
))
.run();
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();
}

0 comments on commit 92ce5a2

Please sign in to comment.