Search code examples
rustrust-cargo

In my Cargo.toml a crate I can use in the package I'm building is not found when used in build.rs How can I fix this?


I want to use the async-process crate/package in my build.rs file.

In the crates.io page it says I can put it in my project's Cargo.toml as:

[dependencies]
async-process = "2.0.1"

And in my rust code as:

use async_process::Command;

I can use it in my own package but it breaks the compile of build.rs:

error[E0432]: unresolved import `async_process`
 --> build.rs:4:5
  |
4 | use async_process::Command;
  |     ^^^^^^^^^^^^^ maybe a missing crate `async_process`?
  |
  = help: consider adding `extern crate async_process` to use the `async_process` crate

I then tried:

extern crate async_process;

And get this error:

error[E0463]: can't find crate for `async_process`
 --> build.rs:4:1
  |
4 | extern crate async_process;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

is it some restriction placed on build.rs?

The reason I want this is I am using the build.rs to spawn a command to run a rather long process and want to log it's stdout to a file (or print to the console while running) the async-process::Command offered a way to do this, that is if I can use it.


Solution

  • In the Cargo Manifest

    [dependencies]
    

    are binaries and library dependencies (runtime).

    As you are using async-process in build.rs script, there is another section you should use:

    [build-dependencies]
    async-process = "2.0.1"
    

    Those dependencies are used for building your crate (compile time)

    Reference: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html