This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Cribo is a Rust-based source bundler for Python projects. It merges a multi-module Python codebase into a single .py file by inlining all first-party modules. Cribo is distributed as a command-line interface on both PyPI and npm.
- Inline first-party modules while preserving original behavior
- Tree-shaking to include only necessary modules
- Detect and remove unused imports
- Generate a
requirements.txtlisting all third-party dependencies - Customizable import classification
- Support for PYTHONPATH and virtual environments
- The bundled output must be functionally equivalent to the original code.
- The resulting bundle should remain clear and easy to understand, particularly for LLM agents. Specifically:
- Preserve the original code structure as much as possible; avoid renaming, restructuring, or wrapping unless required to maintain functionality.
- Perform all resolvable computations and wiring at bundle time to minimize runtime evaluation.
- Runtime performance of the bundle should match or exceed the original code: avoid unnecessary wrappers and favor direct inlined references.
- Maintaining full Python module semantics (e.g.,
__name__,__all__) is only necessary when it affects functionality; otherwise, static analysis and rewriting suffice. - Guaranteeing theoretical compatibility with every potential side effect is not required; address clear side effects without introducing complexity for unlikely scenarios.
The project is organized as a Rust workspace with the main crate in crates/cribo. The architecture follows a clear separation of concerns with dedicated modules for analysis, code generation, and AST traversal.
THE REAL CRITICAL PATH: How Modules Get Bundled
-
Application Orchestration →
crates/cribo/src/orchestrator.rs::BundleOrchestratorbundle()/bundle_to_string()→ Entry pointsbundle_core()→ Module discovery, parsing, dependency resolutionemit_static_bundle()→ Delegates to PhaseOrchestrator
-
🔥 Phase-Based Bundling →
crates/cribo/src/code_generator/phases/orchestrator.rs::PhaseOrchestratorCoordinates 9 bundling phases:PhaseOrchestrator::bundle(&mut bundler, ¶ms)→ Main entryInitializationPhase→ Setup, future importsPreparationPhase→ Trim imports, index ASTsClassificationPhase→ Inlinable vs wrapper decisionProcessingPhase→ Module emission in dependency orderEntryModulePhase→ Special entry module handlingPostProcessingPhase→ Namespace attachments, proxies
-
Module Classification →
crates/cribo/src/analyzers/module_classifier.rs::classify_modules()// THE decision that determines bundle structure: if has_side_effects || has_invalid_identifier || needs_wrapping_for_circular: → wrapper_modules.push() // Becomes init function with circular import guards else: → inlinable_modules.push() // Directly inserted into bundle
-
Side Effect Detection →
crates/cribo/src/visitors/side_effect_detector.rsvisit_expr()→Expr::Call(_),Expr::Lambda(_)= SIDE EFFECTvisit_stmt()→Stmt::ClassDefwithmetaclass,Stmt::Expr(non-literals) = SIDE EFFECT
-
Module Processing →
crates/cribo/src/code_generator/phases/processing.rs::ProcessingPhase- Processes modules in topological order
- Two-phase emission for circular dependencies
- Routes to inlinable or wrapper handlers
Inlining Path (crates/cribo/src/code_generator/bundler.rs + crates/cribo/src/code_generator/inliner.rs)
process_inlinable_module()→ Handles inlined modulesInliner::inline_module()→ Actually inlines the module content- Transforms imports via
RecursiveImportTransformer - Direct variable assignments, no function wrapper
Wrapper Path (crates/cribo/src/code_generator/bundler.rs)
process_wrapper_module()→ Handles wrapper modulescreate_wrapper_module()→ Creates the init functionmodule_wrapper::create_wrapper_module()→ Generates init function with__initializing__and__initialized__guards- Returns module namespace object (types.SimpleNamespace)
Import Rewriting (crates/cribo/src/code_generator/bundler.rs)
transform_bundled_import()→ Routes import to correct handlertransform_wrapper_wildcard_import()→ Special case forfrom wrapper import *transform_wrapper_symbol_imports()→ Handlesfrom wrapper import symbol
1. The Wrapper vs Inline Decision (crates/cribo/src/analyzers/module_classifier.rs::classify_modules())
- THE most important decision - determines entire module structure
- Check
module_has_side_effects()call - Check
has_invalid_identifierusingis_identifier() - Check
needs_wrapping_for_circularfrom circular modules set
2. Circular Dependency Classification (crates/cribo/src/analyzers/dependency_analyzer.rs::classify_cycle_type())
CircularDependencyType::FunctionLevel: Can be resolved by moving importsCircularDependencyType::ClassLevel: Inheritance cycles, needs careful handlingCircularDependencyType::ModuleConstants: UNRESOLVABLE - will failCircularDependencyType::ImportTime: Needs wrapper functions- Check
is_parent_child_package_cycle()for package patterns
3. Global Variable Lifting (crates/cribo/src/analyzers/global_analyzer.rs::analyze())
- Problem:
global xin wrapper function needs real module-levelx - Solution: Lift to bundle top-level with unique names
collect_from_target()collects module-level varsvisit_stmt()forStmt::Globaltracks declarations- Result in
ModuleGlobalInfostruct
4. Tree Shaking Mark & Sweep (crates/cribo/src/tree_shaking.rs::analyze())
- Entry:
TreeShaker::analyze()starts from entry module mark_used_symbols()does recursive marking- For
import module:mark_direct_import()keeps ALL exports - For
from module import x: Only marks specific symbol resolve_relative_with_context()handles relative imports- Gotcha: Circular deps with tree-shaking can break
Module Registry (crates/cribo/src/orchestrator.rs struct ModuleRegistry)
ModuleRegistry {
modules: FxIndexMap<ModuleId, ModuleInfo>,
name_to_id: FxIndexMap<String, ModuleId>,
path_to_id: FxIndexMap<PathBuf, ModuleId>,
}Bundler State (crates/cribo/src/code_generator/bundler.rs struct Bundler)
Bundler {
module_synthetic_names: FxIndexMap<ModuleId, String>,
module_init_functions: FxIndexMap<ModuleId, String>,
wrapper_modules: FxIndexSet<ModuleId>,
inlined_modules: FxIndexSet<ModuleId>,
}Dependency Graph (crates/cribo/src/dependency_graph.rs struct DependencyGraph)
DependencyGraph {
modules: FxIndexMap<ModuleId, ModuleDepGraph>,
module_names: FxIndexMap<String, ModuleId>,
}Phase Modules (crates/cribo/src/code_generator/phases/)
orchestrator.rs→ PhaseOrchestrator coordinates all phasesinitialization.rs→ InitializationPhase: setup, future importsclassification.rs→ ClassificationPhase: inlinable vs wrapperprocessing.rs→ ProcessingPhase: main module emission loopentry_module.rs→ EntryModulePhase: special entry handlingpost_processing.rs→ PostProcessingPhase: namespaces, proxies
"Module unexpectedly wrapped"
→ Set breakpoint in crates/cribo/src/analyzers/module_classifier.rs::classify_modules() at the if statement
→ Check crates/cribo/src/visitors/side_effect_detector.rs::visit_expr() for Expr::Call or Expr::Lambda
→ Look for "has side effects - using wrapper" in debug logs
"Circular import error"
→ crates/cribo/src/code_generator/phases/processing.rs::process_circular_group()
→ crates/cribo/src/analyzers/dependency_analyzer.rs::analyze_circular_dependencies()
→ Look for CircularDependencyType::ModuleConstants (unresolvable)
"Import not transformed correctly"
→ crates/cribo/src/code_generator/bundler.rs::transform_bundled_import() - THE router for all import transforms
→ Check RecursiveImportTransformer::transform_import()
→ Trace which handler was selected in import_transformer/handlers/
"Symbol undefined after bundling"
→ crates/cribo/src/code_generator/phases/processing.rs::execute() - check module processing
→ crates/cribo/src/tree_shaking.rs::mark_used_symbols() - tree-shaking analysis
→ Verify topological sort order
"Global variable not working in wrapper"
→ crates/cribo/src/code_generator/bundler.rs::process_wrapper_module()
→ crates/cribo/src/analyzers/global_analyzer.rs::analyze()
→ crates/cribo/src/code_generator/module_transformer.rs::transform_module_with_globals()
"Module not bundled at all"
→ crates/cribo/src/orchestrator.rs::bundle_core() - module discovery
→ crates/cribo/src/code_generator/phases/orchestrator.rs::PhaseOrchestrator::bundle() - orchestration
→ Check topological sort
# See module classification decisions
RUST_LOG=debug cargo run -- --entry main.py --stdout 2>&1 | grep "side effects\|wrapper\|inline"
# Trace import transformation
RUST_LOG=trace cargo run -- --entry main.py --stdout 2>&1 | grep "transform.*import"
# Check circular dependency detection
RUST_LOG=debug cargo run -- --entry main.py --stdout 2>&1 | grep "circular\|cycle"
# Tree-shaking decisions
RUST_LOG=debug cargo run -- --entry main.py --stdout 2>&1 | grep "Tree-shaking\|Keeping\|Removing"- Module Cache (
crates/cribo/src/orchestrator.rs::process_module()) - Caches parsed ASTs inmodule_cache - Import Deduplication (
crates/cribo/src/code_generator/import_deduplicator.rs::deduplicate_imports()) - O(n²) comparisons - Tree Shaking (
crates/cribo/src/tree_shaking.rs::analyze()) - Full graph traversal - Side Effect Detection (
crates/cribo/src/visitors/side_effect_detector.rs::module_has_side_effects()) - Two-pass visitor
cribo --entry src/main.py --output bundle.py [options]
# Output to stdout instead of file (useful for debugging)
cribo --entry src/main.py --stdout [options]
# Common options
--emit-requirements # Generate requirements.txt with third-party dependencies
--no-tree-shake # Disable tree-shaking optimization (tree-shaking is enabled by default)
-v, --verbose... # Increase verbosity (can be repeated: -v, -vv, -vvv)
# No flag: warnings/errors only
# -v: informational messages
# -vv: debug messages
# -vvv: trace messages
--stdout # Output bundled code to stdout instead of a fileTree-shaking removes unused code from the bundle to reduce size:
- Analyzes which symbols are actually used starting from the entry point
- Preserves all symbols from directly imported modules (
import moduleorfrom pkg import module) - Respects
__all__declarations and side effects - Handles import aliases correctly
- Use
--no-tree-shaketo disable if you need to preserve all code
Known Limitation: Complex circular dependencies with generated init functions may cause issues with tree-shaking. Use --no-tree-shake if you encounter undefined symbol errors.
The --stdout flag is particularly useful for debugging and development workflows:
# Quick inspection of bundled output without creating files
cribo --entry main.py --stdout
# Pipe to tools for syntax validation:
# - set -o pipefail propagates a cribo failure through the pipeline
# - compile() reads from stdin and applies full compiler checks
# (py_compile cannot read source from stdin; ast.parse skips semantic checks)
set -o pipefail
cribo --entry main.py --stdout | python -c "import sys; compile(sys.stdin.buffer.read(), '<stdin>', 'exec')"
# Combine with verbose logging (logs go to stderr, code to stdout)
cribo --entry main.py --stdout -vvKey Benefits:
- No temporary files created
- All log output properly separated to stderr
- Perfect for piping to other tools
- Ideal for containerized environments
- Excellent for quick debugging workflows
ALL TESTS THAT EXIST ON MAIN BRANCH ARE ALWAYS WORKING! YOU MUST NEVER DOUBLE CHECK THAT! THERE ARE NO "PRE-EXISTING ISSUES"! DON'T SPEND TIME ON SEARCHING FOR AN ESCAPE HATCH - DO INVESTIGATE TO FIND THE ROOT CAUSE OF A PROBLEM
CRITICAL: Use this exact template for ANY git operation
- GitHub Tools Check: Verify
ghCLI authenticated - Dependencies: Run
cargo nextest run --workspacefor clean starting state
- Create feature branch:
git checkout -b fix/descriptive-name origin/main - Implement changes
- Test validation:
cargo test --workspace(must pass) - Clippy validation:
cargo clippy --workspace --all-targets --all-features(must be clean) - Commit with conventional message
- Push with upstream:
git push -u origin <branch-name>
- Use gh CLI:
gh pr create - Include comprehensive description (Summary, Changes, Test Results)
- Add coverage impact note if significant
- Add performance impact note if benchmarks show changes
When you need to check how something works on main branch while working on a feature branch, use git worktree instead of git checkout.
- Preserves your current uncommitted changes
- Keeps development context intact
- Enables side-by-side comparison
# Create worktree for main
git worktree add ../main-ref main
# Check the code
cd ../main-ref
# ... examine files ...
cd -
# Clean up when done
git worktree remove ../main-refgit worktree add <path> <branch> # Create
git worktree list # List all
git worktree remove <path> # RemoveOnly use git checkout when you have no uncommitted changes and are permanently switching branches.
# During development (frequent checks)
cargo coverage-text
# Detailed coverage analysis
cargo coverage
# For CI-style validation
cargo coverage-lcov# Before starting work - save baseline
cargo bench-save
# or
./scripts/bench.sh --save-baseline main
# After implementing changes - compare
cargo bench-compare
# or
./scripts/bench.sh --baseline main
# View detailed HTML report
./scripts/bench.sh --openMANDATORY PRACTICES:
- Always check
TodoReadbefore starting new work - Update todos immediately when scope changes
- When resuming work, first verify current state with
git status - Mark todos completed IMMEDIATELY when finished, not in batches
# Build for development (creates a local installable package)
uvx maturin develop
# Build release package
uvx maturin build --release# Generate npm packages
node scripts/generate-npm-packages.js
# Build npm binaries
./scripts/build-npm-binaries.sh# Run all tests
cargo nextest run --workspaceThe bundling snapshot tests use Insta's glob feature for automatic fixture discovery. You can run specific fixtures using glob filters:
# Run a specific fixture using environment variable
INSTA_GLOB_FILTER="**/stickytape_single_file/main.py" cargo nextest run --test test_bundling_snapshots --cargo-quiet --cargo-quiet
# Run all fixtures matching a pattern
INSTA_GLOB_FILTER="**/future_imports_*/main.py" cargo nextest run --test test_bundling_snapshots --cargo-quiet --cargo-quiet
# Run fixture with debug output to see which fixture is running
INSTA_GLOB_FILTER="**/stickytape_single_file/main.py" cargo nextest run --no-capture --test test_bundling_snapshots --cargo-quiet --cargo-quiet
# List available fixtures (useful for finding fixture names)
find crates/cribo/tests/fixtures -name "main.py" -type f | sed 's|.*/fixtures/||' | sed 's|/main.py||' | sortCommon fixture patterns:
stickytape_*- Compatibility tests from stickytape projectfuture_imports_*- Tests for future import handlingast_rewriting_*- Tests for AST transformation featurespyail_*- Expected failure fixtures (must fail by direct python execution)xfail_*- Expected failure fixtures (prefix with xfail_ - bundled code MUST fail)
Tips:
- The glob filter matches against the full path relative to the glob base directory
- Use
**to match any number of directories - The fixture name is the directory name containing
main.py - Fixtures are automatically discovered - just add a new directory with
main.py
- Temporary Directory Usage: When the agent requires a temporary directory for input or output files, it MUST use the
target/tmpdirectory.
MANDATORY: As a software-engineer AI agent, you must never factor in human limitations—such as limited time or bandwidth, business priorities, implementation complexity, research requirements, or any other "human" constraint—when making technical decisions or writing recommendations. Instead, always select and articulate the technically optimal solution, judged solely on criteria like:
- Performance (e.g., latency, throughput)
- Physical resource constraints (e.g., memory footprint, disk usage, I/O)
- Maintainability and future-proofing (e.g., scalability, extensibility, ease of refactoring)
Under no circumstances should you justify a design or implementation by citing "lack of time," "limited resources," "tight deadlines," or similar human factors.
- Always use structured logging instead of
println!for debug output:use log::{debug, info, warn, error}; - Use appropriate log levels:
debug!()for detailed diagnostic information useful during developmentinfo!()for general information about program executionwarn!()for potentially problematic situationserror!()for error conditions that should be addressed
- If debug logging was essential to find a bug in the codebase, that logging should be kept in the codebase at the appropriate log level to aid future debugging
- Avoid temporary
println!statements - replace them with proper logging before committing code - Use structured logging with context where helpful:
debug!("Processing file: {}", file_path)
MANDATORY: Considering the potential use of this tool in deployment scenarios, it is essential to aim for deterministic, reproducible bundle output. This enables users to:
- Avoid unnecessary redeployments when source code hasn't meaningfully changed
- Simplify change diff inspection and validation
- Maintain predictable deployment pipelines
- Enable reliable content-based caching and optimization
This principle explains:
- Why we have disallowed types and methods in
.clippy.toml(e.g.,HashSet→IndexSetfor deterministic iteration order) - Why we must apply sorting or deterministic rules for any output where order doesn't matter
Implementation Rules:
- Sort imports:
from foo import d, a, b→from foo import a, b, d - Sort collections: When outputting multiple items, always apply consistent ordering
- Stable iteration: Use
IndexMap/IndexSetinstead ofHashMap/HashSetfor deterministic order - Consistent formatting: Apply the same formatting rules regardless of input order
- Reproducible timestamps: Avoid embedding timestamps or random values in output
Testing Determinism:
- Run bundler multiple times on same input - output must be identical
- Test with different module discovery orders - final bundle must be same
- Verify sorting applies to all user-visible output elements
MANDATORY: Before implementing custom test logic for bundling features, ALWAYS evaluate if the existing generic snapshot testing framework can be used or extended. This framework provides comprehensive testing with minimal implementation effort.
Framework Location: crates/cribo/tests/test_bundling_snapshots.rs
How It Works:
- Automatic Discovery: Scans
crates/cribo/tests/fixtures/for test directories - Convention-Based: Each directory with
main.pybecomes a test case automatically - Dual Snapshots: Generates both bundled code and execution result snapshots
- Deterministic: All output is sorted and reproducible across runs
Generated Snapshots:
bundled_code@my_new_feature.snap: Clean Python code showing bundling structureexecution_results@my_new_feature.snap: Structured execution results with status/output
ALWAYS prefer this framework when creating a new functionality or fixing a newly discovered regression.
- Never insert hardcoded literals in production code solely to satisfy a test.
- All production logic must implement genuine functionality; tests should validate real behavior, not bypass it.
- If you need to simulate or stub behavior for testing, use dedicated test files or mocking frameworks—do not alter production code.
- Any attempt to hardcode a test value in production code is strictly forbidden and should be treated as a critical violation.
- Violations of this policy must be reported and the offending code reverted immediately.
- Before generating, editing, or refactoring any Rust code, automatically locate and parse the project's
.clippy.tomlfile. - Extract the arrays under
disallowed-typesanddisallowed-methods. Treat each listedpathormethodas an absolute prohibition. - Never emit or import a type identified in
disallowed-types. For example, ifstd::collections::HashSetappears in the list, do not generate any code that uses it—use the approved alternative (e.g.,indexmap::IndexSet) instead. - Never invoke or generate code calling a method listed under
disallowed-methods. If a method is disallowed, replace it immediately with the approved pattern or API. - If any disallowed type or method remains in the generated code, treat it as a critical error: halt code generation for that snippet, annotate the violation with the specific reason from
.clippy.toml, and refuse to proceed until the violation is removed. - Continuously re-validate against
.clippy.tomlwhenever generating new code or applying automated fixes—do not assume a one-time check is sufficient. - Log each check and violation in clear comments or warnings within the pull request or code review context so that maintainers immediately see why a disallowed construct was rejected.
MANDATORY: Since cribo only exposes a binary CLI interface (not a library API), unused methods and functions MUST be removed immediately rather than annotated with deprecation markers.
- No deprecation annotations: Do not use
#[deprecated],#[allow(dead_code)], or similar annotations to preserve unused code - Binary-only interface: This project does not maintain API compatibility for external consumers - all code must serve the current CLI functionality
- Dead code elimination: Aggressively remove any unused functions, methods, structs, or modules during refactoring
- Immediate cleanup: When refactoring or implementing features, remove unused code paths immediately rather than marking them for future removal
When implementing or researching functionality, follow this order:
-
FIRST: Generate and examine local documentation
cargo doc --document-private-items
-
SECOND: Use Context7 for external libraries (only if local docs insufficient)
-
FINAL: Use GitHub MCP tools for implementation patterns (only when steps 1&2 insufficient)
When implementing functionality, consult these high-quality repositories:
- astral-sh/ruff - For Python AST handling, rule implementation, configuration patterns
- astral-sh/uv - For package resolution, dependency management, Python ecosystem integration
- web-infra-dev/rspack - For module graph construction, dependency resolution
Check references/ directory for local clones of above and some other examples
🚨 CRITICAL REQUIREMENT 🚨: Before claiming that any implementation is complete or successful, you MUST run the complete validation suite:
# 1. Run all tests in the workspace
cargo test --workspace
# 2. Run clippy on all targets (CI uses --all-features which enables additional lints)
cargo clippy --workspace --all-targets --all-features
# 3. Fix any clippy errors or warnings
# NEVER use #[allow] annotations as a "fix" - do actual refactoringNO EXCEPTIONS: Do not declare success, claim completion, or say "implementation is working" without running both commands above and ensuring they pass without errors. This applies to:
- Feature implementations
- Bug fixes
- Refactoring
- Any code changes
If tests fail or clippy reports issues, the implementation is NOT complete until these are resolved.
- When given an URL - parse the incoming URL
https://github.com/ophidiarium/cribo/pull/<PR_NUMBER>#discussion_r<COMMENT_ID>
<PR_NUMBER>→ digits between/pull/and#.<COMMENT_ID>→ digits afterdiscussion_r.
- Fetch the comment body
gh api repos/ophidiarium/cribo/pulls/comments/<COMMENT_ID> \
--jq '
"id: \(.id)
pr_number: \(.pull_request_url | split("/") | last)
author: \(.user.login)
created_at: \(.created_at)
file: \( .path )
line: \( .start_line )
--- BEGIN_BODY ---
\(.body)
--- END_BODY ---"'The text between BEGIN_BODY/END_BODY is what you must act on.
- Apply every suggestion immediately
- Treat even “nitpick” remarks as mandatory.
- Do not leave TODOs, placeholders, or defer fixes.
- Commit & push
5.Reply directly to that comment
- DO NOT create a new review
- DO NOT add a top level issue comment
- REPLY DIRECTLY AND SPECIFICALLY to the original comment:
gh api repos/ophidiarium/cribo/pulls/<PR_NUMBER>/comments/<COMMENT_ID>/replies \
-X POST -f body='✅ Addressed in <hash>. Thanks!'Replace <hash> with the short commit SHA.
Follow these five steps exactly to process a GitHub review comment.
If lost in git flow:
- Run
git statusto understand current state - Check
TodoReadto see where you left off - Verify which phase you're in based on branch and remote state
- Continue from appropriate checklist item
If review comments missed:
- Check PR comments immediately
- Create todo item for each comment
- Address systematically before any other work
- Don't add timing complexity estimation to any documents - you don't know the team velocity
- When running on macOS, you should try
gsedinstead ofsedfor GNU sed compatibility on macOS - MANDATORY: When addressing a clippy issue, never treat
#[allow]annotations as a solution—perform actual refactoring to resolve the issue - Remember you have full ruff repository cloned locally at references/type-strip/ruff so you may search in files easier
- lefhook (git hooks) config is at lefthook.yml
- use
bunto manage Node.js dependencies andbunxto run npm packages - use ast-grep if needed
- NEVER drop stashes!
- There are NEVER pre-existing test failures. Every feature development starts from the
mainbranch, which is always in a clean state with all tests passing. If any test fails during or after a change, immediately investigate the root cause—do not assume the failure was present before your work. Never waste time considering the possibility of a pre-existing broken test. - Nightly reserved keywords:
genis reserved on nightly Rust 2026+. Usegenerator_exprinstead ofgeninExpr::Generator(gen)patterns. - Symbol reordering is currently unreachable:
reorder_statements_for_circular_module+SymbolDependencyGraphare populated but never consumed at runtime (circular → always wrappers, entry → returns early). See comment on the function insymbols.rs. - Prefer ruff visitor pattern for AST traversal: Use
ruff_python_ast::visitor::Visitortrait withwalk_stmt/walk_exprinstead of manual match arms when traversing all statement/expression types. SeeClassBodyRefCollectorincircular_deps.rs.
ALL TESTS THAT EXIST ON MAIN BRANCH ARE ALWAYS WORKING! YOU MUST NEVER DOUBLE CHECK THAT! THERE ARE NO "PRE-EXISTING ISSUES"! DON'T SPEND TIME ON SEARCHING FOR AN ESCAPE HATCH - DO INVESTIGATE TO FIND THE ROOT CAUSE OF A PROBLEM