Skip to content

Conversation

@Sheraff
Copy link
Contributor

@Sheraff Sheraff commented Oct 29, 2025

Attempt at fixing #5635

Summary by CodeRabbit

  • Chores

    • Made route traversal deterministic and now preserves insertion/traversal order.
    • Converted internal scoring values to integer-scaled equivalents for more stable route ranking.
    • Added tests ensuring ordering is consistent regardless of input order.
  • User impact

    • No user-visible changes; functionality and public APIs remain unchanged.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 29, 2025

Walkthrough

Scaled fractional scoring constants to integer equivalents and added deterministic traversal-order tracking; traversal now records visited route ids and builds flatRoutes from that ordered list. Added duplicate-id invariant check. New test verifies flatRoutes ordering is independent of input route order. No public API changes.

Changes

Cohort / File(s) Summary
Scoring constants & arithmetic
packages/router-core/src/process-route-tree.ts
Replaced fractional scoring constants with integer-scaled equivalents (×10,000) and adjusted dependent arithmetic to use the new scale.
Traversal order, invariant checks & flatRoutes determinism
packages/router-core/src/process-route-tree.ts
Added order: Array<string> to record route insertion order during traversal; push each visited route.id into order; switched child traversal to index-based loop; added invariant check for duplicate childRoute.id in routesById; build flatRoutes from the recorded ordered ids instead of Object.values(routesById).
Tests: ordering invariance
packages/router-core/tests/processRouteTree.test.ts
Added new test suite "#5635 - sort result is independent of initial route order" which permutes input route order and asserts flatRoutes ordering is invariant.

Sequence Diagram(s)

sequenceDiagram
    participant Builder as RouteTreeBuilder
    participant Traversal as traverseRoutes()
    participant RoutesMap as routesById
    participant Order as order:Array<string>
    participant Flat as flatRoutes

    Builder->>Traversal: start traversal(root)
    Traversal->>RoutesMap: register route (routesById[id] = route)
    Traversal->>Order: push(route.id)
    Traversal->>Traversal: for i = 0..n-1: child = children[i]
    Traversal->>RoutesMap: if routesById[child.id] exists → throw (duplicate id)
    Traversal-->>Builder: traversal complete
    Builder->>Flat: flatRoutes = Order.sort().map(id => routesById[id]!)
    note right of Flat #D6EAF8: Deterministic flatRoutes built from recorded order
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Inspect all arithmetic using the new integer scale to ensure no mixed-type bugs.
  • Verify order is populated in all traversal paths (root/no-path cases) and that sorting yields intended determinism.
  • Check the duplicate-id invariant's error behavior and the impact on callers relying on previous ordering.

Possibly related PRs

Suggested reviewers

  • schiller-manuel

Poem

🐇 I hopped through nodes both big and small,
I turned tiny floats into whole-numbered falls.
I noted each id in an orderly line,
So routes march steady, predictable and fine.
🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "fix(router-core): process route tree should avoid float arithmetics" directly describes the primary change in the changeset: replacing fractional scoring constants with integer-scaled equivalents (multiplied by 10,000) to eliminate floating-point arithmetic operations. This change is clearly related to the main objective of fixing non-deterministic route matching behavior described in issue #5635, where the intermittent failures are likely caused by precision inconsistencies in floating-point calculations across different browser engines.
Linked Issues Check ✅ Passed The PR successfully addresses the primary objective from issue #5635 by implementing the solution indicated in the PR branch name and title: avoiding floating-point arithmetic in route-tree processing. The replacement of fractional scoring constants with integer-scaled equivalents directly eliminates the precision inconsistencies that were likely causing non-deterministic behavior, which manifested as intermittently failing index.tsx route matches across different browser engines. Additionally, the insertion-order tracking and deterministic flatRoutes construction ensure consistent and reproducible route ordering, which reinforces the fix and prevents similar ordering-related issues.
Out of Scope Changes Check ✅ Passed All changes in the PR are directly related to addressing the floating-point arithmetic issue in route-tree processing: the scoring constant updates implement the core fix, the insertion-order tracking and deterministic flatRoutes construction ensure consistent behavior, the traversal control flow changes and invariant checks support the ordering mechanism, and the test suite validates that the fix resolves the non-deterministic ordering problem. No unrelated changes such as style updates, documentation modifications, or feature additions outside the scope of fixing route-tree processing are present.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-process-route-tree-float-arithmetics

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 57302c1 and bb7543e.

📒 Files selected for processing (1)
  • packages/router-core/tests/processRouteTree.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/router-core/tests/processRouteTree.test.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Preview
  • GitHub Check: Test

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link

nx-cloud bot commented Oct 29, 2025

View your CI Pipeline Execution ↗ for commit bb7543e

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 1m 4s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2025-10-29 14:28:22 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 29, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5688

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5688

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5688

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5688

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@5688

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5688

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5688

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5688

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5688

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5688

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5688

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5688

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5688

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5688

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5688

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5688

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5688

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5688

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5688

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5688

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5688

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5688

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5688

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@5688

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5688

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5688

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5688

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5688

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5688

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5688

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@5688

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5688

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5688

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5688

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5688

commit: bb7543e

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/router-core/src/process-route-tree.ts (1)

13-23: LGTM - Consistent integer scaling applied.

The constants have been correctly scaled by a factor of 10,000 to convert from floating-point to integer values. This preserves the relative ordering of routes while avoiding floating-point precision issues. Consider adding a brief comment explaining the scaling factor for future maintainability.

Example documentation comment:

+// Scoring constants scaled by 10,000 to use integer arithmetic and avoid floating-point precision issues
 const SLASH_SCORE = 7500
 const STATIC_SEGMENT_SCORE = 10000
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ec3881 and c7ff589.

📒 Files selected for processing (1)
  • packages/router-core/src/process-route-tree.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use TypeScript in strict mode with extensive type safety across the codebase

Files:

  • packages/router-core/src/process-route-tree.ts
packages/router-core/**

📄 CodeRabbit inference engine (AGENTS.md)

Keep framework-agnostic core router logic in packages/router-core/

Files:

  • packages/router-core/src/process-route-tree.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Preview
  • GitHub Check: Test
🔇 Additional comments (2)
packages/router-core/src/process-route-tree.ts (2)

25-52: LGTM - Function logic unchanged with new integer constants.

The handleParam function correctly uses the scaled integer constants. All arithmetic operations remain integer-based, which aligns with the PR objective of avoiding floating-point arithmetic.


54-179: Sorting logic is sound, but review comment contains incorrect assumptions about the code changes.

The sortRoutes function uses pure integer arithmetic throughout—all score constants are already integers (ranging from 100 to 10,000), and the handleParam function performs only integer operations. There is no evidence of float-to-integer scaling by 10,000 as claimed in the review. The test file provides comprehensive coverage of sorting behavior, including edge cases with similar scores, confirming the algorithm works correctly.

While the conclusion that sorting logic is preserved is correct, the justification provided in the review comment—that scores are "scaled uniformly by 10,000"—does not match the actual codebase implementation.

Likely an incorrect or invalid review comment.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0583040 and 57302c1.

📒 Files selected for processing (2)
  • packages/router-core/src/process-route-tree.ts (3 hunks)
  • packages/router-core/tests/processRouteTree.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/router-core/src/process-route-tree.ts
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use TypeScript in strict mode with extensive type safety across the codebase

Files:

  • packages/router-core/tests/processRouteTree.test.ts
packages/router-core/**

📄 CodeRabbit inference engine (AGENTS.md)

Keep framework-agnostic core router logic in packages/router-core/

Files:

  • packages/router-core/tests/processRouteTree.test.ts
🧠 Learnings (1)
📚 Learning: 2025-10-08T08:11:47.088Z
Learnt from: nlynzaad
PR: TanStack/router#5402
File: packages/router-generator/tests/generator/no-formatted-route-tree/routeTree.nonnested.snapshot.ts:19-21
Timestamp: 2025-10-08T08:11:47.088Z
Learning: Test snapshot files in the router-generator tests directory (e.g., files matching the pattern `packages/router-generator/tests/generator/**/routeTree*.snapshot.ts` or `routeTree*.snapshot.js`) should not be modified or have issues flagged, as they are fixtures used to verify the generator's output and are intentionally preserved as-is.

Applied to files:

  • packages/router-core/tests/processRouteTree.test.ts
🧬 Code graph analysis (1)
packages/router-core/tests/processRouteTree.test.ts (1)
packages/router-core/src/process-route-tree.ts (1)
  • processRouteTree (195-242)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Preview
  • GitHub Check: Test
🔇 Additional comments (1)
packages/router-core/tests/processRouteTree.test.ts (1)

443-462: Excellent test for verifying deterministic sorting.

The test effectively verifies the fix for issue #5635 by testing all permutations of the input routes and ensuring consistent output order. The chosen routes are sufficiently complex (with optional params, static segments, and varying depths) to validate that the sorting behavior is deterministic regardless of input order.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants