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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mesonbuild/interpreter/dependencyfallbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ def lookup(self, kwargs: TYPE_nkwargs, force_fallback: bool = False) -> Dependen
self._subproject_impl(subp_name, varname)
break

# 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.

crate_name = name.rsplit('-', 2)[0]
cargo_wraps = self.wrap_resolver.get_cargo_wraps(crate_name)
if not cargo_wraps:
continue
imports = ', '.join([f'"{n}"' for n in cargo_wraps])
raise DependencyException(f'Rust dependency "{name}" not found, '
f'but following versions were found using Cargo.lock import: {imports}')

candidates = self._get_candidates()

# writing just "dependency('')" is an error, because it can only fail
Expand Down
12 changes: 12 additions & 0 deletions mesonbuild/wrap/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,18 @@ def find_program_provider(self, names: T.List[str]) -> T.Optional[str]:
return wrap_name
return None

def get_cargo_wraps(self, cratename: str) -> T.List[str]:
found = []
for wrap in self.wraps.values():
if wrap.get('method') != 'cargo':
continue
pbo-linaro marked this conversation as resolved.
Show resolved Hide resolved
if not wrap.name.endswith('-rs'):
continue
wrap_cratename = wrap.name.rsplit('-', 2)[0]
if wrap_cratename == cratename:
found.append(wrap.name)
return found

def resolve(self, packagename: str, force_method: T.Optional[Method] = None) -> T.Tuple[str, Method]:
wrap = self.wraps.get(packagename)
if wrap is None:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project('cargo lock')

dependency('bar-crate-1.0-rs')
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"stdout": [
{
"line": "test cases/failing/132 rust cargo lock suggest dependency/meson.build:3:0: ERROR: Rust dependency \"bar-crate-1.0-rs\" not found, but following versions were found using Cargo.lock import: \"bar-crate-1-rs\", \"bar-crate-2-rs\", \"bar-crate-0-rs\", \"bar-crate-0.8-rs\""
}
]
}
Loading