Skip to content

Add artifact kind to binary_id if it's not 'lib'#76

Merged
sunshowers merged 2 commits into
nextest-rs:mainfrom
pmsanford:make-binary-id-unique
Feb 24, 2022
Merged

Add artifact kind to binary_id if it's not 'lib'#76
sunshowers merged 2 commits into
nextest-rs:mainfrom
pmsanford:make-binary-id-unique

Conversation

@pmsanford

Copy link
Copy Markdown
Contributor

In a crate with multiple targets that have the same name as the package, if both have tests, tests from one of the targets will be missing from the JSON output. I ran into this is in a lib + bin package. You can see a minimal example here.

This is because of the way RustTestArtifact.binary_id is constructed. Currently, if the target name is equal to the package name, the package name is taken as the binary_id. There are cases (such as lib + bin) where two targets will have the same name as the package name. This results in binary_id being non-unique in those cases, and the doc comment for binary_id says it should be unique. Currently this only seems to affect the JSON output of the list command - the human readable output of list is still correct and all the tests are still run.

The approach this PR takes is, in cases where the target name is equal to the package name, check the target type. If it's anything other than lib, append the type to the name to build the binary ID. I also considered fixing this at the point where the summaries are built for serialization, but it seems like binary_id is truly intended to uniquely identify a test suite, so I thought this was a more appropriate place.

It's possible for a package to have more than one target
with the same name as the package, such as with lib+bin
crates. This change adds the target kind to the binary_id
in cases where the kind is not lib to ensure binary_id is
unique.
Comment thread nextest-runner/src/test_list.rs Outdated
Comment on lines +88 to +89
} else if !artifact.target.kind.contains(&"lib".to_owned()) {
if let Some(kind) = artifact.target.kind.get(0) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on https://doc.rust-lang.org/cargo/commands/cargo-metadata.html it seems like kind will only ever have one item in it unless it's a lib. I don't have any experience working with cargo metadata, though, so I'm not sure this is the right approach.

This also means if there's nothing in the kind vec, it will silently use the package name for the binary_id - should this be an error instead?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing in the kind vec is definitely weird and should at least be a warning I think.

@sunshowers sunshowers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this and for the PR with the excellent description!

Comment thread nextest-runner/src/test_list.rs Outdated
Comment on lines +88 to +89
} else if !artifact.target.kind.contains(&"lib".to_owned()) {
if let Some(kind) = artifact.target.kind.get(0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing in the kind vec is definitely weird and should at least be a warning I think.

Comment thread nextest-runner/src/test_list.rs Outdated
Comment on lines 85 to 87
if artifact.target.name != package.name() {
binary_id.push_str("::");
binary_id.push_str(&artifact.target.name);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what happens if there's multiple bin targets within a crate, each with their own tests?

I think the best scheme for this is probably something like:
<crate-name> for the lib crate (unit tests)
<crate-name>::<bin-name> for test targets
<crate-name>::<bin/example>/<bin-name> for other targets, e.g in the included test, nextest-tests::bin/nextest-tests

What do you think? Happy to hear suggestions as well.

(And could you add more tests/examples around this? Thanks!)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, you're right - the problem is more general than I thought. I think your naming scheme makes sense - I'll give it a go.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think this works, with one small exception: if the name of the lib is different from the package, it's not reflected in the binary_id now. It's still unique and un-ambiguous, but we could also use
<crate-name>::lib/<lib-name> in that case if you think it'd be confusing not seeing it.

@sunshowers sunshowers Feb 24, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the name of the lib is different from the package, it's not reflected in the binary_id now.

Good catch, I keep forgetting about this situation. I wouldn't address this personally, I think the unambiguous crate name is good enough.

@sunshowers

sunshowers commented Feb 24, 2022

Copy link
Copy Markdown
Member

Thank you! Will let a test run finish and then land this.

@sunshowers

Copy link
Copy Markdown
Member

Could you update the PR for the test failures? We do support Rust 1.54 which doesn't support f-strings.

Binaries within a package are allowed
to have the same name so long as they
are of different types. This changes
the way binary_ids are generated to
take this into account and ensure
binary_ids are unique.
@pmsanford pmsanford force-pushed the make-binary-id-unique branch from fcf20d4 to 6a4e550 Compare February 24, 2022 02:23
@pmsanford

Copy link
Copy Markdown
Contributor Author

Woops! Caught that in one file but missed it in the error impl

@sunshowers sunshowers merged commit d0558b3 into nextest-rs:main Feb 24, 2022
@sunshowers

Copy link
Copy Markdown
Member

Thanks again!

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

Successfully merging this pull request may close these issues.

2 participants