Skip to content

Commit 95f4431

Browse files
committed
Fix false positive unused deps when run from workspace dir (resolve #1642)
1 parent ccc62d6 commit 95f4431

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

packages/knip/src/graph/build.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,16 @@ export async function build({
397397
if (!isIgnored) pp.addEntryPath(filePath, { skipExportsAnalysis: true });
398398
}
399399

400+
const wsDependencies = deputy.getDependencies(workspace.name);
400401
for (const _import of file.imports.imports) {
401-
if (_import.filePath) {
402-
const packageName = getPackageNameFromModuleSpecifier(_import.specifier);
403-
if (packageName && isInternalWorkspace(packageName)) {
404-
file.imports.external.add({ ..._import, specifier: packageName });
405-
if (!isGitIgnored(_import.filePath)) {
406-
pp.addProgramPath(_import.filePath);
407-
}
402+
if (!_import.filePath) continue;
403+
const packageName = getPackageNameFromModuleSpecifier(_import.specifier);
404+
if (!packageName) continue;
405+
const isWorkspace = isInternalWorkspace(packageName);
406+
if (isWorkspace || wsDependencies.has(packageName)) {
407+
file.imports.external.add({ ..._import, specifier: packageName });
408+
if (isWorkspace && !isGitIgnored(_import.filePath)) {
409+
pp.addProgramPath(_import.filePath);
408410
}
409411
}
410412
}

packages/knip/test/workspaces-pnpm.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ test('Find unused dependencies, exports and files in workspaces (loose)', async
2121
total: 4,
2222
});
2323
});
24+
25+
test('Find no false unused workspace dependencies when run from workspace dir', async () => {
26+
const options = await createOptions({ cwd: resolve('fixtures/workspaces-pnpm/apps/app-a') });
27+
const { counters } = await main(options);
28+
29+
assert.equal(counters.dependencies, 0);
30+
});

0 commit comments

Comments
 (0)