Skip to content

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

Closed
@rwalle

Description

@rwalle

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions