flamegraph icon indicating copy to clipboard operation
flamegraph copied to clipboard

--unit-test flag doesn't allow profiling unit tests in a lib-only crate in a workspace

Open rychipman opened this issue 2 years ago • 2 comments

To repro, create a new cargo workspace with two crates containing only lib targets (you can use them exactly as created by cargo new).

If you just pass the --unit-test flag, it fails because it doesn't know which target to build

$ cargo flamegraph --unit-test -- tests::it_works
Error: several possible targets found: [BinaryTarget { package: "subproject_one", target: "subproject_one", kind: ["lib"] }, BinaryTarget { package: "subproject_two", target: "subproject_two", kind: ["lib"] }], please pass an explicit target.

However, if I do provide a target argument, it errors out because it is expecting a bin target, and only finds a lib target with the specified name.

cargo flamegraph --unit-test subproject_one -- tests::it_works
error: no bin target named `subproject_one`
Error: cargo build failed

Is this expected behavior, or a bug? I would expect that with the --unit-test flag specified, it shouldn't care whether the target is bin or lib, since it's building a test binary to profile

rychipman avatar Jul 20 '22 14:07 rychipman

That sure looks like a bug... Would you be up for fixing it?

djc avatar Jul 25 '22 08:07 djc

I would love to, but unfortunately it isn't something I'm likely to have the time to dig into in the near future

rychipman avatar Aug 02 '22 20:08 rychipman

I think the root cause of this issue is that if an argument is passed to --unit-test, the kind value passed to build() is an empty vec: https://github.com/flamegraph-rs/flamegraph/blob/c532c046b171952f8812dd76b09298c0c558bf8d/src/bin/cargo-flamegraph.rs#L406

The build() function then doesn't set the required cargo --lib flag: https://github.com/flamegraph-rs/flamegraph/blob/c532c046b171952f8812dd76b09298c0c558bf8d/src/bin/cargo-flamegraph.rs#L123-L128

It looks like the tool needs a way to determine, based on the named target, whether or not to set the --lib flag. That probably requires looking up the named target in the Cargo metadata to determine its type.

ramosbugs avatar Nov 17 '22 00:11 ramosbugs