Skip to content
Tauri 中文网

调试

在 Tauri 中,有那么多活动的部分,你可能会遇到需要调试的问题。有很多地方会打印错误详情,Tauri 也包含一些工具,使调试过程更加简单明了。

🌐 With all the moving pieces in Tauri, you may run into a problem that requires debugging. There are many locations where error details are printed, and Tauri includes some tools to make the debugging process more straightforward.

🌐 Development Only Code

调试工具中最有用的工具之一是在代码中添加调试语句的能力。然而,你通常不希望这些语句出现在生产环境中,这时检查你是否处于开发模式的能力就非常有用。

🌐 One of the most useful tools in your toolkit for debugging is the ability to add debugging statements in your code. However, you generally don’t want these to end up in production, which is where the ability to check whether you’re running in development mode or not comes in handy.

🌐 In Rust

fn main() {
// Whether the current instance was started with `tauri dev` or not.
#[cfg(dev)]
{
// `tauri dev` only code
}
if cfg!(dev) {
// `tauri dev` only code
} else {
// `tauri build` only code
}
let is_dev: bool = tauri::is_dev();
// Whether debug assertions are enabled or not. This is true for `tauri dev` and `tauri build --debug`.
#[cfg(debug_assertions)]
{
// Debug only code
}
if cfg!(debug_assertions) {
// Debug only code
} else {
// Production only code
}
}

🌐 Rust Console

查找错误的第一个地方是 Rust 控制台。它位于你运行例如 tauri dev 的终端中。你可以使用以下代码从 Rust 文件中向该控制台打印内容:

🌐 The first place to look for errors is in the Rust Console. This is in the terminal where you ran, e.g., tauri dev. You can use the following code to print something to that console from within a Rust file:

println!("Message from Rust: {}", msg);

有时候你的 Rust 代码可能会出错,而 Rust 编译器可以提供大量信息。例如,如果 tauri dev 崩溃,你可以在 Linux 和 macOS 上这样重新运行它:

🌐 Sometimes you may have an error in your Rust code, and the Rust compiler can give you lots of information. If, for example, tauri dev crashes, you can rerun it like this on Linux and macOS:

RUST_BACKTRACE=1 tauri dev

或者在 Windows (PowerShell) 上像这样:

🌐 or like this on Windows (PowerShell):

$env:RUST_BACKTRACE=1
tauri dev

此命令为你提供详细的堆栈跟踪。一般来说,Rust 编译器会通过提供有关问题的详细信息来帮助你,例如:

🌐 This command gives you a granular stack trace. Generally speaking, the Rust compiler helps you by giving you detailed information about the issue, such as:

error[E0425]: cannot find value `sun` in this scope
--> src/main.rs:11:5
|
11 | sun += i.to_string().parse::<u64>().unwrap();
| ^^^ help: a local variable with a similar name exists: `sum`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0425`.

🌐 WebView Console

在 WebView 中右键单击,然后选择 Inspect Element。这会打开一个类似于你熟悉的 Chrome 或 Firefox 开发者工具的网页检查器。 你也可以在 Linux 和 Windows 上使用 Ctrl + Shift + i 快捷键,在 macOS 上使用 Command + Option + i 打开检查器。

🌐 Right-click in the WebView, and choose Inspect Element. This opens up a web-inspector similar to the Chrome or Firefox dev tools you are used to. You can also use the Ctrl + Shift + i shortcut on Linux and Windows, and Command + Option + i on macOS to open the inspector.

检查器是特定于平台的,在 Linux 上渲染 webkit2gtk WebInspector,在 macOS 上渲染 Safari 的检查器,在 Windows 上渲染 Microsoft Edge DevTools。

🌐 The inspector is platform-specific, rendering the webkit2gtk WebInspector on Linux, Safari’s inspector on macOS and the Microsoft Edge DevTools on Windows.

🌐 Opening Devtools Programmatically

你可以使用 WebviewWindow::open_devtoolsWebviewWindow::close_devtools 函数来控制检查器窗口的可见性:

🌐 You can control the inspector window visibility by using the WebviewWindow::open_devtools and WebviewWindow::close_devtools functions:

tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)] // only include this code on debug builds
{
let window = app.get_webview_window("main").unwrap();
window.open_devtools();
window.close_devtools();
}
Ok(())
});

🌐 Using the Inspector in Production

默认情况下,除非你使用 Cargo 功能启用检查器,否则检查器仅在开发和调试版本中启用。

🌐 By default, the inspector is only enabled in development and debug builds unless you enable it with a Cargo feature.

🌐 Create a Debug Build

要创建调试版本,请运行 tauri build --debug 命令。

🌐 To create a debug build, run the tauri build --debug command.

npm run tauri build -- --debug

像正常的构建和开发流程一样,第一次运行此命令时构建会花费一些时间,但后续运行会快得多。最终打包的应用启用了开发控制台,并放置在 src-tauri/target/debug/bundle

🌐 Like the normal build and dev processes, building takes some time the first time you run this command but is significantly faster on subsequent runs. The final bundled app has the development console enabled and is placed in src-tauri/target/debug/bundle.

你也可以直接从终端运行已构建的应用,这会显示 Rust 编译器的提示(如果有错误的话)或你的 println 消息。浏览到文件 src-tauri/target/(release|debug)/[app name] 并直接在控制台中运行它,或者双击文件系统中的可执行文件(注意:使用此方法时控制台遇到错误会关闭)。

🌐 You can also run a built app from the terminal, giving you the Rust compiler notes (in case of errors) or your println messages. Browse to the file src-tauri/target/(release|debug)/[app name] and run it in directly in your console or double-click the executable itself in the filesystem (note: the console closes on errors with this method).

🌐 Enable Devtools Feature

要在生产构建中启用开发工具,你必须在 src-tauri/Cargo.toml 文件中启用 devtools Cargo 功能:

🌐 To enable the devtools in production builds, you must enable the devtools Cargo feature in the src-tauri/Cargo.toml file:

[dependencies]
tauri = { version = "...", features = ["...", "devtools"] }

🌐 Debugging the Core Process

核心进程由 Rust 提供支持,因此你可以使用 GDB 或 LLDB 对其进行调试。你可以按照[在 VS Code 中调试]指南,了解如何使用 LLDB VS Code 扩展调试 Tauri 应用的核心进程。

🌐 The Core process is powered by Rust so you can use GDB or LLDB to debug it. You can follow the Debugging in VS Code guide to learn how to use the LLDB VS Code Extension to debug the Core Process of Tauri applications.


Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站