Skip to content

chat: fix plugin hook workspace root resolution and CLAUDE_PLUGIN_ROOT escaping#297790

Merged
connor4312 merged 3 commits intomainfrom
connor4312/fix-plugin-hooks-workspace-resolution
Feb 25, 2026
Merged

chat: fix plugin hook workspace root resolution and CLAUDE_PLUGIN_ROOT escaping#297790
connor4312 merged 3 commits intomainfrom
connor4312/fix-plugin-hooks-workspace-resolution

Conversation

@connor4312
Copy link
Member

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 parseCopilotHooks 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)

…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)
Copilot AI review requested due to automatic review settings February 25, 2026 20:46
@connor4312 connor4312 enabled auto-merge (squash) February 25, 2026 20:46
@vs-code-engineering vs-code-engineering bot added this to the February 2026 milestone Feb 25, 2026
Copy link
Contributor

Copilot AI left a comment

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 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 resolveWorkspaceRoot helper to determine the correct workspace folder URI for hook cwd resolution.
  • 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.parseHooks only iterates hook entries that have a nested { matcher, hooks: [...] }-style structure (for (const { hooks } of lifecycle)). However parseClaudeHooks explicitly 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 set hook.env.CLAUDE_PLUGIN_ROOT, so the intended escaping can be skipped. Consider handling both shapes (nested hooks arrays 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;
				}
			}
		}

@connor4312 connor4312 merged commit b6558fb into main Feb 25, 2026
20 checks passed
@connor4312 connor4312 deleted the connor4312/fix-plugin-hooks-workspace-resolution branch February 25, 2026 21:34
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.

3 participants