diff --git a/skill/scripts/context.mjs b/skill/scripts/context.mjs index 259e35178..3373819de 100644 --- a/skill/scripts/context.mjs +++ b/skill/scripts/context.mjs @@ -255,10 +255,16 @@ function resolveTargetPath(cwd, targetPath) { } function findUniqueBareTarget(cwd, targetPath) { - if (path.isAbsolute(targetPath) || /[\\/]/.test(targetPath)) return null; - const repoRoot = findMonorepoRoot(path.resolve(cwd)); + const absCwd = path.resolve(cwd); + const abs = path.isAbsolute(targetPath) ? targetPath : path.resolve(absCwd, targetPath); + const rel = path.relative(absCwd, abs); + if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) return null; + const segments = rel.split(path.sep).filter(Boolean); + if (segments.length !== 1) return null; + const name = segments[0]; + const repoRoot = findMonorepoRoot(absCwd); if (!repoRoot) return null; - const matches = discoverTargetCandidates(repoRoot).filter((candidate) => candidate.name === targetPath); + const matches = discoverTargetCandidates(repoRoot).filter((candidate) => candidate.name === name); if (matches.length !== 1) return null; return path.resolve(repoRoot, matches[0].path); } diff --git a/tests/context.test.mjs b/tests/context.test.mjs index 574cf181d..227545349 100644 --- a/tests/context.test.mjs +++ b/tests/context.test.mjs @@ -830,6 +830,35 @@ describe('loadContext (monorepo project context)', () => { assert.equal(ctx.projectRoot, scratch); }); + it('resolves a unique workspace child when the target was already resolved against cwd', () => { + writeMonorepo(); + write('apps/dashboard/PRODUCT.md', '# Dashboard product\n'); + const ctx = loadContext(scratch, { targetPath: path.join(scratch, 'dashboard') }); + assert.equal(ctx.projectRoot, path.join(scratch, 'apps', 'dashboard')); + }); + + it('does not guess a nested product basename in a non-monorepo repo', () => { + write('packages/checkout/PRODUCT.md', '# Checkout product\n'); + write('packages/checkout/file.ts', 'export const x = 1;\n'); + const bare = loadContext(scratch, { targetPath: 'checkout' }); + assert.equal(bare.isMonorepo, false); + assert.equal(bare.projectRoot, scratch); + const explicit = loadContext(scratch, { targetPath: 'packages/checkout/file.ts' }); + assert.equal(explicit.projectRoot, path.join(scratch, 'packages', 'checkout')); + assert.match(explicit.product, /Checkout product/); + }); + + it('does not select a negated workspace child by basename', () => { + write('pnpm-workspace.yaml', 'packages:\n - "packages/*"\n - "!packages/internal"\n'); + write('PRODUCT.md', '# Root product\n'); + write('packages/ui/src/index.ts', 'export {};\n'); + write('packages/internal/PRODUCT.md', '# Internal product\n'); + write('packages/internal/src/index.ts', 'export {};\n'); + const ctx = loadContext(scratch, { targetPath: 'internal' }); + assert.equal(ctx.projectRoot, scratch); + assert.match(ctx.product, /Root product/); + }); + it('asks for app selection even when root PRODUCT.md is absent', () => { write('package.json', JSON.stringify({ private: true, @@ -872,6 +901,16 @@ describe('loadContext (impeccable projectRoots config)', () => { assert.equal(ctx.productPath, 'PRODUCT.md'); }); + it('resolves a unique projectRoots child by bare basename', () => { + writeSkinsConfig(); + write('docs/design/skins/neon-seoul/DESIGN.md', '# Neon Seoul design\n'); + write('docs/design/skins/marble/DESIGN.md', '# Marble design\n'); + const ctx = loadContext(scratch, { targetPath: 'neon-seoul' }); + assert.equal(ctx.projectRoot, path.join(scratch, 'docs', 'design', 'skins', 'neon-seoul')); + assert.match(ctx.design, /Neon Seoul design/); + assert.match(ctx.product, /Root product/); + }); + it('resolves a config-declared child from cwd inside the folder', () => { writeSkinsConfig(); write('docs/design/skins/marble/DESIGN.md', '# Marble design\n');