调试
对于 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.
在 Rust 中
¥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 控制台
¥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=1tauri 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 控制台
¥WebView Console
在 WebView 中右键单击,然后选择 Inspect Element
。这将打开一个类似于你习惯使用的 Chrome 或 Firefox 开发工具的 Web 检查器。你还可以在 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.
以编程方式打开 Devtools
¥Opening Devtools Programmatically
你可以使用 WebviewWindow::open_devtools
和 WebviewWindow::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
yarn tauri build --debug
pnpm tauri build --debug
deno task tauri build --debug
cargo 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).
启用 Devtools 功能
¥Enable Devtools Feature
:::danger 危险
devtools API 在 macOS 上是私有的。在 macOS 上使用私有 API 会阻止你的应用被 App Store 接受。
¥The devtools API is private on macOS. Using private APIs on macOS prevents your application from being accepted to the App Store.
:::
要在生产版本中启用 devtools,你必须在 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号