language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

Auto-import is incorrect for grandparent

Open j-tai opened this issue 2 years ago • 1 comments

Describe the bug

When the Svelte plugin auto-imports a component from the grandparent directory, the import it adds is incorrect:

import Bar from 'src/Bar.svelte';

Although this path looks correct, it causes a compiler error:

[vite]: Rollup failed to resolve import "src/Bar.svelte" from "src/lib/foo/Foo.svelte".

The expected import is:

import Bar from '../../Bar.svelte';

Reproduction

  1. npm create vite@latest - select a framework: Svelte - select a variant: JavaScript
  2. Create an empty file src/Bar.svelte
  3. Create a file src/lib/foo/Foo.svelte. Type <Ba, and accept the Bar auto-completion + auto-import. → The import is incorrect.
  4. (optional) To see the compiler error, Foo needs to be used somewhere. In App.svelte, import it and add a <Foo /> tag, then run npm run build. → There is a compiler error due to the incorrect import.

Expected behaviour

The auto-import should be correct and should not produce a compiler error.

System Info

  • OS: Pop!_OS 22.04
  • IDE: VSCodium 1.71.2

Which package is the issue about?

No response

Additional Information, eg. Screenshots

No response

j-tai avatar Sep 22 '22 03:09 j-tai

This works as expected from the language-tools side of view. The tsconfig.json defines a baseUrl, which in the eyes of TypeScript means these import paths are correct. What's missing is an equivalent of this for Vite, so Vite also knows how to deal with these - this likely means adding an alias.

Keeping this open as a reminder to try this out myself and provide a PR to the Vite repo.

dummdidumm avatar Sep 22 '22 10:09 dummdidumm

Closing as the related PR in create-vite was merged. To remove the buggy behavior, remove the "baseUrl" from your tsconfig.json

dummdidumm avatar Sep 26 '22 13:09 dummdidumm

Thanks!

j-tai avatar Sep 26 '22 15:09 j-tai