"can't find crate" in Workspace directory

I want to have a separated build script (in rust) for my program. The folder structure is like this

|- build
|--- build.rs
|--- Cargo.toml
|- src
|--- main.rs
|- Cargo.toml

My outside Cargo.toml is like this

name = "test"
version = "0.1.0"
edition = "2021"

members = ["build"]

And my Cargo.toml inside the build folder is like this:

name = "build"
version = "0.1.0"
authors = []

publish = false
cargo_metadata = "0.15.2"

name = "build"
path = "build.rs"

Simple enough?

However, upon running cargo run --package build --bin build, I got hit with:

 --> build\build.rs:1:1
1 | extern crate cargo_metadata;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

I have been wasting more than 1 hour on this. Please help :(((((((
Thank you.

I don't immediately see any issues.

Try changing this name. The package name test is reserved.

I think the issue is that you have build.rs in the root of the build package. Cargo thinks it is a build script and as such attempts to compile and run it before the main binary. rustc_metadata is not declared as build dependency, so when compiling as build script it fails. Try moving it to src/main.rs or src/bin/build.rs. In either case the explicit [[bin]] section shouldn't be necessary.

Ok so the issue is naming the file exactly build.rs makes cargo look for [build-dependencies]. Out of every name string possible in this universe, the build.rs costed me 2 hours. Change it to literally anything else will work.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.