Skip to content
Open
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
33 changes: 27 additions & 6 deletions src/node/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
import { existsSync, lstatSync, readdirSync, readFileSync, statSync } from "fs";
import ignore from "ignore";
import { join, relative, sep } from "path";

Expand Down Expand Up @@ -94,9 +94,19 @@ export function getAllFiles(
continue;
}

const stat = statSync(filePath);
const stat = lstatSync(filePath);

if (stat.isDirectory()) {
if (stat.isSymbolicLink() && !existsSync(filePath)) {
// Broken (dangling) symlink: statSync would throw ENOENT. Skip it and
// keep packing instead of aborting the whole bundle.
console.warn(`Warning: Skipping broken symlink: ${relativePath}`);
continue;
}

// Valid symlinks are still followed, preserving the previous behavior.
const targetStat = stat.isSymbolicLink() ? statSync(filePath) : stat;

if (targetStat.isDirectory()) {
getAllFiles(filePath, baseDir, fileList, additionalPatterns);
} else {
// Use forward slashes in zip file paths
Expand Down Expand Up @@ -137,9 +147,20 @@ export function getAllFilesWithCount(
continue;
}

const stat = statSync(filePath);
const stat = lstatSync(filePath);

if (stat.isSymbolicLink() && !existsSync(filePath)) {
// Broken (dangling) symlink: statSync would throw ENOENT. Skip it and
// keep packing instead of aborting the whole bundle.
console.warn(`Warning: Skipping broken symlink: ${relativePath}`);
ignoredCount++;
continue;
}

// Valid symlinks are still followed, preserving the previous behavior.
const targetStat = stat.isSymbolicLink() ? statSync(filePath) : stat;

if (stat.isDirectory()) {
if (targetStat.isDirectory()) {
const result = getAllFilesWithCount(
filePath,
baseDir,
Expand All @@ -153,7 +174,7 @@ export function getAllFilesWithCount(
const zipPath = relativePath.split(sep).join("/");
fileList[zipPath] = {
data: readFileSync(filePath),
mode: stat.mode,
mode: targetStat.mode,
};
}
}
Expand Down
55 changes: 55 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,61 @@ describe("DXT CLI", () => {
expect(fs.existsSync(packedFilePath)).toBe(true);
});

it("should skip a broken symlink instead of failing with ENOENT", () => {
const symlinkDir = join(__dirname, "temp-symlink-test");
const symlinkPackedPath = join(__dirname, "test-symlink.dxt");
const symlinkUnpackedDir = join(__dirname, "temp-symlink-unpack");

try {
// Fixture directory containing a broken (dangling) symlink
fs.mkdirSync(symlinkDir, { recursive: true });
fs.writeFileSync(
join(symlinkDir, "manifest.json"),
JSON.stringify({
manifest_version: DEFAULT_MANIFEST_VERSION,
name: "Test Symlink",
version: "1.0.0",
description: "Test broken symlink handling",
author: {
name: "MCPB",
},
server: {
type: "node",
entry_point: "server/index.js",
mcp_config: {
command: "node",
},
},
}),
);
fs.mkdirSync(join(symlinkDir, "server"), { recursive: true });
fs.writeFileSync(join(symlinkDir, "server", "index.js"), "// fixture");
fs.symlinkSync("./nowhere", join(symlinkDir, "dangling"));

// pack must succeed (exit 0) and write the bundle
execSync(`node ${cliPath} pack ${symlinkDir} ${symlinkPackedPath}`, {
encoding: "utf-8",
});
expect(fs.existsSync(symlinkPackedPath)).toBe(true);

// the broken symlink must not end up in the bundle
execSync(
`node ${cliPath} unpack ${symlinkPackedPath} ${symlinkUnpackedDir}`,
{ encoding: "utf-8" },
);
expect(fs.existsSync(join(symlinkUnpackedDir, "manifest.json"))).toBe(
true,
);
expect(fs.existsSync(join(symlinkUnpackedDir, "dangling"))).toBe(false);
} finally {
fs.rmSync(symlinkDir, { recursive: true, force: true });
fs.rmSync(symlinkUnpackedDir, { recursive: true, force: true });
if (fs.existsSync(symlinkPackedPath)) {
fs.unlinkSync(symlinkPackedPath);
}
}
});

it("should unpack an extension", () => {
execSync(`node ${cliPath} unpack ${packedFilePath} ${unpackedDir}`, {
encoding: "utf-8",
Expand Down