Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions packages/vite/src/node/__tests__/shortcuts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,49 @@ describe('bindCLIShortcuts', () => {
await server.close()
}
})

test('rebinds shortcuts after server restart', async () => {
const server = await createServer()

try {
const action = vi.fn()

bindCLIShortcuts(
server,
{
customShortcuts: [{ key: 'x', description: 'test', action }],
},
true,
)

// Verify shortcut works initially
const initialReadline = server._rl

expect.assert(
initialReadline,
'The readline interface should be defined after binding shortcuts.',
)

initialReadline.emit('line', 'x')

await vi.waitFor(() => expect(action).toHaveBeenCalledOnce())

// Restart the server
action.mockClear()
await server.restart()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not expecting the binding state to be preserved across the server restart (i.e. I was thinking that bindCLIShortcuts should be called at this line as well). But it seems that behavior exists since the beginning #13675 and I guess it's fine to keep it as-is.

const newReadline = server._rl

expect.assert(
newReadline && newReadline !== initialReadline,
'A new readline interface should be created after server restart.',
)

// Shortcuts should still work after restart
newReadline.emit('line', 'x')
await vi.waitFor(() => expect(action).toHaveBeenCalledOnce())
} finally {
await server.close()
}
})
})
5 changes: 4 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,8 @@ async function restartServer(server: ViteDevServer) {
const middlewares = server.middlewares
newServer._configServerPort = server._configServerPort
newServer._currentServerPort = server._currentServerPort
// Ensure the new server has no stale readline reference
newServer._rl = undefined
Object.assign(server, newServer)

// Keep the same connect instance so app.use(vite.middlewares) works
Expand All @@ -1278,7 +1280,8 @@ async function restartServer(server: ViteDevServer) {

if (shortcutsOptions) {
shortcutsOptions.print = false
bindCLIShortcuts(server, shortcutsOptions)
// Skip environment checks since shortcuts were bound before restart
bindCLIShortcuts(server, shortcutsOptions, true)
}
}

Expand Down