microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
160.48k stars 28.1k forks source link

npm.packageManager=auto incorrectly assumes yarn as package manager for running tasks #170101

Open Zelgadis87 opened 1 year ago

Zelgadis87 commented 1 year ago

Type: Bug

I have a fairly standard PNPM project, for which I have configured a few VSCode tasks:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "bar",
            "type": "npm",
            "script": "bar"
        }
    ]
}

When VSCode is set with npm.packageManager=auto, it incorrectly assumes that tasks should be run using yarn. Since the project uses corepack and specifies a packageManager in package.json, using yarn results in an error:

* Executing task in folder foo: yarn run bar 
Usage Error: This project is configured to use pnpm
$ yarn ...
 *  The terminal process "cmd.exe /d /c yarn run bar" terminated with exit code: 1. 

This for me doesn't make much sense, because:

If the auto detection for PNPM failed, I'd expect to at least fallback to NPM. Using npm.packageManager=pnpm of course works fine.

VS Code version: Code - Insiders 1.75.0-insider (6ed4d436a9b6ae131732f4e2a723868231f4473a, 2022-12-23T05:21:57.802Z) OS version: Windows_NT x64 10.0.19045 Modes: Sandboxed: Yes

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i5-7600 CPU @ 3.50GHz (4 x 3504)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off| |Load (avg)|undefined| |Memory (System)|15.95GB (5.19GB free)| |Process Argv|..\\cronicle\\ --crash-reporter-id 2a3507f6-d0a2-424d-8827-2b26df7c63ff| |Screen Reader|no| |VM|0%|
Extensions (5) Extension|Author (truncated)|Version ---|---|--- vscode-eslint|dba|2.2.6 gitlens-insiders|eam|2022.12.2304 intellicode-api-usage-examples|Vis|0.2.6 vscodeintellicode|Vis|1.2.29 markdown-all-in-one|yzh|3.5.0
A/B Experiments ``` vsliv695:30137379 vsins829:30139715 vsliv368:30146709 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 vslsvsres303:30308271 pythonvspyl392:30422396 pythontb:30258533 vsc_aacf:30263846 pythonptprofiler:30281269 vsdfh931:30280409 vshan820:30294714 pythondataviewer:30285072 vscod805cf:30301675 bridge0708:30335490 bridge0723:30353136 cmake_vspar411:30581797 vsaa593cf:30376535 pythonvs932:30404738 cppdebug:30492333 vsclangdf:30492506 c4g48928:30535728 dsvsc012cf:30540253 pynewext54:30618038 pylantcb52:30590116 pyindex848:30611229 nodejswelcome1:30587009 pyind779:30611226 vscrpc:30624061 pythonsymbol12cf:30622697 fim-prod:30623723 ```
fdesforges commented 1 year ago

I have the same issue using npm workspaces, and opening vscode not from the root of the monorepo.

running debug task from package.json try to use yarn.

npm.packageManager=npm solve the issue

Vscode 1.74.3

lawvs commented 1 year ago

Blame to

https://github.com/microsoft/vscode/blob/784bbdab8f28a951e9b327aba1181641a1a282d9/extensions/npm/src/preferred-pm.ts#L46-L48

The find-yarn-workspace-root package assumes that it is being used in a yarn project and only checks for the packageJson.workspaces field.

However, this field also exists in pnpm and npm projects.

As a result, this means that all pnpm projects will be detected as yarn projects.

Also related to https://github.com/microsoft/vscode/issues/139654

wscales10 commented 11 months ago

I just ran into this. I expect it is the same issue as #159465. The workaround I used was changing

"type": "npm",
"script": "compile",

to

"type": "shell",
"command": "npm",
"args": ["run", "compile"],
rotu commented 10 months ago

Yeah this is pretty insane behavior.

It would be very sensible to instead detect the package manager using the project's package.json "packageManager" key if present.