gpt4 book ai didi

How do I integrate MSYS2 shell into Visual studio code on Window?(如何将MSYS2外壳集成到Windows上的Visual Studio代码中?)

转载 作者:bug小助手 更新时间:2023-10-24 22:48:22 28 4
gpt4 key购买 nike



I tried to integrate MSYS2 shell into Visual studio Code integrated terminal. Here's my user settings:

我尝试将MSYS2外壳集成到可视化工作室代码集成终端中。以下是我的用户设置:



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


However, I ran into a problem where --login changes the current working directory to Windows home. I want the current directory to be at the root of my workspace.

但是,我遇到了一个问题--LOGIN将当前工作目录更改为Windows主目录。我希望当前目录位于我的工作区的根目录中。



My further attempt was I tried add a flag -c 'cd ${workspaceRoot}'. However, the bash would crashed on start. I could properly get to current directory by removing --login, but without login mode, all other shell command (ls, cd, etc) are not available.

我的进一步尝试是尝试添加一个标志-c‘cd${workspaceRoot}’。然而,狂欢在START时就会崩溃。我可以通过删除--LOGIN正确地进入当前目录,但是如果没有登录模式,所有其他的外壳命令(ls、cd等)都不可用。



How do I properly integrate MSYS2 shell into my vscode?

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


更多回答

For everybody checking out the answers: since Apr 2021, the setting used to be terminal.integrated.shell.windows is pointing to terminal.integrated.profiles.windows.

对于正在查看答案的每个人来说:自2021年4月以来,该设置曾经是Terminal.Integrated.shell.windows指向Terminal.Integrated.profiles.windows。

优秀答案推荐

Answers here are from the old method which is now (July 2021) deprecated in VSCode
the new suggested way, add this to settings.json:

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


reference: integrated-terminal#_configuring-profiles

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



To inhibit the working directory change from your current directory, set the CHERE_INVOKING environment variable to a non-empty value:

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



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


In MSYS login scripts, setting the CHERE_INVOKING variable serves only to prevent a shell from doing a cd "${HOME}", and nothing else.

在MSYS登录脚本中,设置CHERE_INVOKING变量只是为了防止外壳执行CD“${HOME}”,而不是执行其他操作。



If you require a MinGW toolchain, set the MSYSTEM environment variable to select a toolchain. Recognized values are MSYS (default), MINGW32 or MINGW64.

如果需要MinGW工具链,请设置MSYSTEM环境变量以选择工具链。可识别的值为MSYS(默认)、MINGW32或MINGW64。



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


In full, the VS Code settings might look like so:

总而言之,VS代码设置可能如下所示:



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





To provide some context on the very cryptic nomenclature of CHERE_INVOKING: chere is apparently a Cygwin command for installing and managing a "Command Prompt Here" folder context menu item. Although MSYS2 inherits the environment variable from Cygwin, it doesn't actually inherit the command itself.

为了提供一些关于CHERE_INVOKING非常神秘的命名的上下文:chere显然是一个Cygwin命令,用于安装和管理“Command Prompt Here”文件夹上下文菜单项。尽管MSYS2从Cygwin继承了环境变量,但它实际上并不继承命令本身。



#Original but not working 100% (accepted as the answer)
This will start the MSYS2 bash shell properly so your .bash_login gets executed:

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


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

#Edit
The original answer seemed to work at the time, but when I tried to start using tasks in VSCode it was clearly not working. Trying to run a task that simply called make all caused the following error:

#编辑原始答案在当时似乎有效,但当我尝试在VSCode中开始使用任务时,它显然不起作用。尝试运行名为Make All的任务会导致以下错误:



/usr/bin/bash: /d: No such file or directory

The terminal process terminated with exit code: 127



From the other answers, using "terminal.integrated.shellArgs.windows": ["--login", "-i"] got the almost the correct environment (MSYS instead of MINGW64) but started in the wrong directory, and "terminal.integrated.shellArgs.windows": ["-lic", "cd $OLDPWD; exec bash"] started in the correct directory with the correct environment but could not run tasks.

在其他答案中,使用“Terminal.Integrated.shellArgs.windows”:[“--LOGIN”,“-i”]获得了几乎正确的环境(MSYS而不是MINGW64),但在错误的目录中启动,并且使用“Terminal.Integrated.shellArgs.windows”:[“-ic”,“CD$OLDPWD;exec bash”]在具有正确环境的正确目录中启动,但无法运行任务。


I came up with this solution that so far seems to work fine.

In VSCode settings:

我想出了这个解决方案,到目前为止似乎运行得很好。在VSCode设置中:


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

In .bashrc:

在.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


This works for me:

这对我很管用:



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


When calling the shell to execute task, vs code add /d /c arguments to the shell executable as it would for cmd.exe (see cmd /? in cli). Of course MSYS2 bash don't interpret /d /c as valid params, that s why it answers /d: No such file or directory as it try to interpret /d as a command.
I ve just began to dig in VSCode and I have the feeling some config parameter is missing in user configuration to tell vscode the good way to call msys2_shell.cmd (ie with -c arg) for task execution (no pb for integrated/interactive shell as no arg is passed when bash is invoked).
Anyway, to overcome the problem it is possible to edit msys2_shell.cmd to trap \d \c and call bash with -c:

当调用外壳来执行任务时,VS代码向外壳可执行文件添加/d/c参数,就像对cmd.exe一样(参见cmd/?在CLI中)。当然,MSYS2 bash不会将/d/c解释为有效参数,这是S回答/d原因:没有这样的文件或目录,因为它试图将/d解释为命令。我刚刚开始研究VSCode,我感觉在用户配置中缺少一些配置参数来告诉vscode调用msys2_shell.cmd(即使用-c arg)来执行任务的好方法(对于集成/交互外壳,没有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


The accepted answer 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 greatly helped:

公认的答案对我有效,但使用zsh外壳(“minal.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",
],


This works for me with Visual Studio Code version shown below

这对我来说适用于如下所示的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

VS代码版本



I got this to work

我让这招起作用了



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


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


Worked for me.

对我很管用。



Press F1 + write settings.json
then, at the end of the {} add the next lines:

按F1+WRITE settings.json,然后在{}末尾添加以下行:



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


It would looks like this: (Obviously, the settings.json could be different to the next example)

它看起来如下所示:(显然,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"]


Keep in mind that you have to add a comma , before to paste the suggested lines.

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



This question is more than a year old, but I was still having it.
Just thought I'd share my solution, since it's not on here.

这个问题已经提了一年多了,但我仍然有这个问题。我只是想分享我的解决方案,因为它不在这里。


The accepted answer works, and allows tasks to work too, but when I try getting the vscode-cpptools debugger to launch GDB, GDB will always crash with the following error unless I remove --login:

公认的答案是有效的,并且允许任务也工作,但是当我尝试让vscode-cpptools调试器启动gdb时,gdb将总是崩溃,并显示以下错误,除非我删除--登录:


ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". During startup program exited with code 0xc0000135

To fix this, there were two options, the first was to add "source": "Git Bash" to the terminal profile, but this requires you to have git for windows installed.

要解决这个问题,有两种选择,第一种是将“source”:“Git Bash”添加到终端配置文件中,但这需要您安装用于Windows的Git。


The second option was simpler, and added the path variables without using --login:

第二个选项更简单,它添加了PATH变量,没有使用--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",

Hope this helps.

希望这能帮上忙。



In your settings.json you can enter:

在settings.json中,您可以输入:


"terminal.integrated.profiles.windows": {
"MSYS2 CLANG64": {
"path": ["C:\\msys64\\usr\\bin\\bash.exe"],
"args": ["-l"],
"env": { "MSYSTEM": "CLANG64" },
"overrideName": true
},
"MSYS2 CLANG32": {
"path": ["C:\\msys64\\usr\\bin\\bash.exe"],
"args": ["-l"],
"env": { "MSYSTEM": "CLANG32" },
"overrideName": true
},
"MYSYS2 MINGW64": {
"path": ["C:\\msys64\\usr\\bin\\bash.exe"],
"args": ["-l"],
"env": { "MSYSTEM": "MINGW64" },
"overrideName": true
},
"MYSYS2 MINGW32": {
"path": ["C:\\msys64\\usr\\bin\\bash.exe"],
"args": ["-l"],
"env": { "MSYSTEM": "MINGW32" },
"overrideName": true
}
}

更多回答

To add on to this, the text that you should add to the settings.json file is the "MSYS2" text and downward.

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

I usually start msys2 using -use-full-path argument so my PATH variable stays intact.

我通常使用-Use-Full-Path参数启动msys2,因此我的PATH变量保持不变。

Note that while this answer can successfully launch a basic msys2/mingw shell from within vscode integrated terminal, it may not work when python venv activation is required. For me launching bash.exe directly is required for python projects using venv.

请注意,虽然这个答案可以从vscode集成终端中成功启动基本的msys2/mingw外壳,但当需要激活pythonvenv时,它可能不起作用。对我来说,使用venv的python项目需要直接启动bash.exe。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com