Skip to content

Commit f3315d8

Browse files
authored
Update common toolchain dependencies to use root workspace (#10)
* Mirror IDE dependencies in root package See step 2 of https://yarnpkg.com/getting-started/migration#editor-support * Align typescript version to 4.4.2 Because latest (^4.7.2) doesn't seem to work with yarn pnp module resolution * Upgrade nodeLinker to pnp * Add VSCode Editor SDK for yarn pnp See https://yarnpkg.com/getting-started/editor-sdks#vscode * Update client dependencies to enable build Because "eslintConfig": { "extends": [ "react-app" ] } in package.json would result in the following error during build: "Failed to load config "react-app" to extend from.", eslint-config-react-app was added as an explicit devDependency. Because adding eslint-config-react-app as a devDependency led to react plugin having a conflict in eslint-config-react-app packages, the following was added to .yarnrc.yml packageExtensions: react-scripts@*: peerDependencies: eslint-config-react-app: "*" See the following for explanation of above two: facebook/create-react-app#10463 (comment) Becase @testing-library/jest-dom's types were recognized by build command, @types/testing-library__jest-dom was added as a devDependency. * Update .yarnrc.yml with peerDependencies Because many packages in create-react-app rely on implicit peerDependencies, they need to be stated explicitly. See https://yarnpkg.com/advanced/error-codes#yn0002---missing_peer_dependency for more info. * Update client package.json with devDependencies Because many top-level packages in create-react-app rely on implicit peerDependencies, they need to be stated explicitly. See https://yarnpkg.com/advanced/error-codes#yn0002---missing_peer_dependency for more info. * Remove deps in root from server/package.json Because root workspace needs eslint, prettier, typescript in order to enable vscode dev tools (c.f. b3441de) - eslint can be removed from server/package.json - prettier can be removed from server/package.json - typescript can be removed from server/package.json In addition, jest, rimraf shoudl be common across all workspaces. To enable server workspace level commands that depended on rimraf, tsc, and jest, use guidance at https://yarnpkg.com/getting-started/qa#how-to-share-scripts-between-workspaces * Remove deps in root from common-api/package.json Because root workspace needs eslint, prettier, typescript in order to enable vscode dev tools (c.f. b3441de) - eslint can be removed from common-api/package.json - prettier can be removed from common-api/package.json - typescript can be removed from server/package.json In addition rimraf should be common across all workspaces. To enable server workspace level commands that depended on rimraf and tsc use guidance at https://yarnpkg.com/getting-started/qa#how-to-share-scripts-between-workspaces * Update root deps, yarnrc packageExtensions Because "ts-node-dev@npm:2.0.0 [5e2e0] doesn't provide @types/node (pc8aba), requested by ts-node" and "root-workspace-0b6124@workspace:. doesn't provide @types/node (pde784), requested by ts-node" * Use root cleanscript in client/package.json * Align client prettier to root * Align client test script to other packages * Run eslint/prettier across cli, server
1 parent c70ad9f commit f3315d8

40 files changed

+1145
-795
lines changed

.vscode/extensions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"recommendations": [
3+
"arcanis.vscode-zipfs",
4+
"dbaeumer.vscode-eslint",
5+
"esbenp.prettier-vscode"
6+
]
7+
}

.vscode/settings.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
2-
"typescript.tsdk": "node_modules\\typescript\\lib",
2+
"typescript.tsdk": ".yarn/sdks/typescript/lib",
33
"editor.tabSize": 2,
44
"editor.defaultFormatter": "esbenp.prettier-vscode",
5-
"editor.formatOnSave": true
5+
"editor.formatOnSave": true,
6+
"search.exclude": {
7+
"**/.yarn": true,
8+
"**/.pnp.*": true
9+
},
10+
"eslint.nodePath": ".yarn/sdks",
11+
"prettier.prettierPath": ".yarn/sdks/prettier/index.js",
12+
"typescript.enablePromptUseWorkspaceTsdk": true
613
}

.yarn/sdks/eslint/bin/eslint.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 eslint/bin/eslint.js
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real eslint/bin/eslint.js your application uses
20+
module.exports = absRequire(`eslint/bin/eslint.js`);

.yarn/sdks/eslint/lib/api.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 eslint
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real eslint your application uses
20+
module.exports = absRequire(`eslint`);

.yarn/sdks/eslint/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "eslint",
3+
"version": "8.17.0-sdk",
4+
"main": "./lib/api.js",
5+
"type": "commonjs"
6+
}

.yarn/sdks/integrations.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file is automatically generated by @yarnpkg/sdks.
2+
# Manual changes might be lost!
3+
4+
integrations:
5+
- vscode

.yarn/sdks/prettier/index.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 prettier/index.js
15+
require(absPnpApiPath).setup();
16+
}
17+
}
18+
19+
// Defer to the real prettier/index.js your application uses
20+
module.exports = absRequire(`prettier/index.js`);

.yarn/sdks/prettier/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "prettier",
3+
"version": "2.6.2-sdk",
4+
"main": "./index.js",
5+
"type": "commonjs"
6+
}

.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: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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+
// Update 2021-10-08: VSCode changed their format in 1.61.
65+
// Before | ^zip:/c:/foo/bar.zip/package.json
66+
// After | ^/zip//c:/foo/bar.zip/package.json
67+
//
68+
// Update 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+
case `vscode <1.61`: {
73+
str = `^zip:${str}`;
74+
} break;
75+
76+
case `vscode <1.66`: {
77+
str = `^/zip/${str}`;
78+
} break;
79+
80+
case `vscode`: {
81+
str = `^/zip${str}`;
82+
} break;
83+
84+
// To make "go to definition" work,
85+
// We have to resolve the actual file system path from virtual path
86+
// and convert scheme to supported by [vim-rzip](https://github.com/lbrayner/vim-rzip)
87+
case `coc-nvim`: {
88+
str = normalize(resolved).replace(/\.zip\//, `.zip::`);
89+
str = resolve(`zipfile:${str}`);
90+
} break;
91+
92+
// Support neovim native LSP and [typescript-language-server](https://github.com/theia-ide/typescript-language-server)
93+
// We have to resolve the actual file system path from virtual path,
94+
// everything else is up to neovim
95+
case `neovim`: {
96+
str = normalize(resolved).replace(/\.zip\//, `.zip::`);
97+
str = `zipfile://${str}`;
98+
} break;
99+
100+
default: {
101+
str = `zip:${str}`;
102+
} break;
103+
}
104+
}
105+
}
106+
107+
return str;
108+
}
109+
110+
function fromEditorPath(str) {
111+
switch (hostInfo) {
112+
case `coc-nvim`: {
113+
str = str.replace(/\.zip::/, `.zip/`);
114+
// The path for coc-nvim is in format of /<pwd>/zipfile:/<pwd>/.yarn/...
115+
// So in order to convert it back, we use .* to match all the thing
116+
// before `zipfile:`
117+
return process.platform === `win32`
118+
? str.replace(/^.*zipfile:\//, ``)
119+
: str.replace(/^.*zipfile:/, ``);
120+
} break;
121+
122+
case `neovim`: {
123+
str = str.replace(/\.zip::/, `.zip/`);
124+
// The path for neovim is in format of zipfile:///<pwd>/.yarn/...
125+
return str.replace(/^zipfile:\/\//, ``);
126+
} break;
127+
128+
case `vscode`:
129+
default: {
130+
return process.platform === `win32`
131+
? str.replace(/^\^?(zip:|\/zip)\/+/, ``)
132+
: str.replace(/^\^?(zip:|\/zip)\/+/, `/`);
133+
} break;
134+
}
135+
}
136+
137+
// Force enable 'allowLocalPluginLoads'
138+
// TypeScript tries to resolve plugins using a path relative to itself
139+
// which doesn't work when using the global cache
140+
// https://github.com/microsoft/TypeScript/blob/1b57a0395e0bff191581c9606aab92832001de62/src/server/project.ts#L2238
141+
// VSCode doesn't want to enable 'allowLocalPluginLoads' due to security concerns but
142+
// TypeScript already does local loads and if this code is running the user trusts the workspace
143+
// https://github.com/microsoft/vscode/issues/45856
144+
const ConfiguredProject = tsserver.server.ConfiguredProject;
145+
const {enablePluginsWithOptions: originalEnablePluginsWithOptions} = ConfiguredProject.prototype;
146+
ConfiguredProject.prototype.enablePluginsWithOptions = function() {
147+
this.projectService.allowLocalPluginLoads = true;
148+
return originalEnablePluginsWithOptions.apply(this, arguments);
149+
};
150+
151+
// And here is the point where we hijack the VSCode <-> TS communications
152+
// by adding ourselves in the middle. We locate everything that looks
153+
// like an absolute path of ours and normalize it.
154+
155+
const Session = tsserver.server.Session;
156+
const {onMessage: originalOnMessage, send: originalSend} = Session.prototype;
157+
let hostInfo = `unknown`;
158+
159+
Object.assign(Session.prototype, {
160+
onMessage(/** @type {string | object} */ message) {
161+
const isStringMessage = typeof message === 'string';
162+
const parsedMessage = isStringMessage ? JSON.parse(message) : message;
163+
164+
if (
165+
parsedMessage != null &&
166+
typeof parsedMessage === `object` &&
167+
parsedMessage.arguments &&
168+
typeof parsedMessage.arguments.hostInfo === `string`
169+
) {
170+
hostInfo = parsedMessage.arguments.hostInfo;
171+
if (hostInfo === `vscode` && process.env.VSCODE_IPC_HOOK) {
172+
if (/(\/|-)1\.([1-5][0-9]|60)\./.test(process.env.VSCODE_IPC_HOOK)) {
173+
hostInfo += ` <1.61`;
174+
} else if (/(\/|-)1\.(6[1-5])\./.test(process.env.VSCODE_IPC_HOOK)) {
175+
hostInfo += ` <1.66`;
176+
}
177+
}
178+
}
179+
180+
const processedMessageJSON = JSON.stringify(parsedMessage, (key, value) => {
181+
return typeof value === 'string' ? fromEditorPath(value) : value;
182+
});
183+
184+
return originalOnMessage.call(
185+
this,
186+
isStringMessage ? processedMessageJSON : JSON.parse(processedMessageJSON)
187+
);
188+
},
189+
190+
send(/** @type {any} */ msg) {
191+
return originalSend.call(this, JSON.parse(JSON.stringify(msg, (key, value) => {
192+
return typeof value === `string` ? toEditorPath(value) : value;
193+
})));
194+
}
195+
});
196+
197+
return tsserver;
198+
};
199+
200+
if (existsSync(absPnpApiPath)) {
201+
if (!process.versions.pnp) {
202+
// Setup the environment to be able to require typescript/lib/tsserver.js
203+
require(absPnpApiPath).setup();
204+
}
205+
}
206+
207+
// Defer to the real typescript/lib/tsserver.js your application uses
208+
module.exports = moduleWrapper(absRequire(`typescript/lib/tsserver.js`));

0 commit comments

Comments
 (0)