Skip to content

Commit 92ce5a2

Browse files
committed
Change the logging level and other improvement.
1 parent ac1e66d commit 92ce5a2

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,9 @@ fn build_ar_list(
342342
if let Some(custome_build_path) = t.src_path().path() {
343343
let abs_custome_build_path =
344344
paths::normalize_path(&pkg.root().join(custome_build_path));
345-
if abs_custome_build_path.is_file() {
346-
if !abs_custome_build_path
347-
.ancestors()
348-
.any(|ancestor| ancestor == pkg.root())
349-
{
350-
warn_custom_build_file_not_in_package(pkg, &abs_custome_build_path, t, &ws)?
351-
}
352-
} else {
353-
warn_on_nonexistent_file(&pkg, &custome_build_path, "build", &ws)?
345+
if !abs_custome_build_path.is_file() || !abs_custome_build_path.starts_with(pkg.root())
346+
{
347+
error_custom_build_file_not_in_package(pkg, &abs_custome_build_path, t, &ws)?
354348
}
355349
}
356350
}
@@ -429,21 +423,33 @@ fn warn_on_nonexistent_file(
429423
))
430424
}
431425

432-
fn warn_custom_build_file_not_in_package(
426+
fn error_custom_build_file_not_in_package(
433427
pkg: &Package,
434428
path: &Path,
435429
target: &Target,
436430
ws: &Workspace<'_>,
437431
) -> CargoResult<()> {
432+
let tip = {
433+
if path.is_file() {
434+
format!("the source file of {:?} target `{}` doesn't appear to be a path inside of the package.\n\
435+
It is at `{}`, whereas the root the package is `{}`.\n",
436+
target.kind(), target.name(), path.display(), pkg.root().display()
437+
)
438+
} else {
439+
format!(
440+
"the source file of {:?} target `{}` doesn't appear to exist.\n",
441+
target.kind(),
442+
target.name()
443+
)
444+
}
445+
};
438446
let msg = format!(
439-
"the source file of {:?} target `{}` doesn't appear to be a path inside of the package.\n\
440-
It is at {}, whereas the root the package is {}.\n\
447+
"{}\
441448
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.\n\
442449
Please update the `build` setting in the manifest at `{}` and point to a path inside the root of the package.",
443-
target.kind(), target.name(), path.display(), pkg.root().display(), pkg.manifest_path().display()
450+
tip, pkg.manifest_path().display()
444451
);
445-
446-
ws.config().shell().warn(&msg)
452+
ws.config().shell().error(&msg)
447453
}
448454

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

tests/testsuite/package.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3306,55 +3306,45 @@ fn init_and_add_inner_target(p: ProjectBuilder) -> ProjectBuilder {
33063306
}
33073307

33083308
#[cargo_test]
3309-
fn custom_build_warning() {
3309+
fn build_script_outside_pkg_root() {
33103310
let p = project()
33113311
.file(
33123312
"Cargo.toml",
33133313
r#"
3314-
[package]
3315-
name = "foo"
3316-
version = "0.0.1"
3317-
license = "MIT"
3318-
description = "foo"
3319-
authors = []
3320-
build = "../t_custom_build/custom_build.rs"
3321-
"#,
3314+
[package]
3315+
name = "foo"
3316+
version = "0.0.1"
3317+
license = "MIT"
3318+
description = "foo"
3319+
authors = []
3320+
build = "../t_custom_build/custom_build.rs"
3321+
"#,
33223322
)
33233323
.file("src/main.rs", "fn main() {}")
33243324
.build();
3325-
p.cargo("package -l")
3326-
.with_stderr(format!(
3327-
"\
3325+
let mut expect_msg = String::from("\
33283326
warning: manifest has no documentation, homepage or repository.
33293327
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
3330-
warning: build `{}/../t_custom_build/custom_build.rs` does not appear to exist.
3331-
Please update the build setting in the manifest at `{}/Cargo.toml`
3332-
This may become a hard error in the future.
3333-
",
3334-
p.root().display(),
3335-
p.root().display()
3336-
))
3337-
.run();
3328+
error: the source file of \"custom-build\" target `build-script-custom_build` doesn't appear to exist.
3329+
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.
3330+
Please update the `build` setting in the manifest at `[CWD]/Cargo.toml` and point to a path inside the root of the package.
3331+
");
3332+
// custom_build.rs does not exist
3333+
p.cargo("package -l").with_stderr(&expect_msg).run();
33383334

3339-
// crate custom_build.rs outside the package root
3335+
// custom_build.rs outside the package root
33403336
let custom_build_root = p.root().parent().unwrap().join("t_custom_build");
33413337
_ = fs::create_dir(&custom_build_root).unwrap();
33423338
_ = fs::write(&custom_build_root.join("custom_build.rs"), "fn main() {}");
3343-
3344-
p.cargo("package -l")
3345-
.with_stderr(format!(
3346-
"\
3339+
expect_msg = format!(
3340+
"\
33473341
warning: manifest has no documentation, homepage or repository.
33483342
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
3349-
warning: the source file of \"custom-build\" target `build-script-custom_build` doesn't appear to be a path inside of the package.
3350-
It is at {}/t_custom_build/custom_build.rs, whereas the root the package is {}.
3343+
error: the source file of \"custom-build\" target `build-script-custom_build` doesn't appear to be a path inside of the package.
3344+
It is at `{}/t_custom_build/custom_build.rs`, whereas the root the package is `[CWD]`.
33513345
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.
3352-
Please update the `build` setting in the manifest at `{}/Cargo.toml` and point to a path inside the root of the package.
3353-
",
3354-
p.root().parent().unwrap().display(),
3355-
p.root().display(),
3356-
p.root().display()
3357-
))
3358-
.run();
3346+
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();
33593349
_ = fs::remove_dir_all(&custom_build_root).unwrap();
33603350
}

0 commit comments

Comments
 (0)