Skip to content
Tauri 中文网

在 JetBrains IDE 中调试

在本指南中,我们将设置 JetBrains RustRover 以调试 Tauri 应用的核心流程。它也主要适用于 IntelliJ 和 CLion。

¥In this guide, we’ll be setting up JetBrains RustRover for debugging the Core Process of your Tauri app. It also mostly applies to IntelliJ and CLion.

设置 Cargo 项目

¥Setting up a Cargo project

根据项目中使用的前端堆栈,项目目录可能是或可能不是 Cargo 项目。默认情况下,Tauri 将 Rust 项目放在名为 src-tauri 的子目录中。仅当 Rust 也用于前端开发时,它才会在根目录中创建一个 Cargo 项目。

¥Depending on which frontend stack is used in a project, the project directory may or may not be a Cargo project. By default, Tauri places the Rust project in a subdirectory called src-tauri. It creates a Cargo project in the root directory only if Rust is used for frontend development as well.

如果顶层没有 Cargo.toml 文件,则需要手动附加项目。打开 Cargo 工具窗口(在主菜单中,转到查看 | 工具窗口 | Cargo),单击工具栏上的 +(附加 Cargo 项目),然后选择 src-tauri/Cargo.toml 文件。

¥If there’s no Cargo.toml file at the top level, you need to attach the project manually. Open the Cargo tool window (in the main menu, go to View | Tool Windows | Cargo), click + (Attach Cargo Project) on the toolbar, and select the src-tauri/Cargo.toml file.

或者,你可以通过将以下文件添加到项目的根目录来手动创建顶层 Cargo 工作区:

¥Alternatively, you could create a top-level Cargo workspace manually by adding the following file to the project’s root directory:

Cargo.toml
[workspace]
members = ["src-tauri"]

在继续之前,请确保你的项目已完全加载。如果 Cargo 工具窗口显示工作区的所有模块和目标,那么你就可以开始了。

¥Before you proceed, make sure that your project is fully loaded. If the Cargo tool window shows all the modules and targets of the workspace, you’re good to go.

设置运行配置

¥Setting up Run Configurations

你需要设置两个单独的运行/调试配置:

¥You will need to set up two separate Run/Debug configurations:

  • 一个用于在调试模式下启动 Tauri 应用,

    ¥one for launching the Tauri app in debugging mode,

  • 另一个用于运行你选择的前端开发服务器。

    ¥another one for running your frontend development server of choice.

Tauri 应用

¥Tauri App

  1. 在主菜单中,转到运行 | 编辑配置。

    ¥In the main menu, go to Run | Edit Configurations.

  2. 在运行/调试配置对话框中:

    ¥In the Run/Debug Configurations dialog:

  • 要创建新的配置,请单击工具栏上的 + 并选择 Cargo。

    ¥To create a new configuration, click + on the toolbar and select Cargo.

Add Run/Debug Configuration

创建完成后,我们需要配置 RustRover,以便它指示 Cargo 在没有任何默认功能的情况下构建我们的应用。这将告诉 Tauri 使用你的开发服务器而不是从磁盘读取资源。通常,此标志由 Tauri CLI 传递,但由于我们在这里完全回避了这一点,因此我们需要手动传递标志。

¥With that created, we need to configure RustRover, so it instructs Cargo to build our app without any default features. This will tell Tauri to use your development server instead of reading assets from the disk. Normally this flag is passed by the Tauri CLI, but since we’re completely sidestepping that here, we need to pass the flag manually.

Add --no-default-features flag

现在我们可以选择将运行/调试配置重命名为更容易记住的名称,在此示例中我们将其称为 “运行 Tauri 应用”,但你可以随意命名。

¥Now we can optionally rename the Run/Debug Configuration to something more memorable, in this example we called it “Run Tauri App”, but you can name it whatever you want.

Rename Configuration

开发服务器

¥Development Server

上面的配置将直接使用 Cargo 来构建 Rust 应用并将调试器附加到它。这意味着我们完全避开了 Tauri CLI,因此不会执行 beforeDevCommandbeforeBuildCommand 等功能。我们需要通过手动运行开发服务器来处理这个问题。

¥The above configuration will use Cargo directly to build the Rust application and attach the debugger to it. This means we completely sidestep the Tauri CLI, so features like the beforeDevCommand and beforeBuildCommand will not be executed. We need to take care of that by running the development server manually.

要创建相应的运行配置,你需要检查实际使用的开发服务器。查找 src-tauri/tauri.conf.json 文件并找到以下行:

¥To create the corresponding Run configuration, you need to check the actual development server in use. Look for the src-tauri/tauri.conf.json file and find the following line:

"beforeDevCommand": "pnpm dev"

对于 npmpnpmyarn,你可以使用 npm Run Configuration,例如:

¥For npm, pnpm, or yarn, you could use the npm Run Configuration, for example:

NPM Configuration

确保你在命令、脚本和包管理器字段中具有正确的值。

¥Make sure you have the correct values in the Command, Scripts, and Package Manager fields.

如果你的开发服务器是基于 Rust 的 WebAssembly 前端框架的 trunk,则可以使用通用 Shell 脚本运行配置:

¥If your development server is trunk for Rust-based WebAssembly frontend frameworks, you could use the generic Shell Script Run Configuration:

Trunk Serve Configuration

启动调试会话

¥Launching a Debugging Session

要启动调试会话,你首先需要运行开发服务器,然后通过单击运行配置切换器旁边的调试按钮开始调试 Tauri 应用。RustRover 将自动识别项目中任何 Rust 文件中的断点,并在第一次命中时停止。

¥To launch a debugging session, you first need to run your development server, and then start debugging the Tauri App by clicking the Debug button next to the Run Configurations Switcher. RustRover will automatically recognize breakpoints placed in any Rust file in your project and stop on the first one hit.

Debug Session

从这一点开始,你可以探索变量的值,进一步深入代码,并详细检查运行时发生的情况。

¥From this point, you can explore the values of your variables, step further into the code, and check what’s going at runtime in detail.


Tauri 中文网 - 粤ICP备13048890号