| Crates.io | cargo-detect-package |
| lib.rs | cargo-detect-package |
| version | 1.0.1 |
| created_at | 2025-07-15 18:38:32.042527+00 |
| updated_at | 2025-09-11 02:19:26.205009+00 |
| description | A Cargo tool to detect the package that a file belongs to, passing the package name to a subcommand |
| homepage | |
| repository | https://github.com/folo-rs/folo |
| max_upload_size | |
| id | 1753693 |
| size | 63,777 |
A Cargo tool to detect the package that a file belongs to, passing the package name to a subcommand.
This tool automatically detects which Cargo package a given file belongs to within a workspace, and then executes a subcommand with the appropriate package scope. It supports two operating modes: cargo integration mode (default) and environment variable mode.
Install via cargo install cargo-detect-package and then:
cargo detect-package --path <PATH> [--via-env <ENV_VAR>] [--outside-package <ACTION>] <SUBCOMMAND>...
--path <PATH>: Path to the file for which to detect the package--via-env <ENV_VAR>: Optional. Pass the package name via environment variable instead of cargo arguments--outside-package <ACTION>: Optional. Action to take when path is not in any package (workspace, ignore, error). Defaults to workspace.<SUBCOMMAND>...: The command to execute with the detected package informationIn this mode, the tool automatically adds the appropriate cargo package arguments to the subcommand:
-p <package_name>--workspace# Build the package containing src/lib.rs
cargo detect-package --path packages/events/src/lib.rs build
# Prints: Detected package: events
# Executes: cargo build -p events
# Test the package containing a specific test file
cargo detect-package --path packages/many_cpus/tests/integration.rs test
# Prints: Detected package: many_cpus
# Executes: cargo test -p many_cpus
# Check a file in the workspace root (falls back to workspace by default)
cargo detect-package --path README.md check
# Prints: Path is not in any package, using workspace scope
# Executes: cargo check --workspace
# Error when a file is not in any package
cargo detect-package --path README.md --outside-package error check
# Prints: Error: Path is not in any package
# Exits with code 1, does not execute subcommand
# Ignore when a file is not in any package
cargo detect-package --path README.md --outside-package ignore check
# Prints: Path is not in any package, ignoring as requested
# Exits with code 0, does not execute subcommand
# Run clippy with additional arguments
cargo detect-package --path packages/events/src/lib.rs clippy -- -D warnings
# Prints: Detected package: events
# Executes: cargo clippy -p events -- -D warnings
{
"label": "rust: build (current package)",
"type": "cargo",
"command": "detect-package",
"args": [
"--path",
"${relativeFileDirname}",
"build",
"--all-features",
"--all-targets"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$rustc"
]
}
In this mode, the tool sets an environment variable with the detected package name
and executes a non-cargo command. This is useful for integration with build tools
like just, make, or custom scripts.
# Use with just command runner
cargo detect-package --path packages/events/src/lib.rs --via-env package just build
# Prints: Detected package: events
# Executes: just build (with package=events environment variable)
# Use with custom script
cargo detect-package --path packages/many_cpus/src/lib.rs --via-env PKG_NAME ./build.sh
# Prints: Detected package: many_cpus
# Executes: ./build.sh (with PKG_NAME=many_cpus environment variable)
# Workspace scope with environment variable (default behavior)
cargo detect-package --path README.md --via-env package just test
# Prints: Path is not in any package, using workspace scope
# Executes: just test (no environment variable set, allowing just to handle workspace scope)
# Error when not in package with environment variable mode
cargo detect-package --path README.md --via-env package --outside-package error just test
# Prints: Error: Path is not in any package
# Exits with code 1, does not execute subcommand
The tool requires that both the current directory and target path are within the same Cargo workspace. Cross-workspace operations are rejected with an error.
The tool's behavior when a file is not within any package is configurable via the --outside-package flag:
workspace (default): Falls back to workspace scope and executes the subcommand with --workspace flag or no environment variableignore: Does not execute the subcommand and exits with success (code 0)error: Does not execute the subcommand and exits with failure (code 1)Common scenarios that trigger outside-package behavior: