fix: resolve stop hooks without plugin root

This commit is contained in:
Affaan Mustafa
2026-03-29 21:24:03 -04:00
parent dd675d4258
commit 284d2995ba
3 changed files with 71 additions and 10 deletions

View File

@@ -1965,7 +1965,29 @@ async function runTests() {
passed++;
else failed++;
if (
test('script references use CLAUDE_PLUGIN_ROOT variable or safe SessionStart inline resolver', () => {
test('Stop and SessionEnd hooks use safe inline resolvers when plugin root may be unset', () => {
const hooksPath = path.join(__dirname, '..', '..', 'hooks', 'hooks.json');
const hooks = JSON.parse(fs.readFileSync(hooksPath, 'utf8'));
const lifecycleHooks = [
...(hooks.hooks.Stop || []).flatMap(entry => entry.hooks || []),
...(hooks.hooks.SessionEnd || []).flatMap(entry => entry.hooks || []),
].filter(hook => hook.type === 'command');
assert.ok(lifecycleHooks.length > 0, 'Should define Stop/SessionEnd command hooks');
for (const hook of lifecycleHooks) {
assert.ok(hook.command.startsWith('node -e "'), `Expected inline node resolver: ${hook.command.substring(0, 80)}...`);
assert.ok(hook.command.includes('run-with-flags.js'), 'Inline resolver should invoke run-with-flags.js');
assert.ok(hook.command.includes('CLAUDE_PLUGIN_ROOT'), 'Inline resolver should consult CLAUDE_PLUGIN_ROOT');
assert.ok(hook.command.includes('plugins'), 'Inline resolver should probe known plugin roots');
}
})
)
passed++;
else failed++;
if (
test('script references use CLAUDE_PLUGIN_ROOT variable or safe inline resolvers', () => {
const hooksPath = path.join(__dirname, '..', '..', 'hooks', 'hooks.json');
const hooks = JSON.parse(fs.readFileSync(hooksPath, 'utf8'));
@@ -1973,9 +1995,8 @@ async function runTests() {
for (const entry of hookArray) {
for (const hook of entry.hooks) {
if (hook.type === 'command' && hook.command.includes('scripts/hooks/')) {
// Check for the literal string "${CLAUDE_PLUGIN_ROOT}" in the command
const isSessionStartInlineResolver = hook.command.startsWith('node -e') && hook.command.includes('session:start') && hook.command.includes('run-with-flags.js');
const hasPluginRoot = hook.command.includes('${CLAUDE_PLUGIN_ROOT}') || isSessionStartInlineResolver;
const isInlineResolver = hook.command.startsWith('node -e') && hook.command.includes('run-with-flags.js');
const hasPluginRoot = hook.command.includes('${CLAUDE_PLUGIN_ROOT}') || isInlineResolver;
assert.ok(hasPluginRoot, `Script paths should use CLAUDE_PLUGIN_ROOT: ${hook.command.substring(0, 80)}...`);
}
}

View File

@@ -179,6 +179,17 @@ function cleanupTestDir(testDir) {
fs.rmSync(testDir, { recursive: true, force: true });
}
function stagePluginRuntime(homeDir, relativePaths) {
const pluginRoot = path.join(homeDir, '.claude', 'plugins', 'everything-claude-code');
for (const relativePath of relativePaths) {
const sourcePath = path.join(REPO_ROOT, relativePath);
const destinationPath = path.join(pluginRoot, relativePath);
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
fs.copyFileSync(sourcePath, destinationPath);
}
return pluginRoot;
}
function getHookCommandByDescription(hooks, lifecycle, descriptionText) {
const hookGroup = hooks.hooks[lifecycle]?.find(
entry => entry.description && entry.description.includes(descriptionText)
@@ -267,6 +278,35 @@ async function runTests() {
assert.strictEqual(payload.hookSpecificOutput.hookEventName, 'SessionStart');
})) passed++; else failed++;
if (await asyncTest('Stop hook resolves plugin root when CLAUDE_PLUGIN_ROOT is unset', async () => {
const homeDir = createTestDir();
try {
stagePluginRuntime(homeDir, [
path.join('scripts', 'hooks', 'run-with-flags.js'),
path.join('scripts', 'hooks', 'check-console-log.js'),
path.join('scripts', 'lib', 'hook-flags.js'),
path.join('scripts', 'lib', 'utils.js'),
]);
const hookCommand = getHookCommandByDescription(
hooks,
'Stop',
'Check for console.log in modified files after each response'
);
const result = await runHookCommand(hookCommand, {}, {
HOME: homeDir,
CLAUDE_PLUGIN_ROOT: '',
});
assert.strictEqual(result.code, 0, `Expected stop hook to exit 0, got ${result.code}: ${result.stderr}`);
assert.ok(!result.stderr.includes("Cannot find module '/scripts/hooks/run-with-flags.js'"), 'Should not resolve to the filesystem root');
assert.ok(!result.stderr.includes('could not resolve ECC plugin root'), 'Should discover the plugin root from known install paths');
} finally {
cleanupTestDir(homeDir);
}
})) passed++; else failed++;
if (await asyncTest('PreCompact hook logs to stderr', async () => {
const result = await runHookWithInput(path.join(scriptsDir, 'pre-compact.js'), {});
assert.ok(result.stderr.includes('[PreCompact]'), 'Should output to stderr with prefix');