Skip to content

fix(router-plugin): escape single quotes in code-split importer paths#7760

Open
thribhuvan003 wants to merge 2 commits into
TanStack:mainfrom
thribhuvan003:fix/code-splitter-quote-escaping
Open

fix(router-plugin): escape single quotes in code-split importer paths#7760
thribhuvan003 wants to merge 2 commits into
TanStack:mainfrom
thribhuvan003:fix/code-splitter-quote-escaping

Conversation

@thribhuvan003

@thribhuvan003 thribhuvan003 commented Jul 8, 2026

Copy link
Copy Markdown

when the project path contains a single quote (e.g. /Users/dev/it's a repro/...), the code-splitter interpolates the filename straight into import('<path>?tsr-split=...'), so the apostrophe terminates the string literal early and babel throws while parsing the generated statement — every route in the project fails to compile.

fixed by escaping ' and \ before interpolating. kept the single-quoted style instead of e.g. JSON.stringify so the existing snapshots stay byte-identical (tried stringify first, it churned all 177 of them for no reason).

added a test that compiles a route from such a path for both react and solid — it fails without the fix, and the full router-plugin suite is unchanged with it.

fixes #7754

Summary by CodeRabbit

  • Bug Fixes

    • Fixed route code-splitting to correctly escape special characters in generated dynamic import URLs, including apostrophes (') and backslashes (\).
    • Preserved the original file path and ?tsr-split=component query so compiled routes generate valid output.
  • Tests

    • Added automated coverage to verify split-route generation keeps special characters intact in the produced import('...') specifier.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f3d0ecf5-2dd1-4e3c-abfb-c1f85cb345c7

📥 Commits

Reviewing files that changed from the base of the PR and between f2b8e81 and af932d2.

📒 Files selected for processing (1)
  • .changeset/fix-code-splitter-quote-escaping.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/fix-code-splitter-quote-escaping.md

📝 Walkthrough

Walkthrough

Adds an internal helper to escape backslashes and single quotes when generating single-quoted dynamic import strings in the code-splitter compiler, applies it at both importer generation sites, and adds test and changeset coverage for paths containing apostrophes.

Changes

Path escaping fix

Layer / File(s) Summary
Escaping helper and importer wiring
packages/router-plugin/src/core/code-splitter/compilers.ts
Adds escapeSingleQuotedString and uses it when generating import('...') expressions for both lazyRouteComponent and lazyFn.
Path escaping test coverage
packages/router-plugin/tests/code-splitter-path-escaping.test.ts, .changeset/fix-code-splitter-quote-escaping.md
Adds a Vitest regression test for apostrophe-containing paths and a patch changeset entry documenting the code-splitting quote-escaping fix.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested labels: package: router-plugin

Suggested reviewers: nlynzaad, Sheraff

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The escape fix addresses the parser bug in #7754, but the PR does not implement the required autoCodeSplitting wiring change. Also update @tanstack/start-plugin-core to skip wiring code-splitter plugins when router.autoCodeSplitting is false.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main change: escaping single quotes in router-plugin code-split importer paths.
Out of Scope Changes check ✅ Passed The changes stay focused on the code-splitter bug fix, its test coverage, and the related changeset entry.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (2)
packages/router-plugin/tests/code-splitter-path-escaping.test.ts (2)

64-64: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Guard against null compile result for clearer test failures.

compileCodeSplitReferenceRoute can return null when no modifications are made. If the route stops being split in a future refactor, compileResult.code throws a TypeError rather than a clear assertion failure. A quick null check would improve diagnostics:

🛡️ Proposed guard
-      expect(getImporterUrls(compileResult.code)).toContain(
+      expect(compileResult).not.toBeNull()
+      expect(getImporterUrls(compileResult!.code)).toContain(
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/router-plugin/tests/code-splitter-path-escaping.test.ts` at line 64,
Add a null guard around the result of compileCodeSplitReferenceRoute in
code-splitter-path-escaping.test.ts before accessing compileResult.code, so the
test fails with a clear assertion instead of a TypeError. Update the assertion
near getImporterUrls to first verify compileResult is not null, using the
compileResult variable name and the compileCodeSplitReferenceRoute call site as
the lookup points.

38-67: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider adding a backslash path test case.

The test covers single-quote escaping but doesn't exercise backslash escaping, which is the other half of escapeSingleQuotedString. A path like C:\Users\dev\project\src\routes\index.tsx (or a POSIX path with a literal backslash) would verify both branches of the helper. This is optional since both branches share the same function and the single-quote case is the reported bug.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/router-plugin/tests/code-splitter-path-escaping.test.ts` around
lines 38 - 67, Add a second path-escaping test in the code-splitter path
escaping suite to cover backslash handling in addition to the existing
single-quote case. Extend the coverage around compileCodeSplitReferenceRoute and
getImporterUrls by using a filename that contains literal backslashes (for
example a Windows-style path or a POSIX path with a backslash) so
escapeSingleQuotedString is exercised for both escaping branches. Keep the
existing single-quote test and verify the generated importer URL round-trips the
full filename exactly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/router-plugin/tests/code-splitter-path-escaping.test.ts`:
- Line 64: Add a null guard around the result of compileCodeSplitReferenceRoute
in code-splitter-path-escaping.test.ts before accessing compileResult.code, so
the test fails with a clear assertion instead of a TypeError. Update the
assertion near getImporterUrls to first verify compileResult is not null, using
the compileResult variable name and the compileCodeSplitReferenceRoute call site
as the lookup points.
- Around line 38-67: Add a second path-escaping test in the code-splitter path
escaping suite to cover backslash handling in addition to the existing
single-quote case. Extend the coverage around compileCodeSplitReferenceRoute and
getImporterUrls by using a filename that contains literal backslashes (for
example a Windows-style path or a POSIX path with a backslash) so
escapeSingleQuotedString is exercised for both escaping branches. Keep the
existing single-quote test and verify the generated importer URL round-trips the
full filename exactly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b417562e-ecfd-46b9-b81b-7d37e3ee1899

📥 Commits

Reviewing files that changed from the base of the PR and between a3e24c3 and f2b8e81.

📒 Files selected for processing (2)
  • packages/router-plugin/src/core/code-splitter/compilers.ts
  • packages/router-plugin/tests/code-splitter-path-escaping.test.ts

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.

Code-splitter breaks every route when the project path contains a single quote/apostrophe (autoCodeSplitting: false does not avoid it)

1 participant