【问题标题】:How to re-use a VS Code task window, without closing it如何重用 VS Code 任务窗口而不关闭它
【发布时间】:2019-11-27 09:43:46
【问题描述】:

我的目标是在 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 代码的相关功能请求是here

【问题讨论】:

标签: visual-studio-code vscode-tasks


【解决方案1】:

我认为这是不可能的,它可能是设计使然。

如果您查看schema of 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:

但是,如果您重新运行相同的任务,VS Code 就会变得脾气暴躁:

我在.vscode/settings.json 中看不到支持您的用例的选项,这让我认为这确实是一个设计的选择:

【讨论】:

    【解决方案2】:

    听起来你想在任务完成后在正确的文件夹中启动一个 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。

    【讨论】:

    • 我尝试实施您的解决方案,但似乎无法保持窗口打开?
    • 只是检查:您正在运行复合任务吗?还是原来的任务?
    • 我不确定您的问题?
    【解决方案3】:

    我找到了解决方案,我的任务看起来像这样

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多