@@ -36,7 +36,7 @@ import vscode, {
3636 TextEditor ,
3737 TextEditorRevealType ,
3838} from "vscode" ;
39- import { CodeChatEditorServer , initServer } from "./index.js" ;
39+ import type { CodeChatEditorServer } from "./index.js" ;
4040
4141// ### Local packages
4242import {
@@ -125,14 +125,11 @@ let version = 0.0;
125125
126126// An object to start/stop the CodeChat Editor Server.
127127let codeChatEditorServer : CodeChatEditorServer | undefined ;
128- // Before using `CodeChatEditorServer`, we must initialize it.
129- {
130- const ext = vscode . extensions . getExtension (
131- "CodeChat.codechat-editor-client" ,
132- ) ;
133- assert ( ext !== undefined ) ;
134- initServer ( ext . extensionPath ) ;
135- }
128+
129+ // The in-flight or completed load of the native module holding the CodeChat
130+ // Editor Server, started on demand by `load_native_module`.
131+ type NativeModule = typeof import ( "./index.js" ) ;
132+ let native_module : Promise < NativeModule > | undefined ;
136133
137134// ---
138135//
@@ -2015,6 +2012,56 @@ function queueCaptureEvent(
20152012// Activation/deactivation
20162013// -----------------------
20172014//
2015+ // Import the native module, initializing the Server it contains on the first
2016+ // successful import. Diagnostics which instrument the startup sequence belong
2017+ // here: this is the earliest point at which a failure of the native module can
2018+ // be reported, since the extension is by now fully loaded and running.
2019+ const load_native_module = (
2020+ // This extension's context. Its `extensionPath` gives the extension's
2021+ // install directory, from which the Server locates the Client's files.
2022+ context : vscode . ExtensionContext ,
2023+ // The imported, initialized module.
2024+ ) : Promise < NativeModule > => {
2025+ if ( native_module === undefined ) {
2026+ // Store the promise before the `await` below hands control back to
2027+ // the event loop, so that a caller arriving while the load is in
2028+ // flight joins it rather than starting a second one.
2029+ native_module = ( async ( ) => {
2030+ let loaded : NativeModule ;
2031+ try {
2032+ // The NAPI-RS-generated loader (`index.js`) throws while
2033+ // being imported when it finds no binding for this
2034+ // platform, so a static `import` of it would abort this
2035+ // module's load -- taking the extension down before
2036+ // `activate` runs, and before any diagnostic placed here
2037+ // could report why. A dynamic `import` instead defers that
2038+ // failure into a `try` block which can report it.
2039+ loaded = await import ( "./index.js" ) ;
2040+ } catch ( e ) {
2041+ // Discard a failed import, so that it's retried when this
2042+ // command runs again instead of leaving a permanently
2043+ // rejected promise in place. Repeating it costs nothing:
2044+ // the bundler memoizes a successful import and resets a
2045+ // failed one -- see the `try`/`catch` in the `__commonJS`
2046+ // helper of the bundle in `out/extension.js`.
2047+ native_module = undefined ;
2048+ throw e ;
2049+ }
2050+ // `initServer` performs the Server's global setup, which
2051+ // `webserver::init_server` documents as callable only once.
2052+ // Deliberately leave `native_module` in place when it throws:
2053+ // the rejected promise then replays that first failure to every
2054+ // later caller, whereas retrying would run the setup a second
2055+ // time and report a complaint about the already-installed
2056+ // logger in place of the problem which actually stopped the
2057+ // Server. Recovering from this requires reloading the window.
2058+ loaded . initServer ( context . extensionPath ) ;
2059+ return loaded ;
2060+ } ) ( ) ;
2061+ }
2062+ return native_module ;
2063+ } ;
2064+
20182065// This is invoked when the extension is activated. It either creates a new
20192066// CodeChat Editor Server instance or reveals the currently running one.
20202067export const activate = ( context : vscode . ExtensionContext ) => {
@@ -2111,6 +2158,21 @@ export const activate = (context: vscode.ExtensionContext) => {
21112158 async ( ) => {
21122159 consoleLog ( "CodeChat Editor extension: starting." ) ;
21132160
2161+ // Load the native module first, so that failing to load it
2162+ // leaves behind neither a webview panel nor subscriptions to
2163+ // editor events.
2164+ let native : NativeModule ;
2165+ try {
2166+ native = await load_native_module ( context ) ;
2167+ } catch ( e ) {
2168+ // Report this even when `quiet_next_error` is set.
2169+ quiet_next_error = false ;
2170+ show_error (
2171+ `Unable to load the CodeChat Editor Server: ${ format_error_chain ( e ) } ` ,
2172+ ) ;
2173+ return ;
2174+ }
2175+
21142176 if ( ! subscribed ) {
21152177 subscribed = true ;
21162178
@@ -2375,7 +2437,7 @@ export const activate = (context: vscode.ExtensionContext) => {
23752437
23762438 // Start the server.
23772439 consoleLog ( "CodeChat Editor extension: starting server." ) ;
2378- codeChatEditorServer = new CodeChatEditorServer (
2440+ codeChatEditorServer = new native . CodeChatEditorServer (
23792441 vscode . Uri . joinPath (
23802442 context . globalStorageUri ,
23812443 "capture-spool" ,
@@ -2773,6 +2835,44 @@ const format_struct = (complex_data_structure: any): string =>
27732835 ) . substring ( 0 , MAX_MESSAGE_LENGTH )
27742836 : "" ;
27752837
2838+ // Format an error together with its chain of causes. The NAPI-RS loader places
2839+ // only a generic summary in the error it throws, recording the load failure of
2840+ // each candidate binding in a nested `cause`; without walking the chain, the
2841+ // message naming the actual problem is lost.
2842+ const format_error_chain = (
2843+ // The error to format, of whatever type a `catch` block received.
2844+ error : unknown ,
2845+ // The chain of messages, outermost first.
2846+ ) : string => {
2847+ const messages : string [ ] = [ ] ;
2848+ // A `cause` chain which loops back on itself would spin here forever.
2849+ // This runs only once something has already gone wrong, so it must not
2850+ // be what takes the extension host down.
2851+ const seen = new Set < unknown > ( ) ;
2852+ let current = error ;
2853+ while ( current !== undefined && current !== null && ! seen . has ( current ) ) {
2854+ seen . add ( current ) ;
2855+ // A `catch` block receives whatever was thrown, and `String` throws
2856+ // in turn when that is a symbol.
2857+ messages . push (
2858+ current instanceof Error
2859+ ? current . message
2860+ : typeof current === "symbol"
2861+ ? current . toString ( )
2862+ : String ( current ) ,
2863+ ) ;
2864+ // Test for `cause` rather than reading it directly: it is absent
2865+ // both on errors which record no cause and on the runtimes which
2866+ // predate it, and the test supplies the property's type, which the
2867+ // TypeScript library this file compiles against lacks.
2868+ current =
2869+ current instanceof Error && "cause" in current
2870+ ? current . cause
2871+ : undefined ;
2872+ }
2873+ return messages . join ( "\n caused by: " ) ;
2874+ } ;
2875+
27762876// Send a result (a response to a message from the server) back to the server.
27772877const sendResult = async ( id : number , result ?: ResultErrTypes ) => {
27782878 assert ( codeChatEditorServer ) ;
0 commit comments