跳至内容
Tauri

在 VS Code 中调试

此指南将指导您设置 VS Code 以调试您的 Tauri 应用程序的 核心进程

所有拥有 vscode-lldb 扩展的平台

先决条件

安装 vscode-lldb 扩展。

配置 launch.json

创建一个 .vscode/launch.json 文件,并将以下 JSON 内容粘贴到其中

.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Tauri Development Debug",
"cargo": {
"args": [
"build",
"--manifest-path=./src-tauri/Cargo.toml",
"--no-default-features"
]
},
// task for the `beforeDevCommand` if used, must be configured in `.vscode/tasks.json`
"preLaunchTask": "ui:dev"
},
{
"type": "lldb",
"request": "launch",
"name": "Tauri Production Debug",
"cargo": {
"args": ["build", "--release", "--manifest-path=./src-tauri/Cargo.toml"]
},
// task for the `beforeBuildCommand` if used, must be configured in `.vscode/tasks.json`
"preLaunchTask": "ui:build"
}
]
}

此方法直接使用 cargo 构建 Rust 应用程序并在开发和生产模式下将其加载。

请注意,它不使用 Tauri CLI,因此一些专有的 CLI 功能不会被执行。必须先执行或配置 beforeDevCommandbeforeBuildCommand 脚本来在 preLaunchTask 字段中作为任务执行。以下是示例 .vscode/tasks.json 文件,其中包含两个任务,一个用于在开发服务器上启动 beforeDevCommand,另一个用于 beforeBuildCommand

.vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "ui:dev",
"type": "shell",
// `dev` keeps running in the background
// ideally you should also configure a `problemMatcher`
// see https://vscode.js.cn/docs/editor/tasks#_can-a-background-task-be-used-as-a-prelaunchtask-in-launchjson
"isBackground": true,
// change this to your `beforeDevCommand`:
"command": "yarn",
"args": ["dev"]
},
{
"label": "ui:build",
"type": "shell",
// change this to your `beforeBuildCommand`:
"command": "yarn",
"args": ["build"]
}
]
}

现在您可以在 src-tauri/src/main.rs 或任何其他 Rust 文件中设置断点,然后通过按下 F5 开始调试。

在 Windows 上使用 Visual Studio Windows 调试器

Visual Studio Windows 调试器是仅在 Windows 上运行的调试器,通常比 vscode-lldb 快,并且对一些 Rust 功能(如枚举)有更好的支持。

先决条件

安装 C/C++ 扩展,并按照 https://vscode.js.cn/docs/cpp/config-msvc#_prerequisites 安装 Visual Studio Windows 调试器。

配置 launch.json 和 tasks.json

.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch App Debug",
"type": "cppvsdbg",
"request": "launch",
// change the exe name to your actual exe name
// (to debug release builds, change `target/debug` to `release/debug`)
"program": "${workspaceRoot}/src-tauri/target/debug/your-app-name-here.exe",
"cwd": "${workspaceRoot}",
"preLaunchTask": "ui:dev"
}
]
}

请注意,它不使用 Tauri CLI,因此一些专有的 CLI 功能不会被执行。tasks.jsonlldb 相同,但您需要添加一个配置组,并将您的 preLaunchTasklaunch.json 中添加到其中,如果您希望它总是启动前编译。

以下是一个运行开发服务器(相当于 beforeDevCommand)并将编译(《code dir="auto">cargo build)作为一个组执行的示例。要使用它,请将 launch.json 中的 preLaunchTask 配置改为 dev(或您为其命名的任何名称)。

.vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build:debug",
"type": "cargo",
"command": "build"
},
{
"label": "ui:dev",
"type": "shell",
// `dev` keeps running in the background
// ideally you should also configure a `problemMatcher`
// see https://vscode.js.cn/docs/editor/tasks#_can-a-background-task-be-used-as-a-prelaunchtask-in-launchjson
"isBackground": true,
// change this to your `beforeDevCommand`:
"command": "yarn",
"args": ["dev"]
},
{
"label": "dev",
"dependsOn": ["build:debug", "ui:dev"],
"group": {
"kind": "build"
}
}
]
}

© 2025 Tauri 贡献者。CC-BY / MIT