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

can't find library `{name}`, rename file to `src/lib.rs` or

Error message

can't find library `{name}`, rename file to `src/lib.rs` or specify lib.path

What it means

Error "can't find library `{name}`, rename file to `src/lib.rs` or specify lib.path" thrown in rust-lang/cargo.

Source

Thrown at src/workspace/parser/targets.rs:202

        validate_proc_macro(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?;
        validate_crate_types(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?;

        if lib.path.is_none() {
            if let Some(inferred) = inferred {
                lib.path = Some(PathValue(inferred));
            } else {
                let name = name_or_panic(&lib);
                let legacy_path = Path::new("src").join(format!("{name}.rs"));
                if edition == Edition::Edition2015 && package_root.join(&legacy_path).exists() {
                    warnings.push(format!(
                        "path `{}` was erroneously implicitly accepted for library `{name}`,\n\
                     please rename the file to `src/lib.rs` or set lib.path in Cargo.toml",
                        legacy_path.display(),
                    ));
                    lib.path = Some(PathValue(legacy_path));
                } else {
                    anyhow::bail!(
                        "can't find library `{name}`, \
                     rename file to `src/lib.rs` or specify lib.path",
                    )
                }
            }
        }

        if let Some(PathValue(path)) = lib.path.as_ref() {
            lib.path = Some(PathValue(paths::normalize_path(&path).into()));
        }

        Ok(Some(lib))
    }
}

#[tracing::instrument(skip_all)]
fn to_lib_target(
    original_lib: Option<&TomlLibTarget>,

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Create `src/lib.rs` in the package, or set `[lib] path = "..."` in Cargo.toml pointing at the actual library file.
  2. Rename the existing library source file to `src/lib.rs`.

When it happens

Trigger: Thrown at src/workspace/parser/targets.rs:202 when the library encounters an invalid state.

Common situations: Occurs when Cargo cannot find a library target: `src/lib.rs` is missing and no `[lib] path` is set. Create `src/lib.rs` or add a `[lib]` section with `path = "..."` pointing at the library source file.


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