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 import from yarn workspaces using package's name in the package.json #48723

Closed
Zver64 opened this issue Apr 15, 2022 · 7 comments
Closed
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@Zver64
Copy link

Zver64 commented Apr 15, 2022

Issue Type: Bug
I have a typescript project, that uses yarn workspaces in it. When I import any entity from a package, that is a workspace, using the auto import feature - the only option I have is to import it relative to the project's root. Instead I want to also be able to import them in the same way I import any entity from a regular package from the node_modules. I have to manually refactor all the imports that vscode generates for me to keep up with the code style in the project, which is time consuming.

Steps to reproduce:

  1. clone this minimal demo https://github.com/Zver64/workspaces-test
  2. run yarn in the project root
  3. open the file packages/package-b/index.ts
  4. remove the import of the constant named PACKAGE_A_CONST
  5. try to add this import back using vscode's context menu

Expected behavior:
There is an option to add the import from the node_modules, that would result to this import:

import { PACKAGE_A_CONST } from "package-a-name"

since the package exists in the node_modules as a symlink to the workspace

Actual result:
The only option in the context menu is:
image
Which leads to this import:

import { PACKAGE_A_CONST } from "packages/package-a";

VS Code version: Code - Insiders 1.67.0-insider (Universal) (27dd7b4, 2022-04-12T18:42:30.490Z)
OS version: Darwin x64 21.3.0
Restricted Mode: No

System Info
Item Value
CPUs Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz (16 x 2400)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
metal: disabled_off
multiple_raster_threads: enabled_on
oop_rasterization: enabled
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
Load (avg) 2, 2, 2
Memory (System) 32.00GB (3.43GB free)
Process Argv --crash-reporter-id bc00bb11-be46-473c-958c-3aa4617dbf9e
Screen Reader no
VM 0%
Extensions (52)
Extension Author (truncated) Version
increment-selection alb 0.2.0
Bookmarks ale 13.2.4
clipboard-history Anj 1.0.7
vscode-zipfs arc 3.0.0
vscode-toggle-quotes Bri 0.3.6
better-toml bun 0.3.2
gitignore cod 0.7.0
postcss css 1.0.9
dart-code Dar 3.38.2
flutter Dar 3.38.1
vscode-markdownlint Dav 0.47.0
vscode-eslint dba 2.2.2
xslt-xpath del 1.2.3
xml Dot 2.5.1
eslint-disable-snippets drK 1.3.0
gitlens-insiders eam 2022.4.1302
prettier-vscode esb 9.5.0
copy-relative-path-and-line-numbers ezf 0.3.1
code-runner for 0.11.7
shell-format fox 7.2.2
gitlab-workflow Git 3.42.1
todo-tree Gru 0.0.215
debug-visualizer hed 2.3.1
vscode-drawio hed 1.6.4
openvpn idl 0.2.3
search-node-modules jas 1.3.0
ts-debug kak 0.0.6
vscode-status-bar-title kso 0.0.3
CamelCase Mar 1.0.5
markdown-shortcuts mdi 0.12.0
git-graph mhu 1.30.0
vscode-goto-node-modules mrl 1.2.1
vscode-docker ms- 1.21.0
jupyter ms- 2022.3.1000901801
remote-containers ms- 0.232.6
remote-wsl ms- 0.66.0
vscode-typescript-next ms- 4.7.20220408
color-highlight nau 2.5.0
copy-json-path nid 0.2.0
markdown-checkbox PKi 1.8.0
bracketeer pus 1.3.1
vscode-thunder-client ran 1.16.2
json-to-js-object sal 0.0.4
swapdiff sha 0.1.0
markdown-preview-enhanced shd 0.6.2
vscode-scss-formatter sib 2.4.2
vscode-fileutils sle 3.5.0
rewrap stk 1.16.3
non-breaking-space-highlighter vik 0.0.3
vscodeintellicode Vis 1.2.19
vscode-conventional-commits viv 1.24.0
vim vsc 1.22.2
A/B Experiments
vsliv695:30137379
vsins829:30139715
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
pythonvspyl392:30422396
pythontb:30258533
pythonptprofiler:30281269
vshan820:30294714
pythondataviewer:30285072
vscod805:30301674
pythonvspyt200:30323110
bridge0708:30335490
bridge0723:30353136
vsaa593:30376534
testflightcf:30433671
pythonvs932:30404738
wslgetstarted:30449409
pythonvspyt640:30438691
vscscmwlcmt:30465136
cppdebug:30466689
pynewfile477:30451556

@Zver64 Zver64 changed the title Auto import from yarn workspaces using package's name in package.json Auto import from yarn workspaces using package's name in the package.json Apr 15, 2022
@IllusionMH
Copy link
Contributor

You need either use paths which you commented out or use proper config with project references as packages should be treated independently
https://github.com/RyanCavanaugh/project-references-demo/

@Zver64
Copy link
Author

Zver64 commented Apr 15, 2022

Yes, I can change the tsconfig in the every project that I am going to work on by duplicating packages's names into tsconfig's paths, but I want to avoid doing it as it requires to declare the names twice: in the package and in the config.
From what I see in the project that you have provided: it doesn't seem to have workspaces and hence it doesn't seem to be related to my case. I should have mentioned that I don't have any errors after refactoring the imports the way I want them to be.

@IllusionMH
Copy link
Contributor

You should raise request to support yarn workspaces in TS repo.

In your current configuration there is no indications for TS (which provides these import suggestions for VS Code) to changes how modules are imported.

@mjbvz mjbvz transferred this issue from microsoft/vscode Apr 15, 2022
@mjbvz mjbvz removed their assignment Apr 15, 2022
@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Apr 18, 2022
@RyanCavanaugh
Copy link
Member

Auto-import has to strike a balance between not over-aggressively offering up everything in node_modules (which is bad in other ways, and very bad for performance) and finding things which look like they're "in your project", plus figuring out which name you "wanted" to give it - in most cases, there are many different ways which all appear correct, and it's effectively a guess.

In this situation there's just not enough available evidence to TS that package-a-name is a name that should be found and preferred over the packages/package-a name. You can use configuration to address this.

@cbdeveloper
Copy link

cbdeveloper commented May 19, 2022

Hey, @RyanCavanaugh I have a weird issue that might be related to this.

I'm using yarn workspaces.

The name of the packages in their package.json are: @packages/admin , @packages/common, etc.

packages
  admin
    package.json // Has a dependency to @packages/common
  common
  user
package.json

When I'm working in the admin package files, I'd like to get auto import suggestions from the @packages/common files.

But the weird thing is that the auto import suggestion for the @packages/common are only working for types, and not for actual variables and constants.

Obs 1: If I write the import manually, it works fine.
Obs 2: If there is already an import to @package/common in the file, it will suggest other variables from that import.

Any ideas on what is happening and how could I fix it?

Also: do I need a tsconfig.json in the root of my workspace? Currently I only have one per package.

Thanks


UPDATE

Not sure why I was having this issue, but now it's gone.

@theKashey
Copy link

Still, there is a problem with auto-importing a package that is part of the same workspace but not yet a dependency of a current package.

Importing a known symbol works perfectly without composite, but once proper package references are established auto-import seems to go into some "isolated" mode.

#39778

@github-actions
Copy link

github-actions bot commented Jun 8, 2023

This issue has been marked as 'Not a Defect' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

6 participants