Skip to content

Commit f07d4c0

Browse files
authored
Merge pull request bash-lsp#224 from bash-lsp/handle-glob-error
Handle glob errors instead of crashing
2 parents cca9a26 + 1d8a7df commit f07d4c0

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

server/src/__tests__/analyzer.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import FIXTURES, { FIXTURE_FOLDER } from '../../../testing/fixtures'
22
import { getMockConnection } from '../../../testing/mocks'
33
import Analyzer from '../analyser'
44
import { initializeParser } from '../parser'
5+
import * as fsUtil from '../util/fs'
56

67
let analyzer: Analyzer
78

@@ -206,6 +207,8 @@ describe('fromRoot', () => {
206207

207208
expect(newAnalyzer).toBeDefined()
208209

210+
expect(connection.window.showWarningMessage).not.toHaveBeenCalled()
211+
209212
const FIXTURE_FILES_MATCHING_GLOB = 10
210213

211214
// Intro, stats on glob, one file skipped due to shebang, and outro
@@ -222,4 +225,26 @@ describe('fromRoot', () => {
222225
'Analyzer finished after 0 seconds',
223226
)
224227
})
228+
229+
it('handles glob errors', async () => {
230+
jest
231+
.spyOn(fsUtil, 'getFilePaths')
232+
.mockImplementation(() => Promise.reject(new Error('BOOM')))
233+
234+
const parser = await initializeParser()
235+
236+
const connection = getMockConnection()
237+
238+
const newAnalyzer = await Analyzer.fromRoot({
239+
connection,
240+
rootPath: FIXTURE_FOLDER,
241+
parser,
242+
})
243+
244+
expect(newAnalyzer).toBeDefined()
245+
246+
expect(connection.window.showWarningMessage).toHaveBeenCalledWith(
247+
expect.stringContaining('BOOM'),
248+
)
249+
})
225250
})

server/src/analyser.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ export default class Analyzer {
5555
const getTimePassed = (): string =>
5656
`${(Date.now() - lookupStartTime) / 1000} seconds`
5757

58-
const filePaths = await getFilePaths({ globPattern, rootPath })
58+
let filePaths: string[] = []
59+
try {
60+
filePaths = await getFilePaths({ globPattern, rootPath })
61+
} catch (error) {
62+
connection.window.showWarningMessage(
63+
`Failed to analyze bash files using the glob "${globPattern}". The experience will be degraded. Consider configuring the glob or fix any permission issues. Error: ${error.message}`,
64+
)
65+
}
5966

6067
// TODO: we could load all files without extensions: globPattern: '**/[^.]'
6168

testing/mocks.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ export function getMockConnection(): jest.Mocked<lsp.Connection> {
6262
sendRequest: jest.fn(),
6363
telemetry: {} as any,
6464
tracer: {} as any,
65-
window: {} as any,
65+
window: {
66+
attachWorkDoneProgress: jest.fn(),
67+
connection: {} as any,
68+
createWorkDoneProgress: jest.fn(),
69+
showErrorMessage: jest.fn(),
70+
showInformationMessage: jest.fn(),
71+
showWarningMessage: jest.fn(),
72+
},
6673
workspace: {} as any,
6774
}
6875
}

0 commit comments

Comments
 (0)