Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 6b890eb

Browse files
authored
fix: do not swallow errors in importScripts when loading extensions (#854)
If an extension has side effects (e.g., it has a top-level `const x foo()`) that throw, or if other errors occur during importScripts, the error would have been swallowed. This makes it so that the error is printed.
1 parent f9851af commit 6b890eb

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

packages/sourcegraph-extension-api/src/extension/workerMain.ts

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,49 +32,53 @@ export function extensionHostWorkerMain(self: DedicatedWorkerGlobalScope): void
3232
self.addEventListener('message', receiveExtensionURL)
3333

3434
function receiveExtensionURL(ev: MessageEvent): void {
35-
// Only listen for the 1st URL.
36-
self.removeEventListener('message', receiveExtensionURL)
35+
try {
36+
// Only listen for the 1st URL.
37+
self.removeEventListener('message', receiveExtensionURL)
3738

38-
if (ev.origin && ev.origin !== self.location.origin) {
39-
console.error(`Invalid extension host message origin: ${ev.origin} (expected ${self.location.origin})`)
40-
self.close()
41-
}
39+
if (ev.origin && ev.origin !== self.location.origin) {
40+
console.error(`Invalid extension host message origin: ${ev.origin} (expected ${self.location.origin})`)
41+
self.close()
42+
}
4243

43-
const initData: InitData = ev.data
44-
if (typeof initData.bundleURL !== 'string' || !initData.bundleURL.startsWith('blob:')) {
45-
console.error(`Invalid extension bundle URL: ${initData.bundleURL}`)
46-
self.close()
47-
}
44+
const initData: InitData = ev.data
45+
if (typeof initData.bundleURL !== 'string' || !initData.bundleURL.startsWith('blob:')) {
46+
console.error(`Invalid extension bundle URL: ${initData.bundleURL}`)
47+
self.close()
48+
}
4849

49-
const api = createExtensionHost(initData)
50-
// Make `import 'sourcegraph'` or `require('sourcegraph')` return the extension host's
51-
// implementation of the `sourcegraph` module.
52-
;(self as any).require = (modulePath: string): any => {
53-
if (modulePath === 'sourcegraph') {
54-
return api
50+
const api = createExtensionHost(initData)
51+
// Make `import 'sourcegraph'` or `require('sourcegraph')` return the extension host's
52+
// implementation of the `sourcegraph` module.
53+
;(self as any).require = (modulePath: string): any => {
54+
if (modulePath === 'sourcegraph') {
55+
return api
56+
}
57+
throw new Error(`require: module not found: ${modulePath}`)
5558
}
56-
throw new Error(`require: module not found: ${modulePath}`)
57-
}
5859

59-
// Load the extension bundle and retrieve the extension entrypoint module's exports on the global
60-
// `module` property.
61-
;(self as any).exports = {}
62-
;(self as any).module = {}
63-
self.importScripts(initData.bundleURL)
64-
const extensionExports = (self as any).module.exports
65-
delete (self as any).module
60+
// Load the extension bundle and retrieve the extension entrypoint module's exports on the global
61+
// `module` property.
62+
;(self as any).exports = {}
63+
;(self as any).module = {}
64+
self.importScripts(initData.bundleURL)
65+
const extensionExports = (self as any).module.exports
66+
delete (self as any).module
6667

67-
if ('activate' in extensionExports) {
68-
try {
69-
tryCatchPromise(() => extensionExports.activate()).catch((err: any) => {
70-
console.error(`Error creating extension host:`, err)
71-
self.close()
72-
})
73-
} catch (err) {
74-
console.error(`Error activating extension.`, err)
68+
if ('activate' in extensionExports) {
69+
try {
70+
tryCatchPromise(() => extensionExports.activate()).catch((err: any) => {
71+
console.error(`Error creating extension host:`, err)
72+
self.close()
73+
})
74+
} catch (err) {
75+
console.error(`Error activating extension.`, err)
76+
}
77+
} else {
78+
console.error(`Extension did not export an 'activate' function.`)
7579
}
76-
} else {
77-
console.error(`Extension did not export an 'activate' function.`)
80+
} catch (err) {
81+
console.error(err)
7882
}
7983
}
8084
}

0 commit comments

Comments
 (0)