|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const {existsSync} = require(`fs`); |
| 4 | +const {createRequire} = require(`module`); |
| 5 | +const {resolve} = require(`path`); |
| 6 | + |
| 7 | +const relPnpApiPath = "../../../../.pnp.cjs"; |
| 8 | + |
| 9 | +const absPnpApiPath = resolve(__dirname, relPnpApiPath); |
| 10 | +const absRequire = createRequire(absPnpApiPath); |
| 11 | + |
| 12 | +const moduleWrapper = tsserver => { |
| 13 | + if (!process.versions.pnp) { |
| 14 | + return tsserver; |
| 15 | + } |
| 16 | + |
| 17 | + const {isAbsolute} = require(`path`); |
| 18 | + const pnpApi = require(`pnpapi`); |
| 19 | + |
| 20 | + const isVirtual = str => str.match(/\/(\$\$virtual|__virtual__)\//); |
| 21 | + const isPortal = str => str.startsWith("portal:/"); |
| 22 | + const normalize = str => str.replace(/\\/g, `/`).replace(/^\/?/, `/`); |
| 23 | + |
| 24 | + const dependencyTreeRoots = new Set(pnpApi.getDependencyTreeRoots().map(locator => { |
| 25 | + return `${locator.name}@${locator.reference}`; |
| 26 | + })); |
| 27 | + |
| 28 | + // VSCode sends the zip paths to TS using the "zip://" prefix, that TS |
| 29 | + // doesn't understand. This layer makes sure to remove the protocol |
| 30 | + // before forwarding it to TS, and to add it back on all returned paths. |
| 31 | + |
| 32 | + function toEditorPath(str) { |
| 33 | + // We add the `zip:` prefix to both `.zip/` paths and virtual paths |
| 34 | + if (isAbsolute(str) && !str.match(/^\^?(zip:|\/zip\/)/) && (str.match(/\.zip\//) || isVirtual(str))) { |
| 35 | + // We also take the opportunity to turn virtual paths into physical ones; |
| 36 | + // this makes it much easier to work with workspaces that list peer |
| 37 | + // dependencies, since otherwise Ctrl+Click would bring us to the virtual |
| 38 | + // file instances instead of the real ones. |
| 39 | + // |
| 40 | + // We only do this to modules owned by the the dependency tree roots. |
| 41 | + // This avoids breaking the resolution when jumping inside a vendor |
| 42 | + // with peer dep (otherwise jumping into react-dom would show resolution |
| 43 | + // errors on react). |
| 44 | + // |
| 45 | + const resolved = isVirtual(str) ? pnpApi.resolveVirtual(str) : str; |
| 46 | + if (resolved) { |
| 47 | + const locator = pnpApi.findPackageLocator(resolved); |
| 48 | + if (locator && (dependencyTreeRoots.has(`${locator.name}@${locator.reference}`) || isPortal(locator.reference))) { |
| 49 | + str = resolved; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + str = normalize(str); |
| 54 | + |
| 55 | + if (str.match(/\.zip\//)) { |
| 56 | + switch (hostInfo) { |
| 57 | + // Absolute VSCode `Uri.fsPath`s need to start with a slash. |
| 58 | + // VSCode only adds it automatically for supported schemes, |
| 59 | + // so we have to do it manually for the `zip` scheme. |
| 60 | + // The path needs to start with a caret otherwise VSCode doesn't handle the protocol |
| 61 | + // |
| 62 | + // Ref: https://github.com/microsoft/vscode/issues/105014#issuecomment-686760910 |
| 63 | + // |
| 64 | + // 2021-10-08: VSCode changed the format in 1.61. |
| 65 | + // Before | ^zip:/c:/foo/bar.zip/package.json |
| 66 | + // After | ^/zip//c:/foo/bar.zip/package.json |
| 67 | + // |
| 68 | + // 2022-04-06: VSCode changed the format in 1.66. |
| 69 | + // Before | ^/zip//c:/foo/bar.zip/package.json |
| 70 | + // After | ^/zip/c:/foo/bar.zip/package.json |
| 71 | + // |
| 72 | + // 2022-05-06: VSCode changed the format in 1.68 |
| 73 | + // Before | ^/zip/c:/foo/bar.zip/package.json |
| 74 | + // After | ^/zip//c:/foo/bar.zip/package.json |
| 75 | + // |
| 76 | + case `vscode <1.61`: { |
| 77 | + str = `^zip:${str}`; |
| 78 | + } break; |
| 79 | + |
| 80 | + case `vscode <1.66`: { |
| 81 | + str = `^/zip/${str}`; |
| 82 | + } break; |
| 83 | + |
| 84 | + case `vscode <1.68`: { |
| 85 | + str = `^/zip${str}`; |
| 86 | + } break; |
| 87 | + |
| 88 | + case `vscode`: { |
| 89 | + str = `^/zip/${str}`; |
| 90 | + } break; |
| 91 | + |
| 92 | + // To make "go to definition" work, |
| 93 | + // We have to resolve the actual file system path from virtual path |
| 94 | + // and convert scheme to supported by [vim-rzip](https://github.com/lbrayner/vim-rzip) |
| 95 | + case `coc-nvim`: { |
| 96 | + str = normalize(resolved).replace(/\.zip\//, `.zip::`); |
| 97 | + str = resolve(`zipfile:${str}`); |
| 98 | + } break; |
| 99 | + |
| 100 | + // Support neovim native LSP and [typescript-language-server](https://github.com/theia-ide/typescript-language-server) |
| 101 | + // We have to resolve the actual file system path from virtual path, |
| 102 | + // everything else is up to neovim |
| 103 | + case `neovim`: { |
| 104 | + str = normalize(resolved).replace(/\.zip\//, `.zip::`); |
| 105 | + str = `zipfile://${str}`; |
| 106 | + } break; |
| 107 | + |
| 108 | + default: { |
| 109 | + str = `zip:${str}`; |
| 110 | + } break; |
| 111 | + } |
| 112 | + } else { |
| 113 | + str = str.replace(/^\/?/, process.platform === `win32` ? `` : `/`); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return str; |
| 118 | + } |
| 119 | + |
| 120 | + function fromEditorPath(str) { |
| 121 | + switch (hostInfo) { |
| 122 | + case `coc-nvim`: { |
| 123 | + str = str.replace(/\.zip::/, `.zip/`); |
| 124 | + // The path for coc-nvim is in format of /<pwd>/zipfile:/<pwd>/.yarn/... |
| 125 | + // So in order to convert it back, we use .* to match all the thing |
| 126 | + // before `zipfile:` |
| 127 | + return process.platform === `win32` |
| 128 | + ? str.replace(/^.*zipfile:\//, ``) |
| 129 | + : str.replace(/^.*zipfile:/, ``); |
| 130 | + } break; |
| 131 | + |
| 132 | + case `neovim`: { |
| 133 | + str = str.replace(/\.zip::/, `.zip/`); |
| 134 | + // The path for neovim is in format of zipfile:///<pwd>/.yarn/... |
| 135 | + return str.replace(/^zipfile:\/\//, ``); |
| 136 | + } break; |
| 137 | + |
| 138 | + case `vscode`: |
| 139 | + default: { |
| 140 | + return str.replace(/^\^?(zip:|\/zip(\/ts-nul-authority)?)\/+/, process.platform === `win32` ? `` : `/`) |
| 141 | + } break; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Force enable 'allowLocalPluginLoads' |
| 146 | + // TypeScript tries to resolve plugins using a path relative to itself |
| 147 | + // which doesn't work when using the global cache |
| 148 | + // https://github.com/microsoft/TypeScript/blob/1b57a0395e0bff191581c9606aab92832001de62/src/server/project.ts#L2238 |
| 149 | + // VSCode doesn't want to enable 'allowLocalPluginLoads' due to security concerns but |
| 150 | + // TypeScript already does local loads and if this code is running the user trusts the workspace |
| 151 | + // https://github.com/microsoft/vscode/issues/45856 |
| 152 | + const ConfiguredProject = tsserver.server.ConfiguredProject; |
| 153 | + const {enablePluginsWithOptions: originalEnablePluginsWithOptions} = ConfiguredProject.prototype; |
| 154 | + ConfiguredProject.prototype.enablePluginsWithOptions = function() { |
| 155 | + this.projectService.allowLocalPluginLoads = true; |
| 156 | + return originalEnablePluginsWithOptions.apply(this, arguments); |
| 157 | + }; |
| 158 | + |
| 159 | + // And here is the point where we hijack the VSCode <-> TS communications |
| 160 | + // by adding ourselves in the middle. We locate everything that looks |
| 161 | + // like an absolute path of ours and normalize it. |
| 162 | + |
| 163 | + const Session = tsserver.server.Session; |
| 164 | + const {onMessage: originalOnMessage, send: originalSend} = Session.prototype; |
| 165 | + let hostInfo = `unknown`; |
| 166 | + |
| 167 | + Object.assign(Session.prototype, { |
| 168 | + onMessage(/** @type {string | object} */ message) { |
| 169 | + const isStringMessage = typeof message === 'string'; |
| 170 | + const parsedMessage = isStringMessage ? JSON.parse(message) : message; |
| 171 | + |
| 172 | + if ( |
| 173 | + parsedMessage != null && |
| 174 | + typeof parsedMessage === `object` && |
| 175 | + parsedMessage.arguments && |
| 176 | + typeof parsedMessage.arguments.hostInfo === `string` |
| 177 | + ) { |
| 178 | + hostInfo = parsedMessage.arguments.hostInfo; |
| 179 | + if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK) { |
| 180 | + const [, major, minor] = (process.env.VSCODE_IPC_HOOK.match( |
| 181 | + // The RegExp from https://semver.org/ but without the caret at the start |
| 182 | + /(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/ |
| 183 | + ) ?? []).map(Number) |
| 184 | + |
| 185 | + if (major === 1) { |
| 186 | + if (minor < 61) { |
| 187 | + hostInfo += ` <1.61`; |
| 188 | + } else if (minor < 66) { |
| 189 | + hostInfo += ` <1.66`; |
| 190 | + } else if (minor < 68) { |
| 191 | + hostInfo += ` <1.68`; |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + const processedMessageJSON = JSON.stringify(parsedMessage, (key, value) => { |
| 198 | + return typeof value === 'string' ? fromEditorPath(value) : value; |
| 199 | + }); |
| 200 | + |
| 201 | + return originalOnMessage.call( |
| 202 | + this, |
| 203 | + isStringMessage ? processedMessageJSON : JSON.parse(processedMessageJSON) |
| 204 | + ); |
| 205 | + }, |
| 206 | + |
| 207 | + send(/** @type {any} */ msg) { |
| 208 | + return originalSend.call(this, JSON.parse(JSON.stringify(msg, (key, value) => { |
| 209 | + return typeof value === `string` ? toEditorPath(value) : value; |
| 210 | + }))); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + return tsserver; |
| 215 | +}; |
| 216 | + |
| 217 | +if (existsSync(absPnpApiPath)) { |
| 218 | + if (!process.versions.pnp) { |
| 219 | + // Setup the environment to be able to require typescript/lib/tsserver.js |
| 220 | + require(absPnpApiPath).setup(); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +// Defer to the real typescript/lib/tsserver.js your application uses |
| 225 | +module.exports = moduleWrapper(absRequire(`typescript/lib/tsserver.js`)); |
0 commit comments