Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-compat): update fsevents patch to support virtual path #1671

Merged
merged 20 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
18 changes: 9 additions & 9 deletions .pnp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
30 changes: 30 additions & 0 deletions .yarn/versions/aa14d1fc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
releases:
"@yarnpkg/builder": patch
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/doctor": patch
"@yarnpkg/plugin-compat": patch
"@yarnpkg/plugin-constraints": patch
"@yarnpkg/plugin-dlx": patch
"@yarnpkg/plugin-essentials": patch
"@yarnpkg/plugin-exec": patch
"@yarnpkg/plugin-file": patch
"@yarnpkg/plugin-git": patch
"@yarnpkg/plugin-github": patch
"@yarnpkg/plugin-http": patch
"@yarnpkg/plugin-init": patch
"@yarnpkg/plugin-interactive-tools": patch
"@yarnpkg/plugin-link": patch
"@yarnpkg/plugin-node-modules": patch
"@yarnpkg/plugin-npm": patch
"@yarnpkg/plugin-npm-cli": patch
"@yarnpkg/plugin-pack": patch
"@yarnpkg/plugin-patch": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/plugin-stage": patch
"@yarnpkg/plugin-typescript": patch
"@yarnpkg/plugin-version": patch
"@yarnpkg/plugin-workspace-tools": patch
"@yarnpkg/pnp": patch
"@yarnpkg/pnpify": patch
vscode-zipfs: patch
15 changes: 8 additions & 7 deletions packages/plugin-compat/extra/fsevents/1.2.11.patch
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ diff --git a/fsevents.js b/fsevents.js
semver exclusivity ^1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the API fsevents 1.x exposed makes it quite challenging to watch multiple files so I only watch the path file instead.( same behavior as current patch)

--- a/fsevents.js
+++ b/fsevents.js
@@ -36,11 +36,15 @@ module.exports.Constants = Native.Constants;
@@ -36,11 +36,16 @@ module.exports.Constants = Native.Constants;
var defer = global.setImmediate || process.nextTick;

function watch(path) {
- var fse = new FSEvents(String(path || ''), handler);
+ var VFS = require('./vfs');
+ var vfs = new VFS(String(path || ''));
+
+ var fse = new FSEvents(vfs.resolvedPath, handler);
+ var vfs = new VFS(String(path || ''), Native);
+ var pathMap = vfs.buildPathMap();
+ var fse = new FSEvents(vfs.resolvedRoot, handler);
EventEmitter.call(fse);
return fse;

function handler(path, flags, id) {
+ path = vfs.transpose(path);
+ var rawRoot = pathMap.get(resolvedRoot);
+ path = vfs.transpose(rawRoot, resolvedRoot, path)
arcanis marked this conversation as resolved.
Show resolved Hide resolved
defer(function() {
fse.emit('fsevent', path, flags, id);
var info = getInfo(path, flags);
21 changes: 21 additions & 0 deletions packages/plugin-compat/extra/fsevents/2.1.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
diff --git a/fsevents.js b/fsevents.js
semver exclusivity 2.1.1
--- a/fsevents.js
+++ b/fsevents.js
@@ -21,12 +21,11 @@ function watch(path, handler) {
throw new TypeError(`fsevents argument 2 must be a function and not a ${typeof handler}`);
}

- let instance = Native.start(path, handler);
- if (!instance) throw new Error(`could not watch: ${path}`);
+ let VFS = require('./vfs');
+ let vfs = new VFS(path, Native);
+ vfs.watch(handler);
return () => {
- const result = instance ? Promise.resolve(instance).then(Native.stop) : null;
- instance = null;
- return result;
+ return vfs.stop();
};
}

21 changes: 15 additions & 6 deletions packages/plugin-compat/extra/fsevents/2.1.2.patch
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
diff --git a/fsevents.js b/fsevents.js
semver exclusivity ^2.1
semver exclusivity ^2.1.2
--- a/fsevents.js
+++ b/fsevents.js
@@ -21,5 +21,7 @@ function watch(path, handler) {
@@ -21,13 +21,10 @@ function watch(path, handler) {
throw new TypeError(`fsevents argument 2 must be a function and not a ${typeof handler}`);
}

- let instance = Native.start(path, handler);
- if (!instance) throw new Error(`could not watch: ${path}`);
+ let VFS = require('./vfs');
+ let vfs = new VFS(path);
+ let instance = Native.start(vfs.resolvedPath, vfs.wrap(handler));
if (!instance) throw new Error(`could not watch: ${path}`);
+ let vfs = new VFS(path, Native);
+ vfs.watch(handler);
return () => {
- const result = instance
- ? Promise.resolve(instance).then(Native.stop)
- : Promise.resolve(undefined);
- instance = undefined;
- return result;
+ return vfs.stop();
};
}
121 changes: 92 additions & 29 deletions packages/plugin-compat/extra/fsevents/common.patch
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,108 @@ diff --git a/vfs.js b/vfs.js
new file mode 100644
--- /dev/null
+++ b/vfs.js
@@ -0,0 +1,41 @@
@@ -0,0 +1,104 @@
+const path = require(`path`);
+
+const NUMBER_REGEXP = /^[0-9]+$/;
+const VIRTUAL_REGEXP = /^(\/(?:[^\/]+\/)*?\$\$virtual)((?:\/([^\/]+)(?:\/([^\/]+))?)?((?:\/.*)?))$/;
+function getPnpApi() {
+ let pnpApi;
+ try {
+ if (process.versions.pnp) {
+ pnpApi = require(`pnpapi`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arcanis I am assuming calling require(pnpapi) many times won't have performance implication

+ }
+ } catch (ex) {}
+ return pnpApi;
+}
+
+function resolveVirtual(p) {
+ const match = p.match(VIRTUAL_REGEXP);
+ if (!match)
+ return p;
+
+ const target = path.dirname(match[1]);
+ if (!match[3] || !match[4])
+ return target;
+ const pnpapi = getPnpApi();
+ if (pnpapi) {
+ const resolved = pnpapi.resolveVirtual(p);
+ // pnp resolveVirtual will return null for non-virtual path.
+ return resolved == null ? p : resolved;
+ }
+ return p;
+}
+
+ const isnum = NUMBER_REGEXP.test(match[4]);
+ if (!isnum)
+ return p;
+function findVirtualPaths(root) {
+ const paths = [];
+ const pnpapi = getPnpApi();
+ if (pnpapi) {
+ for (const locator of pnpapi.getDependencyTreeRoots()) {
+ const pkg = pnpapi.getPackageInformation(locator);
+ for (const [name, referencish] of pkg.packageDependencies) {
+ if (referencish === null) continue;
+ if (referencish.indexOf(`virtual:`) === 0) {
+ const virtualLocator = pnpapi.getLocator(name, referencish);
+ const virtualPkg = pnpapi.getPackageInformation(virtualLocator);
+ if (virtualPkg && virtualPkg.packageLocation.indexOf(root) === 0) {
+ // virtual path fall under root
+ paths.push(virtualPkg.packageLocation);
+ }
+ }
+ }
+ }
+ }
+ return paths;
+}
+
+ const depth = Number(match[4]);
+ const backstep = `../`.repeat(depth);
+ const subpath = (match[5] || `.`);
+class VFS {
+ constructor(p, Native) {
+ this.root = path.resolve(p);
+ this.native = Native;
+ this.watchers = [];
+ }
+
+ return resolveVirtual(path.join(target, backstep, subpath));
+}
+ transpose(rawPath, resolvedPath, p) {
+ const transposePath = rawPath + p.substr(resolvedPath.length);
+ return transposePath;
+ }
+
+module.exports = class FsePnp {
+ constructor(p) {
+ this.normalizedPath = path.resolve(p);
+ this.resolvedPath = resolveVirtual(this.normalizedPath);
+ /**
+ * build raw and resolved path mapping
+ * @param {*} root
+ */
+ buildPathMap() {
+ const pathMap = new Map();
+ this.resolvedRoot = resolveVirtual(this.root);
+ pathMap.set(this.resolvedRoot, this.root);
+ if (!path.extname(this.root)) {
+ // find all direct virtual paths for given root.
+ const virtualPaths = findVirtualPaths(this.root);
+ virtualPaths.forEach((virtualPath) => {
+ const resolvedVirtual = resolveVirtual(virtualPath);
+ if (resolvedVirtual.indexOf(this.resolvedRoot) < 0) {
+ pathMap.set(resolvedVirtual, virtualPath);
+ }
+ });
+ }
+ return pathMap;
+ }
+
+ transpose(p) {
+ return this.normalizedPath + p.substr(this.resolvedPath.length);
+ watch(handler) {
+ const pathMap = this.buildPathMap();
+ pathMap.forEach((virtualPath, resolvedPath) => {
+ const watcher = this.native.start(resolvedPath, (p, ...args) => {
+ return handler(this.transpose(virtualPath, resolvedPath, p), ...args);
+ });
arcanis marked this conversation as resolved.
Show resolved Hide resolved
+ if (!watcher) throw new Error(`could not watch: ${resolvedPath}`);
+ this.watchers.push(watcher);
+ });
+ return this.watchers;
+ }
+
+ wrap(fn) {
+ return (path, ...args) => {
+ return fn(this.transpose(path), ...args);
+ };
+ stop() {
+ const results = this.watchers.map((watcher) => {
+ const p = Promise.resolve(watcher);
+ if (watcher) {
+ p.then(this.native.stop);
+ }
+ return p;
+ });
+ this.watchers = [];
+ this.resolvedRoot = undefined;
+ return results[0];
+ }
+};
+}
+
+module.exports = VFS;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ THIS_DIR=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)
TEMP_DIR="$(mktemp -d)"

cat "$THIS_DIR"/1.2.11.patch \
"$THIS_DIR"/2.1.1.patch \
"$THIS_DIR"/2.1.2.patch \
"$THIS_DIR"/common.patch \
> "$TEMP_DIR"/patch.tmp
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-compat/sources/patches/fsevents.patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let patch: string;

export function getPatch() {
if (typeof patch === `undefined`)
patch = require(`zlib`).brotliDecompressSync(Buffer.from(`G8EIABwHuTnyDkxeQiomXep01zJ90cJ3iFSgGcnN+dVTE5YC1CBsZn0bRMFnq2+/bPJOWLRlcCblbWaytN6yn94lDuHQVXEMzob/mhDOafB/uXcOjPnzEX5TF8I/4H+A7n4PCzSY0xTuWjDfxxV8F1neM4x7jymltl+dnYEp13SxCOpkQxUClagaNItavHVUdwD73pT3+c52oJFtOTmagkX/GAaKFyr1bLfAnKMY+OZmY+0YsC6Sci7AJQI2zADQHhdIcc03Dz+GOC05kpj3M0kiNKsdFu1U3ornmwco/hOeYDp3IUlCI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kJgok4/NdMZuOnk4Xj43CaF0zNFPLM0qtRMcic1htxg2XTO5CwX4h7CgMWgzDAvJdFAiXTHsP4eCs2T56c2LxGI4G6zxvW8oP9AYCkqdGrKEjOUvCxqUn86MX9vmxWzvy+i87SvJE4aiEeSTaGHXaIdO5sLFb+4RgOsZpFh/FeSVAmCNLbw7wJvlYipW80oIxOQfD5eS1seiShByZRsQdo7Xrloyofuz1GdsLqAnjfLyBIHdTBTMYQrjbIlLwdWaR+OHLgqtn+5pspHHr5Fb1Lq5zS9D1fdVuz/WVaI1l2pSuPlsSpq3ZuRbiK9X6nFGxWei/kciexfjL8VeEjx8xxvRiK7hUPooRFKyeaFnyY0CCdv8b7dGayeU2jPYMiTjqc4MVQzELtK+8UGKBzF1jSd7puafWfpbW43kvUG4PNAywabVCWEXER6P5ckjWnb477YMyuagBqrT1h/D8UGVT+icblV+d8C8vzgRbB+wmqeNGAQ==`, `base64`)).toString();

return patch;
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12631,20 +12631,20 @@ fsevents@^1.2.7:

"fsevents@patch:fsevents@^1.2.7#builtin<compat/fsevents>":
version: 1.2.7
resolution: "fsevents@patch:fsevents@npm%3A1.2.7#builtin<compat/fsevents>::version=1.2.7&hash=495457"
resolution: "fsevents@patch:fsevents@npm%3A1.2.7#builtin<compat/fsevents>::version=1.2.7&hash=a1ec3e"
dependencies:
nan: ^2.9.2
node-pre-gyp: ^0.10.0
checksum: d56427b46c8efdc74f05853fd81e294d9dac9816d1ca9e536207b7ff85ccaccf3cce1ac03b573cffa83fb1bcab0eb077184de66790313e87b4f00db2247d882c
checksum: bbf0b3dd8462ea03337da5cab6f75637bc696127add5753e5f9b231609695a2056c7824f4da78d00c831c39a47b87ce1b1d601ceb4ef3fcf019322b6677a9efb
languageName: node
linkType: hard

"fsevents@patch:fsevents@^2.1.2#builtin<compat/fsevents>, fsevents@patch:fsevents@~2.1.2#builtin<compat/fsevents>":
version: 2.1.3
resolution: "fsevents@patch:fsevents@npm%3A2.1.3#builtin<compat/fsevents>::version=2.1.3&hash=495457"
resolution: "fsevents@patch:fsevents@npm%3A2.1.3#builtin<compat/fsevents>::version=2.1.3&hash=a1ec3e"
dependencies:
node-gyp: latest
checksum: 0005677b72f38a129a3cbe8c3794bdc83081a2bec53dfc03b085c2e5e4ca7a33a861a779d623313652df89746d97f79d24e4fef3b101c11c39ce1ea8a9690e18
checksum: 972dc3797aba6ce06ce4f502469d60e3781c0cbd2e72067fdb60221655d50f94c7ca69c7108b8336cad8b3819cd77c0c83a1239e5a50416baa22bef7ea52a7e9
languageName: node
linkType: hard

Expand Down