rulesync icon indicating copy to clipboard operation
rulesync copied to clipboard

Feature Request: Copy Referenced Hook Scripts to Target Directories

Open rudironsoni opened this issue 8 months ago • 5 comments

Current Behavior

When hooks.json references script files (e.g., .sh files), rulesync only converts the configuration to tool-specific formats. The actual script files are not copied to the destination directories.

Example:

{
  "sessionStart": [{ "type": "command", "command": ".rulesync/hooks/start.sh" }]
}

Rulesync generates .github/hooks/copilot-hooks.json with the command reference, but start.sh must be manually copied to .github/hooks/.

Desired Behavior

Rulesync should detect script file references in hooks and automatically copy them to the appropriate target hooks directory.

Use Cases

  1. Version Control: Keep scripts in .rulesync/hooks/ for version control, auto-deploy to tool-specific directories
  2. Multi-tool Projects: Same scripts used across Copilot, Cursor, Claude Code, etc. without manual copying
  3. CI/CD Integration: Automated setup of hooks without manual file management

Proposed Implementation

  1. Parse command values in hooks definitions to identify file paths
  2. Detect scripts (.sh, .ps1, .js, etc.) vs inline commands
  3. Copy referenced scripts to target hooks/ directories
  4. Update paths in generated configs to point to new locations

Example Workflow

# Source structure
.rulesync/
├── hooks.json
└── hooks/
    ├── start.sh
    └── lint.sh

# After rulesync generate
.github/hooks/
├── copilot-hooks.json  # References start.sh, lint.sh
├── start.sh            # Copied from .rulesync/hooks/
└── lint.sh             # Copied from .rulesync/hooks/

Questions/Considerations

  • Should this be opt-in via config flag (e.g., copyScripts: true)?
  • How to handle relative vs absolute paths?
  • Should scripts be copied to a subdirectory (e.g., .github/hooks/scripts/) or flat?
  • How to handle naming conflicts when same script referenced by multiple hooks?

rudironsoni avatar Mar 13 '26 17:03 rudironsoni

@dyoshikawa I really want your input on this request before starting to implement anything

rudironsoni avatar Mar 13 '26 17:03 rudironsoni

@rudironsoni Thank you. Overall, your proposal looks good. I'm considering about your questions.

cm-dyoshikawa avatar Mar 16 '26 00:03 cm-dyoshikawa

Adding a concrete use case that argues for this feature and surfaces a path-resolution sub-question worth deciding up front.

I'm converting a Claude Code plugin marketplace (~43 plugins, 359 skills) to OpenCode. Skills and subagents convert cleanly, but hooks are where the missing script copy bites hardest — and in a way that's slightly different from the .rulesync/hooks/ flow in the original report.

In a Claude Code plugin, hooks.json references scripts via the ${CLAUDE_PLUGIN_ROOT} variable:

{
  "PreToolUse": [{
    "matcher": "Bash",
    "hooks": [{ "type": "command", "command": "bash ${CLAUDE_PLUGIN_ROOT}/hooks/validate.sh", "timeout": 10 }]
  }]
}

convert --to opencode currently emits a plugin that passes the path through literally:

await $`bash \${CLAUDE_PLUGIN_ROOT}/hooks/validate.sh`;

Two things break at runtime: (1) ${CLAUDE_PLUGIN_ROOT} doesn't exist outside Claude Code's plugin loader, and (2) validate.sh is never copied to .opencode/, so even a resolved path would 404. Because OpenCode blocks tool calls by throwing on non-zero exit, a missing script doesn't fail silently — it throws ENOENT and blocks every matched tool call. So the absent-script case is worse than a no-op.

This makes the relative-vs-absolute question in the issue ("How to handle relative vs absolute paths?") the crux:

  • For a script referenced relative to the source config dir (the .rulesync/hooks/start.sh case), copy + rewrite to the target hooks dir as proposed.
  • For a script referenced via a tool-specific path variable (${CLAUDE_PLUGIN_ROOT}, $CLAUDE_PROJECT_DIR, etc.), the copy step needs to first resolve the variable against the source location to find the file, then rewrite the command to the copied location (the target has no equivalent variable).

An opt-in copyScripts: true flag (as floated) plus resolution of known source-tool path variables would cover both the .rulesync/ authoring flow and the "import an existing plugin/marketplace" flow. Happy to test against the 43-plugin set once there's a shape to try.

laurigates avatar Jun 13 '26 12:06 laurigates

I re-validated this against the current implementations and upstream hook specifications. This remains legitimate, but the product boundary needs a maintainer decision before there is a defensible implementation.

For normal Rulesync project generation, copying is not required merely because the generated config lives elsewhere: GitHub documents hook cwd as relative to the repository root, so a checked-in .rulesync/hooks/start.sh remains addressable from .github/hooks/*.json (Copilot hooks reference). Claude Code likewise documents ${CLAUDE_PROJECT_DIR} for project-local scripts (hooks reference), and Rulesync already prefixes dot-relative Claude commands with that variable in src/features/hooks/claudecode-hooks.ts / src/features/hooks/tool-hooks-converter.ts.

The plugin-conversion case in the later comment is different. Claude officially defines ${CLAUDE_PLUGIN_ROOT} as the installation directory for files bundled with a plugin (plugins reference). Converting such a plugin to a non-plugin target would require Rulesync to model and transfer source assets, not only translate hook JSON.

Before implementation, a maintainer still needs to decide:

  • whether this covers only files under .rulesync/hooks/, or also assets discovered while converting complete tool plugins/extensions;
  • whether copying is default or opt-in;
  • which command/path forms are supported (a declared script field would be safer than parsing arbitrary shell text);
  • how destination paths, collisions, executable bits, global scope, missing files, and path traversal are handled;
  • whether plugin/extension asset conversion belongs under the broader plugin work in #382.

Leaving this open as inconclusive until those semantics are selected.

dyoshikawa avatar Jul 23 '26 06:07 dyoshikawa

Re-validated against the current GitHub Copilot and Claude Code hook specifications. The original project-generation case does not require copying referenced scripts into each target hook directory.

GitHub documents hook cwd as relative to the repository root, so a command can continue to reference a checked-in .rulesync/hooks/start.sh even when the generated JSON lives under .github/hooks. Claude Code provides ${CLAUDE_PROJECT_DIR} specifically for stable project-relative script references, and Rulesync already applies that prefix to dot-relative Claude hook commands. Copying these scripts would create duplicated assets, collision/deletion semantics, executable-mode drift, and additional generated files without fixing a runtime limitation.

The ${CLAUDE_PLUGIN_ROOT} conversion case is materially different: that variable points into an installed plugin bundle whose scripts are plugin assets. Translating it to a non-plugin target requires modeling and transferring a complete plugin/extension asset graph, including provenance, path rewriting, executable modes, collisions, and lifecycle. That belongs with the plugin/extension work tracked in #382 rather than implicit shell-command parsing in the hooks feature.

Closing as not planned for the hooks generator. Project-local scripts should use repository-root-relative paths (or the target’s documented project-root placeholder); plugin-bundled asset conversion remains tracked by #382.

Sources:

  • https://docs.github.com/en/copilot/reference/hooks-reference
  • https://code.claude.com/docs/en/hooks
  • https://code.claude.com/docs/en/plugins-reference

dyoshikawa avatar Jul 24 '26 07:07 dyoshikawa