Skip to content

Auto import does not pick up shortest path despite correct setting (JavaScript project with jsconfig.json) #54324

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
rwalle opened this issue May 19, 2023 · 5 comments · Fixed by #54868
Assignees
Labels
Bug A bug in TypeScript Domain: Auto-import Fix Available A PR has been opened for this issue

Comments

@rwalle
Copy link

rwalle commented May 19, 2023

Bug Report

🔎 Search Terms

  • shortest path
  • auto import relative path
  • auto import parent folder

I found a few closely related issues, and a few of them are marked as fixed. But since I am using a very recent version of the TS server, I was not expecting the issue to appear in my setup.

🕗 Version & Regression Information

TS Server 5.0.4 in VSCode 1.78.2

⏯ Playground Link

N/A -- project setup

💻 Code

Please use the code in this repository:

https://github.com/rwalle/ts-path-issue

🙁 Actual behavior

We have a monorepo setup without node_modules, and for simplicity, assume all packages live under the same folder. A team only develops one or a few packages, and they may have dependencies between them. In the example repository, package1 depends on package2. jsconfig.json has been correctly setup.

After opening setup1/package1 as the only workspace folder in VSCode, open file1.js and type bar, the import path is ../package2/file1. The value for VSCode extension setting javascript.preferences.importModuleSpecifier is the default "shortest". If this is changed to "non-relative" or "project-relative", it gets worse -- it becomes package1/../package2/file1.

Alternatively, under "shortest" setting, move file1.js to a subfolder and try to import bar, you always see package1/../package2/file1.

Screenshot 2023-05-19 094604 Screenshot 2023-05-19 094732

However, if I manually type import { bar } from 'p', it can find package2. Also go to definition works properly if you uncomment the import at the beginning of that file.

After opening setup2 as the workspace folder in VSCode, type bar, import path is package2/file1. However we don't want to do that -- root folder is huge and nobody wants to have all folders and files from irrelevant packages visible in the workspace tree and potentially watched.

🙂 Expected behavior

For setup1/package1, TS Service correctly provides the shortest path package2/file1.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 19, 2023
@andrewbranch
Copy link
Member

Looks like a duplicate of #53369, which means it was fixed in #53957. Please make sure to try out the nightly TypeScript version before you go through all the trouble of making a repro! Let me know if the nightly doesn’t solve your problem, but I’m going to go ahead and close because it looks identical.

@andrewbranch andrewbranch closed this as not planned Won't fix, can't repro, duplicate, stale May 19, 2023
@andrewbranch andrewbranch added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. Domain: Auto-import labels May 19, 2023
@andrewbranch andrewbranch removed their assignment May 19, 2023
@rwalle
Copy link
Author

rwalle commented May 19, 2023

Looks like a duplicate of #53369, which means it was fixed in #53957. Please make sure to try out the nightly TypeScript version before you go through all the trouble of making a repro! Let me know if the nightly doesn’t solve your problem, but I’m going to go ahead and close because it looks identical.

I just installed typescript@next extension and still seeing the same thing with 5.2.0-dev.20230517, and I think I am using the correct version by checking the command and the TS Server log

Screenshot 2023-05-19 173819

Would you mind validating this?

@andrewbranch
Copy link
Member

I can’t repro anything like package1/../package2/file1, which I kind of got tunnel vision on in your initial description. I get ../package2/file1. But you want package2/file1.

@andrewbranch andrewbranch reopened this May 19, 2023
@andrewbranch andrewbranch added Bug A bug in TypeScript Domain: Auto-import and removed Duplicate An existing issue was already created labels May 19, 2023
@andrewbranch andrewbranch self-assigned this May 19, 2023
@andrewbranch andrewbranch added this to the TypeScript 5.2.0 milestone May 19, 2023
@rwalle
Copy link
Author

rwalle commented May 19, 2023

I can’t repro anything like package1/../package2/file1, which I kind of got tunnel vision on in your initial description. I get ../package2/file1. But you want package2/file1.

Correct, the behavior has changed but is still not what a user would expect. (I was not clear in the previous comment)

@andrewbranch
Copy link
Member

My bad, I was skimming for the most horrifying parts of the actual behavior 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Auto-import Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants