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

Better error message needed when user creates a bench rather than a benches dir #9014

Closed
gilescope opened this issue Dec 24, 2020 · 9 comments · Fixed by #10090
Closed

Better error message needed when user creates a bench rather than a benches dir #9014

gilescope opened this issue Dec 24, 2020 · 9 comments · Fixed by #10090
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@gilescope
Copy link
Contributor

A common error with benchmarking is to call the dir bench rather than benches.

This is the error message a user currently gets:

error: failed to parse manifest at `<snip>/Cargo.toml`

Caused by:
  can't find `my_benchmark` bench, specify bench.path

It fails the esteban test as it doesn't hint as to what we've done wrong.

At a minimum can we change it to this please?

Caused by:
  can't find `benches/my_benchmark` bench, specify bench.path

That way they stand a chance of spotting there's an issue. Personally I think it's worth testing for a bench dir and having an explicit message because so many people must do this and then kick themselves when the penny drops.

@gilescope gilescope added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Dec 24, 2020
@ehuss ehuss added A-diagnostics Area: Error and warning messages generated by Cargo itself. E-help-wanted labels Jan 7, 2021
@CPerezz
Copy link
Contributor

CPerezz commented Jan 30, 2021

That's weird. I just implemented this test and it passes:

#[cargo_test]
fn external_bench_warns_wrong_path() {
    if !is_nightly() {
        return;
    }

    let p = project()
        .file(
            "src/lib.rs",
            r#"
                #![feature(test)]
                #[cfg(test)]
                extern crate test;

                pub fn get_hello() -> &'static str { "Hello" }
            "#,
        )
        .file(
            "./bench/external.rs",
            r#"
                #![feature(test)]
                #[allow(unused_extern_crates)]
                extern crate foo;
                extern crate test;

                #[bench]
                fn external_bench(_b: &mut test::Bencher) {()}
            "#,
        )
        .build();

    p.cargo("bench")
        .with_stderr_contains("can't find `[..]` bench, specify `[..]`")
        .run();
}

@gilescope was the [[bench]] section configured on any way in Cargo.toml? ie. harness=false or anything similar?
Maybe I implemented the test wrong. But seems that bench gets digested correctly by cargo if you use the default bencher.

@gilescope
Copy link
Contributor Author

gilescope commented Jan 30, 2021

In the above case it's not erroring but not running any benchmarks either I think?

Looking back I was using criterion (sorry) and so had this in the Cargo.toml:

[[bench]]
name = "my_benchmark"
harness = false

That at least errored (but didn't point out what the error was). I think Criterion could do with an upgradeed error message (assuming the error message above is ultimately created by criterion) and rust's bench could do with emitting a warning that while no benchmarks were found there's suspiciously a bench dir in existence and did you really mean benches?

@CPerezz
Copy link
Contributor

CPerezz commented Jan 30, 2021

In the above case it's not erroring but not running any benchmarks either I think?

It is, the bench runs and works. that's why the test fails if I include .with_stderr_contains("can't find [..]bench, specify[..]")

So basically I asume (maybe @ehuss or @Eh2406 can bring some light here) that adding this [[bench]] section setting harness = false as said in the docs this makes cargo pass the --test flag to rustc which causes the import of the libtest crate. Later it says about the harness:

If set to false, then you are responsible for defining a main() function to run tests and benchmarks.

And I guess that this is the problem. When the Criterion main.rs is executed here the error that they return is not explicit as it should I agree.

Again I might be wrong, but looks to me that this can be closed and the issue should be opened in Criterion instead.

@ehuss
Copy link
Contributor

ehuss commented Jan 30, 2021

@CPerezz I think you may be misunderstanding the issue. The issue is that specifying a bench target like this in Cargo.toml:

[[bench]]
name = "mybench"

And you place the file in bench/mybench.rs, that it returns a confusing error message. The expected location is benches/mybench.rs.

I think there are two parts to improve the message here:

  1. If path is not specified, the error message could include the implicit path in the message, maybe something like this:
Caused by:
  can't find `mybench` bench at `benches/mybench.rs`, specify bench.path if you want to use a non-default path
  1. If path is not specified, but there is a file found at a "commonly wrong location" like bench/<name>.rs, there could be a note attached suggesting renaming the directory as another option to fix it.

It would be nice if these better error messages worked for all of the target types (testtests and exampleexamples and maybe src/binssrc/bin).

@CPerezz
Copy link
Contributor

CPerezz commented Jan 31, 2021

@ehuss I understood the problem. But I wrongly thought that was harness the problematic option, not the name itself. When (as it makes sense now) it was.

Will start working on it if it's ok with you! I'll probably push a PR for 1. that addresses your final suggestion also (not bin al least for now) and will open another issue/PR for 2.

CPerezz added a commit to CPerezz/cargo that referenced this issue Jan 31, 2021
As it was stated in rust-lang#9014 it's not really useful the message
that you get when using `bench/your_bench.rs` instead of the
correct path `benches/your_bench.rs`.

Therefore, as @ehuss suggested, the error message has been improved
not only suggesting the correct path but also suggesting to
specify `bench.path` in `Cargo.toml` for non-default path usage.
And this has been exented to `test`, `example` and `bin`.
@djmitche
Copy link
Contributor

@CPerezz are you still planning to work on this? If not, I could..

@tverghis
Copy link
Contributor

@djmitche @CPerezz I'd like to claim this if neither of you are actively working on this issue

@djmitche
Copy link
Contributor

I completely forgot about this after polling @CPerezz. I think you're clear to continue!

@CPerezz
Copy link
Contributor

CPerezz commented Sep 13, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
5 participants