如何将MSYS2 shell集成到Windows上的Visual Studio Code?

39

我尝试将MSYS2 shell集成到Visual studio Code的终端中。这是我的用户设置:

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

然而,我遇到了一个问题,--login 会将当前工作目录更改为Windows主目录。我希望当前目录在我的工作区根目录。

我进一步尝试添加一个标志 -c 'cd ${workspaceRoot}'。但是,bash 会在启动时崩溃。如果删除 --login,可以正确地获得当前目录,但是没有登录模式,所有其他 shell 命令(如lscd等)都不可用。

如何正确地将 MSYS2 shell 集成到我的 vscode 中?


1
对于所有查看答案的人:自2021年4月以来,用于terminal.integrated.shell.windows的设置已经指向terminal.integrated.profiles.windows - Abel Cheung
12个回答

42

以下答案均为旧方法(截止于2021年7月),现已在VSCode中停用。 新的建议方法是将以下内容添加到settings.json中:

"terminal.integrated.profiles.windows": {
    "PowerShell": {
      "source": "PowerShell",
      "icon": "terminal-powershell"
    },
    "Command Prompt": {
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "args": [],
      "icon": "terminal-cmd"
    },
    "Git Bash": {
      "source": "Git Bash"
    },
    "MSYS2": {
      "path": "C:\\msys64\\usr\\bin\\bash.exe",
      "args": [
        "--login",
        "-i"
      ],
      "env": {
        "MSYSTEM": "MINGW64",
        "CHERE_INVOKING": "1"
      }
    }
  },

参考:终端集成#_配置文件


3
此外,您应该添加到settings.json文件中的文本是“MSYS2”文本及以下内容。 - BSplitter

39
为了防止工作目录从当前目录更改,请将CHERE_INVOKING环境变量设置为非空值:
    "terminal.integrated.env.windows": {
        "CHERE_INVOKING": "1"
    },

在MSYS登录脚本中,设置CHERE_INVOKING变量仅用于防止shell执行cd "${HOME}",没有其他作用。
如果需要MinGW工具链,请将MSYSTEM环境变量设置为选择工具链。可识别的值为MSYS(默认)、MINGW32或MINGW64。
    "terminal.integrated.env.windows": {
        "MSYSTEM": "MINGW64",
    },

完整的 VS Code 设置可能如下所示:

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

为了解释“CHERE_INVOKING”这个非常晦涩的命名,chere显然是Cygwin命令,用于安装和管理“Command Prompt Here”文件夹上下文菜单项。虽然MSYS2从Cygwin继承了环境变量,但它实际上并没有继承该命令。

24

#原始但不能100%正常工作(被接受为答案) 这将正确启动MSYS2 bash shell,以便执行您的.bash_login

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

#编辑 原始答案在当时似乎是有效的,但当我尝试在VSCode中开始使用任务时,显然它并没有起作用。尝试运行一个简单调用make all的任务会导致以下错误:

/usr/bin/bash: /d: 没有那个文件或目录
终端进程以退出代码127终止

根据其他答案,使用"terminal.integrated.shellArgs.windows": ["--login", "-i"]可以获得几乎正确的环境(MSYS而不是MINGW64),但是它在错误的目录中启动,而"terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"]则在正确的目录中以正确的环境启动,但无法运行任务。

我想出了这个解决方案,到目前为止似乎工作正常。
在VSCode设置中:

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

.bashrc 文件中:
if [ ! -z "$MSVSCODE" ]; then
    unset MSVSCODE
    source /etc/profile
    cd "${OLDPWD}" # this has to be in quotes to handle file paths with spaces
fi

1
我通常使用“-use-full-path”参数启动msys2,这样我的PATH变量就保持不变。 - Bernhard Döbler

8
这对我很有效:

这对我来说可行:

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

1
当调用shell执行任务时,VS Code会像在cli中使用cmd /?一样将/d /c参数添加到shell可执行文件中。当然,MSYS2 bash不会将/d /c解释为有效参数,这就是为什么它会回答/d:没有这样的文件或目录,因为它试图将/d解释为命令。
我刚开始研究VSCode,我感觉缺少一些配置参数来告诉vscode以正确的方式调用msys2_shell.cmd(即带有-c参数)来执行任务(对于集成/交互式shell没有问题,因为调用bash时没有传递任何参数)。
无论如何,为了解决这个问题,可以编辑msys2_shell.cmd以捕获\d \c并使用-c调用bash:
@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

1
这个问题已经超过一年了,但我仍然遇到了它。只是想分享我的解决方案,因为这里没有提到。
接受的答案可以工作,并且也允许任务工作,但是当我尝试让vscode-cpptools调试器启动GDB时,除非我删除--login,否则GDB将始终崩溃并显示以下错误:
ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". During startup program exited with code 0xc0000135

为了解决这个问题,有两个选项,第一个是将 "source": "Git Bash" 添加到终端配置文件中,但这需要你已经安装了 Windows 版本的 Git。
第二个选项更简单,可以在不使用 --login 的情况下添加路径变量。
"terminal.integrated.profiles.windows":
{
    "MINGW64":
    {
        "path": "C:/msys64/usr/bin/bash.exe",
        "env":
        {
            "CHERE_INVOKING": "1",
            "MSYSTEM": "MINGW64",
            // THE FOLLOWING IS A HACK TO PLACE THE MSYS PATHS BEFORE THE WINDOWS ONES
            "PATH": "/usr/bin;/mingw64/bin;${env:PATH}"
        },
        "color": "terminal.ansiMagenta"
    }
},
"terminal.integrated.defaultProfile.windows": "MINGW64",

希望这能有所帮助。

1
这对我来说很有效,使用的是下面显示的Visual Studio Code版本。
"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/"         
},

VS Code版本


1

接受的答案对我有用,但在使用zsh shell ("terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\zsh.exe")时,速度比使用bash慢得多。我不知道为什么,但使用ConEmu的cygwin/msys终端连接器大大提高了速度:

"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",
],

0

我成功地做到了这件事

{
    "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"]
}

对我有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接