5

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.

Community
  • 1
  • 1
jherkel
  • 71
  • 1
  • 6

2 Answers2

7

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • I didn't describe my problem correctly, I forgot to mention that I had problem with the file build.rs. So I assume "build-dependecies" should be used for this case. I also tried to copy "csv" to "dependecies" and yes it works. But I think it is not correct and in cargo (or rust compiler) there is some error how dependencies are resolved. – jherkel Sep 12 '15 at 15:47
  • 1
    @now2 *problem with the file build.rs* — but your error message says `src/lib.rs [...] can't find crate for csv`. That's why I assumed that the code you were showing was the contents of `src/lib.rs`. Your question still doesn't actually show `src/lib.rs`, but see the last sentence of my edited answer for a guess. – Shepmaster Sep 12 '15 at 17:02
4

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
huon
  • 94,605
  • 21
  • 231
  • 225