Skip to content

Confusing behavior with tsconfig and alias in some cases #5351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dummdidumm opened this issue Jul 4, 2022 · 1 comment · Fixed by #8568
Closed

Confusing behavior with tsconfig and alias in some cases #5351

dummdidumm opened this issue Jul 4, 2022 · 1 comment · Fixed by #8568
Labels
bug Something isn't working types / typescript
Milestone

Comments

@dummdidumm
Copy link
Member

dummdidumm commented Jul 4, 2022

Describe the bug

If you import something from a folder which isn't aliased, intellisense will suggest imports which are resolvable by TypeScript but not Vite.

Reproduction

Consider this folder structure:

src
  | utils
    | x.ts
  | routes
    | index.svelte

If you import something from routes that is within utils you'll get the following suggestion: import X from "src/utils/x . Reason: The generated tsconfig.json contains "baseUrl": ".". In the eyes of TS this is a correct import path due to the base path alias, in the eyes of Vite it's not because we don't define such an alias (only $lib by default).

I'm not sure of the best solution here. Maybe we should always add an alias to Vite that resolves src/.. to real/path/to/src.

Logs

No response

System Info

System:                                                    
    OS: Windows 10 10.0.19044                                
    CPU: (12) x64 Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz   
    Memory: 13.04 GB / 31.79 GB                              
  Binaries:                                                  
    Node: 16.15.1 - C:\Program Files\nodejs\node.EXE         
    npm: 8.13.2 - C:\Program Files\nodejs\npm.CMD            
  Browsers:                                                  
    Chrome: 103.0.5060.66                                    
    Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.44)
    Internet Explorer: 11.0.19041.1566                       
  npmPackages:                                               
    @sveltejs/adapter-node: 1.0.0-next.78 => 1.0.0-next.78   
    @sveltejs/kit: 1.0.0-next.357 => 1.0.0-next.357          
    svelte: 3.48.0 => 3.48.0

Severity

annoyance

Additional Information

No response

@dummdidumm
Copy link
Member Author

dummdidumm commented Jan 10, 2023

Turns out it is possible to omit the baseUrl option (microsoft/TypeScript#31869). That would fix this bug, but unfortunately it means that the paths in paths then need to be relative (../src/lib instead of src/lib in our example for the $lib alias) - and that could be regarded as a breaking change since people could have added manual paths in their tsconfig assuming the non-relative path works. Ideally noone should be using paths in their tsconfig and instead use svelte.config.js#kit.alias but that's certainly not the case for everyone.
The linked issue provides some workarounds which I tried out and they either didn't work or sound like they cause problems with other tooling.

I guess the best option is to add a Vite path alias as mentioned in the issue.

dummdidumm added a commit that referenced this issue Jan 10, 2023
Fixes #5351
Also a drive-by-remove of some obsolete code
dummdidumm added a commit that referenced this issue Jan 10, 2023
Fixes #5351
Also a drive-by-remove of some obsolete code
dummdidumm added a commit that referenced this issue Jan 17, 2023
Fixes #5351
Also a drive-by-remove of some obsolete code
Rich-Harris pushed a commit that referenced this issue Jan 17, 2023
* fix: remove baseUrl to prevent wrong TypeScript auto imports

Fixes #5351
Also a drive-by-remove of some obsolete code

* how the hell did this happen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working types / typescript
Projects
None yet
2 participants