Skip to content

#329: Detect 'mcs sync' run from Claude home scope#330

Merged
bguidolim merged 2 commits intomainfrom
bruno/329-detect-claude-home-cwd
Apr 19, 2026
Merged

#329: Detect 'mcs sync' run from Claude home scope#330
bguidolim merged 2 commits intomainfrom
bruno/329-detect-claude-home-cwd

Conversation

@bguidolim
Copy link
Copy Markdown
Collaborator

Context

Running mcs sync from ~/.claude or from $HOME itself silently syncs project artifacts into the global Claude scope — creating ~/.claude/.claude/ junk, writing settings.local.json instead of settings.json, and leaving doctor unable to recognize the install. ProjectDetector returns nil in that location so the sync "succeeds" and the user believes the global install worked.

Fixes #329.

Changes

  • New Environment.isInsideClaudeHome(_:) — returns true when candidate is inside claudeDirectory or equal to homeDirectory, gated on the .claude.json sibling existing to confirm a real Claude Code layout. The sibling check prevents false positives in test fixtures where $HOME points to a tmp dir that happens to contain .claude/.
  • New SyncCommand.guardClaudeHomeCwd(env:output:) at the top of perform():
    • --global already set → silent chdir to $HOME, proceed
    • --pack / --all (non-interactive) → hard error, exit failure
    • Bare interactive → Y/n prompt offering --global; yes flips and chdir's, no hard-errors
  • Returns the effective global flag (rather than mutating self.global) because LockedCommand.run() snapshots self before calling perform().
  • Dedupes the path ?? cwd idiom into effectiveTargetURL, reused by performProject.
  • Shared makeClaudeHome test helper in TestHelpers.swift.

Acceptance Criteria

  • cd ~/.claude && mcs sync (bare) prompts with --global suggestion
  • cd ~/.claude && mcs sync --pack foo hard-errors with clear message
  • cd ~/.claude && mcs sync --global --pack foo silently redirects (no prompt)
  • cd ~ && mcs sync --pack foo hard-errors (same protection applies at $HOME)
  • cd ~/Projects/my-app && mcs sync --pack foo unaffected (no false positives)
  • Fresh Claude install without ~/.claude.json doesn't trigger the guard

Testing

  • 9 new unit tests on Environment.isInsideClaudeHome (exact-match, nested, sibling-missing, prefix-collision, $HOME match, $HOME/subdir non-match)
  • 7 new unit tests on SyncCommand.guardClaudeHomeCwd exercising each branch via SyncCommand.parse([...]) + direct call
  • Full suite: 1000 tests pass
  • SwiftFormat + SwiftLint (strict) clean

- Add `Environment.isInsideClaudeHome` — triggers on cwd/path inside `~/.claude` or equal to `$HOME`, gated on the `.claude.json` sibling existing
- Guard in `SyncCommand.perform` silently redirects when `--global` is set, hard-errors on `--pack`/`--all`, prompts otherwise
- Extract `effectiveTargetURL` and shared `makeClaudeHome` test helper; add 17 unit tests
- Treat --dry-run and closed/non-TTY stdin as non-interactive — hard-error instead of silently accepting askYesNo's default=true
- Warn on chdir failure and announce cwd switch when --global redirects from ~/.claude
- Improve error messages and add tests for non-interactive bare-sync and --dry-run paths
@bguidolim bguidolim merged commit 48094a4 into main Apr 19, 2026
4 checks passed
@bguidolim bguidolim deleted the bruno/329-detect-claude-home-cwd branch April 19, 2026 13:40
bguidolim added a commit that referenced this pull request Apr 19, 2026
- Annotates Environment.swift and SyncCommand.swift index entries with the new `isInsideClaudeHome(_:)` / `guardClaudeHomeCwd()` APIs from #330
- Adds a troubleshooting section covering the prompt, hard-error, and silent-redirect paths users hit when running `mcs sync` from `~/.claude` or `$HOME`
bguidolim added a commit that referenced this pull request Apr 19, 2026
* #328: Delta-aware picker + confirmation for mcs sync

- Picker renders cursor-contextual tags (installed/uncheck/WILL REMOVE/new/will install), dynamic footer verbs, and a live +/- counter so users see which toggles add or remove packs.
- Review screen with add/remove/keep summary appears before any removals and requires explicit y (default: false) before mutation.
- Extract ANSIStyle, PickerDelta, and SyncDeltaSummary as pure, unit-tested helpers; reframe picker header copy.

* Address #332 review: non-TTY guard, full-wipe prompt, palette unification

- Non-TTY guard at top of interactiveConfigure throws ExitCode.failure with --pack/--all hint; prevents piped-stdin silent no-ops.
- Full-wipe case (SyncDeltaSummary.isFullWipe) emits a warn and switches prompt copy to "Remove all configured packs?" before the destructive confirmation.
- ANSIStyle becomes the canonical palette (now includes bold/cyan/blue); CLIOutput's private color vars delegate. DeltaCounts struct deleted. renderReviewBlock converted to instance method. Test assertions tightened with golden strings.

* Document SyncDeltaSummary.swift in CLAUDE.md

- Adds the new file to the Sync/ section inventory so future sessions find it alongside the rest of the convergence engine

* Document #330 claude-home guard in CLAUDE.md + troubleshooting

- Annotates Environment.swift and SyncCommand.swift index entries with the new `isInsideClaudeHome(_:)` / `guardClaudeHomeCwd()` APIs from #330
- Adds a troubleshooting section covering the prompt, hard-error, and silent-redirect paths users hit when running `mcs sync` from `~/.claude` or `$HOME`

* Address Copilot review: decouple stdin/stdout predicates; fix unchanged count

- Split CLIOutput predicates: hasInteractiveStdin (stdin TTY) gates prompts; isInteractiveTerminal (both TTYs) gates raw picker UI. Previously both were coupled to colorsEnabled, breaking mcs sync > out.log even with interactive stdin.
- Non-TTY guard in interactiveConfigure now uses hasInteractiveStdin so piped-stdout-with-TTY-stdin falls through to the working fallback picker + readline prompt.
- Include .notInstalled rows in the "unchanged" counter so additions + removals + unchanged equals total rows shown; the label's truth value no longer depends on baseline install state.
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.

Detect and redirect when mcs sync runs from ~/.claude

1 participant