Fix non-deterministic proc-macro output by replacing HashSet with Ind…#488
Open
capickett wants to merge 1 commit intoAleph-Alpha:mainfrom
Open
Fix non-deterministic proc-macro output by replacing HashSet with Ind…#488capickett wants to merge 1 commit intoAleph-Alpha:mainfrom
capickett wants to merge 1 commit intoAleph-Alpha:mainfrom
Conversation
…exSet HashSet iteration order depends on RandomState, which is seeded randomly per process. This caused visit_dependencies statements and where-clause predicates to be emitted in different orders across rustc invocations, breaking build caching and reproducibility. Replace HashSet with IndexSet (from indexmap) in deps.rs and lib.rs to preserve insertion order.
Collaborator
|
Hey @capickett, thanks for the PR! Just to confirm, what exactly was non-deterministic? The order of the generics? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goal
Meta uses remote execution / distributed builds for rust code (using buck2). This makes our builds susceptible to cache invalidation & build consistency issues. We found that
ts-rsproc-macros were generated non-deterministic token streams due to the use of HashSet/HashMap unordered containers.HashSet iteration order depends on RandomState, which is seeded randomly per process. This caused visit_dependencies statements and where-clause predicates to be emitted in different orders across rustc invocations, breaking build caching and reproducibility.
I discovered this issue internally with buck, but a repro can be made by writing a test like so:
Run this test enough times and it will fail without this PR.
Changes
Replace HashSet with IndexSet (from indexmap) in deps.rs and lib.rs to preserve insertion order. Alternatively, we could define a specific ordering with
Ordand useBTreeSet/BTreeMap, etc. This is the minimal change to fix my issue, however I did notice there are a few more instances of HashMap/HashSet in the proc macro layer. Pending agreement on the approach, I could extend the PR to cover those case.Checklist
I didn't commit the test here, namely because the test is not reliable; it requires multiple runs to find the breakage, making pinning inconsistent. This could cause unwarranted noise on unrelated PRs in case a breakage does make its way back into the codebase. Open to feedback here.