Unable to find crate that is listed in [build-dependencies] section

江枫思渺然 提交于 2020-08-04 21:51:25

问题


I try to compile my project with the command cargo build.

build.rs

extern crate csv;

use std::path::Path;
use std::fs::OpenOptions;
use std::io::BufWriter;
use std::io::Write;

#[allow(non_snake_case)]
fn processCSV(filename: &str, sourcePath: &str, enumName: &str) {
    println!("Generate rust source code from schema {}",filename);

    let mut ret: Vec<String> = Vec::new();
    let mut rdr = csv::Reader::from_file(filename).unwrap().flexible(true);
    for record in rdr.records().map(|r| r.unwrap()) {
    }
    let path = Path::new(sourcePath);
    let file = match OpenOptions::new().write(true).create(true).open(&path) {
        Ok(file) => file,
        Err(..) => panic!("Cannot create file {}",path.display()),
    };
    let mut writer = BufWriter::new(file);

    writer.write_all(b"test\n");
}

fn main() {
    processCSV("../schemas/Test.csv", "./src/mod/common/StatusCode.rs", "StatusCode");
}

and Cargo.toml

[package]
name = "rust-test"
version = "0.0.1"
build = "build.rs"

[lib]
path = "src/lib.rs"

[dependencies]

[build-dependencies]
csv = "*"

I can see this error :

src/lib.rs:1:1: 1:18 error: can't find crate for csv

src/lib.rs:1 extern crate csv;

but when I change flexible(true) to flexible(false) it compiles just fine without any errors. What do I need to do to fix this?

I am using Rust 1.2.0 on Windows 7 64-bit.


回答1:


Changing flexible(false) for flexible(true) makes no difference for me; both fail. The problem is that you've chosen build-dependencies for some reason, instead of just dependencies.

Using the src/lib.rs file that you provided in your answer, and this Cargo.toml file:

[package]
name = "stack-overflow"
version = "0.1.0"
authors = ["A. Developer <a.developer@example.com>"]

[dependencies]
csv = "*"

It compiles fine.

If you need to access a dependency both in your build.rs and in your project, you need to include the dependency in both sections.




回答2:


A build dependency is a dependency for a build script, which is a helper binary compiled and run before your main crate is built (designed to be used for code-generation, and building/finding native C libraries, etc.).

Normal dependencies used by the main code should just fall into the "dependencies" section, e.g.

[dependencies]
csv = "0.14"

There's also a "dev-dependencies" section, which are dependencies that are only needed for testing, i.e. they are compiled and used only for cargo test. This allows crates to depend on, for example, quickcheck for running tests without contaminating the main artifact.

In summary, running cargo build will do something like:

  1. build any build-dependencies
  2. build the build script (pointing the compiler to the built build-dependencies), and run it
  3. build any dependencies
  4. build the main crate (pointing the compiler to the built dependencies)

Running cargo test adds:

  1. build any dev-dependencies
  2. build the main crate with --test to create a test runner for any in-source #[test]s (pointing the compiler to both the dependencies and dev-dependencies)
  3. build any external examples or tests, also pointing to both the dependencies and dev-dependencies


来源:https://stackoverflow.com/questions/32522870/unable-to-find-crate-that-is-listed-in-build-dependencies-section

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!