如何重复使用 VS Code 任务窗口,而无需关闭它。

8

我想要在VS Code中重复使用一个任务窗口,但是当我按下ctrl + c时,任务会停止,但会写出:“终端将被任务重用,请按任意键关闭它。

我不想关闭窗口。这很烦人,因为它强制我打开一个新窗口并导航到正确的目录。

我记录了一个gif来展示问题(右边的窗口):

输入图像描述

我的任务配置看起来像这样:

{
    "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"
    }
}

我尝试了各种presentation属性的组合,但没有帮助。
有关 VS Code 的相关功能请求在这里

看起来这是 https://dev59.com/5abja4cB1Zd3GeqPcS_6#52735332 的副本。试一下宏解决方案,那是最简单的 - 如果可以,请告诉我它是否适用于您。 - Mark
4个回答

2
我认为这是不可能的,而且可能是有意为之的。
如果你查看tasks.json模式,你会发现:
/**
 * 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';

  //...
}

一种自定义任务的类型。类型为“shell”的任务在shell内部执行。
因此,对我来说,这意味着如果您有一个类型为“shell”的倒计时任务正在运行此命令seq 10 1,则在幕后它将执行以下操作:
devbox:~ dev$ bash -c "seq 10 1"
10
9
8
7
6
5
4
3
2
1
devbox:~ dev$

如您所见,它会立即退出,我不确定您能否对此做些什么。(虽然我可能是错的)
即使您设置了一个类型为“进程”的任务(命令是可执行文件的路径),它也不允许您重用终端。
话虽如此,您可以强制执行,但VS Code可能不太满意:(请注意命令末尾的&& sh
{
  "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": [],
    }
  ]
}

当您运行任务时,会立即获取另一个 shell:

enter image description here

然而,如果您重新运行相同的任务,那么VS Code会变得不高兴:

enter image description here

我无法在.vscode/settings.json中找到支持您使用情况的选项,这让我觉得这确实是一个设计上的选择

enter image description here


1
我发现了另一种解决方法,对我非常有效:
  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"
            }
        }
    ]
    

1
我找到了一个解决方案,我的任务看起来像这样。
"tasks": [
        {
            "label": "start server",
            "type": "shell",
            "command": "RUN='cd backend && npm run dev' bash",
            "problemMatcher": [],
        },
   ]


在我的 .bashrc 文件的末尾,我有 eval "$RUN"

0

听起来你想在任务完成后在正确的文件夹中启动 shell。我不确定这是否是最好的方法,但我会通过复合任务实现类似的功能。

{
    "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",
}

这个配置在任务完成后(在同一个窗口中)在正确的文件夹中运行bash。如果需要,将bash替换为您使用的任何shell。


我尝试实现您的解决方案,但似乎无法保持窗口打开? - DauleDK
你只是在检查吗?你正在运行复合任务还是原始任务? - njha
我不确定你的问题是什么? - DauleDK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接