Skip to content

Commit 2330f65

Browse files
fix: separate folder component path use (#299)
Previously, when attempting to call `componentize()` on a with a `sourcePath` in a different directory, the call to Wizer would be generated with the incorrect *relative* path (path prefix stripping). This commit ensure that when Wizer is called with path prefix stripping, the path to the mapped file is correct.
1 parent df715bc commit 2330f65

File tree

4 files changed

+94
-40
lines changed

4 files changed

+94
-40
lines changed

src/componentize.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { fileURLToPath, URL } from 'node:url';
55
import { cwd, stdout, platform } from 'node:process';
66
import { spawnSync } from 'node:child_process';
77
import { tmpdir } from 'node:os';
8-
import { resolve, join, dirname } from 'node:path';
8+
import { resolve, join, dirname, relative } from 'node:path';
99
import { readFile, writeFile, mkdir, rm, stat } from 'node:fs/promises';
1010
import { rmSync, existsSync } from 'node:fs';
1111
import { createHash } from 'node:crypto';
@@ -249,11 +249,18 @@ export async function componentize(
249249
sourcePath = sourceName;
250250
}
251251
let currentDir = maybeWindowsPath(cwd());
252+
252253
if (workspacePrefix.startsWith(currentDir)) {
253254
workspacePrefix = currentDir;
254255
sourcePath = sourcePath.slice(workspacePrefix.length + 1);
255256
}
256257

258+
// Ensure source path is relative to workspacePrefix for the args list,
259+
// regardless of the directory
260+
if (resolve(sourcePath).startsWith(resolve(workspacePrefix))) {
261+
sourcePath = relative(workspacePrefix, sourcePath);
262+
}
263+
257264
let args = `--initializer-script-path ${initializerPath} --strip-path-prefix ${workspacePrefix}/ ${sourcePath}`;
258265
runtimeArgs = runtimeArgs ? `${runtimeArgs} ${args}` : args;
259266

test/api.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { tmpdir } from 'node:os';
2+
import { join, resolve } from 'node:path';
3+
import { fileURLToPath, URL } from 'node:url';
4+
import { copyFile, mkdtemp } from 'node:fs/promises';
5+
6+
import { suite, test, assert } from 'vitest';
7+
8+
import { setupComponent } from "./util.js";
9+
10+
import {
11+
DEBUG_TRACING_ENABLED,
12+
DEBUG_TEST_ENABLED,
13+
} from './util.js';
14+
15+
suite('API', () => {
16+
// When using a different directory sourcePath as an arg to componentize()
17+
// (called via setupComponent() in this test), wizer would fail to initialize the component
18+
// due to a missing file -- the path prefix stripping was not correctly being resolved.
19+
test('componentize() in a different dir', async () => {
20+
const tmpDir = await mkdtemp(join(tmpdir(), 'componentize-diff-dir-'));
21+
const outputPath = join(tmpDir, "index.js");
22+
await copyFile(resolve('./test/api/index.js'), outputPath);
23+
const { instance } = await setupComponent({
24+
componentize: {
25+
opts: {
26+
sourcePath: outputPath,
27+
witPath: fileURLToPath(new URL('./wit', import.meta.url)),
28+
worldName: 'test1',
29+
debugBuild: DEBUG_TEST_ENABLED,
30+
},
31+
},
32+
transpile: {
33+
opts: {
34+
tracing: DEBUG_TRACING_ENABLED,
35+
},
36+
},
37+
});
38+
});
39+
});

test/util.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { join } from 'node:path';
12
import { env } from 'node:process';
3+
import { readFile, readdir, mkdir, writeFile, mkdtemp } from 'node:fs/promises';
24
import { createServer } from 'node:net';
35

6+
import { componentize } from '@bytecodealliance/componentize-js';
7+
import { transpile } from '@bytecodealliance/jco';
8+
49
export const DEBUG_TRACING_ENABLED = isEnabledEnvVar(env.DEBUG_TRACING);
510
export const LOG_DEBUGGING_ENABLED = isEnabledEnvVar(env.LOG_DEBUGGING);
611
export const DEBUG_TEST_ENABLED = isEnabledEnvVar(env.DEBUG_TEST);
@@ -18,3 +23,43 @@ export function maybeLogging(disableFeatures) {
1823
}
1924
return disableFeatures;
2025
}
26+
27+
export async function setupComponent(opts) {
28+
const componentizeSrc = opts?.componentize?.src;
29+
const componentizeOpts = opts?.componentize?.opts;
30+
const transpileOpts = opts?.transpile?.opts;
31+
32+
let component;
33+
if (componentizeSrc) {
34+
const srcBuild = await componentize(componentizeSrc, componentizeOpts);
35+
component = srcBuild.component;
36+
} else if (!componentizeSrc && componentizeOpts) {
37+
const optsBuild = await componentize(componentizeOpts);
38+
component = optsBuild.component;
39+
} else {
40+
throw new Error('no componentize options or src provided');
41+
}
42+
43+
const outputDir = join('./test/output', 'wasi-test');
44+
await mkdir(outputDir, { recursive: true });
45+
46+
await writeFile(join(outputDir, 'wasi.component.wasm'), component);
47+
48+
const { files } = await transpile(component, transpileOpts);
49+
50+
const wasiDir = join(outputDir, 'wasi');
51+
const interfacesDir = join(wasiDir, 'interfaces');
52+
await mkdir(interfacesDir, { recursive: true });
53+
54+
for (const file of Object.keys(files)) {
55+
await writeFile(join(wasiDir, file), files[file]);
56+
}
57+
58+
const componentJsPath = join(wasiDir, 'component.js');
59+
var instance = await import(componentJsPath);
60+
61+
return {
62+
instance,
63+
outputDir,
64+
};
65+
}

test/wasi.js

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { transpile } from '@bytecodealliance/jco';
99

1010
import { suite, test, assert } from 'vitest';
1111

12+
import { setupComponent } from "./util.js";
13+
1214
import {
1315
DEBUG_TRACING_ENABLED,
1416
DEBUG_TEST_ENABLED,
@@ -85,42 +87,3 @@ suite('WASI', () => {
8587
});
8688
});
8789

88-
async function setupComponent(opts) {
89-
const componentizeSrc = opts?.componentize?.src;
90-
const componentizeOpts = opts?.componentize?.opts;
91-
const transpileOpts = opts?.transpile?.opts;
92-
93-
let component;
94-
if (componentizeSrc) {
95-
const srcBuild = await componentize(componentizeSrc, componentizeOpts);
96-
component = srcBuild.component;
97-
} else if (!componentizeSrc && componentizeOpts) {
98-
const optsBuild = await componentize(componentizeOpts);
99-
component = optsBuild.component;
100-
} else {
101-
throw new Error('no componentize options or src provided');
102-
}
103-
104-
const outputDir = join('./test/output', 'wasi-test');
105-
await mkdir(outputDir, { recursive: true });
106-
107-
await writeFile(join(outputDir, 'wasi.component.wasm'), component);
108-
109-
const { files } = await transpile(component, transpileOpts);
110-
111-
const wasiDir = join(outputDir, 'wasi');
112-
const interfacesDir = join(wasiDir, 'interfaces');
113-
await mkdir(interfacesDir, { recursive: true });
114-
115-
for (const file of Object.keys(files)) {
116-
await writeFile(join(wasiDir, file), files[file]);
117-
}
118-
119-
const componentJsPath = join(wasiDir, 'component.js');
120-
var instance = await import(componentJsPath);
121-
122-
return {
123-
instance,
124-
outputDir,
125-
};
126-
}

0 commit comments

Comments
 (0)