Skip to content

Commit 63b9913

Browse files
committed
Deployment symlink fix attempt
1 parent 718c383 commit 63b9913

1 file changed

Lines changed: 56 additions & 21 deletions

File tree

‎tools/pack_server.js‎

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,47 @@ async function ensureDir(dir) {
3737
await fsp.mkdir(dir, { recursive: true });
3838
}
3939

40+
async function fileExists(p) {
41+
try {
42+
await fsp.access(p);
43+
return true;
44+
} catch {
45+
return false;
46+
}
47+
}
48+
49+
// Remove dependency specs that tend to create symlinks/junctions (file:/link:/workspace:)
50+
async function sanitizeStagedBackend(stagedBackendDir) {
51+
const stagedPkgPath = path.join(stagedBackendDir, 'package.json');
52+
if (!(await fileExists(stagedPkgPath))) return;
53+
54+
const pkg = JSON.parse(await fsp.readFile(stagedPkgPath, 'utf8'));
55+
const deps = pkg.dependencies || {};
56+
const badSpecs = new Set(['file:', 'link:', 'workspace:']);
57+
const removed = [];
58+
for (const [name, spec] of Object.entries(deps)) {
59+
if (typeof spec !== 'string') continue;
60+
for (const prefix of badSpecs) {
61+
if (spec.startsWith(prefix)) {
62+
removed.push([name, spec]);
63+
delete deps[name];
64+
break;
65+
}
66+
}
67+
}
68+
if (removed.length) {
69+
console.warn('Sanitizing staged backend dependencies that can create recursive symlinks:');
70+
for (const [name, spec] of removed) console.warn(` - removed ${name}: ${spec}`);
71+
pkg.dependencies = deps;
72+
await fsp.writeFile(stagedPkgPath, JSON.stringify(pkg, null, 2));
73+
}
74+
75+
// Ensure installs are driven from the sanitized package.json
76+
// (package-lock/shrinkwrap can reintroduce local/file deps even with flags)
77+
await fsp.rm(path.join(stagedBackendDir, 'package-lock.json'), { force: true }).catch(() => {});
78+
await fsp.rm(path.join(stagedBackendDir, 'npm-shrinkwrap.json'), { force: true }).catch(() => {});
79+
}
80+
4081
// Copy a directory tree while excluding specified top-level names (e.g., node_modules)
4182
async function copyDirExcluding(src, dest, excludeNames = new Set()) {
4283
await ensureDir(dest);
@@ -67,20 +108,6 @@ async function main() {
67108

68109
const root = process.cwd();
69110
const version = require(path.join(root, 'package.json')).version;
70-
// Preflight: detect and auto-remove any local file: dependencies (e.g. monorepo self-links)
71-
const backendPkgPath = path.join(root, 'backend', 'package.json');
72-
const backendPkg = JSON.parse(fs.readFileSync(backendPkgPath, 'utf8'));
73-
const deps = backendPkg.dependencies || {};
74-
const fileDeps = Object.entries(deps).filter(([, spec]) => typeof spec === 'string' && spec.startsWith('file:'));
75-
if (fileDeps.length > 0) {
76-
console.warn('Detected local file: dependencies in backend/package.json that can cause symlink/EPERM issues. Auto-removing for pack:');
77-
for (const [name, spec] of fileDeps) {
78-
console.warn(` - removing ${name}: ${spec}`);
79-
delete deps[name];
80-
}
81-
backendPkg.dependencies = deps;
82-
fs.writeFileSync(backendPkgPath, JSON.stringify(backendPkg, null, 2));
83-
}
84111
const outBase = path.join(root, 'build', 'server', platform);
85112
const distDir = path.join(root, 'build', 'dist');
86113
await ensureDir(outBase);
@@ -121,7 +148,9 @@ async function main() {
121148

122149
// Copy backend, pythonCode, pythonEnv
123150
// Copy backend excluding node_modules to avoid Windows symlink EPERM
124-
await copyDirExcluding(path.join(root, 'backend'), path.join(outBase, 'backend'), new Set(['node_modules']));
151+
const stagedBackendDir = path.join(outBase, 'backend');
152+
await copyDirExcluding(path.join(root, 'backend'), stagedBackendDir, new Set(['node_modules']));
153+
await sanitizeStagedBackend(stagedBackendDir);
125154
await cpRecursive(path.join(root, 'pythonCode'), path.join(outBase, 'pythonCode'));
126155
await cpRecursive(path.join(root, 'pythonEnv'), path.join(outBase, 'pythonEnv'));
127156

@@ -157,15 +186,21 @@ async function main() {
157186
// Install backend production dependencies into the staged backend
158187
console.log('Installing backend production dependencies...');
159188
// Ensure a clean node_modules in staging
160-
await fsp.rm(path.join(outBase, 'backend', 'node_modules'), { recursive: true, force: true }).catch(() => {});
189+
await fsp.rm(path.join(stagedBackendDir, 'node_modules'), { recursive: true, force: true }).catch(() => {});
161190
// Use npm with --prefix to install into the staged backend folder
162191
// Add --no-bin-links to avoid symlink creation on Windows (EPERM without admin/dev-mode)
163-
// Also disable lockfile to avoid reintroducing local file deps via package-lock
164-
sh(`npm install --omit=dev --no-bin-links --no-package-lock --prefix "${path.join(outBase, 'backend')}"`, { shell: true });
192+
// Also disable lockfile creation; we already removed lockfiles in the staged backend.
193+
sh(`npm install --omit=dev --no-bin-links --no-package-lock --prefix "${stagedBackendDir}"`, { shell: true });
194+
195+
// If the staged backend depends on the parent repo via file:/link:/workspace:,
196+
// npm can create a self-referencing symlink/junction that points back into the repo.
197+
// zip-local follows symlinks when stat'ing and will recurse until it hits ELOOP.
198+
// Remove known self-link candidates proactively.
199+
await removeRecursiveSymlink(path.join(stagedBackendDir, 'node_modules', 'medomics-platform'));
200+
await removeRecursiveSymlink(path.join(stagedBackendDir, 'node_modules', 'medomicslab-application'));
165201

166-
// npm creates a self-referencing symlink (package name matches repo) which causes
167-
// infinite recursion when zip-local traverses the directory. Remove it proactively.
168-
await removeRecursiveSymlink(path.join(outBase, 'backend', 'node_modules', 'medomicslab-application'));
202+
// Extra safety: if a published package ever includes build artifacts, drop them.
203+
await fsp.rm(path.join(stagedBackendDir, 'node_modules', 'medomics-platform', 'build', 'server'), { recursive: true, force: true }).catch(() => {});
169204

170205
const readme = `MEDomicsLab Server Bundle (v${version})\n\n` +
171206
`Quick start:\n` +

0 commit comments

Comments
 (0)