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

前端 未结 9 783
慢半拍i
慢半拍i 2020-12-13 02:55

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

{
    \"terminal.integrated.shell.windows\": \"C:\\\\         


        
相关标签:
9条回答
  • 2020-12-13 03:17

    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"
        }
    }
    
    0 讨论(0)
  • 2020-12-13 03:19

    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:

    @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 讨论(0)
  • 2020-12-13 03:29

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

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

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

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

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

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

    0 讨论(0)
  • 2020-12-13 03:33

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

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

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

    0 讨论(0)
  • 2020-12-13 03:34

    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:

    "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 讨论(0)
  • 2020-12-13 03:34

    This works for me with Visual Studio Code version shown below

    "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

    0 讨论(0)
提交回复
热议问题