trunk icon indicating copy to clipboard operation
trunk copied to clipboard

fix: improve workspace detection for workspace members

Open naoNao89 opened this issue 5 months ago • 3 comments

Summary

Fixes workspace detection when running trunk from within workspace member directories. Previously, trunk would fail to detect the correct package when run from a workspace member directory, now it properly handles both standalone packages and workspace scenarios.

Fixes: #909
Related: #719

Problem

The original `CargoMetadata::new()` method only used `metadata.root_package()` which works for standalone packages but fails for workspace members. When running trunk from within a workspace member directory (e.g., `my-workspace/frontend/`), trunk would fail with "could not find the root package of the target crate".

Solution

Enhanced the workspace detection logic to:

  1. Preserve manifest path context - Store the original manifest path to match against workspace members
  2. Add comprehensive package detection - New `find_target_package()` method that handles:
    • Standalone packages (via `root_package()` - maintains backward compatibility)
    • Workspace members (via `workspace_packages()` with path matching)
    • Path canonicalization for robust matching across different path formats
  3. Improve error handling - Provide helpful error messages listing available workspace members

Changes

  • Modified: `src/config/manifest.rs`
    • Enhanced `new()` method to preserve manifest path context
    • Replaced simple `from_metadata()` with `from_metadata_with_manifest_path()`
    • Added `find_target_package()` with comprehensive workspace detection logic
    • Improved error messages with detailed workspace member information

Testing & Validation

Manual Testing

  1. Create a test workspace:
# Create workspace structure
mkdir test-workspace && cd test-workspace

# Create workspace root Cargo.toml
cat > Cargo.toml << 'EOF'
[workspace]
members = [\"frontend\", \"backend\"]
resolver = \"2\"
EOF

# Create frontend member
mkdir frontend && cd frontend
cargo init --name frontend --lib
echo 'serde = \"1.0\"' >> Cargo.toml
cd ..

# Create backend member  
mkdir backend && cd backend
cargo init --name backend --lib
echo 'serde = \"1.0\"' >> Cargo.toml
cd ..
  1. Test workspace detection:
# Build trunk with the fix
cargo build

# Test from workspace root (should work before and after)
./target/debug/trunk config show

# Test from workspace member (this should now work!)
cd frontend
../../target/debug/trunk config show
cd ../backend  
../../target/debug/trunk config show

Expected Results

Before the fix:

  • ❌ Running trunk from workspace member directories would fail
  • ❌ Error: "could not find the root package of the target crate"

After the fix:

  • ✅ Works from workspace root directories (backward compatible)
  • ✅ Works from workspace member directories
  • ✅ Provides helpful error messages when packages can't be found
  • ✅ Handles edge cases (single member, multiple members, etc.)

Backward Compatibility

Fully backward compatible - standalone packages continue to work exactly as before via the `root_package()` path.

Additional Notes

  • Uses `dunce::canonicalize()` for robust path matching across different platforms
  • Maintains the existing public API - no breaking changes
  • Error messages now include helpful debugging information about available workspace members" Terminal

naoNao89 avatar Jul 10 '25 08:07 naoNao89

@naoNao89 I am curios, was there any AI involved in creating this PR?

ctron avatar Jul 10 '25 13:07 ctron

@naoNao89 I am curios, was there any AI involved in creating this PR?

just claude 4

naoNao89 avatar Jul 10 '25 14:07 naoNao89

@naoNao89 I am curios, was there any AI involved in creating this PR?

just claude 4

Ok, was any code generated by AI? If so, please add a Assisted-by: git footer naming the tool used.

ctron avatar Jul 11 '25 08:07 ctron