From b10562d5a2b90e99dcf39c469dbeddda7fec468a Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Wed, 1 Nov 2023 14:08:34 -0600 Subject: [PATCH] fix: find-root debug logs --- package.json | 1 + src/config/plugin.ts | 2 +- src/util/find-root.ts | 32 +++++++++++++++++++++++++++----- src/util/fs.ts | 3 --- yarn.lock | 12 ++++++++++++ 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index a9946ab2d..d2f532524 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "@types/chai-as-promised": "^7.1.5", "@types/clean-stack": "^2.1.1", "@types/cli-progress": "^3.11.0", + "@types/debug": "^4.1.10", "@types/ejs": "^3.1.3", "@types/indent-string": "^4.0.1", "@types/js-yaml": "^3.12.7", diff --git a/src/config/plugin.ts b/src/config/plugin.ts index 21c483935..46c1a2052 100644 --- a/src/config/plugin.ts +++ b/src/config/plugin.ts @@ -162,7 +162,7 @@ export class Plugin implements IPlugin { this.type === 'link' && !this.parent ? this.options.root : await findRoot(this.options.name, this.options.root) if (!root) throw new CLIError(`could not find package.json with ${inspect(this.options)}`) this.root = root - this._debug('reading %s plugin %s', this.type, root) + this._debug(`loading ${this.type} plugin from ${root}`) this.pjson = await readJson(join(root, 'package.json')) this.flexibleTaxonomy = this.options?.flexibleTaxonomy || this.pjson.oclif?.flexibleTaxonomy || false this.moduleType = this.pjson.type === 'module' ? 'module' : 'commonjs' diff --git a/src/util/find-root.ts b/src/util/find-root.ts index 9ee8f3c6c..82362b93c 100644 --- a/src/util/find-root.ts +++ b/src/util/find-root.ts @@ -1,11 +1,14 @@ /* eslint-disable no-await-in-loop */ import type {PackageInformation, PackageLocator, getPackageInformation} from 'pnpapi' +import makeDebug from 'debug' import {basename, dirname, join} from 'node:path' import {PJSON} from '../interfaces' import {safeReadJson} from './fs' +const debug = makeDebug('find-root') + // essentially just "cd .." function* up(from: string) { while (dirname(from) !== from) { @@ -26,7 +29,9 @@ async function findPluginRoot(root: string, name?: string) { // If we know the plugin name then we just need to traverse the file // system until we find the directory that matches the plugin name. if (name) { + debug.extend(name)(`Finding root starting at ${root}`) for (const next of up(root)) { + debug.extend(name)(`Checking ${next}`) if (next.endsWith(basename(name))) return next } } @@ -44,6 +49,7 @@ async function findPluginRoot(root: string, name?: string) { try { const cur = join(next, 'package.json') + debug.extend('root-plugin')(`Checking ${cur}`) if (await safeReadJson(cur)) return dirname(cur) } catch {} } @@ -56,6 +62,7 @@ async function findPluginRoot(root: string, name?: string) { * See https://github.com/oclif/config/pull/289#issuecomment-983904051 */ async function findRootLegacy(name: string | undefined, root: string): Promise { + debug.extend(name ?? 'root-plugin')('Finding root using legacy method') for (const next of up(root)) { let cur if (name) { @@ -106,9 +113,9 @@ const isPeerDependency = ( */ function findPnpRoot(name: string, root: string): string | undefined { maybeRequirePnpApi(root) - if (!pnp) return + debug.extend(name)('Finding root for using pnp method') const seen = new Set() const traverseDependencyTree = (locator: PackageLocator, parentPkg?: PackageInformation): string | undefined => { @@ -165,15 +172,30 @@ function findPnpRoot(name: string, root: string): string | undefined { */ export async function findRoot(name: string | undefined, root: string) { if (name) { + debug.extend(name)(`Finding root using ${root}`) let pkgPath try { pkgPath = require.resolve(name, {paths: [root]}) - } catch {} + debug.extend(name)(`Found starting point with require.resolve`) + } catch { + debug.extend(name)(`require.resolve could not find plugin starting point`) + } - if (pkgPath) return findPluginRoot(dirname(pkgPath), name) + if (pkgPath) { + const found = await findPluginRoot(dirname(pkgPath), name) + if (found) { + debug.extend(name)(`Found root at ${found}`) + return found + } + } - return process.versions.pnp ? findPnpRoot(name, root) : findRootLegacy(name, root) + const found = process.versions.pnp ? findPnpRoot(name, root) : await findRootLegacy(name, root) + debug.extend(name)(found ? `Found root at ${found}` : 'No root found!') + return found } - return findPluginRoot(root) + debug.extend('root-plugin')(`Finding root plugin using ${root}`) + const found = await findPluginRoot(root) + debug.extend('root-plugin')(found ? `Found root at ${found}` : 'No root found!') + return found } diff --git a/src/util/fs.ts b/src/util/fs.ts index ff78464cc..50e04a99f 100644 --- a/src/util/fs.ts +++ b/src/util/fs.ts @@ -2,8 +2,6 @@ import {Stats, existsSync as fsExistsSync, readFileSync} from 'node:fs' import {readFile, stat} from 'node:fs/promises' import {join} from 'node:path' -const debug = require('debug') - export function requireJson(...pathParts: string[]): T { return JSON.parse(readFileSync(join(...pathParts), 'utf8')) } @@ -51,7 +49,6 @@ export const fileExists = async (input: string): Promise => { } export async function readJson(path: string): Promise { - debug('config')('readJson %s', path) const contents = await readFile(path, 'utf8') return JSON.parse(contents) as T } diff --git a/yarn.lock b/yarn.lock index 347a5e844..f57258534 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1017,6 +1017,13 @@ resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ== +"@types/debug@^4.1.10": + version "4.1.10" + resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.10.tgz#f23148a6eb771a34c466a4fc28379d8101e84494" + integrity sha512-tOSCru6s732pofZ+sMv9o4o3Zc+Sa8l3bxd/tweTQudFn06vAzb13ZX46Zi6m6EJ+RUbRTHvgQJ1gBtSgkaUYA== + dependencies: + "@types/ms" "*" + "@types/ejs@^3.1.3": version "3.1.3" resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.1.3.tgz#ad91d1dd6e24fb60bbf96c534bce58b95eef9b57" @@ -1077,6 +1084,11 @@ resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-10.0.2.tgz#96d63314255540a36bf24da094cce7a13668d73b" integrity sha512-NaHL0+0lLNhX6d9rs+NSt97WH/gIlRHmszXbQ/8/MV/eVcFNdeJ/GYhrFuUc8K7WuPhRhTSdMkCp8VMzhUq85w== +"@types/ms@*": + version "0.7.33" + resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.33.tgz#80bf1da64b15f21fd8c1dc387c31929317d99ee9" + integrity sha512-AuHIyzR5Hea7ij0P9q7vx7xu4z0C28ucwjAZC0ja7JhINyCnOw8/DnvAPQQ9TfOlCtZAmCERKQX9+o1mgQhuOQ== + "@types/node-notifier@^8.0.2": version "8.0.2" resolved "https://registry.yarnpkg.com/@types/node-notifier/-/node-notifier-8.0.2.tgz#77c5de29c6e8adb915222b01864128cc3e78d553"