fix(rust): keep reporter source paths resolvable from CWD#869
fix(rust): keep reporter source paths resolvable from CWD#869saltyfireball wants to merge 2 commits into
Conversation
…able from CWD (empty HTML/JSON code)
|
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)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughCPD now normalizes non-absolute clone source IDs relative to the canonical current working directory. Paths outside it remain absolute, with unit and integration tests covering subdirectory scans and report output. ChangesCWD path normalization
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@rust/crates/cpd/tests/integration.rs`:
- Around line 385-388: Update the path assertion in the integration test to use
a raw string for the escaped Windows path: replace the current
`json.contains("pkg\\a.js")` check with `json.contains(r"pkg\\a.js")`, while
retaining the forward-slash alternative.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a5f235bb-0c61-44d1-9907-f14bd2af2022
📒 Files selected for processing (2)
rust/crates/cpd/src/main.rsrust/crates/cpd/tests/integration.rs
fix(rust): keep reporter source paths resolvable from CWD (empty HTML/JSON code)
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix (Rust cpd / v5).
What is the current behavior? (You can also link to an open issue here)
When a scan target is a subdirectory of the current working directory - e.g.
path: ["frontend", "backend"]in.jscpd.json, orcpd some-subdir- the HTML report's "show code" blocks are empty, and the JSON report's fragment field is an empty string. Runningcpd .(scanning theCWDdirectly) works, which is the confusing part.Root cause: the file reporters (
html,json,console-full) re-read each source file from disk to render its snippet. Since #827, source paths are rewritten to be relative to the scan root for display (/proj/frontend/src/a.js->src/a.js). The reporters then callstd::fs::read_to_string("src/a.js"), which resolves against theCWD(/proj), where that path does not exist. The read fails and is swallowed by read_file_cached'sunwrap_or_default(), yielding empty code. Passing.masks the bug because the scan root then equals theCWD, so the rewritten path still resolves.Display paths are now made relative to the
CWDinstead of the scan root, so they remain resolvable from theCWDand the reporters can re-read the source. Scanningfrontendfrom the project root now yieldsfrontend/src/a.js-- which both renders code correctly and disambiguates multiple scan roots (frontend/...vsbackend/...). Files outside theCWDare left absolute (still readable).--absoluteis unaffected.relativize_to_scan_rootwithrelativize_to_cwdinrust/crates/cpd/src/main.rs(canonicalizes theCWDfor reliable prefix stripping); updated unit tests.report_snippets_populated_when_scan_root_differs_from_cwd) that scans a subdirectory (scan root !=CWD) and asserts both HTML and JSON contain the snippet. Verified it fails on the old behavior and passes with the fix.absolute:falseshould always emit scan-root-relative SARIF artifactLocation.uri paths #827: scanning an absolute path outside theCWDnow displays absolute paths rather than scan-root-relative ones. This is the deliberate tradeoff -- a scan-root-relative path is not resolvable from theCWDand is exactly what caused the empty-code bug.read_file_cachedswallows read failures viaunwrap_or_default(); it should emit a warning so this class of bug is not silent.This change is
Summary by CodeRabbit
Bug Fixes
Tests