跳到內容
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.dev.org.tw/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.dev.org.tw/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 相同,只是您需要新增一個組態群組,並從 launch.jsonpreLaunchTask 目標設定為該群組(如果您希望它在啟動前始終編譯)。

以下是執行開發伺服器(相當於 beforeDevCommand)和編譯 (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.dev.org.tw/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