Skip to content

fix: don't crash on an unknown code in a svelte-ignore comment before <script> - #19037

Open
wfatih wants to merge 1 commit into
sveltejs:mainfrom
wfatih:fix-svelte-ignore-before-script
Open

wfatih wants to merge 1 commit into
sveltejs:mainfrom
wfatih:fix-svelte-ignore-before-script

Conversation

@wfatih

@wfatih wfatih commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

An unknown code in a svelte-ignore comment right before the instance <script> crashes the compiler in runes mode, instead of reporting an unknown_code warning:

<!-- svelte-ignore not_a_code -->
<script>
	let count = $state(0);
</script>
Error: An impossible situation occurred
    at locator (compiler/state.js)
    at w.unknown_code (compiler/warnings.js)
    at extract_svelte_ignore (compiler/utils/extract_svelte_ignore.js)

The parser passes that HTML comment to the analysis as a leading comment of the script's Program. It creates the comment with only type and value, so comment.start + 2 in the analysis is NaN, and the warning can't be located. A known code doesn't need a position, which is why only unknown or legacy codes crash.

This change gives the comment the position of the HTML comment. When reading the leading comment of a Program, the analysis skips <!-- instead of //, so the warning points at the code itself. The comment-before-script parser snapshot changes only by the new start/end of that comment.

If template content follows the <script>, the comment also applies to that content, and the warning is reported for it too. That already happens on main for a comment before <style>, and this PR doesn't change it.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
    • tests/validator/samples/unknown-code-before-script expects the unknown_code warning at line 3, column 19. On main it fails with the crash above.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint
    • pnpm lint is clean. pnpm test: 7894 passed. The 3 failures are the timing-sensitive effect-loop-infinite and error-boundary-20 runtime tests. They fail the same way on main on my machine and pass when run alone.

@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of svelte from 0241d58:

pnpm add https://pkg.svelte.dev/svelte/c/0241d58d2d579414ffd69c1c68a580aa5a257b88

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/svelte/pr/19037

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Oct 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0241d58

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
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.

1 participant