Fix compilation errors, linting issues, and test failures in the warp Rust codebase. Covers presubmit checks, WASM-specific errors, and running specific tests. Use when the user hits build errors, clippy or fmt failures, test failures, or needs to run or interpret presubmit before a PR.
fix-errors
修復 warp Rust 程式碼庫中的編譯錯誤、lint 問題與測試失敗。
概述
此技能協助解決開發過程中常見的問題,包括:
- 編譯錯誤(未使用的匯入、型別不符等)
- Lint 失敗(clippy 警告)
- 格式化違規
- WASM 特定錯誤
- 測試失敗
在開啟或更新 pull request 之前,所有 presubmit 檢查都必須通過。
Presubmit 檢查
一次執行所有 presubmit 檢查:
./script/presubmit
這會執行格式化、lint 以及所有測試。如果通過,你就可以準備發送 PR。
個別檢查
在除錯特定問題時分別執行檢查:
Rust 格式化:
cargo fmt -- --check
Clippy(完整工作區):
cargo clippy --workspace --exclude warp_completer --all-targets --all-features --tests -- -D warnings
cargo clippy -p warp_completer --all-targets --tests -- -D warnings
WASM Clippy:
cargo clippy --target wasm32-unknown-unknown --profile release-wasm-debug_assertions --no-deps
Objective-C/C/C++ 格式化:
./script/run-clang-format.py -r --extensions 'c,h,cpp,m' ./crates/warpui/src/ ./app/src/
所有測試:
cargo nextest run --no-fail-fast --workspace --exclude command-signatures-v2
cargo nextest run -p warp_completer --features v2
Doc 測試:
cargo test --doc
執行特定測試
單一套件:
cargo nextest run -p <package_name>
依測試名稱篩選:
cargo nextest run -E 'test(<substring>)'
特定套件加上篩選:
cargo nextest run -p <package_name> -E 'test(<substring>)'
顯示輸出(不擷取):
cargo nextest run -p <package> --nocapture
常見錯誤類型
未使用的匯入
移除編譯器標示的未使用 use 陳述式。
未使用的常數
移除已定義但從未使用的常數。
未知的匯入
為未定義的型別加入正確的 use 陳述式。搜尋程式碼庫以找到正確的模組路徑。
型別不符
更新函式呼叫,傳入正確型別的引數。常見修正:
- 當預期
&str時,使用.as_str()而非.clone() - 當需要參考時,使用
&value - 當預期
String但提供&str時,使用.to_string()
結構體欄位變更
當結構體新增/移除欄位時,更新所有建構或解構的地方:
- 結構體初始化
- 模式比對(
match、if let) - 解構賦值
函式簽章變更
當函式新增參數時,更新所有呼叫點以提供新的引數:
- 對於
bool參數:根據上下文傳入true或false - 對於
Option<T>參數:預設傳入None,或視需要傳入Some(value)
列舉變體變更
當新增列舉變體時,更新窮舉的 match 陳述式:
- 加入新的 match 分支並適當處理
- 模仿類似變體的實作模式
錯誤的 Trait 實作
修正回傳錯誤型別或未滿足 trait 限制的 trait 實作。
WASM 特定錯誤
WASM 建置(wasm32-unknown-unknown 目標)不支援檔案系統操作。使用檔案系統 API 的程式碼必須透過 local_fs 功能標誌來限制。
常見 WASM 錯誤:
- 僅在非 WASM 建置中使用的程式碼出現 dead code 警告
- 僅在
local_fs可用時才相關的未使用程式碼 - 需要檔案系統存取的測試
修正方式:
將測試限制在 local_fs 之後:
#[test]
#[cfg(feature = "local_fs")]
fn test_find_git_repo_with_worktree() {
// 使用檔案系統操作的測試
}
對僅在啟用 local_fs 時使用的型別有條件地允許 dead code:
#[cfg_attr(not(feature = "local_fs"), allow(dead_code))]
#[derive(Clone, EnumDiscriminants, Serialize)]
pub enum ExampleType {
// 僅在啟用 local_fs 時使用的變體
Variant1,
Variant2,
Variant3,
}
WASM 錯誤可透過執行以下指令發現:
cargo clippy --target wasm32-unknown-unknown --profile release-wasm-debug_assertions --no-deps
最佳實務
修正前:
- 閱讀完整的錯誤訊息以了解根本原因
- 檢查多個錯誤是否相關(修正一個可能解決其他問題)
- 對於 trait/型別錯誤,確認你了解預期型別與實際型別
- 對於 WASM 錯誤,檢查程式碼是否需要透過
local_fs限制
修正時:
- 若有多個問題,一次修正一種錯誤類型
- 經常執行
cargo check以驗證修正 - 對於 WASM 錯誤,執行 WASM clippy 以驗證修正
- 對於複雜的變更,修正後執行相關測試
修正後:
- 推送前務必執行
cargo fmt和cargo clippy - 在開啟或更新 PR 前執行完整的 presubmit 腳本。使用
create-pr技能取得更詳細的說明 - 確認你修改的區域測試通過






