Skip to content

Svelte: every .svelte file reports a syntax error at line 1; functions inside <script> are never extracted (extract_svelte feeds the whole file to the JS grammar) #3928

Closed

Description

@chrisltq97

Summary

extract_svelte calls _extract_generic(path, _JS_CONFIG) on the raw .svelte file. The HTML template is not JavaScript, so tree-sitter hits a top-level ERROR at line 1 and never reaches function_declaration nodes inside <script>. Only imports survive, via the regex rescue pass. On a SvelteKit app with 394 components, every single one was flagged "syntax errors and may be partially extracted".

This is the same failure mode as #2788 / PR #3902 for Astro. Vue already avoids it via _vue_mask_non_script.

Reproduce (graphify 0.9.71, macOS, Python 3.13)

mkdir t && cd t
printf '<script>\n  import { foo } from "./x";\n  let n = 1;\n  function bump() { n++; }\n</script>\n<button onclick={bump}>{n}</button>\n' > A.svelte
graphify extract . --code-only --no-cluster

Output:

warning: 1 file(s) had syntax errors and may be partially extracted: A.svelte (first error at line 1, 1 symbol(s) extracted)
wrote graphify-out/graph.json — 2 nodes, 1 edges

bump() is missing. Same result with <script lang="ts"> and with Svelte 5 runes ($state, $props).

Fix that works locally

Mirror extract_vue: mask non-script regions with _vue_mask_non_script (regex is generic <script …>…</script>), pick _JS_CONFIG when lang is js/jsx else _TS_CONFIG, run _normalize_ts_import_types, and pass source_override= to _extract_generic. The existing regex import-rescue stays as-is.

src = path.read_text(encoding="utf-8", errors="replace")
masked, lang = _vue_mask_non_script(src)
cfg = _JS_CONFIG if lang in ("js", "jsx") else _TS_CONFIG
mb = masked.encode("utf-8")
if cfg is _TS_CONFIG:
    mb = _normalize_ts_import_types(mb) or mb
result = _extract_generic(path, cfg, source_override=mb)

Results on a real SvelteKit repo (658 code files, 394 .svelte):

files with parse errors nodes edges
before 394 1829 3816
after 0 3377 5921

Minimal repro above: 2 nodes / 1 edge -> 3 nodes / 3 edges (bump() present). Line numbers stay correct because the mask preserves \n.

Happy to open a PR if useful.

Activity

  1. github-actions commented on Sep 29, 2026

    @github-actions

    Thanks for opening this issue, @chrisltq97. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. AlienAscension commented on Oct 4, 2026

    @AlienAscension

    Can confirm this on 0.9.66 with a SvelteKit + Svelte 5 codebase (33 components, 30 using runes). Same root cause, same fix.

    Before: 63 nodes / 108 edges across the 33 .svelte files, every file flagged "syntax errors ... partially extracted".
    After masking non-<script> regions with _vue_mask_non_script and passing source_override= (the snippet you posted): 201 nodes / 341 edges, zero parse warnings.

    $props/$derived/$state declarations and functions nested after the first markup error all come through, and line numbers stay correct. No context="module" blocks in this repo, so I can't speak to those.

    One note for whoever picks this up: _vue_mask_non_script returns the first block's lang, which is fine for Svelte too, but a component with both <script context="module"> and <script> would only see the first block's lang. Worth handling if you touch it.

  3. added a commit that references this issue on Oct 4, 2026
    f9b42e8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions