16

What is the working directory of a Visual Studio Code task?

According to the official documentation, the default should be the workspace directory, i.e., workspaceFolder, but running a task which simply executes echo $PWD, it shows the parent directory of workspaceFolder.

In other words, given dir1/dir2/.vscode/tasks.json, the following trivial task prints dir1 while the workspaceFolder refers to dir2 (the actual workspace directory).

What should I do to run a task from the workspaceFolder? Changing the cwd option does not help.

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "Sample",
            "command": "echo PWD=$PWD workspaceFolder=${workspaceFolder}",
            "options": {
              "cwd": "${workspaceFolder}"
            }
        }
    ]
}

Execution output:

> Executing task: echo PWD=$PWD workspaceFolder=/home/ubuntu/dir1/dir2 <

PWD=/home/ubuntu/dir1 workspaceFolder=/home/ubuntu/dir1/dir2

As additional and probably relevant note, I am developing remotely using the Remote - SSH extension.

Antonio Fuschetto
  • 170
  • 1
  • 1
  • 7
  • 1
    I just tried with remote SSH to a VirtualBox Ubuntu and I get the expected behavior, PWD==Workspacefolder – rioV8 May 03 '21 at 12:07
  • 4
    This was probably a bug of the version I was using because today, using the latest version of VSCode and its extensions, the same task configuration works as expected. – Antonio Fuschetto Sep 19 '21 at 15:44
  • Long after your comment, *VSCode* still has the same issue: every time there is a new version, it gets completely broken and needs to be restarted. – arvymetal Oct 29 '22 at 16:03

1 Answers1

15
{
    "label": "Status",
    "detail": "git status",
    "type": "shell",
    "command": "git status",
    "group": "test",
    "options": {
        "cwd": "${workspaceFolder}/dir1/dir2/dir3/"
    },
    "presentation": {
        "reveal": "always",
        "panel": "new",
        "focus": true
    }
}

We can use the "options" key to set the working directory the task should execute in.

upe
  • 1,862
  • 1
  • 19
  • 33
  • 2
    As I said in the description, this configuration did not solve the problem. However, after updating VSCode the problem was resolved without change my `tasks.json` file. – Antonio Fuschetto Dec 29 '21 at 23:46
  • This helped me find what I was looking for, which is setting `"cwd": "${fileDirname}"` under the `options` key. – hb20007 May 28 '23 at 19:30