nhost
nhost copied to clipboard
Functions Hot-Reload Watcher Ignores Newly Created or Regenerated Dependency & Lock Files
1. Architectural Location
- Subsystem: Nhost Functions Service (
services/functions) - File:
services/functions/server.js(Lines 418–435)
2. Root Cause Analysis
The Nhost functions runtime includes a background polling interval in services/functions/server.js to detect when dependencies change and automatically reinstall them:
const depFiles = [
'package.json',
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
];
const depMtimes = new Map();
for (const f of depFiles) {
try {
depMtimes.set(f, fs.statSync(path.join(workingDir, f)).mtimeMs);
} catch {}
}
setInterval(async () => {
for (const file of depFiles) {
const filePath = path.join(workingDir, file);
let mtimeMs;
try {
mtimeMs = fs.statSync(filePath).mtimeMs;
} catch {
if (depMtimes.has(file)) depMtimes.delete(file);
continue;
}
const prev = depMtimes.get(file);
if (prev !== undefined && prev !== mtimeMs) {
depMtimes.set(file, mtimeMs);
console.log(`Dependency file changed: ${file} ── reinstalling...`);
try {
const installScript = path.join(__dirname, 'nhost-install-deps.sh');
execSync(`. "${installScript}" && nhost_install_deps`, {
stdio: 'inherit',
env: { ...process.env, WORK_DIR: workingDir },
});
console.log('Dependencies installed. Rebuilding all functions...');
await rebuildAll();
} catch (err) {
console.error('Failed to reinstall dependencies:', err);
}
return;
}
depMtimes.set(file, mtimeMs);
}
}, 1000);
Look at the condition:
if (prev !== undefined && prev !== mtimeMs)
If a project is started before a lockfile is generated (e.g., initial repository setup or running without pnpm-lock.yaml), or if a lockfile is removed and regenerated while developing:
- At startup:
depMtimeshas no entry forpnpm-lock.yaml. - When the user creates or generates
pnpm-lock.yaml:fs.statSync(filePath).mtimeMssuccessfully retrieves the timestamp.previsdepMtimes.get('pnpm-lock.yaml'), which returnsundefined.- The condition
prev !== undefined && prev !== mtimeMsevaluates tofalse! - The rebuild block is skipped entirely.
- Line 434 executes:
depMtimes.set(file, mtimeMs);
- On all future polling intervals,
prev === mtimeMs, soprev !== mtimeMsis never true unless modified again. - Result: The newly added dependency manifest or lockfile is silently registered into the map without ever triggering
nhost_install_depsorrebuildAll().
3. Steps to Reproduce
- Start the functions server in a working directory without a
pnpm-lock.yaml. - While the server is running, install a new package using
pnpm install <package>, which createspnpm-lock.yamland updatesnode_modules. - Check the terminal logs of
services/functions/server.js. - Observe that no dependency reinstallation or rebuild was triggered. The runtime continues using stale function builds.
4. Expected vs. Actual Behavior
- Expected Behavior: When a dependency file or lockfile is newly created while the runtime is active (
prev === undefinedafter initial startup), the server should trigger dependency reinstallation and invokerebuildAll(). - Actual Behavior: The creation event is completely ignored; only subsequent modifications to already-existing files are tracked.
5. Blast Radius & Impact
- Developers adding new dependencies or switching package managers during development will have their changes silently ignored.
- Functions fail at runtime with
MODULE_NOT_FOUNDuntil the developer manually kills and restarts the container or development server.
6. Proposed Fix
--- a/services/functions/server.js
+++ b/services/functions/server.js
@@ -414,8 +414,9 @@ const main = async () => {
continue;
}
const prev = depMtimes.get(file);
- if (prev !== undefined && prev !== mtimeMs) {
+ if (prev !== mtimeMs) {
depMtimes.set(file, mtimeMs);
+ if (prev === undefined) return; // Ignore on very first tick, or trigger if initial pass already done
console.log(`Dependency file changed: ${file} ── reinstalling...`);
try {
// Reinstall through the same shared library start.sh uses, so a
Or better, explicitly track initial boot completion:
--- a/services/functions/server.js
+++ b/services/functions/server.js
@@ -410,12 +410,13 @@ const main = async () => {
try {
depMtimes.set(f, fs.statSync(path.join(workingDir, f)).mtimeMs);
} catch {}
}
+ let initialBootDone = true;
setInterval(async () => {
for (const file of depFiles) {
const filePath = path.join(workingDir, file);
let mtimeMs;
try {
mtimeMs = fs.statSync(filePath).mtimeMs;
} catch {
if (depMtimes.has(file)) depMtimes.delete(file);
continue;
}
const prev = depMtimes.get(file);
- if (prev !== undefined && prev !== mtimeMs) {
+ if (prev !== mtimeMs) {
depMtimes.set(file, mtimeMs);
- console.log(`Dependency file changed: ${file} ── reinstalling...`);
+ console.log(`Dependency file added or changed: ${file} ── reinstalling...`);
try {
const installScript = path.join(__dirname, 'nhost-install-deps.sh');
execSync(`. "${installScript}" && nhost_install_deps`, {
stdio: 'inherit',
env: { ...process.env, WORK_DIR: workingDir },
});
console.log('Dependencies installed. Rebuilding all functions...');
await rebuildAll();
} catch (err) {
console.error('Failed to reinstall dependencies:', err);
}
return;
}
}
}, 1000);
Verification & Audit Methodology
- Static Analysis & Control-Flow Tracing: Traced request lifecycles from API boundaries to database operations across Go controllers and JavaScript service runtimes.
- Differential Comparison: Compared parity between parallel handlers (e.g.
upload_files.govsreplace_file.go, anddeanonymize_user_sms.govsdeanonymize_user.go), confirming unintentional omissions. - Specification Verification: Validated HTTP header specifications (RFC 9110) against string-splitting logic in media-type negotiation.
- Unfiled Status: Verified against the Nhost GitHub issue tracker to guarantee 0% overlap with existing reported issues.