A sophisticated scope-based runnable detection tool for Rust that supports multiple build systems (Cargo, Bazel, Rustc) and provides intelligent command generation for tests, benchmarks, binaries, and doc tests.
- 🔍 Smart Runnable Detection: Automatically detects tests, benchmarks, binaries, and doc tests at any position in your code
- 🏗️ Multi Build System Support: Works with Cargo, Bazel, and standalone Rust files
- 🎯 Precise Scope Detection: Uses tree-sitter for accurate AST parsing
- 🚀 Fast & Reliable: No compilation required for detection
cargo install cargo-runnerOr clone and build from source:
git clone https://github.com/cargo-runner/cargo-runner
cd cargo-runner
cargo install --path crates/clicargo new my-project
cd my-project
cargo runner initThis would work for simple or complex rust projects , all rust projects during init are added to linkedProjects in .cargo-runner.json file.
Initialize cargo-runner in your project. This command sets up the necessary configuration for your build system.
# Initialize in current directory
cargo runner init # generate config for cargo / bazel
# Initialize with specific options
cargo runner init --rustc # generate config so you can override rustc to your needs
cargo runner init --single-file # generate config so you can override single-file script to your needsRun the code at a specific location in a file. The tool will automatically detect what to run based on the cursor position.
# Run test at line 42
cargo runner run /path/to/src/lib.rs:42
# Run benchmark at line 156
cargo runner run /path/to/benches/benchmark.rs:156
# Run binary (detects main function)
cargo runner run /path/to/src/main.rs
# Run file without specific line (runs appropriate command for file type)
cargo runner run /path/to/src/bin/server.rsWhat it detects:
- Test functions (
#[test]) - Benchmark functions (
#[bench]) - Binary files with
main()function - Module tests (
mod tests) - Doc tests in comments
- Integration tests in
tests/directory
Analyze a file to see all runnable items it contains. This is useful for understanding what can be run in a file.
# Analyze entire file
cargo runner analyze /path/to/src/lib.rs
# Analyze at specific line (shows what would run at that position)
cargo runner analyze /path/to/src/lib.rs:42Example output:
📋 Detailed analyze output example
🔍 Analyzing: project-a/src/lib.rs
================================================================================
📄 File-level command:
🔧 Command breakdown:
• command: cargo
• subcommand: test
• package: project-a
• extraArgs: ["--lib"]
🚀 Final command: cargo test --package project-a --lib
📦 Type: Library (lib.rs)
📏 Scope: lines 1-90
✅ Found 7 runnable(s):
1. Run doc test for 'User'
📏 Scope: lines 2-13
📍 Module path: project-a
🧪 Contains doc tests
🔧 Command breakdown:
• command: cargo
• subcommand: test
• package: project-a
• extraArgs: ["--doc"]
• extraTestBinaryArgs: ["User"]
🚀 Final command: cargo test --doc --package project-a -- User
📦 Type: Doc test for 'User'
📁 Module path: project-a
2. Run doc test for 'impl User'
📏 Scope: lines 15-68
📍 Module path: project-a
🧪 Contains doc tests
🔧 Command breakdown:
• command: cargo
• subcommand: test
• package: project-a
• extraArgs: ["--doc"]
• extraTestBinaryArgs: ["User"]
🚀 Final command: cargo test --doc --package project-a -- User
📦 Type: Doc test for 'impl User'
📁 Module path: project-a
3. Run doc test for 'User::new'
📏 Scope: lines 32-55
📍 Module path: project-a
🧪 Contains doc tests
🔧 Command breakdown:
• command: cargo
• subcommand: test
• package: project-a
• extraArgs: ["--doc"]
• extraTestBinaryArgs: ["User::new"]
🚀 Final command: cargo test --doc --package project-a -- User::new
📦 Type: Doc test for 'User'::new
📁 Module path: project-a
4. Run doc test for 'User::echo'
📏 Scope: lines 57-67
📍 Module path: project-a
🧪 Contains doc tests
🔧 Command breakdown:
• command: cargo
• subcommand: test
• package: project-a
• extraArgs: ["--doc"]
• extraTestBinaryArgs: ["User::echo"]
🚀 Final command: cargo test --doc --package project-a -- User::echo
📦 Type: Doc test for 'User'::echo
📁 Module path: project-a
5. Run all tests in module 'tests'
📏 Scope: lines 70-90
🏷️ Attributes: 1 lines
🔧 Command breakdown:
• command: cargo
• subcommand: test
• package: project-a
• extraArgs: ["--lib"]
• extraTestBinaryArgs: ["tests"]
🚀 Final command: cargo test --package project-a --lib -- tests
📦 Type: Test module 'tests'
6. Run test 'test_it_works'
📏 Scope: lines 74-78
📍 Module path: tests
🏷️ Attributes: 1 lines
🔧 Command breakdown:
• command: cargo
• subcommand: test
• package: project-a
• extraArgs: ["--lib"]
• extraTestBinaryArgs: ["tests::test_it_works", "--exact"]
🚀 Final command: cargo test --package project-a --lib -- tests::test_it_works --exact
📦 Type: Test function 'test_it_works'
📁 Module path: tests
7. Run test 'test_user'
📏 Scope: lines 80-89
📍 Module path: tests
🏷️ Attributes: 1 lines
🔧 Command breakdown:
• command: cargo
• subcommand: test
• package: project-a
• extraArgs: ["--lib"]
• extraTestBinaryArgs: ["tests::test_user", "--exact"]
🚀 Final command: cargo test --package project-a --lib -- tests::test_user --exact
📦 Type: Test function 'test_user'
📁 Module path: tests
🎯 Command to run:
cargo test --package project-a --lib -- tests::test_user --exact
================================================================================
Cargo Runner automatically detects your build system in this order:
- Bazel - Looks for
BUILD.bazelorBUILDfiles - Cargo - Looks for
Cargo.toml - Rustc - Fallback for standalone
.rsfiles
For testing cargo-runner with complex Bazel setups, check out: https://github.com/codeitlikemiley/complex-bazel-setup
- Exact match: Specific test/benchmark functions
- Module tests:
mod testsblocks - Binary detection: Files with
main()function - Doc tests: Code blocks in doc comments
- File-level fallback: Appropriate command for file type
The tool correctly resolves module paths for individual tests for binaries, integration tests, and benchmarks.
e.g. tests::test_user
Automatic detection order:
- Bazel (presence of
BUILD.bazelorBUILD) - Cargo (presence of
Cargo.toml) - Rustc (fallback for standalone files)
Bazel project have both Cargo.toml , and MODULE.bazel and BUILD.bazel files.
We don't support WORKSPACE file for now it would be deprecated this year 2025.
Contributions are welcome! Please read our contributing guidelines and submit PRs.
MIT