Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/lazy-wasi-warning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@offckb/cli': patch
---

fix(ckb-debugger): lazy-load WASI module to suppress ExperimentalWarning

Convert static import of node:wasi to dynamic import with caching.
This prevents the ExperimentalWarning from being emitted when running
non-debugger commands like 'offckb accounts' or 'offckb config list'.

The WASI module is now only loaded when debugger functionality is
actually executed.

Fixes #405
16 changes: 14 additions & 2 deletions src/tools/ckb-debugger-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

import * as fs from 'node:fs';
import * as wasi from 'node:wasi';
import * as path from 'node:path';

/**
Expand Down Expand Up @@ -60,6 +59,7 @@
private env: Record<string, string>;
private captureOutput: boolean;
private wasm: WebAssembly.Module | null = null;
private static wasiModule: typeof import('node:wasi') | null = null;

constructor(options: CkbDebuggerOptions = {}) {
this.wasmPath = options.wasmPath || './ckb-debugger.wasm';
Expand All @@ -77,6 +77,17 @@
}
}

private async getWasiModule(): Promise<typeof import('node:wasi')> {
if (!CkbDebuggerWasi.wasiModule) {
try {
CkbDebuggerWasi.wasiModule = await import('node:wasi');
} catch (_error) {
throw new Error('Failed to load WASI module. Node.js >= 20.0.0 is required for WASM debugger support.');
}
}
return CkbDebuggerWasi.wasiModule;
}

private extractFilePathPreopens(args: string[]): Record<string, string> {
const additionalPreopens: Record<string, string> = {};
for (let i = 0; i < args.length - 1; i++) {
Expand All @@ -99,6 +110,7 @@

async run(args: string[] = [], preopens: Record<string, string> = {}): Promise<CkbDebuggerResult> {
await this.initialize();
const wasiModule = await this.getWasiModule();

return new Promise((resolve, reject) => {
try {
Expand All @@ -122,7 +134,7 @@
}

// Configure WASI options
const wasiOptions: any = {

Check warning on line 137 in src/tools/ckb-debugger-wasm.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 137 in src/tools/ckb-debugger-wasm.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
version: 'preview1',
args: ['ckb-debugger', ...wasiArgs],
env: this.env,
Expand All @@ -136,7 +148,7 @@
// For output capture, we'll handle it differently since WASI expects file descriptors
// We'll use the default stdout/stderr and capture via different means if needed

const wasihost = new wasi.WASI(wasiOptions);
const wasihost = new wasiModule.WASI(wasiOptions);

// Instantiate the WebAssembly module
WebAssembly.instantiate(this.wasm!, wasihost.getImportObject() as WebAssembly.Imports)
Expand All @@ -148,7 +160,7 @@
output: undefined, // Output goes directly to stdout for now
error: undefined,
});
} catch (e: any) {

Check warning on line 163 in src/tools/ckb-debugger-wasm.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 163 in src/tools/ckb-debugger-wasm.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
// WASI programs exit with a specific exception
if (e.code === 'WASI_EXIT') {
resolve({
Expand All @@ -162,7 +174,7 @@
}
})
.catch(reject);
} catch (e: any) {

Check warning on line 177 in src/tools/ckb-debugger-wasm.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 177 in src/tools/ckb-debugger-wasm.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
reject(new Error(`Failed to run debugger: ${e.message}`));
}
});
Expand Down
Loading