How to share tasks between vscode workspace folders?
Asked Answered
D

2

18

My VSCode project is multi-root workspace. That is I have multiple workspaces each in separate directories, each has a .vscode folder.

I'm trying to build a workspace with dependencies between each module via build tasks.

I've tried to set dependsOn parameter for a task, but it yields a following error:

Couldn't resolve dependent task 'build FOO' in workspace folder 'BAR' (here BAR build task depends on FOO)

Is there a better/correct/any way to achieve my goal?

Dithyramb answered 30/7, 2018 at 18:57 Comment(1)
I have added a feature request for this. Vote (thumbs up at first comment) if you're interested: github.com/microsoft/vscode/issues/185084Cuneal
H
14

You can have a tasks section in your .code-workspace file. There you can create a task like "Build all" and define dependsOn tasks. However, there you also cannot reference build tasks from the workspace folders (to me this sounds like a reasonable feature and I believe they should implement it at some time). My workaround is to copy the relevant build task from the sub-tasks files into the .code-workspace file and reference them in my "Build all" task.

Example .code-workspace:

{
    "folders": [
        {
            "path": "proj-A"
        },
        {
            "path": "proj-B"
        }
    ],
    "tasks": {
        "version": "2.0.0",
        "tasks": [
            {
                "type": "shell",
                "label": "Build A",
                "command": "make",
                "args": ["-j4"],
                "options": {
                    "cwd": "${workspaceFolder}/../proj-A",
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": "build",
            },
            {
                "type": "shell",
                "label": "Build B",
                "command": "make",
                "args": ["-j4"],
                "options": {
                    "cwd": "${workspaceFolder}/../proj-B",
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": "build",
            },
            {
                "type": "shell",
                "label": "Build all",
                "command": "echo",
                "args": ["Building everything"],
                "options": {},
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "dependsOrder":"sequence",
                "dependsOn": ["Build A", "Build B"]
            },
        ]
    }
}

Note the cwd option in the individual build tasks. ${workspaceFolder} seems to be set to the first path in the "folders" section. You could also set cwd to absolute paths.

This is a bit hacky and the fact that one has to copy the build tasks is not beautiful, but it works for now and it can be easily adapted once it is possible to reference tasks of sub-tasks files.

Hephzibah answered 14/11, 2019 at 9:38 Comment(2)
Thanks, this is what I wanted. You can use ${workspaceFolder:proj-B} to define the specific workspace folder to use instead of the 1st.Menorah
I don't quite like putting folder specific task logic into the .code-workspace file. What if I want to have two .code-workspace files sharing some folders?Memorable
T
2

Check if the new VSCode 1.42 User level Tasks can help share a task.

tasks.json is now supported at a user settings level.

You can run the the Tasks: Open User Tasks command to create a user level tasks.
These tasks will be available across all folders and workspaces.
Only shell and process task types are supported here.

Thud answered 3/2, 2020 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.