Skip to content

Commit cb18518

Browse files
committed
fix(cli): make status resilient to ENOENT races
1 parent 0932508 commit cb18518

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

src/cli/opencode-cursor.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,23 @@ function resolvePluginSource(): string {
310310
throw new Error("Unable to locate plugin-entry.js next to CLI distribution files");
311311
}
312312

313+
function isErrnoException(error: unknown): error is NodeJS.ErrnoException {
314+
return typeof error === "object" && error !== null && "code" in error;
315+
}
316+
313317
function readConfig(configPath: string): any {
314318
if (!existsSync(configPath)) {
315319
return { plugin: [], provider: {} };
316320
}
317-
const raw = readFileSync(configPath, "utf8");
321+
let raw: string;
322+
try {
323+
raw = readFileSync(configPath, "utf8");
324+
} catch (error) {
325+
if (isErrnoException(error) && error.code === "ENOENT") {
326+
return { plugin: [], provider: {} };
327+
}
328+
throw error;
329+
}
318330
try {
319331
return JSON.parse(raw);
320332
} catch (error) {
@@ -455,14 +467,22 @@ export function getStatusResult(configPath: string, pluginPath: string): StatusR
455467
let pluginType: "symlink" | "file" | "missing" = "missing";
456468
let pluginTarget: string | undefined;
457469
if (existsSync(pluginPath)) {
458-
const stat = lstatSync(pluginPath);
459-
pluginType = stat.isSymbolicLink() ? "symlink" : "file";
460-
if (pluginType === "symlink") {
461-
try {
462-
pluginTarget = readFileSync(pluginPath, "utf8");
463-
} catch {
464-
pluginTarget = undefined;
470+
try {
471+
const stat = lstatSync(pluginPath);
472+
pluginType = stat.isSymbolicLink() ? "symlink" : "file";
473+
if (pluginType === "symlink") {
474+
try {
475+
pluginTarget = readFileSync(pluginPath, "utf8");
476+
} catch {
477+
pluginTarget = undefined;
478+
}
479+
}
480+
} catch (error) {
481+
if (!isErrnoException(error) || error.code !== "ENOENT") {
482+
throw error;
465483
}
484+
pluginType = "missing";
485+
pluginTarget = undefined;
466486
}
467487
}
468488

0 commit comments

Comments
 (0)