claude-code - 💡(How to fix) Fix [FEATURE] Workspace-conditional / lazy hook loading (analog to MCP on-demand discovery) [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#60632Fetched 2026-05-20 03:53:28
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
0
Timeline (top)
labeled ×2commented ×1

Fix Action

Fix / Workaround

…and have Claude Code dispatch the right bundle per workspace, with the same on-demand loading semantics that MCP servers already enjoy.

Code Example

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "condition": {
          "workspace_has": ["Cargo.toml"]
        },
        "hooks": [{ "type": "command", "command": "cargo clippy" }]
      },
      {
        "matcher": "Edit|Write",
        "condition": {
          "workspace_has": ["pyproject.toml"]
        },
        "hooks": [{ "type": "command", "command": "ruff format --check ." }]
      }
    ]
  }
}
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing requests and this feature hasn't been requested yet
  • This is a single feature request (not multiple features)

Problem Statement

Claude Code hooks currently fire on every PreToolUse / PostToolUse event regardless of workspace context. In multi-language or multi-project setups, this means:

  • Rust-specific hooks (cargo clippy, cargo deny, cargo audit) fire even in pure-Python repos
  • Python-specific hooks (ruff format --check, pytest, pip-audit) fire even in pure-Rust repos
  • Every hook pays the cost of running its workspace-detection guard ([ -f Cargo.toml ] && ... || exit 0) for every event

For polyglot users juggling N projects across Rust/Python/TS, this creates either (a) unnecessary hook execution latency, or (b) requires building a "router hook" that introspects workspace + delegates. The router pattern works but pushes the burden to each user.

Proposed Solution

Extend settings.json hook config with a condition field (or workspace matcher):

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "condition": {
          "workspace_has": ["Cargo.toml"]
        },
        "hooks": [{ "type": "command", "command": "cargo clippy" }]
      },
      {
        "matcher": "Edit|Write",
        "condition": {
          "workspace_has": ["pyproject.toml"]
        },
        "hooks": [{ "type": "command", "command": "ruff format --check ." }]
      }
    ]
  }
}

The condition.workspace_has (or equivalent) accepts file globs / paths relative to the workspace root. Claude Code evaluates the condition once at workspace-bind time (or with a short TTL cache), not on every event.

Alternative Solutions

  • Router hook: single hook that introspects + delegates. Works today but pushes complexity to user.
  • Per-repo .claude/hooks/: already supported, but doesn't help when ONE repo is polyglot.
  • Skill-based manual loading: requires explicit invocation; defeats the "automatic guard" purpose of hooks.

Priority

Low - Nice to have

Feature Category

Configuration and settings

Use Case Example

I run Claude Code across ~6 active repos: 2 Rust (orchestration framework + benchmark harness), 3 Python (research/ML/CLI tools), 1 polyglot. I want to define hook bundles ONCE in settings:

  • hooks.rust.* — fire only when Cargo.toml is present in the workspace
  • hooks.python.* — fire only when pyproject.toml is present
  • hooks.always.* — fire regardless

…and have Claude Code dispatch the right bundle per workspace, with the same on-demand loading semantics that MCP servers already enjoy.

Additional Context

No response

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING