fix(lsp): preserve workspace cancellation reason

This commit is contained in:
Tianyi Cui
2026-08-08 21:27:57 +08:00
parent 098f826001
commit 4d63552d00
2 changed files with 24 additions and 1 deletions
+4 -1
View File
@@ -42,7 +42,10 @@ export async function canonicalizeWorkspace(
throw new Error(`workspace root "${workspaceRoot}" cannot be resolved: ${messageOf(error)}`, { cause: error })
}
throwIfAborted(signal)
const info = await fs.stat(target, signal)
const info = await fs.stat(target, signal).catch((error: unknown) => {
throwIfAborted(signal)
throw error
})
throwIfAborted(signal)
if (info?.type !== 'directory') {
throw new Error(`workspace root "${workspaceRoot}" is not a directory`)
+20
View File
@@ -57,11 +57,31 @@ describe('canonicalizeWorkspace', () => {
await expect(canonicalizeWorkspace(fs, join(root, 'nope'))).rejects.toThrow(/not a directory/)
})
it('wraps a provider failure while resolving the workspace', async () => {
fs.resolve = async () => { throw 'raw workspace resolve failure' }
await expect(canonicalizeWorkspace(fs, ws))
.rejects.toThrow(`workspace root "${ws}" cannot be resolved: raw workspace resolve failure`)
})
it('rejects a non-directory workspace', async () => {
const file = join(root, 'file.txt')
await writeFile(file, 'x')
await expect(canonicalizeWorkspace(fs, file)).rejects.toThrow(/not a directory/)
})
it('normalizes workspace metadata cancellation and preserves other provider failures', async () => {
const providerFailure = new Error('workspace metadata failed')
fs.stat = async () => { throw providerFailure }
await expect(canonicalizeWorkspace(fs, ws)).rejects.toBe(providerFailure)
const controller = new AbortController()
fs.stat = async () => {
controller.abort(new Error('workspace metadata cancelled'))
throw providerFailure
}
await expect(canonicalizeWorkspace(fs, ws, controller.signal))
.rejects.toThrow('workspace metadata cancelled')
})
})
describe('readHostSource', () => {