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

问题描述 投票:11回答:8

我试图将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中?

bash shell visual-studio-code msys2
8个回答
10
投票

Original but not working 100% (accepted as the answer)

这将正确启动MSYS2 bash shell,以便执行.bash_login:

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

Edit

原来的答案似乎在当时有效,但当我尝试在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
fi

22
投票

要禁止从当前目录更改工作目录,请将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 for installing and managing a "Command Prompt Here" folder context menu item.虽然MSYS2继承了Cygwin的环境变量,但它实际上并没有继承命令本身。


7
投票

这对我有用:

{
    "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
投票

我得到了这个工作

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

1
投票

accepted answer适用于我,但使用zsh shell("terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\zsh.exe"),事情比使用bash慢得多。我不知道为什么,但使用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",
],

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

为我工作。


0
投票

当调用shell执行任务时,vs代码将/d /c参数添加到shell可执行文件中,就像它对cmd.exe一样(参见cli中的cmd /?)。当然MSYS2 bash不会将/d /c解释为有效的参数,这就是为什么它会回答/d: No such file or directory,因为它试图将/d解释为命令。我刚开始挖掘VSCode,我觉得用户配置中缺少一些配置参数,告诉vscode调用msys2_shell.cmd(即用-c arg)执行任务的好方法(没有用于集成/交互式shell的pb,因为没有arg在调用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

0
投票

这适用于我使用下面显示的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 version

© www.soinside.com 2019 - 2024. All rights reserved.