Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 941
jherkel Avatar asked Sep 11 '15 11:09

jherkel


2 Answers

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 <[email protected]>"]

[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.

like image 180
Shepmaster Avatar answered Dec 04 '22 18:12

Shepmaster


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
like image 35
huon Avatar answered Dec 04 '22 17:12

huon