【问题标题】:VS Code cannot find similar task, only the last oneVS Code 找不到类似的任务,只能找到最后一个
【发布时间】:2023-02-23 03:54:35
【问题描述】:

最接近我的问题的是this SO question,但我认为这里还有其他问题。我有两个启动配置,每个都调用类似的预启动任务,start-localstart-dev。这两个任务的主体几乎相同,除了 VS Code 只能找到在 task.json 文件中最后声明的那个。我通过复制第一个任务并简单地更改标签来对此进行测试,但除了最后一个任务之外,没有其他任务可以找到。这是一个错误还是我做错了什么?粘贴我的配置以供参考:

VS 代码版本:1.72.2

操作系统版本:MacOS 12.6

启动.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Start Dev",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceFolder}",
            "preLaunchTask": "start-dev",
            "postDebugTask": "Terminate All Tasks"
        },
        {
            "name": "Start Local",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:3001",
            "webRoot": "${workspaceFolder}",
            "preLaunchTask": "start-local",
            "postDebugTask": "Terminate All Tasks"
        },
    ]
}

任务.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "start-local",
            "type": "npm",
            "script": "start",
            "isBackground": true,
            "problemMatcher": {
                "owner": "npm",
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": ".*",
                    "endsPattern": "To ignore, add.*eslint-disable-next-line to the line before.*"
                },
                "pattern": {
                    "regexp": ""
                }
            },
            "dependsOrder": "sequence",
            "dependsOn": [
                "setup-local-env"
            ]
        },
        {
            "label": "setup-local-env",
            "command": "echo REACT_APP_STAGE=local > ./.env; echo BROWSER=none >> ./.env",
            "type": "shell",
            "presentation": {
                "echo": false,
                "reveal": "never",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false,
                "close": true
            }
        },
        {
            "label": "start-dev",
            "type": "npm",
            "script": "start",
            "isBackground": true,
            "problemMatcher": {
                "owner": "npm",
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": ".*",
                    "endsPattern": "To ignore, add.*eslint-disable-next-line to the line before.*"
                },
                "pattern": {
                    "regexp": ""
                }
            },
            "dependsOrder": "sequence",
            "dependsOn": [
                "setup-dev-env"
            ]
        },
        {
            "label": "setup-dev-env",
            "command": "echo REACT_APP_STAGE=dev > ./.env; echo BROWSER=none >> ./.env",
            "type": "shell",
            "presentation": {
                "echo": false,
                "reveal": "never",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false,
                "close": true
            }
        },
        {
            "label": "Terminate All Tasks",
            "command": "echo ${input:terminate}",
            "type": "shell",
            "problemMatcher": []
        },
    ],
    "inputs": [
        {
            "id": "terminate",
            "type": "command",
            "command": "workbench.action.tasks.terminate",
            "args": "terminateAll"
        }
    ]
}

【问题讨论】:

  • 我遇到了同样的问题,但我的任务被标记为“foo:bar”和“foo:bing”

标签: visual-studio-code vscode-debugger vscode-tasks


【解决方案1】:

我尝试使用 VSC 1.75.0(内部人员)使用 Python 启动配置和 shell echo 命令而不是 npm 命令进行重现。它按预期执行。 .env 文件的内容包含 localdev,具体取决于所选的启动配置。

查看tasks.json,唯一的区别是.env文件的内容。

您可以使用扩展名 Command Variable 在启动配置中创建一个选择列表,并将字符串从 launch.json 传递到 tasks.json

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Start Dev/Local",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:${input:select-type-port}",
      "webRoot": "${workspaceFolder}",
      "preLaunchTask": "start-config",
      "postDebugTask": "Terminate All Tasks"
    }
  ],
  "inputs": [
    {
      "id": "select-type-port",
      "type": "command",
      "command": "extension.commandvariable.pickStringRemember",
      "args": {
        "description": "Select configuration",
        "key": "port-number",
        "options": [
          { "label": "Local", "value": { "start-config": "local", "port-number": "3001" } },
          { "label": "Dev", "value": { "start-config": "dev", "port-number": "3000" } }
        ]
      }
    }
  ]
}

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "start-config",
      "type": "npm",
      "script": "start",
      "isBackground": true,
      "problemMatcher": {
        "owner": "npm",
        "background": {
          "activeOnStart": true,
          "beginsPattern": ".*",
          "endsPattern": "To ignore, add.*eslint-disable-next-line to the line before.*"
        },
        "pattern": {
          "regexp": ""
        }
      },
      "dependsOrder": "sequence",
      "dependsOn": [
        "setup-config-env"
      ]
    },
    {
      "label": "setup-config-env",
      "command": "echo REACT_APP_STAGE=${input:get-start-config} > ./.env; echo BROWSER=none >> ./.env",
      "type": "shell",
      "presentation": {
        "echo": false,
        "reveal": "never",
        "focus": false,
        "panel": "shared",
        "showReuseMessage": true,
        "clear": false,
        "close": true
      }
    },
    {
      "label": "Terminate All Tasks",
      "command": "echo ${input:terminate}",
      "type": "shell",
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "terminate",
      "type": "command",
      "command": "workbench.action.tasks.terminate",
      "args": "terminateAll"
    },
    {
      "id": "get-start-config",
      "type": "command",
      "command": "extension.commandvariable.remember",
      "args": { "key": "start-config" }
    }
  ]
}

我还尝试使用与 OP 完全相同的任务名称。只有启动或任务的作用不同。我上面的原始示例使用了不同的名称,因为原始名称对于任务的作用是不正确的。

【讨论】:

  • 嗯,不完全一样。您进行了一些更改(python 而不是 npm,就像您说的那样),但任务的名称也不相似,有些任务具有依赖性?你能用更接近 OP 的设置重新创建吗?
  • @AncientSwordRage 为什么启动的类型会决定是否可以找到任务,VSC 解析launch.json 并处理preLaunchTask 字段并执行命名任务,然后启动。更有可能是任务的命名导致它找不到任务。如果不同的启动任务具有相同的行为,OP 可以尝试。 OP 没有显示 npm run start 的作用。我没有做出反应,所以类似的设置是不可能的。如果启动类型很重要,您应该使用 2 个不同的启动/任务 json 文件创建问题。使用随机名称是否可以解决问题(无前缀)。
  • @AncientSwordRage 我尝试使用完全相同的任务名称。只有启动或任务的作用不同。我的替代方案使用不同的名称,因为原始名称与任务的作用不符。
  • 感谢您检查任务名称是否已更改。也许我和 rhenOP 遇到的问题与名字无关。我会尝试更新我的 VSCode 版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2021-05-06
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多