Skip to content

[ty] Use diagnostic message as tie breaker when sorting#25424

Merged
MichaReiser merged 4 commits into
astral-sh:mainfrom
jelle-openai:diagnostic-order
May 28, 2026
Merged

[ty] Use diagnostic message as tie breaker when sorting#25424
MichaReiser merged 4 commits into
astral-sh:mainfrom
jelle-openai:diagnostic-order

Conversation

@jelle-openai

Copy link
Copy Markdown
Contributor

Nondeterminism

When ty checks files in parallel, non-panic diagnostics with the same rendered source location, severity, and diagnostic ID can be printed in worker-completion order instead of a canonical order. This manifests as nondeterminism on the homeassistant/core project.

Root Cause

CollectReporter receives checked-file diagnostics as Rayon workers finish, then sorts them with Diagnostic::rendering_sort_key. The existing key compared primary file/range, severity, and diagnostic ID, but not the visible diagnostic message. In core, the two diagnostics had the same primary span and tied on error DiagnosticId::InvalidArgumentType, so the sort key considered visibly different diagnostics equal and Rust's stable sort preserved the race-dependent arrival order.

Fix

Use the concise rendered message as the final tie-breaker.

@astral-sh-bot

astral-sh-bot Bot commented May 27, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented May 27, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@AlexWaygood AlexWaygood added ty Multi-file analysis & type inference bug Something isn't working labels May 27, 2026
@astral-sh-bot

astral-sh-bot Bot commented May 27, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@carljm carljm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you!

@AlexWaygood

Copy link
Copy Markdown
Member

This manifests as nondeterminism on the homeassistant/core project.

Huh, how did you come across this nondeterminism? I don't remember seeing it on any of our ecosystem diffs on PRs, and homeassistant/core isn't listed as a flaky project in https://github.com/astral-sh/ruff/blob/main/crates/ty_python_semantic/resources/primer/flaky.txt.

@jelle-openai

Copy link
Copy Markdown
Contributor Author

I had Codex look for nondeterminism. Sounds like it was checking projects that had previously seen nondeterminism and ran ty on homeassistant a bunch of times, found this nondeterminism, and root caused it.

@AlexWaygood

Copy link
Copy Markdown
Member

I'd be curious to know why ecosystem-analyzer didn't detect the nondeterminism. It might be because ecosystem-analyzer sorts diagnostics internally before it does flakiness detection? Could be interesting to look into it as a followup

@jelle-openai

This comment was marked as outdated.

@astral-sh-bot

astral-sh-bot Bot commented May 27, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 91.94%. The percentage of expected errors that received a diagnostic held steady at 87.09%. The number of fully passing files held steady at 92/134.

@AlexWaygood

This comment was marked as outdated.

@AlexWaygood

This comment was marked as outdated.

@MichaReiser MichaReiser changed the title [ty] Stabilize equal-location diagnostic order [ty] Use diagnostic message as tie breaker when sorting May 28, 2026
@MichaReiser MichaReiser merged commit 1bd77e1 into astral-sh:main May 28, 2026
116 checks passed
@jelle-openai

Copy link
Copy Markdown
Contributor Author

I'd be curious to know why ecosystem-analyzer didn't detect the nondeterminism. It might be because ecosystem-analyzer sorts diagnostics internally before it does flakiness detection? Could be interesting to look into it as a followup

Sorry for the Codex noise on this, I asked it to investigate and it went a bit further than I wanted.

I think the answer is that ecosystem-analyzer sorts diagnostics internally, and therefore it hid the nondeterminism that this PR fixes. That's probably for the better; we should fix nondeterminism in diagnostic order but it's not a huge deal and we don't ecosystem-analyzer to do a side job in finding nondeterminism in ordering.

anishgirianish pushed a commit to anishgirianish/ruff that referenced this pull request May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants