Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/remove-next-lazy-discovery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@workflow/next': major
'@workflow/builders': patch
---

Remove the Next.js lazy discovery/deferred builder path and the `workflows.lazyDiscovery` option.

Fall back to direct generated-file overwrites on Windows when atomic rename is blocked by Next.js dev server file handles.
1 change: 0 additions & 1 deletion .github/workflows/event-log-race-repro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ jobs:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
WORKFLOW_PUBLIC_MANIFEST: '1'
WORKFLOW_NEXT_LAZY_DISCOVERY: '0'

steps:
- name: Checkout Repo
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ jobs:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
WORKFLOW_PUBLIC_MANIFEST: '1'
WORKFLOW_NEXT_LAZY_DISCOVERY: ${{ matrix.app.canary != true && (matrix.app.name == 'nextjs-turbopack' || matrix.app.name == 'nextjs-webpack') && '0' || '' }}
steps:
- name: Checkout Repo
uses: actions/checkout@v4
Expand Down Expand Up @@ -477,7 +476,6 @@ jobs:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
WORKFLOW_PUBLIC_MANIFEST: '1'
WORKFLOW_NEXT_LAZY_DISCOVERY: ${{ matrix.app.lazyDiscovery == false && '0' || matrix.app.lazyDiscovery == true && '1' || '' }}

steps:
- name: Checkout Repo
Expand Down Expand Up @@ -561,7 +559,6 @@ jobs:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
WORKFLOW_PUBLIC_MANIFEST: '1'
WORKFLOW_NEXT_LAZY_DISCOVERY: ${{ matrix.app.lazyDiscovery == false && '0' || matrix.app.lazyDiscovery == true && '1' || '' }}

steps:
- name: Checkout Repo
Expand Down Expand Up @@ -666,7 +663,6 @@ jobs:
WORKFLOW_PUBLIC_MANIFEST: '1'
WORKFLOW_TARGET_WORLD: "@workflow/world-postgres"
WORKFLOW_POSTGRES_URL: "postgres://world:world@localhost:5432/world"
WORKFLOW_NEXT_LAZY_DISCOVERY: ${{ matrix.app.lazyDiscovery == false && '0' || matrix.app.lazyDiscovery == true && '1' || '' }}

steps:
- name: Checkout Repo
Expand Down Expand Up @@ -848,7 +844,11 @@ jobs:
NODE_OPTIONS: "--enable-source-maps"
APP_NAME: "nextjs-turbopack"
DEPLOYMENT_URL: "http://localhost:3000"
DEV_TEST_CONFIG: '{"generatedStepPath":"app/.well-known/workflow/v1/flow/__step_registrations.js","generatedWorkflowPath":"app/.well-known/workflow/v1/flow/route.js","apiFilePath":"app/api/chat/route.ts","apiFileImportPath":"../../..","port":3000}'
# Use a real workflow file for the Windows HMR test. Most of this
# workbench's workflows are symlinks into workbench/example, and
# Windows/Turbopack can rebuild from the symlink path while still
# serving the previous module contents from the realpath cache.
DEV_TEST_CONFIG: '{"generatedStepPath":"app/.well-known/workflow/v1/flow/__step_registrations.js","generatedWorkflowPath":"app/.well-known/workflow/v1/flow/route.js","apiFilePath":"app/api/chat/route.ts","apiFileImportPath":"../../..","port":3000,"testWorkflowFile":"96_many_steps.ts"}'

- name: Print Next.js server logs
if: always()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const nextConfig: NextConfig = {};

export default withWorkflow(nextConfig, {
workflows: {
lazyDiscovery: true,
local: {
port: 4000,
},
Expand All @@ -79,7 +78,6 @@ export default withWorkflow(nextConfig, {

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `workflows.lazyDiscovery` | `boolean` | `false` | When `true`, defers workflow discovery until files are requested instead of scanning eagerly at startup. Useful for large projects where startup time matters. |
| `workflows.local.port` | `number` | — | Overrides the `PORT` environment variable for local development. Has no effect when deployed to Vercel. |
| `workflows.sourcemap` | `boolean \| 'inline' \| 'linked' \| 'external' \| 'both'` | `'inline'` (dev) / `false` (prod) | Controls source maps on generated workflow bundles. See [Source maps](#source-maps) below. |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ const nextConfig: NextConfig = {};

export default withWorkflow(nextConfig, {
workflows: {
lazyDiscovery: false,
local: {
port: 4000,
},
Expand All @@ -103,7 +102,6 @@ export default withWorkflow(nextConfig, {

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `workflows.lazyDiscovery` | `boolean` | `true` | Defers workflow discovery until files are requested instead of scanning eagerly at startup. Set to `false` to force eager discovery (scanning the project up front). Requires a Next.js version that supports deferred entries; older versions fall back to eager discovery automatically. |
| `workflows.local.port` | `number` | — | Overrides the `PORT` environment variable for local development. Has no effect when deployed to Vercel. |
| `workflows.sourcemap` | `boolean \| 'inline' \| 'linked' \| 'external' \| 'both'` | `'inline'` (dev) / `false` (prod) | Controls source maps on generated workflow bundles. See [Source maps](#source-maps) below. |

Expand Down
62 changes: 43 additions & 19 deletions packages/builders/src/base-builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { randomUUID } from 'node:crypto';
import { mkdir, readFile, realpath, rename, writeFile } from 'node:fs/promises';
import {
mkdir,
readFile,
realpath,
rename,
rm,
writeFile,
} from 'node:fs/promises';
import { createRequire } from 'node:module';
import { basename, dirname, join, relative, resolve } from 'node:path';
import { promisify } from 'node:util';
Expand Down Expand Up @@ -467,6 +474,37 @@ export abstract class BaseBuilder {
return state;
}

/**
* Writes generated files atomically where possible. On Windows, Next.js can
* briefly hold generated route files open while compiling them, which makes
* rename-over-existing fail with EPERM/EACCES. In that case, fall back to a
* direct overwrite so watch rebuilds can still make progress.
*/
private async writeGeneratedFile(
targetPath: string,
content: string
): Promise<void> {
const tempPath = `${targetPath}.${randomUUID()}.tmp`;
await writeFile(tempPath, content);
try {
await rename(tempPath, targetPath);
} catch (error) {
const errorCode =
error && typeof error === 'object' && 'code' in error
? (error as NodeJS.ErrnoException).code
: undefined;
if (
process.platform === 'win32' &&
(errorCode === 'EPERM' || errorCode === 'EACCES')
) {
await writeFile(targetPath, content);
await rm(tempPath, { force: true });
return;
}
throw error;
}
}

/**
* Writes debug information to a JSON file for troubleshooting build issues.
* Uses atomic write (temp file + rename) to prevent race conditions when
Expand Down Expand Up @@ -504,12 +542,7 @@ export abstract class BaseBuilder {
2
);

// Write atomically: write to temp file, then rename.
// rename() is atomic on POSIX systems and provides best-effort atomicity on Windows.
// Prevents race conditions where concurrent builds read partially-written files.
const tempPath = `${targetPath}.${randomUUID()}.tmp`;
await writeFile(tempPath, mergedData);
await rename(tempPath, targetPath);
await this.writeGeneratedFile(targetPath, mergedData);
} catch (error: unknown) {
console.warn('Failed to write debug file:', error);
}
Expand Down Expand Up @@ -1213,11 +1246,7 @@ export const POST = workflowEntrypoint(workflowCode${workflowEntrypointOptionsCo
const outputDir = dirname(outfile);
await mkdir(outputDir, { recursive: true });

// Atomic write: write to temp file then rename to prevent
// file watchers from reading partial file during write
const tempPath = `${outfile}.${randomUUID()}.tmp`;
await writeFile(tempPath, workflowFunctionCode);
await rename(tempPath, outfile);
await this.writeGeneratedFile(outfile, workflowFunctionCode);
return;
}

Expand Down Expand Up @@ -1396,10 +1425,7 @@ const workflowCode = \`${escapedVMCode}\`;
export const POST = workflowEntrypoint(workflowCode${workflowEntrypointOptionsCode});`;

if (!bundleFinalOutput) {
// Write directly (Next.js will bundle)
const tempPath = `${flowOutfile}.${randomUUID()}.tmp`;
await writeFile(tempPath, combinedFunctionCode);
await rename(tempPath, flowOutfile);
await this.writeGeneratedFile(flowOutfile, combinedFunctionCode);
} else {
// Bundle the combined code for standalone use
const bundleStartTime = Date.now();
Expand Down Expand Up @@ -1469,9 +1495,7 @@ export const POST = workflowEntrypoint(workflowCode${workflowEntrypointOptionsCo

const outputDir = dirname(flowOutfile);
await mkdir(outputDir, { recursive: true });
const tempPath = `${flowOutfile}.${randomUUID()}.tmp`;
await writeFile(tempPath, code);
await rename(tempPath, flowOutfile);
await this.writeGeneratedFile(flowOutfile, code);
};

if (this.config.watch) {
Expand Down
Loading
Loading