Skip to content

Commit 2e8cf47

Browse files
committed
Fix package-entry hint for bare specifier in main (resolve #1693)
1 parent c2c0323 commit 2e8cf47

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const placeholder = true;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@fixture/package-entry-bare",
3+
"main": "bare-dep/entry",
4+
"dependencies": {
5+
"bare-dep": "*"
6+
}
7+
}

packages/knip/src/graph/build.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,16 @@ export async function build({
175175
}
176176

177177
for (const identifier of entrySpecifiersFromManifest) {
178-
if (!identifier.startsWith('!') && !isGitIgnored(join(dir, identifier))) {
179-
const exists = identifier.includes('*')
180-
? _syncGlob({ patterns: [identifier], cwd: dir }).length > 0
181-
: existsSync(join(dir, identifier));
182-
if (!exists) {
183-
collector.addConfigurationHint({ type: 'package-entry', filePath, identifier, workspaceName: name });
184-
}
178+
if (identifier.startsWith('!') || isGitIgnored(join(dir, identifier))) continue;
179+
if (!identifier.startsWith('.') && !identifier.startsWith('/')) {
180+
const packageName = getPackageNameFromModuleSpecifier(identifier);
181+
if (packageName && dependencies.has(packageName)) continue;
182+
}
183+
const exists = identifier.includes('*')
184+
? _syncGlob({ patterns: [identifier], cwd: dir }).length > 0
185+
: existsSync(join(dir, identifier));
186+
if (!exists) {
187+
collector.addConfigurationHint({ type: 'package-entry', filePath, identifier, workspaceName: name });
185188
}
186189
}
187190

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import assert from 'node:assert/strict';
2+
import test from 'node:test';
3+
import { main } from '../src/index.ts';
4+
import { createOptions } from './helpers/create-options.ts';
5+
import { resolve } from './helpers/resolve.ts';
6+
7+
const cwd = resolve('fixtures/package-entry-bare');
8+
9+
test('No package-entry hint for bare specifier in main when first segment is a listed dependency', async () => {
10+
const options = await createOptions({ cwd });
11+
const { configurationHints } = await main(options);
12+
13+
assert.deepEqual(configurationHints, []);
14+
});

0 commit comments

Comments
 (0)