PreLaunchTask 找不到任务 - Powershell

PreLaunchTask could not find the task - Powershell

我正在尝试创建一个代码工作区来处理 powershell 模块和一些自动化脚本。所以对于这个例子,我的文件夹结构是这样的 -

一般文件夹结构
- workspace.code-workspace
- README.md
>- .vscode
    - tasks.json
    - install-module-locally.ps1
>- src
    >- Automations
        - Invoke-Test.ps1
    >- Modules
        >- MyModule
            > public
            > private
            - MyModule.psd1
            - MyModule.psm1

public 和 private 文件夹包含 PowerShell 模块中使用的 cmdlet,该模块按预期工作。

workspace.code-workspace 文件中,我创建了一个启动配置,试图允许用户执行 F5 构建,这将 运行 他们当前关注的任何文件。在设置中我还有一个 preLaunchTask 到 运行 默认任务,它应该导入正在处理的模块。

workspace.code-工作区
{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {},
    "launch": {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "PowerShell",
                "request": "launch",
                "name": "Current File",
                "script": "${file}",
                "preLaunchTask": "${defaultBuildTask}"
            }
        ]
    }
}

tasks.json中有如下代码,当运行通过Tasks: Run Task > import-module > Continue without scanning the task output执行成功。

tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "import-module",
            "detail": "Imports the module locally for testing individual scripts that would require it without having logic in each script.",
            "type": "shell",
            "command": "${workspaceFolder}\.vscode\install-module-locally.ps1",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

然而,当我访问脚本时,例如 Invoke-Test.ps1,然后按 F5,出现错误,指出找不到任务。

当我单击 Configure Task 时,它会将我带到我的 tasks.json 文件,并在执行我所在的当前文件之前突出显示我期望它 运行 的任务。

我一直在搜索,大多数其他问题或 github 问题似乎没有显示实际问题与此问题的解决方案之间的直接关系。我希望如果我最终不能自己回答这个问题,那么其他人可以。

github 例子

github 上的示例,如果您打开工作区并加载 Invoke-Test.ps1 并按 F5 或构建它将显示如上图所示的弹出窗口。

感谢 @Daniel 对原版的评论 post 我已经设法让它工作了。

Seems like bug with preLaunchTask not working from workspace file. I've seen a couple issues on Github about it. Try moving the configurations section out of the workspace file into a .vscode\launch.json file. This worked for me

我还向示例存储库推送了一个新分支,因此可以看到所有差异。

  1. Original Error Example
  2. Working Example

问题似乎是 workspace.code-workspace 无法正常工作。我将 .vscode 文件夹移动到工作区目录中,并将启动配置分离到它自己的文件中 launch.json,任务开始按预期执行。