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

4

9

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.

Coppola answered 27/11, 2019 at 9:43 Comment(1)
It looks like a duplicate of #46899980 Try the macro solution, that is the easiest - let me know if it works for you.Orcinol
H
2

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

Hawkins answered 6/12, 2019 at 10:16 Comment(0)
S
1

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"

Sacculus answered 15/11, 2020 at 14:35 Comment(0)
E
1

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"
            }
        }
    ]
    
Eley answered 9/6, 2022 at 12:36 Comment(0)
V
0

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.

Vineland answered 5/12, 2019 at 1:19 Comment(3)
I tried to implement your solution, but it does not seem to keep the window open?Coppola
Just checking: Are you running the composite task? Or the original task?Vineland
I am unsure about your question?Coppola

© 2022 - 2024 — McMap. All rights reserved.