Rust や Go 言語などでコンパイルされた独自のCLIツールを Sidecar として実行する方法を解説します。
高性能なバックエンド処理をアプリに組み込む際に最適です。
前提条件
プラグインのインストールが必要です。
npm run tauri add shell
Permissions (権限) の設定
src-tauri/capabilities/default.json に以下の権限を追加します。
{
"permissions": [
...,
"shell:allow-open",
{
"identifier": "shell:allow-execute",
"allow": [
{
"name": "binaries/mytool",
"sidecar": true,
"args": true
}
]
}
]
}
手順
1. バイナリのビルドと配置
Rust であれば cargo build --release、Go であれば go build でバイナリを作成します。
ターゲットプラットフォームに合わせてファイル名を変更し、所定のディレクトリ(src-tauri/binaries 等)に配置します。
命名規則: <command>-<target-triple><extension>
例: mytool-aarch64-apple-darwin (Apple Silicon Mac)
ターゲットトリプル (target-triple) の確認方法:
以下のコマンドで現在の環境のトリプルを確認できます。
rustc -Vv | grep host
# Windows (PowerShell) の場合:
# rustc -Vv | Select-String "host:"
出力例: host: x86_64-pc-windows-msvc -> トリプルは x86_64-pc-windows-msvc です。
2. 設定 (tauri.conf.json)
{
"bundle": {
"externalBin": [
"binaries/mytool"
]
}
}
1. フロントエンドから作成する (TypeScript)
import { Command } from '@tauri-apps/plugin-shell';
async function runMyTool() {
// 引数つきで実行する例
const cmd = Command.sidecar('binaries/mytool', ['--json', 'input.txt']);
const output = await cmd.execute();
if (output.code === 0) {
console.log(output.stdout);
}
}
2. バックエンドから作成する (Rust)
tauri_plugin_shell::ShellExt を使用し、Sidecar として登録されたツールを実行します。
use tauri_plugin_shell::ShellExt;
use tauri::AppHandle;
#[tauri::command]
async fn run_my_tool(app: AppHandle) -> Result<String, String> {
let output = app.shell()
// Rust では tauri.conf.json のパスではなく「コマンド名」のみを指定します
.sidecar("mytool")
.map_err(|e| e.to_string())?
.args(["--json", "input.txt"]) // 引数の追加
.output()
.await
.map_err(|e| e.to_string())?;
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).to_string())
} else {
Err(String::from_utf8_lossy(&output.stderr).to_string())
}
}
トラブルシューティング
「指定されたパスが見つかりません」などのエラーが出る場合は、以下を確認してください。
- TypeScript:
tauri.conf.jsonと同じパス文字列(例:"binaries/mytool")を指定します。 - Rust: パスではなく コマンド名のみ(例:
"mytool")を指定します。
※ 引数を渡す場合、Permissions 設定で args が許可されている必要があります。