Fix SDK embed ENOENT when package.json missing (closes #5226)

- Wrap package.json read in try/catch to handle missing file gracefully
- Use defaults (APP_NAME=pi, CONFIG_DIR_NAME=.pi, etc.) when package.json not found
- Preserves rebranding via piConfig when package.json exists
- Only ignores ENOENT; other errors still throw
This commit is contained in:
Mario Zechner
2026-06-02 12:12:10 +02:00
parent 7c08227019
commit 4c6c4482b4
2 changed files with 8 additions and 1 deletions

View File

@@ -452,7 +452,13 @@ interface PackageJson {
};
}
const pkg = JSON.parse(readFileSync(getPackageJsonPath(), "utf-8")) as PackageJson;
let pkg: PackageJson = {};
try {
pkg = JSON.parse(readFileSync(getPackageJsonPath(), "utf-8")) as PackageJson;
} catch (e: unknown) {
const err = e as NodeJS.ErrnoException;
if (err.code !== "ENOENT") throw e;
}
const piConfigName: string | undefined = pkg.piConfig?.name;
export const PACKAGE_NAME: string = pkg.name || "@earendil-works/pi-coding-agent";