Skip to content
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

Auto imported file path is wrong (absolute vs relative) and raises error at runtime #137446

Closed
DavideViolante opened this issue Nov 18, 2021 · 8 comments
Assignees
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)

Comments

@DavideViolante
Copy link

DavideViolante commented Nov 18, 2021

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.62.2
  • OS Version: Ubuntu 20.03

Steps to Reproduce:

  1. I've a TypeScript project with the following folders: server/validators/* and server/shared/*
  2. When I try to automatically import something from server/shared/constants.ts to server/validators/file.ts the import string is:
  3. import { usernameRegexp } from 'shared/constants'; that gives me an error because the correct path should be:
  4. import { usernameRegexp } from '../shared/constants';

Short vid of what I mean with autoimport:

recordautoimport.mp4
@IllusionMH
Copy link
Contributor

/needsMoreInfo

Do you have these folders in paths in tsconfig.json?
Please provide used configuration

@DavideViolante
Copy link
Author

DavideViolante commented Nov 18, 2021

I don't have any paths configured in tsconfig. I tried some configs of paths but without luck. Example:

"baseUrl": "",
"paths": {
  "shared/*": ["shared/*"]
},

But anyway I would like any folder to import correctly, shared is just an example.
I also tried to change baseUrl to something else but same output.

tsconfig is the same as this. Also the project strucure is very similar to that repo.

@mjbvz
Copy link
Collaborator

mjbvz commented Nov 18, 2021

Please share a minimal example project that demonstrates the issue

@DavideViolante
Copy link
Author

DavideViolante commented Nov 18, 2021

Ok I found that it happens in the parent project too, so you can repro with this:

  1. git clone git@github.com:DavideViolante/Angular-Full-Stack.git
  2. Open the project in VS Code
  3. Edit controllers/cat.ts, delete line 1
  4. Auto import Cat in line 4 using suggested action by VS Code
  5. Line added in the top file will be import Cat from 'models/cat'; instead of import Cat from '../models/cat';
  6. npm run predev it will compile correctly...
  7. npm start
  8. Error: Cannot find module 'models/cat'

@mjbvz
Copy link
Collaborator

mjbvz commented Nov 19, 2021

Have you tried configuring "typescript.preferences.importModuleSpecifier": "relative"?

@DavideViolante
Copy link
Author

DavideViolante commented Nov 19, 2021

Have you tried configuring "typescript.preferences.importModuleSpecifier": "relative"?

This works! I didn't know about this config. I wonder now if relative should be the default one or not, but I don't have enough knowledge to give credit to either cases.
The only thing I can say is that, using the default option and auto-importing the cat, no error is raised whatsoever during compilation (tsc) but only at runtime, that is quite dangerous to discover and quite "far" from the development cycle.
Would be nice to know your opinion about it or create a small discussion.

@DavideViolante DavideViolante changed the title Auto imported file path is wrong Auto imported file path is wrong (absolute vs relative) and raises error at runtime Nov 19, 2021
@mjbvz
Copy link
Collaborator

mjbvz commented Nov 19, 2021

Thanks for testing! Closing as by-design / question

The type of the path used by default depends on your jsconfig / tsconfig. Try changing it something like es2020 if you don't want node style paths by be allowed

The setting then offers more fine grained control over which path style is selected

@mjbvz mjbvz closed this as completed Nov 19, 2021
@mjbvz mjbvz added *question Issue represents a question, should be posted to StackOverflow (VS Code) and removed info-needed Issue requires more information from poster labels Nov 19, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)
Projects
None yet
Development

No branches or pull requests

4 participants
@IllusionMH @DavideViolante @mjbvz and others