Skip to content

Commit c1b4a80

Browse files
committed
Add warning for invalid custom build file
1 parent 80326ca commit c1b4a80

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::Arc;
77
use std::task::Poll;
88

99
use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor};
10+
use crate::core::manifest::Target;
1011
use crate::core::resolver::CliFeatures;
1112
use crate::core::{registry::PackageRegistry, resolver::HasDevUnits};
1213
use crate::core::{Feature, Shell, Verbosity, Workspace};
@@ -331,6 +332,29 @@ fn build_ar_list(
331332
warn_on_nonexistent_file(&pkg, &readme_path, "readme", &ws)?;
332333
}
333334
}
335+
336+
for t in pkg
337+
.manifest()
338+
.targets()
339+
.iter()
340+
.filter(|t| t.is_custom_build())
341+
{
342+
if let Some(custome_build_path) = t.src_path().path() {
343+
let abs_custome_build_path =
344+
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)?
354+
}
355+
}
356+
}
357+
334358
result.sort_unstable_by(|a, b| a.rel_path.cmp(&b.rel_path));
335359

336360
Ok(result)
@@ -405,6 +429,23 @@ fn warn_on_nonexistent_file(
405429
))
406430
}
407431

432+
fn warn_custom_build_file_not_in_package(
433+
pkg: &Package,
434+
path: &Path,
435+
target: &Target,
436+
ws: &Workspace<'_>,
437+
) -> CargoResult<()> {
438+
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\
441+
This may cause issue during packaging, as modules resolution and resources included via macros are often relative to the path of source files.\n\
442+
Please update the `build` setting in the manifest at `{}` and point to a path inside the root of the package.\n",
443+
target.kind(), target.name(), path.display(), pkg.root().display(), pkg.manifest_path().display()
444+
);
445+
446+
ws.config().shell().warn(&msg)
447+
}
448+
408449
/// Construct `Cargo.lock` for the package to be published.
409450
fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
410451
let config = ws.config();

0 commit comments

Comments
 (0)