Skip to content

Commit d8037fd

Browse files
fix: Ensure typegen CLI command doesn't hang due to open handles (#83966)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> ### What? Running `next typegen` can hang even after `✓ Route types generated successfully` prints. ``` Generating route types... Using vars defined in .dev.vars ✓ Route types generated successfully ... ``` ### Why? This is very problematic when needing to run `typegen` before `tsc` on CI. If the command hangs, then `tsc` never starts. ### How? `next build` has the same potential issue and it works around this by waiting for the promise to resolve and then calling `process.exit(0)`. I copied this same solution over to the `typegen` command. Co-authored-by: Joseph <[email protected]>
1 parent e9e5eca commit d8037fd

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

packages/next/src/bin/next.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,10 @@ program
395395
)}`
396396
)
397397
.action((directory: string, options: NextTypegenOptions) =>
398+
// ensure process exits after typegen completes so open handles/connections
399+
// don't cause process to hang
398400
import('../cli/next-typegen.js').then((mod) =>
399-
mod.nextTypegen(options, directory)
401+
mod.nextTypegen(options, directory).then(() => process.exit(0))
400402
)
401403
)
402404
.usage('[directory] [options]')

test/e2e/app-dir/typed-routes/next.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// intervals/open connections shouldn't block typegen from exiting
2+
setInterval(() => {}, 250)
3+
14
/**
25
* @type {import('next').NextConfig}
36
*/

test/e2e/app-dir/typed-routes/typed-routes.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { nextTestSetup } from 'e2e-utils'
2+
import { runNextCommand } from 'next-test-utils'
23

34
const expectedDts = `
45
type AppRoutes = "/" | "/_shop/[[...category]]" | "/dashboard" | "/dashboard/settings" | "/docs/[...slug]" | "/gallery/photo/[id]" | "/project/[slug]"
@@ -90,4 +91,12 @@ type InvalidRoute = RouteContext<'/api/users/invalid'>`
9091
)
9192
})
9293
}
94+
95+
it('should exit typegen successfully', async () => {
96+
const { code } = await runNextCommand(['typegen'], {
97+
cwd: next.testDir,
98+
})
99+
100+
expect(code).toBe(0)
101+
})
93102
})

0 commit comments

Comments
 (0)