Skip to content

fix(cli): make --diff/--staged work when the project is not at the git repo root - #114

Merged
oekazuma merged 1 commit into
mainfrom
advisor/001-diff-staged-path-mismatch
Jul 5, 2026
Merged

oekazuma merged 1 commit into
mainfrom
advisor/001-diff-staged-path-mismatch

Conversation

@oekazuma

@oekazuma oekazuma commented Jul 5, 2026 •

Copy link
Copy Markdown
Owner

Summary

svelte-vitals --diff / --staged silently reported zero findings (exit 0) whenever the analyzed SvelteKit project was not at the git repository root (e.g. a monorepo's apps/web/).

Root cause: a path-basis mismatch. git diff --name-only outputs repo-root-relative paths (apps/web/src/routes/+page.svelte) while findings carry cwd-relative locations (src/routes/+page.svelte), so changed.has(location) never matched — the worst failure mode for a CI/pre-commit gate (silent false pass). Subtly, untracked files merged via git ls-files --others were already cwd-relative, so only tracked changes were affected.

Fix

Add --relative to both git diff invocations in getChangedFiles (packages/cli/src/changed-files.ts), making the output cwd-relative and scoped to the analyzed directory — the same basis as Result.location. git ls-files --others is untouched (already cwd-relative).

Tests

New regression tests in packages/cli/test/changed-files.test.ts using real temporary git repositories:

  • tracked change in a repo subdirectory → cwd-relative path returned (the regression core)
  • --staged in a subdirectory
  • untracked file via the ls-files path
  • run from the repo root (previous behavior preserved)

Verification

  • pnpm typecheck / pnpm lint — clean
  • pnpm test — 719 tests pass across all packages (core 343 / vite 79 / cli 288 incl. 4 new / mcp 9)
  • Changeset included (svelte-vitals, patch)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved change detection for projects inside subdirectories, including monorepos, so --diff and --staged now report the correct changed files instead of missing results.
    • Changed file paths are now resolved relative to the current project directory, making output consistent across staged and unstaged diffs.
  • Tests

    • Added broader coverage for change detection in repository root and subdirectory setups, including tracked, staged, and new files.

…t repo root

git diff --name-only reports repo-root-relative paths, but Result.location is
resolved relative to the analyzed cwd. When a SvelteKit project lives in a
monorepo subdirectory (e.g. apps/web/), the mismatch made changed.has(location)
always false, silently dropping every finding and exiting 0 -- the worst
failure mode for a CI/pre-commit gate. Add --relative to both git diff calls so
paths match the cwd-relative basis of Result.location; git ls-files --others
was already cwd-relative and needed no change.
@coderabbitai

coderabbitai Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR fixes getChangedFiles in the CLI to return cwd-relative file paths by adding --relative to the staged git diff command, aligning it with the non-staged path. Documentation and a changeset are updated, and new tests cover subdirectory/monorepo and repo-root scenarios.

Changes

Changed-files path resolution fix

Layer / File(s) Summary
Fix and document cwd-relative git diff paths
packages/cli/src/changed-files.ts, .changeset/fix-diff-staged-repo-subdirectory.md
Adds --relative to the staged git diff invocation so it matches non-staged behavior, updates the doc comment to describe cwd-relative paths, and adds a changeset describing the fix.
Tests for getChangedFiles path resolution scenarios
packages/cli/test/changed-files.test.ts
Adds imports and a temp-git-repo test scaffold, plus tests validating cwd-relative paths for tracked changes, staged changes, untracked files, and repo-root (non-subdirectory) projects.

Estimated code review effort: 2 (Simple) | ~12 minutes

Possibly related PRs

  • oekazuma/svelte-vitals#73: Both PRs modify packages/cli/src/changed-files.ts and its tests to implement --diff/--staged changed-file filtering, with this PR refining the path resolution for subdirectory/monorepo cases.

Poem

A rabbit dug beneath the repo tree,
Found paths gone wrong in subdirectory,
Added --relative to set them right,
Staged and diffed now aligned in sight,
Hop, commit, and test — clean as can be! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: making --diff and --staged work in subdirectory repositories.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch advisor/001-diff-staged-path-mismatch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/cli/test/changed-files.test.ts (1)

62-123: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider extracting shared commit-setup helper.

The "create dir, write file, git add, git commit" sequence is repeated near-identically across all four tests. A small commitFile(repo, projectDir, relPath, contents) helper would reduce duplication.

🤖 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 `@packages/cli/test/changed-files.test.ts` around lines 62 - 123, The four
tests in changed-files.test.ts repeat the same repository setup and commit
sequence, making the fixture noisy and harder to maintain. Extract the shared
“create directories, write file, git add, git commit” flow into a small helper
such as commitFile used by the getChangedFiles test cases. Keep the helper
flexible enough to cover both the subdirectory projectDir cases and the
repo-root case, and update each test to call it with the appropriate repo,
projectDir, relative path, and contents.
🤖 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 `@packages/cli/test/changed-files.test.ts`:
- Around line 62-123: The four tests in changed-files.test.ts repeat the same
repository setup and commit sequence, making the fixture noisy and harder to
maintain. Extract the shared “create directories, write file, git add, git
commit” flow into a small helper such as commitFile used by the getChangedFiles
test cases. Keep the helper flexible enough to cover both the subdirectory
projectDir cases and the repo-root case, and update each test to call it with
the appropriate repo, projectDir, relative path, and contents.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 92dd4f13-a6fb-4c1c-beb5-b7b20547a537

📥 Commits

Reviewing files that changed from the base of the PR and between 83d3078 and 0be8d49.

📒 Files selected for processing (3)
  • .changeset/fix-diff-staged-repo-subdirectory.md
  • packages/cli/src/changed-files.ts
  • packages/cli/test/changed-files.test.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes svelte-vitals --diff / --staged failing silently (reporting no findings) when run from a project directory that is a subdirectory of a git repository (e.g., monorepos). It aligns git diff --name-only output with the CLI’s Result.location path basis by making git emit cwd-relative paths.

Changes:

  • Add --relative to git diff --name-only invocations so changed-file paths are cwd-relative when running from a subdirectory.
  • Add regression tests that create real temporary git repositories to cover tracked, staged, untracked, and repo-root scenarios.
  • Add a changeset for a patch release documenting the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/cli/src/changed-files.ts Uses git diff --relative so changed paths match Result.location when the project is not at the repo root.
packages/cli/test/changed-files.test.ts Adds regression tests using temp git repos to verify subdirectory behavior for tracked/staged/untracked changes and root behavior.
.changeset/fix-diff-staged-repo-subdirectory.md Patch changeset describing the behavioral fix for monorepos/subdirectory projects.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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