Skip to content

Importing TS files from component is not working when script tag spans multiple lines #14384

Closed
@fehnomenal

Description

@fehnomenal

Describe the bug

If the <script lang="ts"> has a long generics attribute that causes prettier to break it into multiple lines, imports with a .js extension that should resolve to a TS file are no longer working.

<script
  lang="ts"
  generics="SomethingLongToTriggerReformat extends Record<string, unknown>, MoreStrings extends keyof SomethingLongToTriggerReformat"
>
  import { test } from './lib/test.js';
</script>

{test}

If you change the import to './lib/test.ts' or './lib/test' it works again.
If you add a space after <script it also works again.

Reproduction

Repo: https://github.com/fehnomenal/svelte-ts-import

This is the vite@latest svelte template stripped of all unneeded stuff.

The second commit (https://github.com/fehnomenal/svelte-ts-import/commit/91e042f4858924246aa374ce9f4beedd1a3b1538) gives the diff as an example.

Logs

❯ pnpm build

> [email protected] build /tmp/svelte-ts-import
> vite build

vite v5.4.11 building for production...
✓ 15 modules transformed.
x Build failed in 70ms
error during build:
Could not resolve "./lib/test.js" from "src/App.svelte"
file: /tmp/svelte-ts-import/src/App.svelte
    at getRollupError (file:///tmp/svelte-ts-import/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:396:41)
    at error (file:///tmp/svelte-ts-import/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:392:42)
    at ModuleLoader.handleInvalidResolvedId (file:///tmp/svelte-ts-import/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:20085:24)
    at file:///tmp/svelte-ts-import/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:20045:26
 ELIFECYCLE  Command failed with exit code 1.

System Info

System:
    OS: Linux 6.6 cpe:/o:nixos:nixos:24.11 24.11 (Vicuna)
    CPU: (16) x64 AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
    Memory: 9.77 GB / 60.64 GB
    Container: Yes
    Shell: 0.99.1 - /run/current-system/sw/bin/nu
  Binaries:
    Node: 23.1.0 - /etc/profiles/per-user/andi/bin/node
    npm: 10.9.0 - /etc/profiles/per-user/andi/bin/npm
    pnpm: 9.14.1 - /etc/profiles/per-user/andi/bin/pnpm
    bun: 1.1.31 - /etc/profiles/per-user/andi/bin/bun
  Browsers:
    Chromium: 130.0.6723.91
  npmPackages:
    svelte: 5.2.7 => 5.2.7

Severity

annoyance

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