Skip to content

fix: respect AMENT_PREFIX_PATH precedence when resolving Rust packages#41

Open
azerupi wants to merge 1 commit into
colcon:mainfrom
azerupi:fix/overlay-prefix-precedence
Open

fix: respect AMENT_PREFIX_PATH precedence when resolving Rust packages#41
azerupi wants to merge 1 commit into
colcon:mainfrom
azerupi:fix/overlay-prefix-precedence

Conversation

@azerupi

@azerupi azerupi commented May 17, 2026

Copy link
Copy Markdown

Summary

find_installed_cargo_packages iterated AMENT_PREFIX_PATH and overwrote prefix_for_package[pkg] = prefix on every hit, so the last prefix containing a package won. Since AMENT_PREFIX_PATH is ordered from highest to lowest priority (like PATH) and an overlay sourced on top of an underlay puts the overlay prefix first, last-wins meant the underlay silently overrode a locally rebuilt overlay of the same package in the generated .cargo/config.toml.

Switched to dict.setdefault so the first prefix that contains a given Rust package wins, matching ament_cmake / CMAKE_PREFIX_PATH semantics.

Why this matters

Without the fix, anyone who locally rebuilds a Rust message crate (e.g. std_msgs) while overlaying a packaged ROS distro gets downstream Rust binaries linked against the distro .rlib, not the local one. The build succeeds, code runs, only behavior is silently wrong.

This was found while debugging a perf change in ros2-rust/ros2_rust#628 where a locally-rebuilt std_msgs was silently bypassed in favor of the distro-installed one, leading to benchmarks measuring the wrong code.

find_installed_cargo_packages iterated AMENT_PREFIX_PATH and overwrote
prefix_for_package[pkg] = prefix on every hit, so the LAST prefix
containing a package won. AMENT_PREFIX_PATH is conventionally ordered
from highest to lowest priority (like PATH), and an overlay sourced
on top of an underlay puts the overlay prefix first. Last-wins meant
the underlay (e.g. /opt/ros/<distro>) silently overrode a locally
rebuilt overlay of the same package in the .cargo/config.toml that
downstream Rust packages then link against.

Use dict.setdefault so the first prefix that contains a given Rust
package wins, matching ament_cmake / CMAKE_PREFIX_PATH semantics.

Adds two unit tests:
- test_find_installed_cargo_packages_overlay_first_wins: an overlay
  listed before an underlay must take precedence for shared package
  names; non-overlapping packages still resolve from whichever prefix
  contains them.
- test_find_installed_cargo_packages_empty_ament_prefix_path: an
  unset AMENT_PREFIX_PATH returns an empty mapping without raising.

This was found while debugging a perf change in ros2-rust/ros2_rust#628
where a locally-rebuilt std_msgs was silently bypassed in favor of the
distro-installed one, leading to end-to-end benchmarks measuring the
wrong code.
@esteve

esteve commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

@cottsay changes look good to me, but it'd be great if you can have a look too. Thanks!

@codecov-commenter

codecov-commenter commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@dc26ba5). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #41   +/-   ##
=======================================
  Coverage        ?   88.29%           
=======================================
  Files           ?        5           
  Lines           ?      205           
  Branches        ?       17           
=======================================
  Hits            ?      181           
  Misses          ?       20           
  Partials        ?        4           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

3 participants