2

The closest to my issue is this SO question, but I think something else is going on here. I have two launch configurations each which call a similar preLaunch task, start-local and start-dev. The body of these two tasks is almost identical, except that VS Code can only find whichever of these is declared last in the task.json file. I tested this by duplicating the first task and simply changing the label, and none but the last task can ever be found. Is this a bug or am I doing something wrong? Pasting my configs for reference:

VS Code Version: 1.72.2

OS Version: MacOS 12.6

launch.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"
        },
    ]
}

task.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"
        }
    ]
}
  • I've got the same issue, but my tasks are labelled like "foo:bar" and "foo:bing" – AncientSwordRage Feb 21 '23 at 14:19
  • Related: [Why does only the last NPM task in VS Code of several that only vary by label and options (and not the script) get picked up?](/q/76237122/11107541) – starball May 12 '23 at 22:49
  • 1
    Does this answer your question? [Why does only the last NPM task in VS Code of several that only vary by label and options (and not the script) get picked up?](https://stackoverflow.com/questions/76237122/why-does-only-the-last-npm-task-in-vs-code-of-several-that-only-vary-by-label-an) – AncientSwordRage May 15 '23 at 12:48

1 Answers1

2

I tried to reproduce using VSC 1.75.0 (insiders) using a Python launch config and shell echo commands instead of npm commands. It executes as expected. The content of the .env file contains local or dev depending on the launch config selected.

Looking at the tasks.json the only difference is the content of the .env file.

You can use the extension Command Variable to create a Pick list in the launch configuration and pass strings from the launch.json to the 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" }
    }
  ]
}

I also tried with exactly the same task names as the OP. Only what the launch or task does is different. My original example above uses different names because the original names would not be correct for what the task does.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 1
    Hmm, it's not exactly the same. You've got a few changes (python instead of npm like you said) but also the tasks aren't similarly named and some have dependencies? Are you able to recreate with the a set up closer to the OP? – AncientSwordRage Feb 22 '23 at 17:07
  • @AncientSwordRage Why would the kind of a launch determine if a task can be found, VSC parses `launch.json` and processes the `preLaunchTask` field and executes the named task and then the launch. More likely is the naming of tasks a cause why it can't find a task. The OP could try if a different launch task has the same behavior. The OP does not show what `npm run start` does. I don't do react so a similar setup is not possible. If the launch kind is important you should create an issue with the 2 different launch/task json files. Does using random names fix the issue (no prefixes). – rioV8 Feb 22 '23 at 18:03
  • @AncientSwordRage I tried with exactly the same task names. Only what the launch or task does is different. My alternative uses different names because the original names would not be correct of what the task does. – rioV8 Feb 22 '23 at 18:06
  • thanks for checking with task names changed. Perhaps the issue me and rhenOP are having isn't name related then. I'll try to update my VSCode version – AncientSwordRage Feb 22 '23 at 19:35
  • you only have one `npm: start` command, vs the OPs two. See https://stackoverflow.com/q/76237122/11107541 – AncientSwordRage May 15 '23 at 12:48