diff --git a/scripts/skills-health.js b/scripts/skills-health.js index 4cea9ec50..aefbe82ee 100644 --- a/scripts/skills-health.js +++ b/scripts/skills-health.js @@ -1,8 +1,27 @@ #!/usr/bin/env node 'use strict'; -const { collectSkillHealth, formatHealthReport } = require('./lib/skill-evolution/health'); -const { renderDashboard } = require('./lib/skill-evolution/dashboard'); +let collectSkillHealth; +let formatHealthReport; +let renderDashboard; +try { + ({ collectSkillHealth, formatHealthReport } = require('./lib/skill-evolution/health')); + ({ renderDashboard } = require('./lib/skill-evolution/dashboard')); +} catch (error) { + // The Claude Code plugin marketplace clones the repo but does not run + // npm/install-apply, so scripts/lib can be missing from a managed install. + // Fail with the remedy instead of a bare module-resolution stack (#3259). + const missing = (String(error && error.message).match(/Cannot find module '([^']+)'/) || [])[1]; + if (error && error.code === 'MODULE_NOT_FOUND' && missing && /skill-evolution/.test(error.message)) { + console.error( + `skills-health could not load ${missing}; this installation is missing part of scripts/lib/skill-evolution.\n` + + 'For a one-off report, run from a full ECC checkout: node scripts/skills-health.js --dashboard. ' + + 'For a non-marketplace install, select commands-core: node scripts/install-apply.js --target --modules commands-core.' + ); + process.exit(1); + } + throw error; +} function showHelp() { console.log(` diff --git a/tests/scripts/skills-health-missing-lib.test.js b/tests/scripts/skills-health-missing-lib.test.js new file mode 100644 index 000000000..578a190fa --- /dev/null +++ b/tests/scripts/skills-health-missing-lib.test.js @@ -0,0 +1,83 @@ +/** + * A managed install without scripts/lib must get the remedy, not a stack trace + * (#3259). + */ + +const assert = require('assert'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const { spawnSync } = require('child_process'); + +const script = path.join(__dirname, '..', '..', 'scripts', 'skills-health.js'); + +function test(name, fn) { + try { + fn(); + console.log(` ✓ ${name}`); + return true; + } catch (error) { + console.log(` ✗ ${name}`); + console.log(` Error: ${error.message}`); + return false; + } +} + +let passed = 0; +let failed = 0; + +const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-health-missing-lib-')); +fs.mkdirSync(path.join(tmp, 'scripts'), { recursive: true }); +fs.copyFileSync(script, path.join(tmp, 'scripts', 'skills-health.js')); + +if ( + test('missing scripts/lib prints the install remedy instead of a stack trace', () => { + const result = spawnSync('node', [path.join(tmp, 'scripts', 'skills-health.js')], { + encoding: 'utf8', + timeout: 15000 + }); + assert.strictEqual(result.status, 1); + assert.match(result.stderr, /could not load \.\/lib\/skill-evolution\/health/); + assert.match(result.stderr, /skills-health\.js --dashboard/); + assert.match(result.stderr, /--modules commands-core/); + assert.match(result.stderr, /full ECC checkout/); + assert.doesNotMatch(result.stderr, /Cannot find module/); + }) +) + passed++; +else failed++; + +const nested = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-health-nested-missing-')); +fs.mkdirSync(path.join(nested, 'scripts', 'lib'), { recursive: true }); +fs.copyFileSync(script, path.join(nested, 'scripts', 'skills-health.js')); +fs.cpSync( + path.join(__dirname, '..', '..', 'scripts', 'lib', 'skill-evolution'), + path.join(nested, 'scripts', 'lib', 'skill-evolution'), + { recursive: true } +); +// Keep skill-evolution in place but drop the shared helper it requires via +// `require('../utils')`; the error message must name that module, not claim +// the whole skill-evolution directory is absent. +fs.rmSync(path.join(nested, 'scripts', 'lib', 'utils.js'), { force: true }); + +if ( + test('a nested missing helper names the real module instead of the directory', () => { + const result = spawnSync('node', [path.join(nested, 'scripts', 'skills-health.js')], { + encoding: 'utf8', + timeout: 15000 + }); + assert.strictEqual(result.status, 1); + assert.match(result.stderr, /could not load \.\.\/utils/); + assert.match(result.stderr, /skills-health\.js --dashboard/); + assert.match(result.stderr, /--modules commands-core/); + assert.doesNotMatch(result.stderr, /Cannot find module/); + }) +) + passed++; +else failed++; + +fs.rmSync(tmp, { recursive: true, force: true }); +fs.rmSync(nested, { recursive: true, force: true }); + +console.log(`\n ${passed} passed, ${failed} failed\n`); +process.exit(failed > 0 ? 1 : 0);