Search code examples
typescriptvisual-studio-codevscode-debugger

How/Where is the VS Code task named "tsc: build - tsconfig.json" defined?


I'm new to VS Code debugging setup so I'm hoping for someone who will provide me with a deeper understanding of how it works. Please note that I have read the documentation, but preLaunchTask property in launch.json in particular is not clear to me.

Using VS Code's documentation, I've created a launch.json file and customized the parameters to match my project. This all works.

However, what I'm not understanding is why the value for preLaunchTask is tsc: build - tsconfig.json. Why does this value work? I can't find any information regarding this other than the example shown in the docs.

Looking through StackOverflow, I see an example on how to customize preLaunchTask. It points to the creation of a tasks.json file that will execute the command to kick off transpiling the TypeScript code into JavaScript.

Looking at my own project, when I use tsc: build - tsconfig.json, it works without having a task.json defined under .vscode folder. When I change it to anything else, it doesn't work. Does anyone know why that is?

Can anyone point me to the documentation page, perhaps, that explains how this part of preLaunchTask works?


Solution

  • This appears to be a special globally-defined(?) task provided by the builtin TypeScript Language Support extension. As proof of this, if you disable that extension (you can to find it in the Extensions View if you search @builtin typescript and javascript), then that task will no longer be available for selection under the menu that appears if you run Tasks: Run Task in the command palette (you may need to click "Show All Tasks..." and search under that state). See https://github.com/microsoft/vscode/blob/main/extensions/typescript-language-features/src/task/taskProvider.ts. Of initial interest is getBuildTask:

    private getBuildTask(workspaceFolder: vscode.WorkspaceFolder | undefined, label: string, command: string, args: string[], buildTaskidentifier: TypeScriptTaskDefinition): vscode.Task {
        const buildTask = new vscode.Task(
            buildTaskidentifier,
            workspaceFolder || vscode.TaskScope.Workspace,
            vscode.l10n.t("build - {0}", label),
            'tsc',
            new vscode.ShellExecution(command, args),
            '$tsc');
        buildTask.group = vscode.TaskGroup.Build;
        buildTask.isBackground = false;
        return buildTask;
    }
    

    , which is called in getTasksForProject, which is called in provideTasks in a loop over all tsconfigs.

    You can find examples of people having issues with this task not being defined if you google "github vscode issues "tsc: build - tsconfig.json"", but unfortunately many of those tickets don't contain enough info from the issue raiser to figure out the cause. There is, however, this ticket: https://github.com/microsoft/vscode/issues/87525, where it was found that localization from language packs could interfere with usage of the string "tsc: build - tsconfig", and a localized version of the string needed to be used (not too surprising, given the part of the above code that calls vscode.l10n.t).