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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
os: [ ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-latest ]
node: [ 22, 24 ]
rust: [ "1.88.0", "1.93.0" ]
test: [ example/node-bun, example/node-webpack, example/typed-imports, example/esbuild, example/rspack, example/rollup, example/vite, example/next, example/next-turbopack, example/electron ]
test: [ ., example/node-bun, example/node-webpack, example/typed-imports, example/esbuild, example/rspack, example/rollup, example/vite, example/next, example/next-turbopack, example/electron ]
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
Expand Down
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default defineConfig([
files: [
"src/**/*.js",
"bin/**/*.js",
"tsserver.js",
"*.cjs",
"docs/**/*.js",
"example/**/*.config.js",
Expand Down
4 changes: 4 additions & 0 deletions example/typed-imports/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
3 changes: 2 additions & 1 deletion example/typed-imports/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"allowArbitraryExtensions": true,
"skipLibCheck": true,
"target": "es2020",
"types": []
"types": [],
"plugins": [{ "name": "rust-wasmpack-loader/tsserver" }]
},
"files": ["node_modules/rust-wasmpack-loader/types/rs.d.ts"],
"include": ["src/index.ts"]
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"rust-wasmpack-loader": "bin/rust-wasmpack-loader.js"
},
"scripts": {
"test": "node --test --test-concurrency=1 --test-force-exit \"src/**/*.test.js\"",
"release": "npx commit-and-tag-version && npm run postrelease",
"postrelease": "cd ./example/node-webpack && npm i && cd ../../ && cd ./example/web-webpack && npm i && cd ../../ && cd ./example/node-bun && npm i && cd ../../ && cd ./example/esbuild && npm i && cd ../../ && cd ./example/rspack && npm i && cd ../../ && cd ./example/rollup && npm i && cd ../../ && cd ./example/vite && npm i && cd ../../ && cd ./example/next && npm i && cd ../../ && cd ./example/next-turbopack && npm i && cd ../../ && cd ./example/electron && npm i",
"docs:start": "docusaurus start --config ./docs/docusaurus.config.js",
Expand Down Expand Up @@ -164,6 +165,7 @@
"!src/**/*.test.js",
"types",
"bin",
"tsserver.js",
"LICENSE",
"README.md"
]
Expand Down
13 changes: 9 additions & 4 deletions src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ test("--watch regenerates the sidecar on source change", { skip }, async () => {
`watch did not pick up the new export:\n${output()}`,
);
} finally {
child.kill();
await new Promise((resolve) => {
child.once("exit", resolve);
});
child.kill("SIGKILL");
await Promise.race([
new Promise((resolve) => {
child.once("exit", resolve);
}),
new Promise((resolve) => {
setTimeout(resolve, 5000);
}),
]);
// Windows can hold the directory briefly after the child exits.
await waitFor(() => {
try {
Expand Down
170 changes: 170 additions & 0 deletions src/tsserver.integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const ts = require("typescript");
const init = require("./tsserver");
const { buildTypedDts } = require("./utils/generateTypes.util");
const findWasmPack = require("./utils/findWasmPack.util");

const CRATE = path.join(__dirname, "..", "example", "typed-imports");

const skip = (() => {
try {
findWasmPack();
return false;
} catch {
return "wasm-pack is not installed";
}
})();

const COMPILER_OPTIONS = {
noEmit: true,
strict: true,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.Bundler,
allowArbitraryExtensions: true,
skipLibCheck: true,
target: ts.ScriptTarget.ES2020,
types: [],
};

const IMPORTER = [
'import lib from "./math.rs";',
"export const ok = lib.fibonacci(1);",
"export const bad = lib.nope();",
"",
].join("\n");

// An isolated copy of the example crate with a unique marker, so the test owns
// its content-addressed build dir.
function isolatedCrate() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "rs-ls-"));
["Cargo.toml", "Cargo.lock", "math.rs"].forEach((file) =>
fs.copyFileSync(path.join(CRATE, file), path.join(dir, file)),
);
fs.appendFileSync(
path.join(dir, "math.rs"),
`\n// ${path.basename(dir)}\n`,
);
return dir;
}

function waitFor(predicate, timeoutMs) {
const start = Date.now();
const attempt = async () => {
if (predicate()) return true;
if (Date.now() - start > timeoutMs) return false;
await new Promise((resolve) => {
setTimeout(resolve, 100);
});
return attempt();
};
return attempt();
}

const messagesOf = (diagnostics) =>
diagnostics.map((diagnostic) =>
ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
);

// A real `ts.createLanguageService` over a virtual host that holds the `.ts`
// importer and serves the rest (the `.rs` and the lib files) from disk. The
// plugin's `create` decorates it exactly as tsserver would.
function harness(dir) {
// tsserver hands the host forward-slash paths, so key in-memory files the
// same way and normalize every lookup.
const toUnix = (file) => file.replace(/\\/g, "/");
const importer = toUnix(path.join(dir, "app.ts"));
const sources = new Map([[importer, IMPORTER]]);
const refreshed = [];

const readDisk = (file) =>
fs.existsSync(file) ? fs.readFileSync(file, "utf8") : undefined;
const read = (file) =>
sources.has(toUnix(file)) ? sources.get(toUnix(file)) : readDisk(file);

const host = {
getCompilationSettings: () => COMPILER_OPTIONS,
getScriptFileNames: () => [importer],
getScriptKind: () => ts.ScriptKind.TS,
getScriptVersion: () => "1",
getScriptSnapshot: (file) => {
const text = read(file);
return text === undefined
? undefined
: ts.ScriptSnapshot.fromString(text);
},
getCurrentDirectory: () => toUnix(dir),
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
fileExists: (file) => sources.has(toUnix(file)) || fs.existsSync(file),
readFile: read,
readDirectory: () => [],
resolveModuleNameLiterals: (literals, containingFile, _r, options) =>
literals.map((literal) =>
ts.resolveModuleName(
literal.text,
containingFile,
options,
host,
),
),
};

const project = {
markAsDirty: () => undefined,
refreshDiagnostics: () => refreshed.push(1),
getFileNames: () => [...sources.keys()],
projectService: { logger: { info: () => undefined } },
};

const languageService = init({ typescript: ts }, { buildTypedDts }).create({
project,
languageServiceHost: host,
languageService: {},
serverHost: {},
config: {},
});

return { languageService, importer, refreshed };
}

test(
"serves the floor first, then precise types that flag a bad call",
{ skip },
async () => {
const dir = isolatedCrate();
const { languageService, importer, refreshed } = harness(dir);
try {
// Before the build lands, the floor keeps every member callable, so
// the import (and the bad call) type-check loosely.
assert.deepEqual(
messagesOf(languageService.getSemanticDiagnostics(importer)),
[],
);

// The first snapshot read enqueued the background build; wait for it
// to finish and ask the project to refresh.
assert.ok(
await waitFor(() => refreshed.length > 0, 180000),
"the background build never completed",
);

// The precise sidecar now overrides the floor. The only error is the
// unknown export: `lib.nope()` is rejected, while the valid
// `lib.fibonacci(1)` produces none (a bad call would be a second one).
const messages = messagesOf(
languageService.getSemanticDiagnostics(importer),
);
assert.equal(
messages.length,
1,
`expected only the lib.nope() error, got: ${messages.join(" | ") || "(none)"}`,
);
assert.match(messages[0], /Property 'nope' does not exist/);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
},
);
Loading
Loading