vite-plugin-svelte: Cannot read properties of undefined (reading 'startsWith')

Describe the bug

Error on use Typescript type. All explications in “reproduction” field.

Using:

  • SvelteKit
  • Svelte 5
  • Vite 6
  • Selected Typescript in setup

Reproduction URL

http://localhost:5173/

Reproduction

How to reproduce the bug:

  1. Create a new Sveltekit project, with typescript
  2. Install deps and run dev
  3. in +page.svelte, create a typed var: const test: string = "abc";

How to be sure that the bug comes from there: 4. Remove the type: const test = "abc"; 5. The bug disappears

Logs

No response

System Info

at couldBeFixedByCssPreprocessor (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/@sveltejs+vite-plugin-svelte@5.0.2_svelte@5.14.2_vite@6.0.3/node_modules/@sveltejs/vite-plugin-svelte/src/utils/error.js:111:72)
    at enhanceCompileError (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/@sveltejs+vite-plugin-svelte@5.0.2_svelte@5.14.2_vite@6.0.3/node_modules/@sveltejs/vite-plugin-svelte/src/utils/error.js:126:6)
    at compileSvelte (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/@sveltejs+vite-plugin-svelte@5.0.2_svelte@5.14.2_vite@6.0.3/node_modules/@sveltejs/vite-plugin-svelte/src/utils/compile.js:148:4)
    at async handleHotUpdate (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/@sveltejs+vite-plugin-svelte@5.0.2_svelte@5.14.2_vite@6.0.3/node_modules/@sveltejs/vite-plugin-svelte/src/handle-hot-update.js:33:17)
    at async handleHMRUpdate (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:44193:33)
    at async onHMRUpdate (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:43585:7)
    at async FSWatcher.<anonymous> (file:///C:/Users/360matt/WebstormProjects/lemonade-loader/node_modules/.pnpm/vite@6.0.3/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:43622:5

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 21 (5 by maintainers)

Most upvoted comments

The bug isn’t in esrap, it’s in svelte — we’re handing esrap a malformed AST. Working on it

fixed in 5.14.3

Adding some context here.

Having this +page.svelte:

<script lang="ts">
  const test: string = "abc";
</script>

<h1>Welcome to SvelteKit</h1>
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p>

Gives the error that OP reported:

|- TypeError: Cannot read properties of undefined (reading 'startsWith')
    at couldBeFixedByCssPreprocessor (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/@sveltejs/vite-plugin-svelte/src/utils/error.js:111:72)
    at enhanceCompileError (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/@sveltejs/vite-plugin-svelte/src/utils/error.js:126:6)
    at compileSvelte (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/@sveltejs/vite-plugin-svelte/src/utils/compile.js:148:4)
    at async TransformPluginContext.transform (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/@sveltejs/vite-plugin-svelte/src/index.js:164:20)
    at async EnvironmentPluginContainer.transform (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:47001:18)
    at async loadAndTransform (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:40852:27)

Diving into node_modules and commenting out enhanceCompileError at https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/vite-plugin-svelte/src/utils/compile.js#L148 reveals the true error beneath.

Side node: enhanceCompileError should probably be wrapped in its own try-catch block, so that if it fails (like it does here) it doesn’t obscure the actual error being thrown)

Actual error:

Error: Not implemented type annotation EmptyStatement
    at handle_type_annotation (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:560:10)
    at Identifier (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:1028:28)
    at handle (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:62:2)
    at VariableDeclarator (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:1531:3)
    at handle (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:62:2)
    at handle_var_declaration (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:319:3)
    at VariableDeclaration (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:1526:3)
    at handle (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:62:2)
    at handle_body (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:274:3)
    at BlockStatement|ClassBody (file:///Users/jeppe/dev/temp/esrap-issue/node_modules/esrap/src/handlers.js:620:3) {
  id: '/Users/jeppe/dev/temp/esrap-issue/src/routes/+page.svelte',
  frame: '',
  code: undefined,
  plugin: 'vite-plugin-svelte',
  pluginCode: '<script lang="ts">\n' +
    '  const test: string = "abc";\n' +
    '</script>\n' +
    '\n' +
    '<h1>Welcome to SvelteKit</h1>\n' +
    '<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p>\n',

This is most likely caused by this recent PR https://github.com/Rich-Harris/esrap/pull/13

Adding the following to https://github.com/Rich-Harris/esrap/blob/main/src/handlers.js#L560, reveals more:

console.dir({node, state}, {depth: 10});

logged result:

{
  node: { type: 'EmptyStatement' },
  state: {
    commands: [
      { type: 'Chunk', content: '_page', loc: null },
      '[',
      { type: 'Chunk', content: '$.FILENAME', loc: null },
      ']',
      ' = ',
      {
        type: 'Chunk',
        content: '"src/routes/+page.svelte"',
        loc: null
      },
      ';',
      { type: 'Sequence', children: [ '\n' ] },
      { type: 'Newline' },
      'import ',
      { type: 'Chunk', content: '* as $', loc: null },
      ' from ',
      { type: 'Chunk', content: '"svelte/internal/server"', loc: null },
      ';',
      { type: 'Sequence', children: [] },
      { type: 'Newline' },
      'function ',
      { type: 'Chunk', content: '_page', loc: null },
      '(',
      { type: 'Sequence', children: [] },
      { type: 'Chunk', content: '$$payload', loc: null },
      ',',
      { type: 'Sequence', children: [ ' ' ] },
      { type: 'Chunk', content: '$$props', loc: null },
      { type: 'Sequence', children: [] },
      ')',
      ' ',
      '{',
      { type: 'Indent' },
      { type: 'Newline' },
      { type: 'Chunk', content: '$.push', loc: null },
      '(',
      { type: 'Sequence', children: [] },
      { type: 'Chunk', content: '_page', loc: null },
      { type: 'Sequence', children: [] },
      ')',
      ';',
      { type: 'Sequence', children: [] },
      { type: 'Newline' },
      'const ',
      { type: 'Sequence', children: [] },
      {
        type: 'Chunk',
        content: 'test',
        loc: SourceLocation {
          start: Position { line: 2, column: 8 },
          end: Position { line: 2, column: 20 }
        }
      }
    ],
    comments: [],
    multiline: false
  }
}

And that’s as far as I got… 😴

The workaround is stated above:

please try downgrading esrap to ~1.2.0 via override in package.json

@midirhee12 Add the following to the package.json file.

{
  "pnpm": {
    "overrides": {
      "esrap": "~1.2.0"
    }
  }
}

About overrides:

please try downgrading esrap to ~1.2.0 via override in package.json

opened a fresh issue on sveltejs/svelte https://github.com/sveltejs/svelte/issues/14743

Please don’t comment on closed issues, it’s easy for updates to get missed

@webJose Running pnpm install esrap@1.2.0 on a clean install does not fix the issue. Is there something I’m missing here?

Also, instead of telling people to “read the thread”, it would be better to link the actual comment of the thread which noted the workaround.

please try downgrading esrap to ~1.2.0 via override in package.json

This is the correct temporary fix pending an update to the esrap package.

confirmed a clean project using npx sv create triggers the issue when typescript is added to the +page.svelte