Extract try_canonicalize helper in package.rs (BT-2149)#2174
Conversation
The pattern `std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf())` appeared 5 times in this file. Extract to a named private helper to reduce duplication and make intent explicit. https://claude.ai/code/session_01HwXCZnFJb8Ckp75DKFSzsb
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughRefactors internal path canonicalization in ChangesPath Canonicalization Consolidation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Pull request overview
Refactors crates/beamtalk-core/src/project/package.rs by extracting a small private helper to centralize the “best-effort canonicalize” behavior used during package-root discovery and source-file deduplication.
Changes:
- Added a private
try_canonicalize(&Path) -> PathBufhelper that falls back to the original path onstd::fs::canonicalizeerrors. - Replaced repeated inline
canonicalize(...).unwrap_or_else(...)call sites infind_package_root,collect_package_source_files_with_errors, andresolve_extraction_files.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/beamtalk-core/src/project/package.rs (1)
141-149: ⚡ Quick winDedup appended targets by updating
pkg_canonicalduring the loop.If
source_filescontains duplicate external paths, they can be pushed multiple times becausepkg_canonicalis computed once and never updated afterpush.Proposed tweak
- let pkg_canonical: HashSet<PathBuf> = + let mut pkg_canonical: HashSet<PathBuf> = pkg_files.iter().map(|p| try_canonicalize(p)).collect(); @@ for f in source_files { let key = try_canonicalize(f); if !pkg_canonical.contains(&key) { + pkg_canonical.insert(key); pkg_files.push(f.clone()); } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/beamtalk-core/src/project/package.rs` around lines 141 - 149, The loop that ensures explicitly-targeted files are included uses a snapshot HashSet pkg_canonical computed once, so pushing a new file into pkg_files can still allow duplicates when source_files contains the same external path multiple times; update pkg_canonical as you append (or check/insert into the set instead of only checking) so duplicates are prevented: when iterating source_files, compute key = try_canonicalize(f), if pkg_canonical does not contain key then push f.clone() into pkg_files and also insert key into pkg_canonical (using the same try_canonicalize result) to keep the set current.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@crates/beamtalk-core/src/project/package.rs`:
- Around line 141-149: The loop that ensures explicitly-targeted files are
included uses a snapshot HashSet pkg_canonical computed once, so pushing a new
file into pkg_files can still allow duplicates when source_files contains the
same external path multiple times; update pkg_canonical as you append (or
check/insert into the set instead of only checking) so duplicates are prevented:
when iterating source_files, compute key = try_canonicalize(f), if pkg_canonical
does not contain key then push f.clone() into pkg_files and also insert key into
pkg_canonical (using the same try_canonicalize result) to keep the set current.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8b3663de-2746-4a6f-a0b2-33ca64eeaf8a
📒 Files selected for processing (1)
crates/beamtalk-core/src/project/package.rs
If source_files contained duplicate external paths, each would be pushed into pkg_files on every occurrence since pkg_canonical was a snapshot that never updated. Switch to HashSet::insert which returns true only when the key is newly inserted, keeping the set current and preventing duplicates. Suggested by CodeRabbit review. https://claude.ai/code/session_01HwXCZnFJb8Ckp75DKFSzsb
Summary
Extract a private
try_canonicalize(path: &Path) -> PathBufhelper incrates/beamtalk-core/src/project/package.rsto replace 5 identical inline occurrences ofstd::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()).Changes
try_canonicalizehelper with doc comment explaining fallback semanticsfind_package_root,collect_package_source_files_with_errors, andresolve_extraction_filesVerification
cargo test -p beamtalk-core)cargo clippyclean (no warnings)cargo fmt --checkcleanLinear Issue
https://linear.app/beamtalk/issue/BT-2149/extract-try-canonicalize-helper-in-packagers-5x-duplication
https://claude.ai/code/session_01HwXCZnFJb8Ckp75DKFSzsb
Generated by Claude Code
Summary by CodeRabbit