如何从 Visual Studio 2017(基于目录的项目)中的任意构建任务 运行 bash?

How to run bash from arbitrary build tasks in Visual Studio 2017 (directory-based project)?

我正在尝试启用 custom build tasks 以使用 bash 在 Visual Studio 2017 年构建基于 DocBook 的文档。我使用内置的 Windows 子系统 Linux in Windows 10. 我可以启动 cmd.exe 和 运行 dir,就像 MS 的例子一样。但是,我需要 运行 make ARGS 在 bash 中,这就是我失败的地方:

  '"bash"' is not recognized as an internal or external command,
  operable program or batch file.

我可以 运行 cmd.exe 交互地启动 bash 没有任何问题。但它在 Visual Studio 中失败了。 VS2017是否使用了一些特殊的环境?如何通过这些任务实现 运行 bash?我想我错过了一些非常简单和琐碎的东西,但我不知道在哪里看。

本质上,我想 运行 make all-html 命令针对我的 DocBook 源文件的路径。

这是一个最小的任务示例:

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskName": "Build all-html",
      "appliesTo": "*",
      "type": "command",
      "command": "bash"
    }
  ]
}

就在您的 link 中,文档给出了一个使用 cmd 列出输出的示例:您应该使用 ${env.COMSPEC} 来使用 cmd.exe,然后给出 bash 作为参数。

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskName": "Build all-html",
      "appliesTo": "*",
      "type": "command",
      "command": "${env.COMSPEC}",
      "args": [
        "bash"
      ]
    }
  ]
}

这个 运行 bash 在交互模式下,但是你可以像这样用 bash -c 给 bash 命令:bash -c 'echo "$PATH"'.

注意:这个 运行 bash 在你的解决方案的根目录中(你可以用 bash -c 'echo $PWD' 检查这个)。

命令不能像您期望的那样自然地找到 bash 的原因是因为 Visual Studio 是一个 32 位程序。这意味着它不会在您指定命令时使用 64 位 cmd.exe。

您可以通过打开 32 位命令 window C:\Windows\SysWOW64\cmd.exe 并从那里尝试 bash 命令来重现 Visual Studio 正在做的事情。

解决方案是通过 C:\Windows\Sysnative 虚拟文件夹直接调用 bash.exe

您可以找到 Sysnative 文件夹的 discussion/explanation 以及为什么需要 here in the official Bash on Windows GitHub

这是一个适合您的示例命令:

{
  "taskName": "print-pwd",
  "appliesTo": "*",
  "type": "command",
  "command": "%WINDIR%\Sysnative\bash.exe",
  "args": [
    "-c",
    "pwd"
  ]
}