diff --git a/scripts/lib/project-detect.js b/scripts/lib/project-detect.js index 2aac4afbc..71ee09a01 100644 --- a/scripts/lib/project-detect.js +++ b/scripts/lib/project-detect.js @@ -9,7 +9,6 @@ const fs = require('fs'); const path = require('path'); -const yaml = require('js-yaml'); /** * Language detection rules. @@ -350,6 +349,10 @@ function hasFlutterSdkDependency(projectDir) { const pubspecPath = path.join(projectDir, 'pubspec.yaml'); if (!fs.existsSync(pubspecPath)) return false; const content = fs.readFileSync(pubspecPath, 'utf8'); + // Loaded here, not at the top: the SessionStart hook requires this module + // from plugin installs that have no node_modules (#2822). Without js-yaml + // the catch below costs only the Flutter check, not the whole hook. + const yaml = require('js-yaml'); const pubspec = yaml.load(content, { schema: yaml.JSON_SCHEMA }); // Only a declared Flutter SDK dependency identifies the framework. // Unrelated configuration, comments, and strings must not count. diff --git a/tests/scripts/plugin-install-without-node-modules.test.js b/tests/scripts/plugin-install-without-node-modules.test.js index 6f7d5ce26..61b770d92 100644 --- a/tests/scripts/plugin-install-without-node-modules.test.js +++ b/tests/scripts/plugin-install-without-node-modules.test.js @@ -13,7 +13,7 @@ const assert = require('assert'); const fs = require('fs'); const os = require('os'); const path = require('path'); -const { execFileSync } = require('child_process'); +const { execFileSync, spawnSync } = require('child_process'); const REPO_ROOT = path.join(__dirname, '..', '..'); @@ -109,6 +109,42 @@ function runTests() { assert.ok(result.stdout.includes('Usage:')); })) passed++; else failed++; + if (test('session-start.js runs without js-yaml installed', () => { + // The SessionStart hook loads scripts/lib/project-detect.js, so a + // top-level require of an npm package there fails every session start. + const sandbox = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-session-start-')); + try { + const project = path.join(sandbox, 'project'); + const home = path.join(sandbox, 'home'); + const data = path.join(home, '.claude'); + fs.mkdirSync(project, { recursive: true }); + fs.mkdirSync(data, { recursive: true }); + fs.writeFileSync(path.join(project, 'pubspec.yaml'), 'name: app\ndependencies:\n flutter:\n sdk: flutter\n'); + + // A NODE_PATH that exposes js-yaml would hide the missing dependency. + const { NODE_PATH: _nodePath, ...inheritedEnv } = process.env; + const env = { ...inheritedEnv, HOME: home, USERPROFILE: home, ECC_AGENT_DATA_HOME: data, + CLAUDE_CONFIG_DIR: data, CLAUDE_PROJECT_DIR: project, XDG_DATA_HOME: path.join(home, '.local', 'share'), + ECC_HOMUNCULUS_DIR: path.join(data, 'homunculus'), ECC_SESSION_START_CONTEXT: 'true', + ECC_SESSION_START_MAX_CHARS: '8000' }; + + const result = spawnSync(process.execPath, [path.join(pluginDir, 'scripts', 'hooks', 'session-start.js')], { + cwd: project, + env, + input: JSON.stringify({ hook_event_name: 'SessionStart', source: 'startup', session_id: 'no-deps', cwd: project }), + encoding: 'utf8', + timeout: 15000, + }); + assert.ifError(result.error); + assert.strictEqual(result.status, 0, `stderr: ${result.stderr}`); + assert.ok(!result.stderr.includes('Cannot find module'), `stderr: ${result.stderr}`); + const context = JSON.parse(result.stdout).hookSpecificOutput.additionalContext; + assert.ok(context.includes('"languages":["dart"]'), `additionalContext: ${context}`); + } finally { + fs.rmSync(sandbox, { recursive: true, force: true }); + } + })) passed++; else failed++; + if (test('install-plan.js --config gives an actionable error when ajv is genuinely missing', () => { const configPath = path.join(pluginDir, 'ecc-install.json'); fs.writeFileSync(configPath, JSON.stringify({ version: 1, profile: 'minimal' }));