diff --git a/tooling/rx-language-server/README.md b/tooling/rx-language-server/README.md index 6f4ef29a..618c820c 100644 --- a/tooling/rx-language-server/README.md +++ b/tooling/rx-language-server/README.md @@ -36,7 +36,9 @@ node bin/server.js --stdio ``` `initializationOptions: { typescript: { tsdk } }` picks TypeScript's `lib` -folder; without it, the server uses its own `typescript`. +folder; without it, the server uses the nearest `node_modules/typescript` at or +above a workspace folder, up to the repository root (the folder with `.git`), +else its own `typescript`. ## Type-checking from the command line diff --git a/tooling/rx-language-server/bin/server.js b/tooling/rx-language-server/bin/server.js index 6655ac25..17a4a8e1 100644 --- a/tooling/rx-language-server/bin/server.js +++ b/tooling/rx-language-server/bin/server.js @@ -4,6 +4,7 @@ const { createConnection, createServer, createTypeScriptProject, loadTsdkByPath } = require("@volar/language-server/node"); const { create: createTypeScriptServices } = require("volar-service-typescript"); const { rxLanguagePlugin, rxDiagnostics } = require("../src/index.js"); +const { tsdkFor } = require("../src/tsdk.js"); const connection = createConnection(); const server = createServer(connection); @@ -11,8 +12,7 @@ const server = createServer(connection); connection.listen(); connection.onInitialize(params => { - const tsdkPath = params.initializationOptions?.typescript?.tsdk ?? require("node:path").dirname(require.resolve("typescript")); - const tsdk = loadTsdkByPath(tsdkPath, params.locale); + const tsdk = loadTsdkByPath(tsdkFor(params), params.locale); return server.initialize( params, createTypeScriptProject(tsdk.typescript, tsdk.diagnosticMessages, () => ({ languagePlugins: [rxLanguagePlugin(tsdk.typescript)] })), diff --git a/tooling/rx-language-server/package.json b/tooling/rx-language-server/package.json index 70596071..e3b03e95 100644 --- a/tooling/rx-language-server/package.json +++ b/tooling/rx-language-server/package.json @@ -22,7 +22,7 @@ "node": ">=18" }, "scripts": { - "test": "node test/run.js && node test/tsc.js", + "test": "node test/run.js && node test/tsdk.js && node test/tsc.js", "prepack": "node scripts/wasm.js" }, "dependencies": { diff --git a/tooling/rx-language-server/src/tsdk.js b/tooling/rx-language-server/src/tsdk.js new file mode 100644 index 00000000..1a999c05 --- /dev/null +++ b/tooling/rx-language-server/src/tsdk.js @@ -0,0 +1,31 @@ +// Which TypeScript the server loads (spec/11 CP-8): the client's, else the project's, else our own. +const fs = require("node:fs"); +const path = require("node:path"); +const { fileURLToPath } = require("node:url"); + +/** TypeScript's lib folder: the client's, else the nearest `node_modules/typescript` at or above a workspace folder + * (a hoisted workspace's), up to the repository root (the dir holding `.git`), else our own. Walks the folders itself: + * require.resolve would also try global folders. */ +function tsdkFor(params) { + const given = params.initializationOptions?.typescript?.tsdk; + if (given) return given; + const folders = params.workspaceFolders?.length ? params.workspaceFolders.map(f => f.uri) : params.rootUri ? [params.rootUri] : []; + for (const uri of folders) { + if (!uri.startsWith("file:")) continue; + let start; + try { + start = fileURLToPath(uri); // throws on some valid file URIs (a host, an encoded slash): skip the folder + } catch { + continue; + } + for (let dir = start; ; dir = path.dirname(dir)) { + const lib = path.join(dir, "node_modules", "typescript", "lib"); + if (fs.existsSync(path.join(lib, "typescript.js"))) return lib; + if (fs.existsSync(path.join(dir, ".git"))) break; // the repository root (a worktree's `.git` is a file) + if (path.dirname(dir) === dir) break; + } + } + return path.dirname(require.resolve("typescript")); +} + +module.exports = { tsdkFor }; diff --git a/tooling/rx-language-server/test/tsdk.js b/tooling/rx-language-server/test/tsdk.js new file mode 100644 index 00000000..cb40e7fb --- /dev/null +++ b/tooling/rx-language-server/test/tsdk.js @@ -0,0 +1,58 @@ +// Which TypeScript the server loads (src/tsdk.js), on temp trees: the client's tsdk, a hoisted `node_modules/typescript` +// above a workspace folder, the repository root as the end of the walk, a URI it can't read, `rootUri` as the fallback. +// node test/tsdk.js +const fs = require("node:fs"); +const os = require("node:os"); +const path = require("node:path"); +const { pathToFileURL } = require("node:url"); +const { tsdkFor } = require("../src/tsdk.js"); + +let pass = 0, fail = 0; +function check(name, ok, got) { + if (ok) pass++; else fail++; + console.log(`${ok ? " ✓" : " ✗"} ${name}${ok ? "" : `\n ${JSON.stringify(got)}`}`); +} + +const own = path.dirname(require.resolve("typescript")); +const root = fs.mkdtempSync(path.join(os.tmpdir(), "rx-tsdk-")); +const dir = (...p) => { const d = path.join(root, ...p); fs.mkdirSync(d, { recursive: true }); return d; }; +/** Puts a `node_modules/typescript/lib/typescript.js` in `d`; its lib folder. */ +const withTs = d => { const lib = path.join(d, "node_modules", "typescript", "lib"); fs.mkdirSync(lib, { recursive: true }); fs.writeFileSync(path.join(lib, "typescript.js"), ""); return lib; }; +const uri = d => ({ uri: pathToFileURL(d).href, name: "w" }); +const folders = (...ds) => ({ workspaceFolders: ds.map(uri) }); +/** The answer for `params`, and whether it threw. */ +function ask(params) { + try { return { got: tsdkFor(params), threw: false }; } catch { return { got: undefined, threw: true }; } +} + +try { + const hoisted = withTs(dir("hoist")); + check("hoisted above a nested folder", tsdkFor(folders(dir("hoist", "packages", "app"))) === hoisted); + const here = withTs(dir("here")); + check("at the folder itself", tsdkFor(folders(dir("here"))) === here); + check("explicit tsdk wins", tsdkFor({ ...folders(dir("hoist", "a")), initializationOptions: { typescript: { tsdk: "/x/lib" } } }) === "/x/lib"); + check("no TypeScript above: the server's own", tsdkFor(folders(dir("bare", "a"))) === own); + + withTs(dir("repo")); + fs.mkdirSync(path.join(dir("repo", "pkg"), ".git")); + check(".git between the folder and a hoisted TypeScript: stops", tsdkFor(folders(dir("repo", "pkg", "app"))) === own); + const beside = withTs(dir("mono")); + fs.mkdirSync(path.join(root, "mono", ".git")); + check(".git beside the hoisted node_modules: still found", tsdkFor(folders(dir("mono", "app"))) === beside); + fs.writeFileSync(path.join(dir("wt", "pkg"), ".git"), "gitdir: x\n"); + withTs(dir("wt")); + check("a .git file (a worktree's) stops too", tsdkFor(folders(dir("wt", "pkg", "app"))) === own); + + let r = ask({ workspaceFolders: [{ uri: "untitled:Untitled-1", name: "u" }] }); + check("untitled: URI: no throw, the server's own", !r.threw && r.got === own, r); + r = ask({ workspaceFolders: [{ uri: "file://server/share/x", name: "s" }] }); + check("file://server/share/x: no throw, the server's own", !r.threw && r.got === own, r); + r = ask({ workspaceFolders: [{ uri: "file:///tmp/a%2Fb", name: "s" }, uri(dir("hoist", "n"))] }); + check("an unreadable folder is skipped, the next is tried", !r.threw && r.got === hoisted, r); + + check("empty workspaceFolders: rootUri", tsdkFor({ workspaceFolders: [], rootUri: uri(dir("hoist", "r")).uri }) === hoisted); +} finally { + fs.rmSync(root, { recursive: true, force: true }); +} +console.log(`${pass} passed, ${fail} failed`); +process.exit(fail ? 1 : 0); diff --git a/tooling/zed-rx/README.md b/tooling/zed-rx/README.md index 1cf8ebc4..4c75d6b9 100644 --- a/tooling/zed-rx/README.md +++ b/tooling/zed-rx/README.md @@ -14,7 +14,9 @@ with Zed's Node over stdio. The project needs a `tsconfig.json` with Solid's JSX (`"jsx": "preserve"`, `"jsxImportSource": "@solidjs/web"`) and `@solidjs/web` in its `node_modules`; the server writes the projection's prelude to `node_modules/.reactivescript/`. -It uses the worktree's `node_modules/typescript`, else its own TypeScript. +It uses the worktree's `node_modules/typescript`, else the nearest one above the +worktree, up to the repository root (a server newer than 0.3.1 looks), else the +server's own TypeScript. ## Installing as a dev extension @@ -79,8 +81,9 @@ All under `lsp.rx-language-server` in Zed's `settings.json`: - `settings.rxc.path`: an `rxc` binary for the server to run per request instead of its bundled WebAssembly build (sets `RXC`). - `settings.typescript.tsdk`: TypeScript's `lib` folder. Unset: the worktree's - `node_modules/typescript/lib` if there is one (the worktree root only, not a - parent workspace's), else the server's own. + `node_modules/typescript/lib` if there is one, else the nearest one above the + worktree, up to the repository root, such as a hoisted workspace's (the server + looks, from the release after 0.3.1), else the server's own. - `initialization_options`: merged over what the extension sends (`{ "typescript": { "tsdk": … } }`). diff --git a/tooling/zed-rx/src/lib.rs b/tooling/zed-rx/src/lib.rs index ede36587..275d0002 100644 --- a/tooling/zed-rx/src/lib.rs +++ b/tooling/zed-rx/src/lib.rs @@ -8,7 +8,8 @@ //! Settings, under `lsp.rx-language-server`: //! - `settings.rxc.path`: an `rxc` binary the server runs instead of its bundled WebAssembly build (`RXC`). //! - `settings.typescript.tsdk`: TypeScript's `lib` folder. Default: the worktree's -//! `node_modules/typescript/lib`, else the server's own TypeScript. +//! `node_modules/typescript/lib`, else the server looks above the worktree up to the repository root (from the +//! release after 0.3.1), else its own TypeScript. //! - `initialization_options`: merged over what this extension sends. use zed_extension_api::{