Search code examples
powershellvisual-studio-codevscode-debuggervscode-tasks

PreLaunchTask could not find the task - Powershell


I am attempting to make a code-workspace that handles a powershell module and a few automation scripts. So for this example my folder structure looks like this -

general folder structure
- 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

The public and private folder holds the cmdlets that are used in the PowerShell module, and the module works as expected.

In the workspace.code-workspace file I create a launch configuration that attempts to allow the user to execute an F5 build which will run any file they currently have in focus. In the settings I also have a preLaunchTask to run the default task which is supposed to import the module being worked on.

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

The tasks.json has the following code in it and executes successfully when ran through the 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
            }
        }
    ]
}

However when I visit a script, for example Invoke-Test.ps1, and press F5, an error appears stating the task could not be found.

Could not find the task 'import-module'.

and when I click Configure Task it brings me to my tasks.json file and highlights the task that I was expecting it to run before executing the current file I was in.

I have been searching and most other questions or github issues don't seem to show a direct relation between an actual issue and the resolution of this problem. I am hoping if I can't eventually answer this myself, that someone else can.

github example

Example on github, if you open the workspace and load the Invoke-Test.ps1 and press F5 or build it will show the pop up pictured above.


Solution

  • Thanks to @Daniel for his comment on the original post I have managed to get it working.

    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

    I have also pushed a new branch to the example repository so all differences can be seen.

    1. Original Error Example
    2. Working Example

    The issue appears to be from within the workspace.code-workspace not working correctly. I moved the .vscode folder into the workspace directory and separated the launch configuration into its own file launch.json and the task started executing as expected.