Skip to content

Commit e4230b4

Browse files
Fix npm resolution crash on reinstall/uninstall due to missing platform package skeletons (#599)
- [x] Identify root cause: `preinstall.cjs` only creates a skeleton for the current platform, but `optionalDependencies` references all 4 platforms as `file:` deps - [x] Fix `preinstall.cjs` to create skeletons for all platforms so npm can resolve all `file:` dependencies - [x] Address review feedback: derive ALL_PLATFORMS from package.json optionalDependencies instead of hardcoding - [x] Address review feedback: remove unused `os` require - [x] Address review feedback: fix placeholder version comment to reference correct script --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: baijumeswani <12852605+baijumeswani@users.noreply.github.com> Co-authored-by: Baiju Meswani <baijumeswani@gmail.com>
1 parent c3e5841 commit e4230b4

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

sdk/js/script/preinstall.cjs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,52 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const os = require('os');
43

54
console.log('[foundry-local] Preinstall: creating platform package skeletons...');
65

7-
const platformKey = `${os.platform()}-${os.arch()}`;
6+
// Derive all platform packages from optionalDependencies in package.json
7+
// so this script stays in sync automatically.
8+
const rootPackageJsonPath = path.join(__dirname, '..', 'package.json');
9+
const rootPackageJson = JSON.parse(fs.readFileSync(rootPackageJsonPath, 'utf8'));
10+
const optionalDependencies = rootPackageJson.optionalDependencies || {};
11+
const platformPackagePrefix = '@foundry-local-core/';
12+
13+
const ALL_PLATFORMS = Object.keys(optionalDependencies)
14+
.filter((packageName) => packageName.startsWith(platformPackagePrefix))
15+
.map((packageName) => {
16+
const key = packageName.slice(platformPackagePrefix.length);
17+
const parts = key.split('-');
18+
const cpu = parts[parts.length - 1];
19+
const platformOs = parts.slice(0, -1).join('-');
20+
21+
return {
22+
key,
23+
os: platformOs,
24+
cpu,
25+
};
26+
});
827

928
const packagesRoot = path.join(__dirname, '..', 'packages', '@foundry-local-core');
1029

11-
const dir = path.join(packagesRoot, platformKey);
30+
for (const platform of ALL_PLATFORMS) {
31+
const dir = path.join(packagesRoot, platform.key);
1232

13-
if (!fs.existsSync(dir)) {
14-
fs.mkdirSync(dir, { recursive: true });
15-
}
33+
if (!fs.existsSync(dir)) {
34+
fs.mkdirSync(dir, { recursive: true });
35+
}
1636

17-
const pkgJsonPath = path.join(dir, 'package.json');
18-
if (!fs.existsSync(pkgJsonPath)) {
19-
const pkgContent = {
20-
name: `@foundry-local-core/${platformKey}`,
21-
version: "0.0.0", // Placeholder version, will be replaced during install.cjs
22-
description: `Native binaries for Foundry Local SDK (${platformKey})`,
23-
os: [os.platform()],
24-
cpu: [os.arch()],
25-
private: true
26-
};
27-
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgContent, null, 2));
28-
console.log(` Created skeleton for ${platformKey}`);
37+
const pkgJsonPath = path.join(dir, 'package.json');
38+
if (!fs.existsSync(pkgJsonPath)) {
39+
const pkgContent = {
40+
name: `@foundry-local-core/${platform.key}`,
41+
version: "0.0.0", // Placeholder version, will be replaced during script/install-utils.cjs (installPackage())
42+
description: `Native binaries for Foundry Local SDK (${platform.key})`,
43+
os: [platform.os],
44+
cpu: [platform.cpu],
45+
private: true
46+
};
47+
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgContent, null, 2));
48+
console.log(` Created skeleton for ${platform.key}`);
49+
}
2950
}
3051

3152
console.log('[foundry-local] Preinstall complete.');

0 commit comments

Comments
 (0)