Skip to content

Commit 6289d18

Browse files
authored
Merge pull request #2 from jrr/fix/vscode-typescript
Fix/vscode typescript
2 parents e2c7ca3 + e8b3645 commit 6289d18

File tree

12 files changed

+909
-3
lines changed

12 files changed

+909
-3
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
"search.exclude": {
33
"**/.yarn": true,
44
"**/.pnp.*": true
5-
}
5+
},
6+
"typescript.tsdk": ".yarn/sdks/typescript/lib",
7+
"typescript.enablePromptUseWorkspaceTsdk": true
68
}

.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Lines changed: 363 additions & 0 deletions
Large diffs are not rendered by default.

.yarn/sdks/typescript/bin/tsc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire, createRequireFromPath} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
11+
12+
if (existsSync(absPnpApiPath)) {
13+
if (!process.versions.pnp) {
14+
// Setup the environment to be able to require typescript/bin/tsc
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real typescript/bin/tsc your application uses
20+
module.exports = absRequire(`typescript/bin/tsc`);

.yarn/sdks/typescript/bin/tsserver

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire, createRequireFromPath} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
11+
12+
if (existsSync(absPnpApiPath)) {
13+
if (!process.versions.pnp) {
14+
// Setup the environment to be able to require typescript/bin/tsserver
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real typescript/bin/tsserver your application uses
20+
module.exports = absRequire(`typescript/bin/tsserver`);

.yarn/sdks/typescript/lib/tsc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire, createRequireFromPath} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
11+
12+
if (existsSync(absPnpApiPath)) {
13+
if (!process.versions.pnp) {
14+
// Setup the environment to be able to require typescript/lib/tsc.js
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real typescript/lib/tsc.js your application uses
20+
module.exports = absRequire(`typescript/lib/tsc.js`);

.yarn/sdks/typescript/lib/tsserver.js

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
#!/usr/bin/env node
2+
3+
const {existsSync} = require(`fs`);
4+
const {createRequire, createRequireFromPath} = require(`module`);
5+
const {resolve} = require(`path`);
6+
7+
const relPnpApiPath = "../../../../.pnp.cjs";
8+
9+
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
10+
const absRequire = (createRequire || createRequireFromPath)(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+
}
113+
}
114+
115+
return str;
116+
}
117+
118+
function fromEditorPath(str) {
119+
switch (hostInfo) {
120+
case `coc-nvim`: {
121+
str = str.replace(/\.zip::/, `.zip/`);
122+
// The path for coc-nvim is in format of /<pwd>/zipfile:/<pwd>/.yarn/...
123+
// So in order to convert it back, we use .* to match all the thing
124+
// before `zipfile:`
125+
return process.platform === `win32`
126+
? str.replace(/^.*zipfile:\//, ``)
127+
: str.replace(/^.*zipfile:/, ``);
128+
} break;
129+
130+
case `neovim`: {
131+
str = str.replace(/\.zip::/, `.zip/`);
132+
// The path for neovim is in format of zipfile:///<pwd>/.yarn/...
133+
return str.replace(/^zipfile:\/\//, ``);
134+
} break;
135+
136+
case `vscode`:
137+
default: {
138+
return str.replace(/^\^?(zip:|\/zip(\/ts-nul-authority)?)\/+/, process.platform === `win32` ? `` : `/`)
139+
} break;
140+
}
141+
}
142+
143+
// Force enable 'allowLocalPluginLoads'
144+
// TypeScript tries to resolve plugins using a path relative to itself
145+
// which doesn't work when using the global cache
146+
// https://github.com/microsoft/TypeScript/blob/1b57a0395e0bff191581c9606aab92832001de62/src/server/project.ts#L2238
147+
// VSCode doesn't want to enable 'allowLocalPluginLoads' due to security concerns but
148+
// TypeScript already does local loads and if this code is running the user trusts the workspace
149+
// https://github.com/microsoft/vscode/issues/45856
150+
const ConfiguredProject = tsserver.server.ConfiguredProject;
151+
const {enablePluginsWithOptions: originalEnablePluginsWithOptions} = ConfiguredProject.prototype;
152+
ConfiguredProject.prototype.enablePluginsWithOptions = function() {
153+
this.projectService.allowLocalPluginLoads = true;
154+
return originalEnablePluginsWithOptions.apply(this, arguments);
155+
};
156+
157+
// And here is the point where we hijack the VSCode <-> TS communications
158+
// by adding ourselves in the middle. We locate everything that looks
159+
// like an absolute path of ours and normalize it.
160+
161+
const Session = tsserver.server.Session;
162+
const {onMessage: originalOnMessage, send: originalSend} = Session.prototype;
163+
let hostInfo = `unknown`;
164+
165+
Object.assign(Session.prototype, {
166+
onMessage(/** @type {string | object} */ message) {
167+
const isStringMessage = typeof message === 'string';
168+
const parsedMessage = isStringMessage ? JSON.parse(message) : message;
169+
170+
if (
171+
parsedMessage != null &&
172+
typeof parsedMessage === `object` &&
173+
parsedMessage.arguments &&
174+
typeof parsedMessage.arguments.hostInfo === `string`
175+
) {
176+
hostInfo = parsedMessage.arguments.hostInfo;
177+
if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK) {
178+
const [, major, minor] = (process.env.VSCODE_IPC_HOOK.match(
179+
// The RegExp from https://semver.org/ but without the caret at the start
180+
/(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-]+)*))?$/
181+
) ?? []).map(Number)
182+
183+
if (major === 1) {
184+
if (minor < 61) {
185+
hostInfo += ` <1.61`;
186+
} else if (minor < 66) {
187+
hostInfo += ` <1.66`;
188+
} else if (minor < 68) {
189+
hostInfo += ` <1.68`;
190+
}
191+
}
192+
}
193+
}
194+
195+
const processedMessageJSON = JSON.stringify(parsedMessage, (key, value) => {
196+
return typeof value === 'string' ? fromEditorPath(value) : value;
197+
});
198+
199+
return originalOnMessage.call(
200+
this,
201+
isStringMessage ? processedMessageJSON : JSON.parse(processedMessageJSON)
202+
);
203+
},
204+
205+
send(/** @type {any} */ msg) {
206+
return originalSend.call(this, JSON.parse(JSON.stringify(msg, (key, value) => {
207+
return typeof value === `string` ? toEditorPath(value) : value;
208+
})));
209+
}
210+
});
211+
212+
return tsserver;
213+
};
214+
215+
if (existsSync(absPnpApiPath)) {
216+
if (!process.versions.pnp) {
217+
// Setup the environment to be able to require typescript/lib/tsserver.js
218+
require(absPnpApiPath).setup();
219+
}
220+
}
221+
222+
// Defer to the real typescript/lib/tsserver.js your application uses
223+
module.exports = moduleWrapper(absRequire(`typescript/lib/tsserver.js`));

0 commit comments

Comments
 (0)