From dea1aed2f04afb71c6451e842de61fd88e8712e0 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Mon, 25 Feb 2019 18:28:31 -0800 Subject: [PATCH] Perf fix: cache the resolutions identically to how require does. Turns out this is extremely expensive, and apps with many apps/engines/in-repo addons spend up to minutes here. --- package.json | 3 +- src/npm-dependency-version-checker.js | 48 +++------------------------ tests/index.js | 1 - yarn.lock | 48 +++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index 27baa8a..5781de1 100644 --- a/package.json +++ b/package.json @@ -22,10 +22,11 @@ }, "scripts": { "test": "mocha tests", + "test:debug": "mocha debug tests", "changelog": "lerna-changelog" }, "dependencies": { - "resolve": "^1.10.0", + "resolve-package-path": "^1.0.3", "semver": "^5.6.0" }, "devDependencies": { diff --git a/src/npm-dependency-version-checker.js b/src/npm-dependency-version-checker.js index 98a083c..006958b 100644 --- a/src/npm-dependency-version-checker.js +++ b/src/npm-dependency-version-checker.js @@ -1,56 +1,16 @@ 'use strict'; -const resolve = require('resolve'); const DependencyVersionChecker = require('./dependency-version-checker'); const getProject = require('./get-project'); +const resolvePackage = require('resolve-package-path'); -const ALLOWED_ERROR_CODES = [ - // resolve package error codes - 'MODULE_NOT_FOUND', - - // Yarn PnP Error Codes - 'UNDECLARED_DEPENDENCY', - 'MISSING_PEER_DEPENDENCY', - 'MISSING_DEPENDENCY', -]; - -let pnp; - -try { - pnp = require('pnpapi'); -} catch (error) { - // not in Yarn PnP; not a problem -} - -class NPMDependencyVersionChecker extends DependencyVersionChecker { +module.exports = class NPMDependencyVersionChecker extends DependencyVersionChecker { constructor(parent, name) { super(parent, name); let addon = this._parent._addon; - - let target = this.name + '/package.json'; let basedir = addon.root || getProject(addon).root; - - let jsonPath; - - try { - // the custom `pnp` code here can be removed when yarn 1.13 is the - // current release this is due to Yarn 1.13 and resolve interoperating - // together seemlessly - jsonPath = pnp - ? pnp.resolveToUnqualified(target, basedir) - : resolve.sync(target, { basedir }); - } catch (e) { - if (ALLOWED_ERROR_CODES.includes(e.code)) { - jsonPath = null; - } else { - throw e; - } - } - - this._jsonPath = jsonPath; + this._jsonPath = resolvePackage(this.name, basedir); this._type = 'npm'; } -} - -module.exports = NPMDependencyVersionChecker; +}; diff --git a/tests/index.js b/tests/index.js index 7e77470..9ebc1b5 100644 --- a/tests/index.js +++ b/tests/index.js @@ -203,7 +203,6 @@ describe('ember-cli-version-checker', function() { describe('exists', function() { it('returns true when present', function() { let thing = checker.for('ember'); - assert.ok(thing.exists()); }); diff --git a/yarn.lock b/yarn.lock index e421530..437b0f8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1106,6 +1106,15 @@ fs-extra@^0.30.0: path-is-absolute "^1.0.0" rimraf "^2.2.8" +fs-extra@^7.0.1: + version "7.0.1" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9" + integrity sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw== + dependencies: + graceful-fs "^4.1.2" + jsonfile "^4.0.0" + universalify "^0.1.0" + fs-tree-diff@^0.5.6, fs-tree-diff@^0.5.9: version "0.5.9" resolved "https://registry.yarnpkg.com/fs-tree-diff/-/fs-tree-diff-0.5.9.tgz#a4ec6182c2f5bd80b9b83c8e23e4522e6f5fd946" @@ -1628,6 +1637,13 @@ jsonfile@^2.1.0: optionalDependencies: graceful-fs "^4.1.6" +jsonfile@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb" + integrity sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss= + optionalDependencies: + graceful-fs "^4.1.6" + kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0: version "3.2.2" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64" @@ -2295,11 +2311,28 @@ path-posix@^1.0.0: resolved "https://registry.yarnpkg.com/path-posix/-/path-posix-1.0.0.tgz#06b26113f56beab042545a23bfa88003ccac260f" integrity sha1-BrJhE/Vr6rBCVFojv6iAA8ysJg8= +path-root-regex@^0.1.0: + version "0.1.2" + resolved "https://registry.yarnpkg.com/path-root-regex/-/path-root-regex-0.1.2.tgz#bfccdc8df5b12dc52c8b43ec38d18d72c04ba96d" + integrity sha1-v8zcjfWxLcUsi0PsONGNcsBLqW0= + +path-root@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/path-root/-/path-root-0.1.1.tgz#9a4a6814cac1c0cd73360a95f32083c8ea4745b7" + integrity sha1-mkpoFMrBwM1zNgqV8yCDyOpHRbc= + dependencies: + path-root-regex "^0.1.0" + pluralize@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-7.0.0.tgz#298b89df8b93b0221dbf421ad2b1b1ea23fc6777" integrity sha512-ARhBOdzS3e41FbkW/XWrTEtukqqLoK5+Z/4UeDaLuSW+39JPeFgs4gCGqsrJHVZX0fUrx//4OF0K1CUGwlIFow== +pnpapi@^0.0.0: + version "0.0.0" + resolved "https://registry.yarnpkg.com/pnpapi/-/pnpapi-0.0.0.tgz#90fe6bd18be67d3bb39cc93cb67188b1bf63a384" + integrity sha512-UemFU6zCZ4MZwsBcgmcFZX71nnNUUCwxKxdxctxfiEDB39SOzvUUJ28g+Arx5JF8kO3N0yxyjY/4+N6+jBFqZw== + posix-character-classes@^0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab" @@ -2475,6 +2508,16 @@ resolve-from@^1.0.0: resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226" integrity sha1-Jsv+k10a7uq7Kbw/5a6wHpPUQiY= +resolve-package-path@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/resolve-package-path/-/resolve-package-path-1.0.0.tgz#bb48d3aed8653efdd97cd86af5698bebdc3dccd6" + integrity sha512-YxkxtS7lD3lc0/C3JcQ2lfo91wp+Gc9YEv4pZucKryVuBLTxnlvPUUsx8+ZCIDYZT/4E8GKwNyKNPQCHqxvvIA== + dependencies: + fs-extra "^7.0.1" + path-root "^0.1.1" + pnpapi "^0.0.0" + resolve "^1.10.0" + resolve-path@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/resolve-path/-/resolve-path-1.4.0.tgz#c4bda9f5efb2fce65247873ab36bb4d834fe16f7" @@ -3018,6 +3061,11 @@ unique-slug@^2.0.0: dependencies: imurmurhash "^0.1.4" +universalify@^0.1.0: + version "0.1.2" + resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66" + integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg== + unpipe@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"