Skip to content

Commit f5dac38

Browse files
authored
fix: handle indexing library import when require.main is undefined (#982)
1 parent 68e692a commit f5dac38

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

server/aws-lsp-codewhisperer/src/shared/localProjectContextController.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import { URI } from 'vscode-uri'
1313

1414
const fs = require('fs').promises
1515
const path = require('path')
16-
const LIBRARY_DIR = path.join('//TODO FIXME', 'indexing')
16+
const LIBRARY_DIR = (() => {
17+
if (require.main) {
18+
return path.join(dirname(require.main.filename), 'indexing')
19+
}
20+
return path.join(__dirname, 'indexing')
21+
})()
1722

1823
export class LocalProjectContextController {
1924
private static instance: LocalProjectContextController | undefined
@@ -40,11 +45,16 @@ export class LocalProjectContextController {
4045

4146
public async init(vectorLib?: any): Promise<void> {
4247
try {
43-
const vecLib = vectorLib ?? (await import(path.join(LIBRARY_DIR, 'dist', 'extension.js')))
44-
const root = this.findCommonWorkspaceRoot(this.workspaceFolders)
45-
this._vecLib = await vecLib.start(LIBRARY_DIR, this.clientName, root)
46-
await this.buildIndex()
47-
LocalProjectContextController.instance = this
48+
const libraryPath = path.join(LIBRARY_DIR, 'dist', 'extension.js')
49+
const vecLib = vectorLib ?? (await import(libraryPath))
50+
if (vecLib) {
51+
const root = this.findCommonWorkspaceRoot(this.workspaceFolders)
52+
this._vecLib = await vecLib.start(LIBRARY_DIR, this.clientName, root)
53+
await this.buildIndex()
54+
LocalProjectContextController.instance = this
55+
} else {
56+
this.log.warn(`Vector library could not be imported from: ${libraryPath}`)
57+
}
4858
} catch (error) {
4959
this.log.error('Vector library failed to initialize:' + error)
5060
}

0 commit comments

Comments
 (0)