Skip to content

Conversation

@DirectXMan12
Copy link
Contributor

@DirectXMan12 DirectXMan12 commented Oct 21, 2025

in v1.220.0, specifically 28ea1a0, package source pathing was switched over to a helper utility. the PackageSourceMap::paths method looks very similar to the logic before, but has a very subtle difference:

in order to avoid duplicate paths, it collects paths from each source map's source files into a HashSet and then returns an iterator over that (in contrast to before, which just presented a flat-mapped iterator over all source files directly).

unfortunately, this means that iteration order is now random, which can produce non-deterministic compilation when wit-parser is used as part of the compilation macro, say for proc-macros like wasmtime-component macro.

it's... difficult to notice this in ordinary cargo compilation, but it causes extremely strange behavior in deterministic/caching build tooling like buck2: given a series of deps outer -> inner -> [something that uses wasmtime-component-macro, like wasmtime-wasi], compiling outer will cause rustc to reject wasmtime-wasi on a hash mismatch, which will then surface as "unable to find crate inner" in your rustc output 1

fixing this is relatively easy: since our input iteration order is deterministic, simply collecting into an IndexSet (already used elsewhere in the file for this purpose) instead of a HashSet gives us deterministic iteration order, and thus deterministic compilation.

Footnotes

  1. i know this happens when compiling outer as a static-pic for library; i have not yet tested if it occurs in other compilation modes like binaries or shared libs.

in v1.220.0, specifically 28ea1a0, package source pathing was switched
over to a helper utility.  the `PackageSourceMap::paths` method looks
very similar to the logic before, but has a very subtle difference:

in order to avoid duplicate paths, it collects paths from each source
map's source files into a `HashSet` and then returns an iterator over
than (in contrast to before, which just presented a flat-mapped iterator
over all source files directly).

unfortunately, this means that iteration order is now random, which can
produce non-deterministic compilation when wit-parser is used as part of
the compilation macro, say for proc-macros like wasmtime-component macro.

it's... difficult to notice this in ordinary cargo compilation, but it
causes _extremely_ strange behavior in deterministic/caching build
tooling like buck2: given a series of deps `outer -> inner -> [something
that uses wasmtime-component-macro, like wasmtime-wasi]`, compiling
`outer` will cause rustc to reject `wasmtime-wasi` on a hash mismatch,
which will then surface as "unable to find crate "inner"` [^1]

fixing this is relatively easy: since our input iteration order is
deterministic, simply collecting into an `IndexMap` (already used
elsewhere in the file for this purpose) instead of a `HashMap` gives us
deterministic iteration order, and thus deterministic compilation.

[^1]: i know this happens when compiling `outer` as a static-pic for
library; i have not yet tested if it occurs in other compilation modes
like binaries or shared libs.
@DirectXMan12 DirectXMan12 requested a review from a team as a code owner October 21, 2025 02:30
@DirectXMan12 DirectXMan12 requested review from fitzgen and removed request for a team October 21, 2025 02:30
@DirectXMan12
Copy link
Contributor Author

this maybe also merits a rustc error fix as well -- needa check if the error is better on nightly, but it's extremely unclear on a number of stable rusts

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Oh definitely a mistake, thanks for the PR!

@alexcrichton alexcrichton added this pull request to the merge queue Oct 21, 2025
Merged via the queue into bytecodealliance:main with commit 6f96797 Oct 21, 2025
64 of 66 checks passed
@DirectXMan12 DirectXMan12 deleted the bug/nondeterministic-macros branch October 22, 2025 16:38
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