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

rust: suggest correct dependency when found in Cargo.lock #13448

Conversation

pbo-linaro
Copy link

As we refer to crates using API version instead of plain version, it can be confusing to user which is left with a simple "Dependency X not found".

Thus, when we detect that a rust dependency is missing, and is not available using a wrap file, if we find something compatible using Cargo.lock import, we just list the correct dependencies names.

@pbo-linaro
Copy link
Author

@xclaesse Followup for conversation on #12945

cargo_wraps = self.wrap_resolver.get_cargo_wraps(crate)
if not cargo_wraps:
continue
raise DependencyException(f'Rust dependency "{name}" not found, ' +
Copy link
Member

Choose a reason for hiding this comment

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

DependencyException means this would be fatal, regardless of the value of required...

Copy link
Author

Choose a reason for hiding this comment

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

Indeed. If we don't find it at this step, there is no chance we find it later, and the error message is thus confusing "Dependency X not found (tried pkgconfig and cmake)".
So I think it's better to make it an hard error instead of a simple warning.

Copy link
Author

Choose a reason for hiding this comment

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

Someone could make this dep not required, but if it's present in the Cargo.lock file, what would be the use case?
I can check the required flag, if it makes more sense.

Copy link
Member

Choose a reason for hiding this comment

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

The usual reason for making a dependency not required is to make it a build option so users can decide which components of the project they would like to build.

Copy link
Author

Choose a reason for hiding this comment

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

Ok. Will check the required flag 👍

Copy link
Member

Choose a reason for hiding this comment

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

I would probably think to put this below in:

            elif required and (dep or i == last):
                # This was the last candidate or the dependency has been cached
                # as not-found, or cached dependency version does not match,
                # otherwise func() would have returned None instead.
                raise DependencyException(f'Dependency {self._display_name!r} is required but not found.')

as a special-casing of the message you seek to improve. Save cargo_wraps where you currently calculate them, but emit the error message in the existing error condition.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, but I'm not getting what this changes concretely.
This PR just tries to add an error message when a user specifiies a Rust dep, and did a mistake in version number used. We specifically want a different error message compared to the normal one.

Copy link
Member

Choose a reason for hiding this comment

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

It should mean that if you have a C dependency with the same name it gets used as @tristan957 was worried about.

Instead of raising an error during candidate collection if "*-rs" isn't a valid crate dependency we raise an error after all dependency lookups (including both pkg-config and wraps) fail.

Copy link
Member

Choose a reason for hiding this comment

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

(I currently have a fever. I eyeballed the code and my analysis feels right, but I cannot discount the possibility that what I'm saying is a fever dream in the most literal sense.)

Copy link
Author

Choose a reason for hiding this comment

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

The problem is that cargo wrap detection is specifically done in a different place than pkg-config/cmake path.

Initially, I tried to implement something more unified, but I realized that cargo integration was modeled quite differently. So I'm not sure how to make this better.

mesonbuild/interpreter/dependencyfallbacks.py Outdated Show resolved Hide resolved
{
"stdout": [
{
"line": "test cases/rust/26 cargo lock suggest dependency/meson.build:3:0: ERROR: Rust dependency \"bar-1.0-rs\" not found, but following versions were found using Cargo.lock import: \"bar-1-rs\", \"bar-2-rs\", \"bar-0-rs\", \"bar-0.8-rs\""
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure whether this really needs a test. All we are testing is the warning message, no? Does that really need an integration test?

Copy link
Author

Choose a reason for hiding this comment

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

If someone changes the cargo.lock import feature later, it would be useful to notice this. But if you don't see any value in this test, I can remove it too.

@pbo-linaro pbo-linaro force-pushed the rust_suggest_dep_from_cargolock branch from 5ce9af5 to f288c1e Compare July 19, 2024 01:39
@pbo-linaro
Copy link
Author

fixed extra parentheses on the if.

@pbo-linaro pbo-linaro force-pushed the rust_suggest_dep_from_cargolock branch from f288c1e to 69f30dc Compare July 19, 2024 02:13
@pbo-linaro
Copy link
Author

Report error only if dependency is required.

s = name.split('-')
if len(s) < 3:
continue
rs_ending = s.pop()

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable rs_ending is not used.
if len(s) < 3:
continue
rs_ending = s.pop()
version = s.pop()

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable version is not used.
s = wrap.name.split('-')
if len(s) < 3:
continue
rs_ending = s.pop()

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable rs_ending is not used.
if len(s) < 3:
continue
rs_ending = s.pop()
version = s.pop()

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable version is not used.
@pbo-linaro pbo-linaro force-pushed the rust_suggest_dep_from_cargolock branch from 69f30dc to f0fed38 Compare July 19, 2024 20:06
@pbo-linaro
Copy link
Author

fixed lint issues

@pbo-linaro
Copy link
Author

@eli-schwartz How can we indicate to meson test infra that the test is supposed to fail?

@pbo-linaro pbo-linaro force-pushed the rust_suggest_dep_from_cargolock branch 2 times, most recently from 83d2050 to 38d4b72 Compare July 21, 2024 18:26
@pbo-linaro
Copy link
Author

More idiomatic python.

mesonbuild/wrap/wrap.py Outdated Show resolved Hide resolved
@pbo-linaro pbo-linaro force-pushed the rust_suggest_dep_from_cargolock branch from 38d4b72 to 5bc92f9 Compare July 21, 2024 22:52
@pbo-linaro
Copy link
Author

Use rsplit to extract crate name + update test to have crate with - in the name.

@pbo-linaro
Copy link
Author

Thanks for your recommendations @dnicolodi @eli-schwartz. I'm still looking for a way to indicate the test is supposed to fail. If you have suggestion, I would be happy to implement it to fix CI.

As we refer to crates using API version instead of plain version, it can
be confusing to user which is left with a simple "Dependency X not
found".

Thus, when we detect that a rust dependency is missing, and is not
available using a wrap file, if we find something compatible using
Cargo.lock import, we just list the correct dependencies names.
@pbo-linaro pbo-linaro force-pushed the rust_suggest_dep_from_cargolock branch from 5bc92f9 to 2272524 Compare July 22, 2024 16:55
@pbo-linaro
Copy link
Author

Found how to implement a "failing" test, and rebased on top of master.

@pbo-linaro
Copy link
Author

Test failure does not seem related to this patch (lto issue on cygwin).

# Check if we could have a matching Rust crate imported from
# Cargo.lock, with expected api version. We know there is no
# wrap-file defined to match this crate.
if name.endswith('-rs') and required:
Copy link
Contributor

@tristan957 tristan957 Jul 29, 2024

Choose a reason for hiding this comment

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

This is not a good heuristic. What is stopping me from name a C subproject with a -rs suffix? cc @xclaesse

Copy link
Author

Choose a reason for hiding this comment

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

In case we don't identify a matching cargo wrap, we just continue to next dependency, so the heuristic can't create a false positive.

The only possibility is if someone has a cargo.lock with this exact dependency, and needs a C library named exactly with -rs suffix, which is, IMHO, unlikely to be a real use case.

Copy link
Member

Choose a reason for hiding this comment

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

This ties into what I said before, I think.

Copy link
Member

Choose a reason for hiding this comment

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

@pbo-linaro
Copy link
Author

Polite ping after two weeks. Review comments have been addressed so far. I still think this error message can be useful for people looking for integrating rust code with meson.

@pbo-linaro
Copy link
Author

I'm closing this PR for now. If someone is interested to work on this, feel free to reuse the code presented here.

@pbo-linaro pbo-linaro closed this Aug 16, 2024
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.

4 participants