chat: fix plugin hook workspace root resolution and CLAUDE_PLUGIN_ROOT escaping#297790
Merged
connor4312 merged 3 commits intomainfrom Feb 25, 2026
Merged
Conversation
…T escaping Refactors agent plugin format adapters to use dependency injection and correctly resolve workspace root URIs for hook cwd resolution, matching the pattern from PromptsService. Also uses IInstantiationService for creating adapter instances instead of manual construction. - Converts CopilotPluginFormatAdapter and ClaudePluginFormatAdapter from const objects to proper classes with @IWorkspaceContextService injection - Adds resolveWorkspaceRoot helper that mirrors PromptsService logic to resolve the workspace folder containing the plugin, falling back to the first workspace folder for plugins outside the workspace - Fixes workspace root passed to parseC opilotHooks and parseClaudeHooks (was incorrectly passing pluginUri instead of the workspace folder URI) - Updates _detectPluginFormatAdapter to use _instantiationService .createInstance instead of manual constructor calls (Commit message generated by Copilot)
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors chat agent plugin format adapters to be DI-created class instances and fixes hook cwd resolution by passing the containing workspace folder URI (mirroring PromptsService) instead of the plugin URI. It also adds ${CLAUDE_PLUGIN_ROOT} command expansion/quoting logic plus unit tests for that helper.
Changes:
- Convert Copilot/Claude plugin format adapters from const objects to DI-backed classes and instantiate them via
IInstantiationService. - Add
resolveWorkspaceRoothelper to determine the correct workspace folder URI for hookcwdresolution. - Add
${CLAUDE_PLUGIN_ROOT}expansion helper (shellQuotePluginRootInCommand) and tests; wrap hook parsing in try/catch with logging.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/vs/workbench/contrib/chat/common/plugins/agentPluginServiceImpl.ts |
Refactors plugin format adapters to DI classes, fixes workspace root passed to hook parsers, adds ${CLAUDE_PLUGIN_ROOT} expansion/quoting helper, and adds parse error handling. |
src/vs/workbench/contrib/chat/test/common/plugins/shellQuotePluginRootInCommand.test.ts |
Adds unit tests validating ${PLUGIN_ROOT} / ${CLAUDE_PLUGIN_ROOT} expansion and quoting behavior. |
.vscode/settings.json |
Flips js/ts.experimental.useTsgo setting (appears unrelated to plugin hook fixes). |
Comments suppressed due to low confidence (2)
src/vs/workbench/contrib/chat/common/plugins/agentPluginServiceImpl.ts:118
- The regex suffix capture in
shellQuotePluginRootInCommand(([^\s"']*)) will also consume shell metacharacters when they are adjacent to the token (e.g.cd ${PLUGIN_ROOT}&& echo ok), causing operators like&&/;/|to be pulled into the quoted path and changing command semantics. The suffix should stop at shell metacharacters (not just whitespace/quotes), or be restricted to path-like characters (e.g./,\\,.,-,_, alphanumerics).
const escapedToken = escapeRegExpCharacters(token);
const pattern = new RegExp(
// Capture an optional leading quote so we know if it's already quoted
`(["']?)` + escapedToken + `([^\\s"']*)`,
'g',
);
src/vs/workbench/contrib/chat/common/plugins/agentPluginServiceImpl.ts:159
ClaudePluginFormatAdapter.parseHooksonly iterates hook entries that have a nested{ matcher, hooks: [...] }-style structure (for (const { hooks } of lifecycle)). HoweverparseClaudeHooksexplicitly supports the simpler Claude format where the array contains direct command objects (e.g.{ type: 'command', command: '...' }). In that case this loop won’t apply${CLAUDE_PLUGIN_ROOT}quoting or sethook.env.CLAUDE_PLUGIN_ROOT, so the intended escaping can be skipped. Consider handling both shapes (nestedhooksarrays and direct command objects) when walking the JSON.
const typedJson = json as { hooks?: Record<string, { hooks: Mutable<IHookCommand>[]; originalId: string }[]> };
for (const lifecycle of Object.values(typedJson.hooks ?? {})) {
for (const { hooks } of lifecycle) {
for (const hook of hooks || []) {
for (const field of ['command', 'windows', 'linux', 'osx'] as const) {
if (typeof hook[field] === 'string') {
hook[field] = shellQuotePluginRootInCommand(hook[field], fsPath, token);
}
}
hook.env ??= {};
hook.env.CLAUDE_PLUGIN_ROOT = fsPath;
}
}
}
meganrogge
approved these changes
Feb 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactors agent plugin format adapters to use dependency injection and
correctly resolve workspace root URIs for hook cwd resolution, matching
the pattern from PromptsService. Also uses IInstantiationService for
creating adapter instances instead of manual construction.
const objects to proper classes with @IWorkspaceContextService injection
resolve the workspace folder containing the plugin, falling back to the
first workspace folder for plugins outside the workspace
(was incorrectly passing pluginUri instead of the workspace folder URI)
.createInstance instead of manual constructor calls
(Commit message generated by Copilot)