Rust: Cargo fails to build

Created on 25 Apr 2015  ·  22Comments  ·  Source: rust-lang/rust

System:

Macbook pro running OSX 10.9.5

Rust version:

1.0.0-beta.2 (Mac installer)

Meta:

$ rustc --version --verbose
rustc 1.0.0-beta.2 (e9080ec39 2015-04-16) (built 2015-04-16)
binary: rustc
commit-hash: e9080ec39d9a44eb0773d648b348575b19f655e5
commit-date: 2015-04-16
build-date: 2015-04-16
host: x86_64-apple-darwin
release: 1.0.0-beta.2

Steps to reproduce:

  1. Generate simple .rs file as per (http://doc.rust-lang.org/1.0.0-beta.2/book/hello-world.html)
  2. Generate Cargo.toml file as per (http://doc.rust-lang.org/1.0.0-beta.2/book/hello-cargo.html)
  3. Run cargo build

    Expected result:

Cargo builds target/debug/hello_world

Actual result:

Cargo fails with the following error:

$ cargo build
failed to parse manifest at `/Users/karl/Projects/rust/hello/Cargo.toml`

Caused by:
  either a [lib] or [[bin]] section must be present

Note also:

If I put "1.0" in the version field of Cargo.toml, it fails with the following error:

$ cargo build
failed to parse manifest at `/Users/karl/Projects/rust/hello/Cargo.toml`

Caused by:
  cannot parse '1.0' as a semver for the key `package.version`

Most helpful comment

@ishankhare07 yeah you can have something like:

[[bin]]
name = "foo"
path = "src/some-random-path.rs"

All 22 comments

Could you gist the contents of the Cargo.toml you wrote? It looks like the version key needs to be updated with an extra numeric component (e.g. the book's version is 0.0.1)

It's very simple so I'll post it here. The first error was caused by this Cargo.toml (copied from the book):

[package]

name = "hello_world"
version = "0.0.1"
authors = [ "somebody" ]

The second error (the parse error) was caused by this Cargo.toml:

[package]

name = "hello_world"
version = "1.0"
authors = [ "somebody" ]

Is that second Cargo.toml in the book? Otherwise this is a legitimate error that Cargo is saying.

I just copied from (http://doc.rust-lang.org/1.0.0-beta.2/book/hello-cargo.html), where it says:

Put this inside:

[package]

name = "hello_world"
version = "0.0.1"
authors = [ "Your name <[email protected]>" ]

Oh sorry the second one. No, for the second one I had just assumed that you could put any dotted number like 1.0 in the version field. It seems to only work if the first digit is 0.

What about "1.0.0"?

Ah so the version field is required to be a valid semver which requires three components (period separated). Other than that though it looks like everything is working as intended here, so closing, but thanks for the report!

OK cool, but what do I need to put in the original Cargo.toml file to make it work? The book doesn't say.

@steveklabnik's suggestion of using "1.0.0" as the version should do the trick!

Oh yes, that solves the version issue, but there's still the main issue that it fails with this:

Caused by:
    either a [lib] or [[bin]] section must be present

So I'm guessing that rust has changed since this section of the book was written, but as a result, I don't know what to put in a lib or bin section.

Ah that just means you need to have either src/lib.rs or src/main.rs to build.

Oh I think I found the problem:

I hadn't created the src directory, and instead had everything at the top level:

$ tree
.
├── Cargo.toml
└── main.rs

0 directories, 2 files

If you do this, it gives a misleading error:

$ cargo build
failed to parse manifest at `/Users/karl/Projects/rust/hello/Cargo.toml`

Caused by:
  either a [lib] or [[bin]] section must be present

But move main.rs into a src subdir, and everything works:

$ tree
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
$ cargo build
   Compiling hello v0.1.0 (file:///Users/karl/Projects/rust/hello)

Normally you have a src/lib.rs for your library.
If you don't, you need [[bin]] or [lib] otherwise cargo doesn't know what to build.

Please somebody fix this in the book and also show tree of directories how is should be.

Yes, it's important to be very explicit in an introductory book. I have 20 years professional experience covering more than 10 languages, and this stopped me dead in the water. Even worse: the error message cargo spits out doesn't even come close to explaining what's actually wrong. It feels like the bad old days of cryptic C compilers.

Ahh I forgot about one important moment
you should to set version to 0.1.0 in Cargo.toml
cuz if you will try to run this command cargo new hello_world --bin then you will notice in Cargo.toml the correct version and it will be 0.1.0 according with current version of _Rust_

Just create an empty src/lib.rs

Thanks for this issue and comments. I'm really looking forward to working through the book but this shut me down quickly. So I'm echoing @kstenerud on the need for explicit docs.

The book obviously has this part wrong, as it should explicitly say to put Cargo.html into the root directory. Please fix it. Thanks.

just for my knowledge
what if i don't want to name my file as main.rs
is there a flag/variable in Cargo.toml that i can set
in order to let cargo know that which file to build
and not just the default src/main.rs file

@ishankhare07 yeah you can have something like:

[[bin]]
name = "foo"
path = "src/some-random-path.rs"

@alexcrichton thanks, that worked

Was this page helpful?
0 / 5 - 0 ratings

Related issues

withoutboats picture withoutboats  ·  213Comments

alexcrichton picture alexcrichton  ·  240Comments

thestinger picture thestinger  ·  234Comments

nikomatsakis picture nikomatsakis  ·  236Comments

withoutboats picture withoutboats  ·  202Comments