DauleDK
DauleDK

Reputation: 3443

How to re-use a VS Code task window, without closing it

My goal is to re-use a task window in VS Code. However, when I enter ctrl + c, the task stops, but then writes: "Terminal will be reused by tasks, press any key to close it.".

I don't want to close the window. It's frustrating because it forces me to open a new window and navigate to the correct directory.

I recorded a gif of the problem (It's the window on the right):

enter image description here

My task config look like this:

{
    "label": "some label",
    "type": "npm",
    "script": "build",
    "path": "some-path/",
    "problemMatcher": [],
    "runOptions": { "runOn": "folderOpen" },
    "group": "build",
    "presentation": {
        "echo": true,
        "reveal": "silent",
        "focus": false,
        "panel": "shared",
        "showReuseMessage": false,
        "clear": false,
        "group": "build"
    }
}

I tried various combination of the presentation properties, but to no help.

Related feature request on VS code is here.

Upvotes: 9

Views: 4124

Answers (4)

Jared
Jared

Reputation: 3176

I found yet another solution for this that works great for me:

  1. using bash:

    "tasks": [
        {
            "label": "start server",
            "type": "shell",
            "command": "bash -c 'cd backend && npm run dev; exec bash'",
            "isBackground": false,
            "presentation": {
                "panel": "new",
                "close": true
            },
            "runOptions": {
                "runOn": "folderOpen"
            }
        }
    ]
    
  2. or if you use fish (like me):

    "tasks": [
        {
            "label": "start server",
            "type": "shell",
            "command": "fish -C 'cd backend && npm run dev'",
            "isBackground": false,
            "presentation": {
                "panel": "new",
                "close": true
            },
            "runOptions": {
                "runOn": "folderOpen"
            }
        }
    ]
    

Upvotes: 1

Zaydme
Zaydme

Reputation: 53

I found a solution for this, my task looks something like this

"tasks": [
        {
            "label": "start server",
            "type": "shell",
            "command": "RUN='cd backend && npm run dev' bash",
            "problemMatcher": [],
        },
   ]

and at the end of my .bashrc I have eval "$RUN"

Upvotes: 1

customcommander
customcommander

Reputation: 18961

I don't think this is possible and it may be by design.

If you look at the schema of tasks.json, you see:

/**
 * The description of a task.
 */
interface TaskDescription {
  /**
   * The task's name
   */
  label: string;

  /**
   * The type of a custom task. Tasks of type "shell" are executed
   * inside a shell (e.g. bash, cmd, powershell, ...)
   */
  type: 'shell' | 'process';

  //...
}

The type of a custom task. Tasks of type "shell" are executed inside a shell

So to me this implies that if you have a countdown task of type "shell" running this command seq 10 1, behind the scene it would do:

devbox:~ dev$ bash -c "seq 10 1"
10
9
8
7
6
5
4
3
2
1
devbox:~ dev$

As you can see it immediately exits and I'm not sure you can do anything about it. (I may be wrong though)

Even if you set a task of type "process" (command being the path to an executable), it doesn't allow you to reuse the terminal.

Having said that you can force it but VS Code wouldn't be too happy about it: (notice the && sh at the end of the command)

{
  "version": "2.0.0",
  "tasks": [
    {

      "label": "10 9 8 ...",
      "type": "shell",
      "command": "seq 10 1 && sh",
      "presentation": {
        "echo": true,
        "focus": true,
        "reveal": "always",
        "panel": "shared",
      },
      "problemMatcher": [],
    }
  ]
}

When you run the task, you do get another shell immediately:

enter image description here

However if you re-run the same task, then VS Code gets grumpy:

enter image description here

The fact that I couldn't see an option in .vscode/settings.json to support your use case makes me think that it really is a by design choice:

enter image description here

Upvotes: 2

njha
njha

Reputation: 1118

It sounds like you want to launch a shell in the right folder after the task is complete. I'm not sure if this is the best way to do it, but I do something similar with compound tasks.

{
    "label": "some label",
    "type": "npm",
    "script": "build",
    "path": "some-path/",
    "problemMatcher": [],
    "runOptions": { "runOn": "folderOpen" },
    "group": "build",
    "presentation": {
        "echo": true,
        "reveal": "silent",
        "focus": false,
        "panel": "shared",
        "showReuseMessage": false,
        "clear": false,
        "group": "build"
    }
},
{
    "label": "shell",
    "type": "shell",
    "command": "cd app; bash",
    "group": {
        "kind": "build",
        "isDefault": true
    }
},
{
    "label": "Task and Shell",
    "group": "build",
    "dependsOn": ["some label", "shell"],
    "dependsOrder": "sequence",
}

This configuration runs bash in the right folder after the task (in the same window). Replace bash with whatever shell you use if necessary.

Upvotes: 0

Related Questions