使用 launch.json 配置调试环境时,会涉及到多个参数,用于定义调试器的行为和目标执行环境。以下是一些常用的配置参数:
1、"type" :指定调试器的类型,例如 "node" 表示 Node.js 调试器,"python" 表示 Python 调试器,"java" 表示 Java 调试器等。
2、"request" :指定调试的请求类型,可以是 "launch"(启动一个新的进程)或 "attach"(附加到已有的进程)。
3、"name" :为配置提供一个友好的名称,方便识别不同的调试配置。
4、"program" :用于指定程序的入口文件路径,可以是绝对路径或相对于工作目录的路径。 5、"args" :传递给程序的命令行参数,以数组形式提供。
6、"cwd" :指定程序的工作目录,可以是绝对路径或相对于工作目录的路径。
7、"env" :设置程序运行时的环境变量,以对象形式提供。
8、"stopOnEntry" :设置为 true 时,在启动后会在入口处停止,等待调试器连接。 9、"preLaunchTask" :指定在启动调试前运行的任务,通常是一个编译任务。
10、"postDebugTask" :指定在调试结束后运行的任务,比如清理任务。
11、"outFiles" :设置输出文件的路径,用于映射源代码和编译后的文件。
12、"sourceMaps" :控制是否启用源代码映射,可以是 "inline"、"both" 或 "false"。 13、"sourceMapPathOverrides" :用于根据源代码映射调整文件路径。
14、"externalConsole" :设置为 true 时,将在外部控制台中运行程序。
15、"internalConsoleOptions" :控制内部控制台的显示方式,可以是 "neverOpen"、"openOnSessionStart" 或 "openOnFirstSessionStart"。
16、"showAsyncStacks" :设置为 true 时,在堆栈跟踪中显示异步调用的信息。
17、"stopOnError" :设置为 true 时,当发生错误时暂停调试。
18、"smartStep" :设置为 true 时,跳过无需调试的代码。
19、"skipFiles" :指定不需要调试的文件或文件夹。
20、"justMyCode" :设置为 true 时,只调试自己的代码。 通过对这些常用配置参数的理解,可以根据不同的调试场景和需求,灵活地配置 launch.json 文件,从而更高效地进行代码调试。
常见的调试场景以及相应的 launch.json 配置示例。以下是一些常用编程语言和场景的示例: 1. Node.js 调试
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node.js Launch",
"program": "${file}",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "node"
}]
}
Java 调试
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Java Debug",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopOnEntry": false,
"mainClass": "${file}"
}]
}
Python 调试
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env":
{
"PYTHONPATH": "",
},
"args": []
},
]
}
C# 调试
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/app.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"serverReadyAction":
{
"action": "openExternally",
"pattern": "\bNow listening on:\s+(https?://\S+)"
},
"env":
{
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap":
{
"/Views": "${workspaceFolder}/Views"
}
}]
}
客户端-服务器模式调试
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Client/Server",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}/server",
"program": "${workspaceFolder}/server/app.js",
"env":
{
"NODE_ENV": "development"
},
"outFiles": ["${workspaceFolder}/client/dist/**/*.js"],
"sourceMaps": true,
"restart": true,
"console": "integratedTerminal"
}]
}
Web 开发调试
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}/src"
}]
}