-
Notifications
You must be signed in to change notification settings - Fork 227
fix(next): skip eager workflow discovery in deferred mode with explicit dirs #1585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ijjk
wants to merge
23
commits into
main
Choose a base branch
from
cursor/workflow-discovery-deferred-build-2820
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
481e8c7
fix(next): skip eager workflow discovery in deferred mode when dirs a…
cursoragent bf0603e
chore: add changeset for deferred discovery fix
cursoragent bcb112b
fix: add dirsAreEntrypoints to NextConfig type definition
cursoragent d504a2a
fix(next): remove eager workflow discovery from deferred mode entirely
cursoragent dce734c
chore: update changeset description to reflect simplified fix
cursoragent 13eefa0
fix: remove unused loadDiscoveredEntriesFromInputGraph method
cursoragent d205b62
bump
ijjk 9aa1f07
fix(ai): add workflow export condition to agent entrypoint
cursoragent 9876bd7
chore: update changeset to include @workflow/ai fix
cursoragent 4a7e1df
test: update deferred builder test to use cache scenario
cursoragent 24d4202
fix(ai): point workflow export to compiled dist instead of source
cursoragent 3fe4916
fix(next): allow @workflow packages in node_modules to be discovered
cursoragent 5d31437
fix(next): add @workflow/ai to transpilePackages for loader processing
cursoragent 51eaf15
fix(next): restore discovery for production builds and first dev build
cursoragent b6b2126
chore: update changeset and test to reflect conditional discovery
cursoragent 8516d99
fix: address AI review comments
cursoragent c5201e7
fix: improve test to spy on real implementation instead of mocking
cursoragent 3213358
Merge branch 'cursor/workflow-discovery-deferred-build-2820' of githu…
ijjk 796949a
test(next): make deferred builder tests runnable and representative
ijjk 5d4f6a4
Merge branch 'main' into cursor/workflow-discovery-deferred-build-2820
ijjk 6860eda
fix(next): remove upfront discovery in deferred mode
ijjk 8f5a208
fix(next): include @workflow package imports in deferred step graph
ijjk 9e7015b
fix(next): keep turbopack loader conditions in deferred mode
ijjk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@workflow/next": patch | ||
| --- | ||
|
|
||
| Skip upfront workflow discovery scans in deferred (`lazyDiscovery`) mode and rely on deferred/loader-driven discovery only. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@workflow/ai": patch | ||
| "@workflow/next": patch | ||
| --- | ||
|
|
||
| Skip redundant workflow discovery on dev server restart when cache exists. Add workflow export condition and transpile configuration for @workflow/ai. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| type BuilderWithInit = { | ||
| initializeDiscoveryState: () => Promise<void>; | ||
| }; | ||
|
|
||
| type DiscoverEntriesOwner = { | ||
| discoverEntries: (...args: unknown[]) => Promise<unknown>; | ||
| }; | ||
|
|
||
| type BuilderWithTransitiveSteps = { | ||
| collectTransitiveStepFiles: (args: { | ||
| stepFiles: string[]; | ||
| seedFiles?: string[]; | ||
| }) => Promise<string[]>; | ||
| }; | ||
|
|
||
| describe('NextDeferredBuilder discovery behavior', () => { | ||
| let testDir: string; | ||
| let discoverEntriesSpy: ReturnType<typeof vi.spyOn>; | ||
| let evalSpy: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| const createBuilder = async (watch: boolean) => { | ||
| const { getNextBuilderDeferred } = await import('./builder-deferred.js'); | ||
| const NextDeferredBuilder = await getNextBuilderDeferred(); | ||
|
|
||
| return new NextDeferredBuilder({ | ||
| dirs: ['app'], | ||
| workingDir: testDir, | ||
| buildTarget: 'next', | ||
| stepsBundlePath: '', | ||
| workflowsBundlePath: '', | ||
| webhookBundlePath: '', | ||
| distDir: '.next', | ||
| watch, | ||
| }); | ||
| }; | ||
|
|
||
| const createAppEntrypoint = async () => { | ||
| const appDir = join(testDir, 'app'); | ||
| await mkdir(appDir, { recursive: true }); | ||
| const workflowFilePath = join(appDir, 'page.ts'); | ||
| await writeFile( | ||
| workflowFilePath, | ||
| '"use workflow";\nexport async function test() {}', | ||
| 'utf-8' | ||
| ); | ||
| return workflowFilePath; | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.resetModules(); | ||
| testDir = await mkdtemp(join(tmpdir(), 'workflow-deferred-builder-')); | ||
|
|
||
| const { BaseBuilder } = await import('@workflow/builders'); | ||
| discoverEntriesSpy = vi.spyOn( | ||
| BaseBuilder.prototype as unknown as DiscoverEntriesOwner, | ||
| 'discoverEntries' | ||
| ); | ||
|
|
||
| // Vitest executes modules in a VM context where eval('import("...")') | ||
| // requires a dynamic import callback. Forward that specific eval call to | ||
| // native dynamic import so getNextBuilderDeferred can run in tests. | ||
| evalSpy = vi.spyOn(globalThis, 'eval').mockImplementation((source) => { | ||
| if (source === 'import("@workflow/builders")') { | ||
| return import('@workflow/builders'); | ||
| } | ||
| throw new Error(`Unexpected eval source in test: ${source}`); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| discoverEntriesSpy.mockRestore(); | ||
| evalSpy.mockRestore(); | ||
| await rm(testDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('should skip discovery in dev mode when cache exists', async () => { | ||
| const cachedWorkflowFilePath = await createAppEntrypoint(); | ||
|
|
||
| // Create cache directory and cache file | ||
| const cacheDir = join(testDir, '.next', 'cache'); | ||
| await mkdir(cacheDir, { recursive: true }); | ||
| await writeFile( | ||
| join(cacheDir, 'workflows.json'), | ||
| JSON.stringify({ | ||
| workflowFiles: [cachedWorkflowFilePath], | ||
| stepFiles: [], | ||
| }), | ||
| 'utf-8' | ||
| ); | ||
|
|
||
| const builder = await createBuilder(true); | ||
| await (builder as unknown as BuilderWithInit).initializeDiscoveryState(); | ||
|
|
||
| // In dev mode with cache, discoverEntries should NOT be called | ||
| expect(discoverEntriesSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should skip discovery in production builds', async () => { | ||
| await createAppEntrypoint(); | ||
| const builder = await createBuilder(false); | ||
| await (builder as unknown as BuilderWithInit).initializeDiscoveryState(); | ||
|
|
||
| expect(discoverEntriesSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should skip discovery on first dev build when no cache', async () => { | ||
| await createAppEntrypoint(); | ||
| const builder = await createBuilder(true); | ||
| await (builder as unknown as BuilderWithInit).initializeDiscoveryState(); | ||
|
|
||
| expect(discoverEntriesSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should collect transitive @workflow package step files from workflow imports', async () => { | ||
| const appDir = join(testDir, 'app'); | ||
| const workflowFilePath = join(appDir, 'page.ts'); | ||
| await mkdir(appDir, { recursive: true }); | ||
| await writeFile( | ||
| workflowFilePath, | ||
| [ | ||
| '"use workflow";', | ||
| "import { closeStream } from '@workflow/ai/agent';", | ||
| 'export async function workflowEntry() {', | ||
| ' return closeStream();', | ||
| '}', | ||
| ].join('\n'), | ||
| 'utf-8' | ||
| ); | ||
|
|
||
| const workflowAiPackageDir = join(testDir, 'node_modules/@workflow/ai'); | ||
| await mkdir(workflowAiPackageDir, { recursive: true }); | ||
| await writeFile( | ||
| join(workflowAiPackageDir, 'package.json'), | ||
| JSON.stringify({ name: '@workflow/ai', version: '0.0.0' }), | ||
| 'utf-8' | ||
| ); | ||
| await writeFile( | ||
| join(workflowAiPackageDir, 'agent.js'), | ||
| [ | ||
| "import { nestedStep } from './nested.js';", | ||
| 'export async function closeStream() {', | ||
| " 'use step';", | ||
| ' return nestedStep();', | ||
| '}', | ||
| ].join('\n'), | ||
| 'utf-8' | ||
| ); | ||
| await writeFile( | ||
| join(workflowAiPackageDir, 'nested.js'), | ||
| [ | ||
| 'export async function nestedStep() {', | ||
| " 'use step';", | ||
| " return 'ok';", | ||
| '}', | ||
| ].join('\n'), | ||
| 'utf-8' | ||
| ); | ||
|
|
||
| const builder = await createBuilder(true); | ||
| const transitiveStepFiles = await ( | ||
| builder as unknown as BuilderWithTransitiveSteps | ||
| ).collectTransitiveStepFiles({ | ||
| stepFiles: [], | ||
| seedFiles: [workflowFilePath], | ||
| }); | ||
|
|
||
| const normalizedStepFiles = transitiveStepFiles.map((filePath) => | ||
| filePath.replace(/\\/g, '/') | ||
| ); | ||
| expect( | ||
| normalizedStepFiles.some((filePath) => | ||
| filePath.includes('/node_modules/@workflow/ai/agent') | ||
| ) | ||
| ).toBe(true); | ||
| expect( | ||
| normalizedStepFiles.some((filePath) => | ||
| filePath.includes('/node_modules/@workflow/ai/nested.js') | ||
| ) | ||
| ).toBe(true); | ||
| }); | ||
| }); | ||
ijjk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.