rust-lang/cargo · error · anyhow::Error

{}This may cause issue during packaging, as modules resoluti

Error message

{}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 `{}` and point to a path inside the root of the package.

What it means

During packaging, every target with is_custom_build() (a build = "build.rs" script) has its src_path normalized and checked to be both an existing file and to start_with(pkg.root()). If it is missing or escapes the package root, error_custom_build_file_not_in_package builds a message explaining that module/macro resource resolution is path-relative and bails. This prevents publishing crates whose build script would not behave identically when unpacked.

Source

Thrown at src/ops/cargo_package/mod.rs:739

        if path.is_file() {
            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",
                path.display(),
                pkg.root().display()
            )
        } else {
            format!("the source file of {description_name} doesn't appear to exist.\n",)
        }
    };
    let msg = format!(
        "{}\
        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.",
        tip,
        pkg.manifest_path().display()
    );
    anyhow::bail!(msg)
}

/// Construct `Cargo.lock` for the package to be published.
fn build_lock(
    ws: &Workspace<'_>,
    opts: &PackageOpts<'_>,
    publish_pkg: &Package,
    local_reg: Option<&TmpRegistry<'_>>,
) -> CargoResult<String> {
    let gctx = ws.gctx();
    let mut orig_resolve = ops::load_pkg_lockfile(ws)?;

    let mut tmp_ws = Workspace::ephemeral(publish_pkg.clone(), ws.gctx(), None, true)?;

    // The local registry is an overlay used for simulating workspace packages
    // that are supposed to be in the published registry, but that aren't there
    // yet.
    if let Some(local_reg) = local_reg {

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Move the build script inside the package: `build = "build.rs"` with the file at the crate root
  2. Create the missing build.rs file, or remove the `build = ` field from Cargo.toml if no build script is needed
  3. If the script is shared across workspace members, copy it into each crate (or generate it) so the published package is self-contained

Example fix

# before
# Cargo.toml: build = "../shared/build.rs"
cargo package   # -> error
# after
cp ../shared/build.rs ./build.rs
# Cargo.toml: build = "build.rs"
cargo package
Defensive patterns

Strategy: validation

Validate before calling

use std::path::Path;
fn validate_build_script(root: &Path, build_rel: Option<&str>) -> Result<(), String> {
    let Some(rel) = build_rel else { return Ok(()); };
    let abs = root.join(rel);
    if !abs.is_file() || !abs.starts_with(root) {
        return Err(format!("build script `{rel}` missing or outside package root"));
    }
    Ok(())
}

Type guard

import { existsSync, statSync } from 'fs';
import { join, resolve, relative } from 'path';
function buildScriptInPackage(root: string, rel: string): boolean {
  const abs = join(root, rel);
  if (!existsSync(abs) || !statSync(abs).isFile()) return false;
  const rel2 = relative(root, resolve(abs));
  return !rel2.startsWith('..');
}

Prevention

When it happens

Trigger: `cargo package` with `build = "../../shared/build.rs"` or `build = "build.rs"` when the file does not exist, or a build script path pointing outside the crate via `..`.

Common situations: Workspace crates that share a build script via a relative `..` path (forbidden for publishing); deleted build.rs without removing the `build = ` key; typo in the build path; symlinks that resolve outside the package root.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/9176b40e599f7db8.json. Report an issue: GitHub.