From 846ed76dfd8fadc6bc3d84b430e330ecace643e4 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Mon, 28 Oct 2024 03:02:58 +0100 Subject: [PATCH] Fix edge case for binary disguised as entry (#161) --- packages/knip/src/util/handle-referenced-inputs.ts | 8 +++++++- packages/knip/test/util/get-inputs-from-scripts.test.ts | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/knip/src/util/handle-referenced-inputs.ts b/packages/knip/src/util/handle-referenced-inputs.ts index 116a5dbd..fdad8447 100644 --- a/packages/knip/src/util/handle-referenced-inputs.ts +++ b/packages/knip/src/util/handle-referenced-inputs.ts @@ -1,9 +1,10 @@ import type { ConfigurationChief, Workspace } from '../ConfigurationChief.js'; import type { DependencyDeputy } from '../DependencyDeputy.js'; import type { IssueCollector } from '../IssueCollector.js'; +import { trimBinary } from '../binaries/util.js'; import { IGNORED_RUNTIME_DEPENDENCIES } from '../constants.js'; import { debugLog } from './debug.js'; -import { toDebugString } from './input.js'; +import { toBinary, toDebugString } from './input.js'; import { type Input, fromBinary, isBinary, isConfigPattern, isDeferResolveEntry, isDependency } from './input.js'; import { getPackageNameFromSpecifier } from './modules.js'; import { dirname, isAbsolute, isInternal, join } from './path.js'; @@ -27,6 +28,11 @@ export const getReferencedInputsHandler = if (!containingFilePath || IGNORED_RUNTIME_DEPENDENCIES.has(specifier)) return; + if (isDeferResolveEntry(input) && specifier.includes('node_modules/.bin')) { + // TODO? this quick-fixes edge case for entry like `node node_modules/.bin/jest`, maybe do it in binaries/node.ts alone or call this function again properly to avoid this mutation + Object.assign(input, toBinary(trimBinary(input.specifier))); + } + if (isBinary(input)) { const binaryName = fromBinary(input); const ws = (input.dir && chief.findWorkspaceByFilePath(`${input.dir}/`)) || workspace; diff --git a/packages/knip/test/util/get-inputs-from-scripts.test.ts b/packages/knip/test/util/get-inputs-from-scripts.test.ts index f0305239..5c872401 100644 --- a/packages/knip/test/util/get-inputs-from-scripts.test.ts +++ b/packages/knip/test/util/get-inputs-from-scripts.test.ts @@ -64,6 +64,7 @@ test('getInputsFromScripts (node -r)', () => { t('node -r @scope/package/register ./dir', [toBinary('node'), toDeferResolveEntry('./dir'), toDeferResolve('@scope/package/register')]); t('node -r @scope/package/register ./dir/index', [toBinary('node'), toDeferResolveEntry('./dir/index'), toDeferResolve('@scope/package/register')]); t('node --inspect-brk -r pkg/register node_modules/.bin/exec --runInBand', [toBinary('node'), toDeferResolveEntry('node_modules/.bin/exec'), toDeferResolve('pkg/register')]); + t('node -r ts-node/register node_modules/.bin/jest', [toBinary('node'), toDeferResolveEntry('node_modules/.bin/jest'), toDeferResolve('ts-node/register')]); }); test('getInputsFromScripts (ts-node)', () => {