Skip to content

Commit

Permalink
Fix edge case for binary disguised as entry (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 28, 2024
1 parent 9defcb9 commit 846ed76
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/knip/src/util/handle-referenced-inputs.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/knip/test/util/get-inputs-from-scripts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down

0 comments on commit 846ed76

Please sign in to comment.