Skip to content

fix(build): defer module-level process.cwd() to fix Edge bundling#174

Merged
TerrifiedBug merged 2 commits intomainfrom
fix/edge-bundle-process-cwd
Apr 25, 2026
Merged

fix(build): defer module-level process.cwd() to fix Edge bundling#174
TerrifiedBug merged 2 commits intomainfrom
fix/edge-bundle-process-cwd

Conversation

@TerrifiedBug
Copy link
Copy Markdown
Owner

Summary

Main CI broke after PR #172 (demo mode) merged: https://github.com/TerrifiedBug/vectorflow/actions/runs/24942402486

The Edge runtime bundler now traces into src/server/services/{audit,backup,system-vector}.ts and rejects them because they call process.cwd() (and process.on() for SIGTERM/SIGINT) at module scope. The Edge bundler evaluates module-level code statically; even though the runtime guard in instrumentation.ts (if (process.env.NEXT_RUNTIME !== "nodejs") return;) prevents execution in Edge, it doesn't prevent bundling.

Fix

Defer the offending process.cwd() calls to call-time via lazy getters:

// Before:
const VECTORFLOW_DATA_DIR = join(process.cwd(), ".vectorflow");

// After:
let _vectorflowDataDir: string | null = null;
function getVectorflowDataDir(): string {
  if (_vectorflowDataDir === null) {
    _vectorflowDataDir = join(process.cwd(), ".vectorflow");
  }
  return _vectorflowDataDir;
}

Same pattern for AUDIT_LOG_PATH, MIGRATIONS_DIR, and SYSTEM_CONFIG_PATH. Module-level process.on() calls in system-vector.ts were also moved into a function called from the existing startup path.

Runtime behaviour unchanged — the values are computed on first access and cached for the process lifetime.

Files

  • src/server/services/audit.ts
  • src/server/services/backup.ts
  • src/server/services/system-vector.ts

Test plan

  • Local pnpm build passes (Edge bundle accepts the new lazy patterns)
  • Local pnpm test passes (2526 tests, same as before)
  • CI green on this PR

@github-actions github-actions Bot added the fix label Apr 25, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 25, 2026

Greptile Summary

This PR fixes the Edge bundler build failure by deferring process.cwd() and process.on() calls out of module-level scope across three service files. Both previously-flagged critical issues — startSystemVector referencing undefined symbols after the constant-to-getter refactor, and setupShutdownHook() still being invoked unconditionally at module load — are fully resolved in this revision.

Confidence Score: 5/5

Safe to merge — both previously-flagged critical issues are fully resolved and no new issues introduced.

All three files correctly defer process.cwd() and process.on() to function call time. The previous P0 (undefined symbols inside startSystemVector) and P1 (setupShutdownHook at module scope) are both fixed. No remaining P0 or P1 findings.

No files require special attention.

Important Files Changed

Filename Overview
src/server/services/audit.ts Module-level AUDIT_LOG_PATH constant replaced with lazy getAuditLogPath() getter with memoization; fs/promises imports moved to dynamic await import() inside the function body. Change is correct and complete.
src/server/services/backup.ts MIGRATIONS_DIR constant replaced with lazy getMigrationsDir() with memoization; process.cwd() deferred correctly to call time. All call sites updated.
src/server/services/system-vector.ts process.cwd() deferred into getVectorflowDataDir()/getSystemConfigPath(); process.on() moved out of module scope into ensureShutdownHook() called from startSystemVector. _shutdownHookRegistered flag is now correctly read and written. Both previously-flagged P0/P1 issues are resolved.

Sequence Diagram

sequenceDiagram
    participant EB as Edge Bundler
    participant Inst as instrumentation.ts
    participant SV as system-vector.ts
    participant A as audit.ts
    participant B as backup.ts

    Note over EB: Static analysis / module tracing
    EB->>Inst: trace imports
    Inst-->>EB: guard: NEXT_RUNTIME === "nodejs"
    EB->>SV: trace (still bundles file)
    Note over SV: process.cwd() now deferred to getVectorflowDataDir()<br/>process.on() now in ensureShutdownHook()
    EB->>A: trace (still bundles file)
    Note over A: process.cwd() now deferred to getAuditLogPath()
    EB->>B: trace (still bundles file)
    Note over B: process.cwd() now deferred to getMigrationsDir()
    Note over EB: No module-level Node API calls - build passes

    participant RT as Node.js Runtime
    RT->>SV: startSystemVector(configYaml)
    SV->>SV: getSystemConfigPath() -> process.cwd()
    SV->>SV: getVectorflowDataDir() -> process.cwd()
    SV->>A: getAuditLogPath() -> process.cwd() (cached)
    SV->>SV: ensureShutdownHook() -> process.on(SIGTERM/SIGINT)
Loading

Reviews (2): Last reviewed commit: "fix(system-vector): wire startSystemVect..." | Re-trigger Greptile

@TerrifiedBug
Copy link
Copy Markdown
Owner Author

@greptile review

@TerrifiedBug TerrifiedBug merged commit 43c5dc8 into main Apr 25, 2026
11 checks passed
@TerrifiedBug TerrifiedBug deleted the fix/edge-bundle-process-cwd branch April 25, 2026 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant