如何将 MSYS2 shell 集成到 Window 上的 Visual studio 代码中?

How do I integrate MSYS2 shell into Visual studio code on Window?

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

{
    "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 中?

我成功了

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

原始但未 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: No such file or directory
The terminal process terminated with exit code: 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
fi
{
    "terminal.integrated.shell.windows": "C:\msys64\usr\bin\sh.exe",
    "terminal.integrated.shellArgs.windows": ["--login", "-i"]
}

对我有用。

这对我有用:

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

要禁止从当前目录更改工作目录,请将 CHERE_INVOKING 环境变量设置为非空值:

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

在 MSYS 登录脚本中,设置 CHERE_INVOKING 变量仅用于防止 shell 执行 cd "${HOME}",没有其他作用。

如果您需要 MinGW 工具链,请将 MSYSTEM 环境变量设置为 select 工具链。可识别的值为 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 for installing and managing a "Command Prompt Here" folder context menu item. 虽然 MSYS2 从 Cygwin 继承了环境变量,但它实际上并没有继承命令本身。

当调用 shell 执行任务时,vs 代码将 /d /c 参数添加到 shell 可执行文件,就像 cmd.exe 一样(参见 cmd /?在命令行中)。当然 MSYS2 bash 不会将 /d /c 解释为有效参数,这就是为什么它在尝试将 /d 解释为命令时回答 /d: No such file or directory 的原因。 我刚刚开始研究 VSCode,我感觉用户配置中缺少一些配置参数来告诉 vscode 调用 msys2_shell.cmd 的好方法(即使用 -c arg) 用于任务执行(integrated/interactive shell 没有 pb,因为调用 bash 时没有传递 arg)。 无论如何,要克服这个问题,可以编辑 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

works for me but with zsh shell ("terminal.integrated.shell.windows": "C:\msys64\usr\bin\zsh.exe"), things are much slower than with bash. I don't know why but using ConEmu's cygwin/msys terminal connector 帮助很大:

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

这对我有用 Visual Studio 下面显示的代码版本

"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 version

按 F1 + 写入 settings.json 然后,在 {} 的末尾添加下一行:

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

它看起来像这样:(显然,settings.json 可能与下一个示例不同)

"telemetry.enableTelemetry": true,
"code-runner.saveFileBeforeRun": true,
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.runInTerminal": true,
"liveshare.audio.startCallOnShare": true,
"window.zoomLevel": 1,
"explorer.confirmDelete": false,
"C_Cpp.updateChannel": "Insiders",

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

请记住,在粘贴建议的行之前,您必须添加一个逗号。

这里的答案来自现在(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"
      }
    }
  },

参考:integrated-terminal#_configuring-profiles