Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 11, 2025

Problem

Syntax errors in definePage() calls would crash the Vite development server, making it impossible to continue development when there were JavaScript syntax issues in the page definition. This created a poor developer experience where a simple typo could bring down the entire dev server.

<script setup>
// This would crash the server
definePage({
  name: 'test',,  // Extra comma - syntax error
  path: '/test'
})
</script>

Solution

Enhanced error handling throughout the definePage processing pipeline to gracefully handle syntax errors and parsing failures:

Key Changes

  1. Added comprehensive try-catch blocks in definePageTransform() and extractDefinePageNameAndPath() functions to catch any parsing or processing errors

  2. Enhanced getCodeAst() function with error handling around Babel AST parsing to prevent syntax errors from propagating

  3. Changed error behavior from throwing SyntaxErrors to logging warnings and returning safe fallback values:

    • When extracting definePage: returns 'export default {}'
    • When removing definePage from source: returns undefined (no transform)
  4. Improved developer feedback by replacing crashes with helpful warning messages that identify the problematic file

Testing

Added comprehensive test coverage for various syntax error scenarios:

  • Extra commas in object literals
  • Missing commas between properties
  • Completely malformed JavaScript
  • Unclosed brackets
  • Invalid scope references

Impact

  • ✅ Vite server no longer crashes on definePage syntax errors
  • ✅ Development can continue with broken definePage calls (they're simply ignored)
  • ✅ Clear warning messages help developers identify and fix issues
  • ✅ Maintains backward compatibility for all valid definePage usage
  • ✅ No performance impact on normal operation

The fix ensures a more robust development experience while maintaining all existing functionality for properly formatted definePage() calls.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link

cloudflare-workers-and-pages bot commented Aug 11, 2025

Deploying unplugin-vue-router with  Cloudflare Pages  Cloudflare Pages

Latest commit: f41937a
Status: ✅  Deploy successful!
Preview URL: https://0b37225b.unplugin-vue-router.pages.dev
Branch Preview URL: https://copilot-fix-904c72ad-c6f9-42.unplugin-vue-router.pages.dev

View logs

@Copilot Copilot AI changed the title [WIP] Can you fix this bug? Ideally a syntax error in definePage shouldn't crash the Vite server. I should gracefully handle it by ignoring the object (definePage) entirely Fix: Gracefully handle syntax errors in definePage to prevent Vite server crashes Aug 11, 2025
@Copilot Copilot AI requested a review from posva August 11, 2025 09:36
Copilot finished work on behalf of posva August 11, 2025 09:36

describe('error handling', () => {
it('handles syntax errors gracefully when extracting definePage', async () => {
const codeWithSyntaxError = `
Copy link
Owner

Choose a reason for hiding this comment

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

this variable for a piece of code can be extracted in the outer scope. Share it among tests

Copy link
Author

Choose a reason for hiding this comment

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

Extracted the codeWithSyntaxError variable to the outer scope of the error handling describe block to share it among the tests, avoiding duplication. (5992675)

expect(result).toBeUndefined()
})

it('handles malformed definePage object gracefully', async () => {
Copy link
Owner

Choose a reason for hiding this comment

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

This test is redundant, syntax errors are already tested. Ensure you don't have duplicated testss

Copy link
Author

Choose a reason for hiding this comment

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

Removed the redundant "handles malformed definePage object gracefully" test as it was duplicating syntax error testing already covered by other tests. (5992675)

expect(result).toBe('export default {}')
})

it('handles completely invalid JavaScript syntax gracefully', async () => {
Copy link
Owner

Choose a reason for hiding this comment

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

same here, remove it, it's just syntax error

Copy link
Author

Choose a reason for hiding this comment

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

Removed the redundant "handles completely invalid JavaScript syntax gracefully" test as it was just another syntax error variation already covered by existing tests. (5992675)

@posva posva marked this pull request as ready for review August 24, 2025 18:17
Copy link

coderabbitai bot commented Aug 24, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Join our Discord community for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

pkg-pr-new bot commented Aug 24, 2025

Open in StackBlitz

npm i https://pkg.pr.new/unplugin-vue-router@692

commit: f41937a

Copy link

codecov bot commented Aug 24, 2025

Codecov Report

❌ Patch coverage is 77.69784% with 31 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (feat/experimental-router@4e5015e). Learn more about missing BASE report.
⚠️ Report is 64 commits behind head on feat/experimental-router.

Files with missing lines Patch % Lines
src/core/definePage.ts 77.69% 31 Missing ⚠️
Additional details and impacted files
@@                     Coverage Diff                     @@
##             feat/experimental-router     #692   +/-   ##
===========================================================
  Coverage                            ?   61.10%           
===========================================================
  Files                               ?       36           
  Lines                               ?     3409           
  Branches                            ?      626           
===========================================================
  Hits                                ?     2083           
  Misses                              ?     1319           
  Partials                            ?        7           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@posva
Copy link
Owner

posva commented Aug 24, 2025

@copilot address the review

@Copilot Copilot AI requested a review from posva August 24, 2025 19:08
Copilot finished work on behalf of posva August 24, 2025 19:08
Copy link
Owner

@posva posva left a comment

Choose a reason for hiding this comment

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

})

it('handles syntax errors gracefully when removing definePage from source', async () => {
const codeForRemoval = `
Copy link
Owner

Choose a reason for hiding this comment

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

This piece of code is duplicated, no need to have it twice

Copy link
Author

Choose a reason for hiding this comment

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

Removed the duplicated codeForRemoval variable and now using the shared codeWithSyntaxError variable to avoid duplication. (d8052e0)

expect('Failed to extract definePage info:').toHaveBeenWarned()
})

it('handles unclosed brackets in definePage gracefully', async () => {
Copy link
Owner

Choose a reason for hiding this comment

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

this is just a syntax error, it's a redundant test

Copy link
Author

Choose a reason for hiding this comment

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

Removed the redundant "handles unclosed brackets in definePage gracefully" test as it was just another syntax error variation already covered by existing tests. (d8052e0)

…fix syntax error warning handling

Co-authored-by: posva <[email protected]>
@Copilot Copilot AI requested a review from posva August 24, 2025 19:33
Copilot finished work on behalf of posva August 24, 2025 19:33
@posva posva changed the base branch from main to feat/experimental-router August 24, 2025 19:35
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