Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo test doesn't mention which file(s) in examples/ is affected when the main function is missing #4948

Closed
Cobrand opened this issue Jan 16, 2018 · 7 comments

Comments

@Cobrand
Copy link

Cobrand commented Jan 16, 2018

Basically, if you have a lib crate with multiple files in "examples", but one of them doesn't have a main, you get this error when running cargo test:

error[E0601]: main function not found

But you don't know which file is affected by this error.

It would be better to have an error message that says "main function not found in examples/something.rs".

Note that it may be related to rustc itself, but just in case I opened an issue here as it seemed more fit.

@alexcrichton
Copy link
Member

Thanks for the report! Can you gist the full error message as well as perhaps an example project to reproduce?

@Cobrand
Copy link
Author

Cobrand commented Jan 16, 2018

Extremely simple example: https://github.com/Cobrand/cargo_error_example

Just cargo new --lib and create an empty file in examples/.

Full error message for cargo test:

   Compiling cargo_error_example v0.1.0 (file:///home/cobrand/Code/cargo_error_example)
error[E0601]: main function not found

error: aborting due to previous error

error: Could not compile `cargo_error_example`.

To learn more, run the command again with --verbose.

@alexcrichton
Copy link
Member

Hm ok, isn't this covered by

error: Could not compile `cargo_error_example`.

?

@lukaslueg
Copy link
Contributor

lukaslueg commented Jan 16, 2018

The error refers to the name of the package, not the name of the example. Put a file examples/foo.rs and the error still says cargo_error_example.

There are three things going on here:

  • We could place an additional (?) warning here and refer to key.target.src_path().to_str_lossy() or something.
  • The errors in part come directly from rustc and cargo itself and the output is already quite clobbered. Notice that there are four error:-lines and one warning:-line referring to the same error.
  • The overall output is actually different depending on whether or not the queue is empty. Running cargo test the first time (or after cargo clean) gives
Compiling issue4948 v0.1.0 (file:///home/lukas/issue4948)
error[E0601]: main function not found

error: aborting due to previous error

error: Could not compile `issue4948`.
warning: build failed, waiting for other jobs to finish...
error: build failed

Running a second time gives

Compiling issue4948 v0.1.0 (file:///home/lukas/issue4948)
error[E0601]: main function not found

error: aborting due to previous error

error: Could not compile `issue4948`.

To learn more, run the command again with --verbose.

Notice that there is no warning, no error: build failed, yet a info referring to --verbose the second time around. Maybe this needs to get cleaned up?

@ehuss
Copy link
Contributor

ehuss commented Jan 16, 2018

One option is to just change the error message (here) to include the crate name. Messages without spans (thus filenames) are very rare, and this is the only one that I think might be confusing. Another example is the lint warning that a crate name should have snake case, but it clearly tells you the name of the crate. Other messages without spans include linker errors and incorrect command-line arguments (presumably the user can figure these out?).

kennytm added a commit to kennytm/rust that referenced this issue Mar 15, 2018
…tebank

Add crate name to "main function not found" error message.

Fixes rust-lang#44798 and rust-lang/cargo#4948.

I was wondering if it might be cleaner to update the ui tests to add a simple `fn main() {}` for the unrelated tests.  Let me know if you would prefer that.
@ehuss
Copy link
Contributor

ehuss commented Apr 20, 2018

I think this can be closed now that rust-lang/rust#48706 has been merged.

@alexcrichton
Copy link
Member

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants