microsoft / vscode

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

npm scripts do not run in integrated terminal when it has init script for cmd and powershell #177550

Open Maxim-Mazurok opened 1 year ago

Maxim-Mazurok commented 1 year ago

Type: Bug

  1. In order to run nvs/nvm every time terminal is open I've added these scripts:

    • .vscode\terminal\bash-init.sh:

      #!/bin/bash
      # inspired by https://stackoverflow.com/a/60438403/4536543
      source ~/.bashrc # restore default terminal behaviour
      if command -v nvm &> /dev/null
      then
      nvm i
      else
      echo "WARNING: nvm (Node Version Manager) can't be found, install it from https://github.com/nvm-sh/nvm#installing-and-updating and restart VS Code"
      fi
    • .vscode\terminal\cmd-init.cmd:

      @echo off
      where /q nvs
      if %ERRORLEVEL% NEQ 0 (
      :: echo without quotes, see https://stackoverflow.com/a/41274461/4536543
      echo|set /p="WARNING: nvs (Node Version Switcher) can't be found, install it from https://github.com/jasongin/nvs#setup and restart VS Code"
      exit /b 1
      )
      
      call nvs install
      call nvs use
    • .vscode\terminal\powershell-init.ps1:

      Function Test-CommandExists # inspired by https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
      {
      Param ($command)
      $oldPreference = $ErrorActionPreference
      $ErrorActionPreference = 'stop'
      try {if(Get-Command $command){RETURN $true}}
      Catch {RETURN $false}
      Finally {$ErrorActionPreference=$oldPreference}
      } #end function test-CommandExists
      
      If(Test-CommandExists nvs){
      nvs install
      nvs use
      } else {
      # echo without quotes, see https://stackoverflow.com/a/41274461/4536543
      Write-Host "WARNING: nvs (Node Version Switcher) can't be found, install it from https://github.com/jasongin/nvs#setup and restart VS Code"
      }

      And added this to my .vscode/settings.json:

      {
      // inspired by https://stackoverflow.com/a/70029070/4536543
      "terminal.integrated.profiles.linux": {
      "My bash": {
        "path": "bash",
        "args": ["--init-file", "${workspaceFolder}/.vscode/terminal/bash-init.sh"]
      }
      },
      "terminal.integrated.defaultProfile.linux": "My bash",
      "terminal.integrated.profiles.windows": {
      "My PowerShell": {
        "path": "pwsh.exe",
        "args": ["-noexit", "-file", "${workspaceFolder}\\.vscode\\terminal\\powershell-init.ps1"]
      },
      "My cmd": {
        "path": "cmd.exe",
        "args": ["/K", "${workspaceFolder}\\.vscode\\terminal\\cmd-init.cmd"]
      },
      "Git Bash": null // remove as we don't use it, see https://code.visualstudio.com/docs/terminal/profiles#_removing-builtin-profiles
      },
      "terminal.integrated.defaultProfile.windows": "My cmd", // use cmd by default because it's easier to set env vars in it than in powershell
      }
  2. Now I'm trying to run some npm scripts from package.json, but it doesn't work as expected:
    • When My cmd or My PowerShell are used - they run the init scripts, but do not execute the actual npm script after that, integrated terminal still thinkgs that it's running, as indicated by spinning circle.
    • When My Bash is used in WSL - the actual npm script runs, however the init script doesn't seem to run in that case.

How can I have the best of both worlds? Have my init scripts AND npm scripts run when I run them from explorer?

VS Code version: Code 1.76.2 (ee2b180d582a7f601fa6ecfdad8d9fd269ab1884, 2023-03-14T17:55:54.936Z) OS version: Windows_NT x64 10.0.19044 Modes: Sandboxed: Yes Remote OS version: Linux x64 5.15.90.1-microsoft-standard-WSL2

System Info |Item|Value| |---|---| |CPUs|12th Gen Intel(R) Core(TM) i9-12900H (20 x 2918)| |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)|63.68GB (37.51GB free)| |Process Argv|--crash-reporter-id 97a0b52e-337e-4352-b480-06a5f2d9f058| |Screen Reader|no| |VM|0%| |Item|Value| |---|---| |Remote|WSL: Ubuntu| |OS|Linux x64 5.15.90.1-microsoft-standard-WSL2| |CPUs|12th Gen Intel(R) Core(TM) i9-12900H (20 x 2918)| |Memory (System)|31.19GB (29.53GB free)| |VM|0%|
Extensions (43) Extension|Author (truncated)|Version ---|---|--- overtype|ada|0.2.0 vscode-nginx-conf|ahm|0.1.3 toml|be5|0.6.0 unique-lines|bib|1.0.0 npm-intellisense|chr|1.4.4 flip|Chr|0.0.4 vcard|cst|1.0.1 vscode-eslint|dba|2.4.0 xml|Dot|2.5.1 gitlens|eam|13.4.0 EditorConfig|Edi|0.16.4 prettier-vscode|esb|9.10.4 nunjucks-template|ese|0.5.1 vscode-jest-runner|fir|0.4.60 markmap-vscode|ger|0.0.17 copilot|Git|1.78.9758 todo-tree|Gru|0.0.224 ts-type-expand|kim|1.0.1 render-crlf|med|1.6.1 dotenv|mik|1.0.1 vscode-docker|ms-|1.24.0 isort|ms-|2022.8.0 python|ms-|2023.4.1 jupyter|ms-|2023.2.1200692131 jupyter-keymap|ms-|1.1.0 jupyter-renderers|ms-|1.0.15 vscode-jupyter-cell-tags|ms-|0.1.8 vscode-jupyter-slideshow|ms-|0.1.5 remote-wsl|ms-|0.76.1 live-server|ms-|0.4.7 vsliveshare|ms-|1.0.5834 copy-json-path|nid|0.3.0 angular-console|nrw|17.32.0 vetur|oct|0.37.3 vscode-thunder-client|ran|2.5.1 nginx-formatter|ray|0.0.13 vscode-xml|red|0.24.0 code-spell-checker|str|2.20.1 jest-snapshot-language-support|tle|1.1.1 es6-string-html|Tob|2.12.1 sort-lines|Tyr|1.9.1 vscode-wakatime|Wak|24.0.7 markdown-all-in-one|yzh|3.5.0
A/B Experiments ``` vsliv368:30146709 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 vswsl492:30256859 vslsvsres303:30308271 pythonvspyl392:30443607 vserr242cf:30382550 pythontb:30283811 vsjup518:30340749 pythonptprofiler:30281270 vsdfh931:30280409 vshan820:30294714 vstes263cf:30335440 vscorecescf:30445987 pythondataviewer:30285071 vscod805cf:30301675 binariesv615:30325510 bridge0708:30335490 bridge0723:30353136 cmake_vspar411:30581797 vsaa593:30376534 pythonvs932:30410667 cppdebug:30492333 vsclangdf:30486550 c4g48928:30535728 dsvsc012:30540252 pynewext54:30681850 azure-dev_surveyone:30548225 pyindex848:30662994 nodejswelcome1:30587005 2e4cg342:30602488 pyind779:30671433 f6dab269:30613381 pythonsymbol12:30671437 2i9eh265:30646982 vsctsb:30677850 functionswalk:30687959 pythonms35:30686772 ```
Maxim-Mazurok commented 1 year ago

Workaround to run actual npm scripts without init scripts for cmd and ps:

{
  "terminal.integrated.automationProfile.windows": { // workaround for https://github.com/microsoft/vscode/issues/177550
    "path": "cmd.exe"
  },
}
meganrogge commented 1 year ago

Yeah, the npm scripts run from the package.json assume terminal profiles without startup scripts will be used. I assume what's happening is in your init script, you run nvm then when you click run script from the package.json, we run nvm so it doesn't work

Maxim-Mazurok commented 1 year ago

Well yeah basically it would be great to add support for startup scripts :)

Not sure what you mean there about nvm tho... What I expect it this:

Hope this makes sense, thank you for looking into this! Quite a niche request but would be super useful and make node version management in big corporate teams easier (so do GitHub Codespaces, IKR 😀)