2017-08-23 2 views
4

J'ai essayé d'intégrer le shell MSYS2 dans le terminal Visual Studio Code intégré. Voici mes paramètres utilisateur:Comment intégrer le shell MSYS2 dans le code Visual Studio sous Windows?

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

Cependant, je suis tombé sur un problème où --login modifie le répertoire de travail à la maison de Windows. Je veux que le répertoire courant soit à la racine de mon espace de travail. J'ai essayé d'ajouter un drapeau -c 'cd ${workspaceRoot}'. Cependant, le bash s'est écrasé au démarrage. Je pourrais correctement accéder 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 le shell MSYS2 dans mon vscode?

Répondre

2

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

Cela va démarrer le shell bash MSYS2 proprement votre .bash_login est exécuté:

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

Modifier

La réponse originale semblait fonctionner à l'époque, mais quand j'ai essayé de commencer à utiliser des tâches dans VSCode, cela ne fonctionnait pas. Essayer d'exécuter une tâche que simplement appelé faire tous a causé l'erreur suivante:

/usr/bin/bash: /d: No such file or directory
The terminal process terminated with exit code: 127

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

Je suis venu avec cette solution qui, jusqu'à présent, semble fonctionner correctement.
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" 
}, 

En .bashrc:

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

Je commence habituellement à l'aide msys2 argument '-use-plein path' donc ma variable PATH reste intacte. –

1

J'ai eu que cela fonctionne

{ 
    "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe", 
    "terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"], 
} 
0
{ 
    "terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\sh.exe", 
    "terminal.integrated.shellArgs.windows": ["--login", "-i"] 
} 

a marché pour moi.

1

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" 
    } 
} 
6

pour inhiber le changement de répertoire de travail à partir de votre répertoire en cours, définissez la variable d'environnement CHERE_INVOKING à une valeur non vide:

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

Dans les scripts de connexion MSYS , la définition de la variable CHERE_INVOKING 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", 
    }, 

En pleine, les paramètres de code VS pourrait 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 contexte sur la nomenclature très cryptique de CHERE_INVOKING: chere est apparemment un Cygwin command for installing and managing a "Command Prompt Here" folder context menu item. Bien que MSYS2 hérite de la variable d'environnement de Cygwin, il n'hérite pas réellement la commande elle-même.

+0

Cela fonctionne vraiment bien pour moi. Je vous remercie. – realhu

0

Lors de l'appel du shell pour exécuter la tâche, vs code ajouter /d /c arguments à l'exécutable shell comme il le ferait pour cmd.exe (voir cmd /? en cli). Bien sûr, MSYS2 bash n'interprète pas /d /c comme paramètres valides, c'est pourquoi il répond /d: No such file or directory en essayant d'interpréter /d comme une commande. Je viens de commencer à creuser en VSCode et j'ai le sentiment que certains paramètres config manquent dans la configuration utilisateur pour dire à vscode le bon moyen d'appeler msys2_shell.cmd (ie avec -c arg) pour l'exécution de la tâche (pas de pb pour le shell intégré/interactif arg est passé lorsque bash est invoqué). Quoi qu'il en soit, pour surmonter le problème, il est possible d'éditer msys2_shell.cmd pour piéger \d \c et appeler 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