Skip to content

Conversation

naoNao89
Copy link

@naoNao89 naoNao89 commented Jul 10, 2025

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

@ctron
Copy link
Collaborator

ctron commented Jul 10, 2025

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

@naoNao89
Copy link
Author

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

just claude 4

@ctron
Copy link
Collaborator

ctron commented Jul 11, 2025

@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.

- Enhanced CargoMetadata::new() to preserve manifest path context
- Added comprehensive find_target_package() method that handles both standalone packages and workspace members
- Improved error messages with detailed workspace member information
- Maintains full backward compatibility for standalone packages

Fixes trunk-rs#909

Assisted-by: Claude 4
@naoNao89 naoNao89 force-pushed the fix/workspace-detection-issue-909 branch from e863d64 to b37e33a Compare July 12, 2025 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants