From 480fd0cf573f53625de92e7581c84cf90d2e021c Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Wed, 19 Nov 2025 19:19:50 +0900 Subject: [PATCH] Fix turbo boot issue --- .../changepack_log_0F-1MEkW9sLWK_J0m2245.json | 5 ++ .../next-plugin/src/__tests__/plugin.test.ts | 61 +++++++++++++++++++ packages/next-plugin/src/plugin.ts | 8 ++- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 .changepacks/changepack_log_0F-1MEkW9sLWK_J0m2245.json diff --git a/.changepacks/changepack_log_0F-1MEkW9sLWK_J0m2245.json b/.changepacks/changepack_log_0F-1MEkW9sLWK_J0m2245.json new file mode 100644 index 00000000..4283ae1c --- /dev/null +++ b/.changepacks/changepack_log_0F-1MEkW9sLWK_J0m2245.json @@ -0,0 +1,5 @@ +{ + "changes": { "packages/next-plugin/package.json": "Patch" }, + "note": "Add forced boot code", + "date": "2025-11-19T08:36:56.984126800Z" +} diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index a6c77ae9..2ea02a41 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -15,6 +15,7 @@ vi.mock('@devup-ui/wasm', async (original) => ({ registerTheme: vi.fn(), getThemeInterface: vi.fn(), getDefaultTheme: vi.fn(), + getCss: vi.fn(() => ''), exportSheet: vi.fn(() => JSON.stringify({ css: {}, @@ -89,6 +90,15 @@ describe('DevupUINextPlugin', () => { }) }) describe('turbo', () => { + beforeEach(() => { + // Mock fetch globally to prevent "http://localhost:undefined" errors + global.fetch = vi.fn(() => Promise.resolve({} as Response)) + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + it('should apply turbo config', async () => { vi.stubEnv('TURBOPACK', '1') vi.mocked(existsSync) @@ -390,5 +400,56 @@ describe('DevupUINextPlugin', () => { DEVUP_UI_DEFAULT_THEME: 'light', }) }) + it('should handle debugPort fetch failure in development mode', async () => { + vi.stubEnv('TURBOPACK', '1') + vi.stubEnv('NODE_ENV', 'development') + vi.stubEnv('PORT', '3000') + vi.mocked(existsSync) + .mockReturnValueOnce(true) + .mockReturnValueOnce(true) + .mockReturnValueOnce(true) + .mockReturnValueOnce(false) + vi.mocked(writeFileSync).mockReturnValue() + + // Mock process.exit to prevent actual exit + const originalExit = process.exit + const exitSpy = vi.fn() + process.exit = exitSpy as any + + // Mock process.debugPort + const originalDebugPort = process.debugPort + process.debugPort = 9229 + + // Mock fetch globally before calling DevupUI + const originalFetch = global.fetch + const fetchMock = vi.fn((url: string | URL) => { + const urlString = typeof url === 'string' ? url : url.toString() + if (urlString.includes('9229')) { + return Promise.reject(new Error('Connection refused')) + } + return Promise.resolve({} as Response) + }) + global.fetch = fetchMock as any + + // Use fake timers to control setTimeout + vi.useFakeTimers() + + try { + DevupUI({}) + + // Wait for the fetch promise to reject (this triggers the catch handler) + // The catch handler sets up a setTimeout, so we need to wait for that + await vi.runAllTimersAsync() + + // Verify process.exit was called with code 77 + expect(exitSpy).toHaveBeenCalledWith(77) + } finally { + // Restore + vi.useRealTimers() + global.fetch = originalFetch + process.exit = originalExit + process.debugPort = originalDebugPort + } + }) }) }) diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index 15dcfef9..c0ac38e3 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -83,7 +83,13 @@ export function DevupUI( ) if (process.env.NODE_ENV !== 'production') { - // dev + // check if debugger is attached + fetch('http://localhost:' + process.env.PORT) + fetch('http://localhost:' + process.debugPort).catch(() => { + setTimeout(() => { + process.exit(77) + }, 500) + }) process.env.TURBOPACK_DEBUG_JS = '*' process.env.NODE_OPTIONS ??= '' process.env.NODE_OPTIONS += ' --inspect-brk'