web-dev-qa-db-fra.com

Comment intégrer MSYS2 Shell au code Visual studio sur Windows?

J'ai essayé d'intégrer MSYS2 Shell dans le terminal intégré Visual Studio Code. Voici mes paramètres utilisateur:

{
    "terminal.integrated.Shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": ["--login", "-i"]
}

Cependant, j'ai rencontré un problème où --login change le répertoire de travail actuel en Windows home. Je veux que le répertoire courant soit à la racine de mon espace de travail.

Ma nouvelle tentative a consisté à ajouter un indicateur -c 'cd ${workspaceRoot}'. Cependant, le bash s'est écrasé au démarrage. J'ai pu accéder correctement au répertoire actuel en supprimant --login, mais sans mode de connexion, toutes les autres commandes Shell (ls, cd, etc.) ne sont pas disponibles.

Comment intégrer correctement MSYS2 Shell dans mon vscode?

13
pbeta

Original mais ne fonctionne pas à 100% (accepté comme réponse)

Cela démarrera correctement le shell bash MSYS2 pour que votre . Bash_login soit exécuté:

"terminal.integrated.Shell.windows": "C:\\msys64\\msys2_Shell.cmd",
"terminal.integrated.shellArgs.windows": ["-defterm", "-mingw64", "-no-start", "-here"]

Éditer

La réponse originale semblait fonctionner à l'époque, mais lorsque j'ai essayé de commencer à utiliser des tâches dans VSCode, cela ne fonctionnait clairement pas. Essayer d'exécuter une tâche qui appelait simplement make all a provoqué l'erreur suivante:

/ usr/bin/bash:/d: aucun fichier ou répertoire de ce type
Le processus terminal s'est terminé avec le code de sortie: 127

À partir des autres réponses, en utilisant "terminal.integrated.shellArgs.windows": ["--login", "-i"] a obtenu l'environnement presque correct (MSYS au lieu de MINGW64) mais a commencé dans le mauvais répertoire, et "terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"] a commencé dans le bon répertoire avec l'environnement correct mais n'a pas pu exécuter les tâches.

J'ai trouvé cette solution qui, jusqu'à présent, semble bien fonctionner.
Dans les paramètres VSCode:

"terminal.integrated.Shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
"terminal.integrated.env.windows":
{
    "MSYSTEM": "MINGW64",
    //"MSYS2_PATH_TYPE": "inherit",
    "MSVSCODE": "1"
},

Dans . Bashrc:

if [ ! -z "$MSVSCODE" ]; then
    unset MSVSCODE
    source /etc/profile
    cd $OLDPWD
fi
10
cola

Pour empêcher la modification du répertoire de travail à partir de votre répertoire actuel, définissez le CHERE_INVOKING variable d'environnement à une valeur non vide:

    "terminal.integrated.env.windows": {
        "CHERE_INVOKING": "1"
    },

Dans les scripts de connexion MSYS, la définition de CHERE_INVOKING la variable sert uniquement à empêcher un shell de faire un cd "${HOME}", et rien d'autre.

Si vous avez besoin d'une chaîne d'outils MinGW, définissez la variable d'environnement MSYSTEM pour sélectionner une chaîne d'outils. Les valeurs reconnues sont MSYS (par défaut), MINGW32 ou MINGW64.

    "terminal.integrated.env.windows": {
        "MSYSTEM": "MINGW64",
    },

Dans leur intégralité, les paramètres du code VS pourraient ressembler à ceci:

{
    "terminal.integrated.Shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": [
        "--login",
    ],
    "terminal.integrated.env.windows": {
        "CHERE_INVOKING": "1",
        "MSYSTEM": "MINGW64",
    },
}

Pour fournir un certain contexte sur la nomenclature très cryptique de CHERE_INVOKING: chere est apparemment un commande Cygwin pour installer et gérer un élément de menu contextuel du dossier "Command Prompt Here". Bien que MSYS2 hérite de la variable d'environnement de Cygwin, il ne le fait pas réellement héritez de la commande elle-même.

23
Jānis Rūcis

Cela fonctionne pour moi:

{
    "terminal.integrated.Shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": ["--login", "-i"],
    "terminal.integrated.env.windows":
    {
        "MSYSTEM": "MINGW64",
        "CHERE_INVOKING":"1"
    }
}
7
Rodrigo Hernandez

Je l'ai fait fonctionner

{
    "terminal.integrated.Shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"],
}
1
user2241515

réponse acceptée fonctionne pour moi mais avec zsh Shell ("terminal.integrated.Shell.windows": "C:\\msys64\\usr\\bin\\zsh.exe"), les choses sont beaucoup plus lentes qu'avec bash. Je ne sais pas pourquoi mais l'utilisation de connecteur terminal cygwin/msys de ConEm a grandement aidé:

"terminal.integrated.Shell.windows": "C:\\conemu\\ConEmu\\conemu-msys2-64.exe",
"terminal.integrated.env.windows": {
    "CHERE_INVOKING": "1",
    "MSYSTEM": "MINGW64",
    "MSYS2_PATH_TYPE": "inherit",
},
"terminal.integrated.shellArgs.windows": [
    "/usr/bin/zsh",
    "-l",
    "-i",
],
1
Borek Bernard
{
    "terminal.integrated.Shell.windows": "C:\\msys64\\usr\\bin\\sh.exe",
    "terminal.integrated.shellArgs.windows": ["--login", "-i"]
}

A travaillé pour moi.

0
n370

Cela fonctionne pour moi avec la version de Visual Studio Code illustrée ci-dessous

"terminal.integrated.Shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": ["--login", "-i"],
"terminal.integrated.env.windows":
{
    "MSYSTEM": "MINGW64",
    "CHERE_INVOKING":"1",
    "PATH" : "/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/"         
},

version VS Code

0
Sy Tran

Lors de l'appel du shell pour exécuter la tâche, vs code ajoute /d /c arguments à l'exécutable Shell comme pour cmd.exe (voir cmd /? in cli). Bien sûr, MSYS2 bash n'interprète pas /d /c en tant que paramètres valides, c'est pourquoi il répond /d: No such file or directory comme il essaie d'interpréter /d comme commande. Je viens de commencer à creuser dans VSCode et j'ai le sentiment qu'il manque un paramètre de configuration dans la configuration utilisateur pour dire à vscode la bonne façon d'appeler msys2_Shell.cmd (c'est-à-dire avec -c arg) pour l'exécution de la tâche (pas de pb pour le shell intégré/interactif car aucun argument n'est passé lorsque bash est invoqué). Quoi qu'il en soit, pour surmonter le problème, il est possible de modifier msys2_Shell.cmd piéger \d \c et appelez bash avec -c:

@echo off
setlocal

rem usefull get what VSCode pass 
rem echo "given params %1 %2 %3 %4 %5 %6 %7 %8 %9"

set "WD=%__CD__%"
if NOT EXIST "%WD%msys-2.0.dll" set "WD=%~dp0usr\bin\"

rem To activate windows native symlinks uncomment next line
rem set MSYS=winsymlinks:nativestrict

rem Set debugging program for errors
rem set MSYS=error_start:%WD%../../mingw64/bin/qtcreator.exe^|-debug^|^<process-id^>

rem To export full current PATH from environment into MSYS2 use '-use-full-path' parameter
rem or uncomment next line
rem set MSYS2_PATH_TYPE=inherit

:checkparams
rem Help option
if "x%~1" == "x-help" (
  call :printhelp "%~nx0"
  exit /b %ERRORLEVEL%
)
if "x%~1" == "x--help" (
  call :printhelp "%~nx0"
  exit /b %ERRORLEVEL%
)
if "x%~1" == "x-?" (
  call :printhelp "%~nx0"
  exit /b %ERRORLEVEL%
)
if "x%~1" == "x/?" (
  call :printhelp "%~nx0"
  exit /b %ERRORLEVEL%
)
rem Shell types
if "x%~1" == "x-msys" shift& set MSYSTEM=MSYS& goto :checkparams
if "x%~1" == "x-msys2" shift& set MSYSTEM=MSYS& goto :checkparams
if "x%~1" == "x-mingw32" shift& set MSYSTEM=MINGW32& goto :checkparams
if "x%~1" == "x-mingw64" shift& set MSYSTEM=MINGW64& goto :checkparams
if "x%~1" == "x-mingw" shift& (if exist "%WD%..\..\mingw64" (set MSYSTEM=MINGW64) else (set MSYSTEM=MINGW32))& goto :checkparams
rem Console types
if "x%~1" == "x-mintty" shift& set MSYSCON=mintty.exe& goto :checkparams
if "x%~1" == "x-conemu" shift& set MSYSCON=conemu& goto :checkparams
if "x%~1" == "x-defterm" shift& set MSYSCON=defterm& goto :checkparams
rem Other parameters
if "x%~1" == "x-full-path" shift& set MSYS2_PATH_TYPE=inherit& goto :checkparams
if "x%~1" == "x-use-full-path" shift& set MSYS2_PATH_TYPE=inherit& goto :checkparams
if "x%~1" == "x-here" shift& set CHERE_INVOKING=enabled_from_arguments& goto :checkparams
if "x%~1" == "x-where" (
  if "x%~2" == "x" (
    echo Working directory is not specified for -where parameter. 1>&2
    exit /b 2
  )
  cd /d "%~2" || (
    echo Cannot set specified working diretory "%~2". 1>&2
    exit /b 2
  )
  set CHERE_INVOKING=enabled_from_arguments
)& shift& shift& goto :checkparams
if "x%~1" == "x-no-start" shift& set MSYS2_NOSTART=yes& goto :checkparams

rem VS CODE  CMD.EXE argument PATCH
if "x%~1" == "x/d" (
    if "x%~2" == "x/c" (
        rem we got a vs code task issued
        echo "VSCODE_TASK detected"
        shift& shift
        set VSCODE_TASK=yes 
        goto :checkparams
    )
)& shift& goto :checkparams


rem Setup proper title
if "%MSYSTEM%" == "MINGW32" (
  set "CONTITLE=MinGW x32"
) else if "%MSYSTEM%" == "MINGW64" (
  set "CONTITLE=MinGW x64"
) else (
  set "CONTITLE=MSYS2 MSYS"
)

if "x%MSYSCON%" == "xmintty.exe" goto startmintty
if "x%MSYSCON%" == "xconemu" goto startconemu
if "x%MSYSCON%" == "xdefterm" goto startsh

if NOT EXIST "%WD%mintty.exe" goto startsh
set MSYSCON=mintty.exe
:startmintty
if not defined MSYS2_NOSTART (
  start "%CONTITLE%" "%WD%mintty" -i /msys2.ico -t "%CONTITLE%" /usr/bin/bash --login %1 %2 %3 %4 %5 %6 %7 %8 %9
) else (
  "%WD%mintty" -i /msys2.ico -t "%CONTITLE%" /usr/bin/bash --login %1 %2 %3 %4 %5 %6 %7 %8 %9
)
exit /b %ERRORLEVEL%

:startconemu
call :conemudetect || (
  echo ConEmu not found. Exiting. 1>&2
  exit /b 1
)
if not defined MSYS2_NOSTART (
  start "%CONTITLE%" "%ComEmuCommand%" /Here /Icon "%WD%..\..\msys2.ico" /cmd "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
) else (

  "%ComEmuCommand%" /Here /Icon "%WD%..\..\msys2.ico" /cmd "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
)
exit /b %ERRORLEVEL%

:startsh
set MSYSCON=
if not defined MSYS2_NOSTART (
  start "%CONTITLE%" "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
) else (
  rem integrated Shell called (interactive)
  if not defined VSCODE_TASK (
    "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
  ) else (
    rem Call bash with -c arg (execute the command in argument and quit)
    rem echo  "start  %WD%bash" --login -c "%1 %2 %3 %4 %5 %6 %7 %8 %9"
    "%WD%bash" --login -c "%1 %2 %3 %4 %5 %6 %7 %8 %9"
  )
)
exit /b %ERRORLEVEL%

:EOF
exit /b 0

:conemudetect
set ComEmuCommand=
if defined ConEmuDir (
  if exist "%ConEmuDir%\ConEmu64.exe" (
    set "ComEmuCommand=%ConEmuDir%\ConEmu64.exe"
    set MSYSCON=conemu64.exe
  ) else if exist "%ConEmuDir%\ConEmu.exe" (
    set "ComEmuCommand=%ConEmuDir%\ConEmu.exe"
    set MSYSCON=conemu.exe
  )
)
if not defined ComEmuCommand (
  ConEmu64.exe /Exit 2>nul && (
    set ComEmuCommand=ConEmu64.exe
    set MSYSCON=conemu64.exe
  ) || (
    ConEmu.exe /Exit 2>nul && (
      set ComEmuCommand=ConEmu.exe
      set MSYSCON=conemu.exe
    )
  )
)
if not defined ComEmuCommand (
  FOR /F "tokens=*" %%A IN ('reg.exe QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ConEmu64.exe" /ve 2^>nul ^| find "REG_SZ"') DO (
    set "ComEmuCommand=%%A"
  )
  if defined ComEmuCommand (
    call set "ComEmuCommand=%%ComEmuCommand:*REG_SZ    =%%"
    set MSYSCON=conemu64.exe
  ) else (
    FOR /F "tokens=*" %%A IN ('reg.exe QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ConEmu.exe" /ve 2^>nul ^| find "REG_SZ"') DO (
      set "ComEmuCommand=%%A"
    )
    if defined ComEmuCommand (
      call set "ComEmuCommand=%%ComEmuCommand:*REG_SZ    =%%"
      set MSYSCON=conemu.exe
    )
  )
)
if not defined ComEmuCommand exit /b 2
exit /b 0

:printhelp
echo Usage:
echo     %~1 [options] [bash parameters]
echo.
echo Options:
echo     -mingw32 ^| -mingw64 ^| -msys[2]   Set Shell type
echo     -defterm ^| -mintty ^| -conemu     Set terminal type
echo     -here                            Use current directory as working
echo                                      directory
echo     -where DIRECTORY                 Use specified DIRECTORY as working
echo                                      directory
echo     -[use-]full-path                 Use full currnent PATH variable
echo                                      instead of triming to minimal
echo     -no-start                        Do not use "start" command and
echo                                      return bash resulting errorcode as
echo                                      this batch file resulting errorcode
echo     -help ^| --help ^| -? ^| /?         Display this help and exit
echo.
echo Any parameter that cannot be treated as valid option and all
echo following parameters are passed as bash command parameters.
echo.
exit /b 0
0
Dje53