Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/finalize-symlink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ test('finalize rejects file paths resolving to the output root before cleaning',
}
} finally { await rm(dir, { recursive: true, force: true }); }
});

test('finalize rejects conflicting file and directory paths before cleaning', async () => {
const dir = await mkdtemp(join(tmpdir(), 'spec2mcp-conflicting-files-'));
try {
const forge = await init(DOC as never);
const out = join(dir, 'out');
await mkdir(out);
const existing = join(out, 'keep.txt');
await writeFile(existing, 'keep');
await assert.rejects(forge.finalize(out, [
{ path: 'sub', content: 'a file' },
{ path: 'sub/nested.txt', content: 'nested file' },
], { clean: true }), /conflicting output paths/i);
assert.equal(await readFile(existing, 'utf8'), 'keep');
} finally { await rm(dir, { recursive: true, force: true }); }
});
12 changes: 12 additions & 0 deletions vendor/forge/forge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ export class Forge {
resolvedFiles.set(fullPath, file.content);
}

// A file cannot also be an ancestor directory of another output file.
// Reject impossible layouts before cleaning or writing any output.
for (const fullPath of resolvedFiles.keys()) {
let parent = dirname(fullPath);
while (parent !== resolvedOutputDir) {
if (resolvedFiles.has(parent)) {
throw new Error(`Conflicting output paths: "${parent}" is both a file and a directory`);
}
parent = dirname(parent);
}
}

if (options?.clean) {
await rm(resolvedOutputDir, { recursive: true, force: true });
}
Expand Down
Loading