Search code examples
visual-studio-codevscode-tasks

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?


This is very similar to VS Code cannot find similar task, only the last one but the answerer there can't reproduce, and I have a different task set up... so this could be a different issue.

Here is my tasks.json (more or less):

{
  "version": "2.0.0",
  "inputs": [
    {
      "type": "pickString",
      "id": "locale",
      "description": "Which localisation should be used in this task",
      "options": ["british", "american", "canadian"],
      "default": "british",
    }
  ],
  "tasks": [
    {
      "label": "localise",
      "script": "localise:make",
      "options": { "env": { "NODE_ENV": "test", "PROJECT_UI_LOCALISATION": "${input:locale}" } },
      "type": "npm",
    },
    {
      "label": "localise:british",
      "script": "localise:make",
      "options": { "env": { "NODE_ENV": "test", "PROJECT_UI_LOCALISATION": "british" } },
      "type": "npm",
    },
    {
      "label": "localise:american",
      "script": "localise:make",
      "options": { "env": { "NODE_ENV": "test", "PROJECT_UI_LOCALISATION": "american" } },
      "type": "npm",
    },
    {
      "label": "localise:canadian",
      "script": "localise:make",
      "options": { "env": { "NODE_ENV": "test", "PROJECT_UI_LOCALISATION": "canadian" } },
      "type": "npm",
    }
  ]
}

When I press ctrl+shift+P and click Tasks: run tasks, only the last one appears. When I try to run one of these via my launch.json (with "preLaunchTask": "localise:${input:locale}" and "postDebugTask": "localise:british", and a matching pickString set of options I've re-defined/copy and pasted.) it can't find localise:british twice (once in the preLaunchTask if I select it from the drop down in vscode, and another in the postDebug.)

Why is that, and how can I get around it?


Solution

  • If you google "github vscode issues npm task unique script", you'll find resolving multiple tasks with type npm and same script fails #93001.

    One of the maintainers (@alexr00) states:

    This is by design. An extension provided task, such as a task of "type": "npm" is identified by it task-type-specific properties (for npm tasks, this is script). Tasks are not identified by label, a label is just a nice human readable string.

    Since this is very easily worked around by having a separate npm script in your package.json I wouldn't make any changes in the behavior. However, it is not nice that the first time you learn that you can't have two of the same task is when you try to run a task that depends on it. I will add an error earlier, so that it's clear what's happening when the task list shows.

    So you can write multiple scripts in your package.json to get around this (I don't particularly like that suggestion).

    Here are some other workarounds people have suggested in the comments there:

    Apparently it's not currently possible for the tasks system to include the task options in the unique identification of a task. See this and this.

    You can give that issue ticket a thumbs up reaction to show support for it / that you're having the same issue, and subscribe to it to get notified of discussion and progress.