根据文件扩展名在VS Code中定义多个启动配置

问题描述

我有一个包含JS和PY文件的简单项目。

在我的launch.json中,我想基于文件类型定义启动配置,以便JS将与Node运行,而PY将与Python运行。

launch.json中有这样的配置吗?

解决方法

您可以使用扩展名Command Variable v1.6.2。

您可以使用文件路径为特定的调试器选择替换项。

如果您使用包含Python Debugger和Node Debugger的Composite启动,并在扩展名与该调试器不匹配的情况下将文件替换为虚拟文件。

在workspaceRoot / .vscode文件夹中,创建一个名为dummy的空文件。

launch.json设置为

{
  "version": "0.2.0","configurations": [
    {
      "type": "node","request": "launch","name": "Node File if .js","program": "${input:file_if_js}"
    },{
      "name": "Python: Current File if .py","type": "python","program": "${input:file_if_python}","console": "integratedTerminal"
    }

  ],"compounds": [
    {
      "name": "Debug Python or Node","configurations": ["Python: Current File if .py","Node File if .js"]
    }
  ],"inputs": [
    {
      "id": "file_if_python","type": "command","command": "extension.commandvariable.file.fileAsKey","args": {
        ".py": "${file}",".js": "${workspaceFolder}\\.vscode\\dummy"
      }
    },{
      "id": "file_if_js","args": {
        ".js": "${file}",".py": "${workspaceFolder}\\.vscode\\dummy"
      }
    }
  ]
}

如果使用Linux / MacOS,请调整目录分隔符。