Skip to content

Commit 42497c2

Browse files
committed
Handle package.json exports for outDir="." (resolve #1738)
1 parent 6e726a2 commit 42497c2

7 files changed

Lines changed: 45 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 a = 'a';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@fixtures/package-entry-points-root-dir",
3+
"exports": {
4+
"./entry": "./dist/entry.js",
5+
"./types": { "types": "./dist/types.d.ts" }
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "dist",
4+
"rootDir": "./"
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type A = 'a';

packages/knip/src/util/to-source-path.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ export const augmentWorkspace = (
3333

3434
const isUnderOutDir = (absPath: string, outDir: string) => absPath === outDir || absPath.startsWith(`${outDir}/`);
3535

36-
const isUnderSrcDir = (absPath: string, srcDir: string) => absPath === srcDir || absPath.startsWith(`${srcDir}/`);
37-
3836
const rewritePattern = (sourceMaps: SourceMap[], absSpecifier: string, extensions: string) => {
3937
for (const { srcDir, outDir } of sourceMaps) {
40-
if (!isUnderSrcDir(absSpecifier, srcDir) && isUnderOutDir(absSpecifier, outDir)) {
38+
if (srcDir !== outDir && isUnderOutDir(absSpecifier, outDir)) {
4139
return srcDir + absSpecifier.slice(outDir.length).replace(matchExt, extensions);
4240
}
4341
}
@@ -93,7 +91,7 @@ export const toSourceMappedSpecifiers = (
9391
const out: string[] = [];
9492
if (!ws?.sourceMaps) return out;
9593
for (const { srcDir, outDir } of ws.sourceMaps) {
96-
if (!isUnderSrcDir(absSpecifier, srcDir) && isUnderOutDir(absSpecifier, outDir)) {
94+
if (srcDir !== outDir && isUnderOutDir(absSpecifier, outDir)) {
9795
out.push(srcDir + absSpecifier.slice(outDir.length).replace(matchExt, extensions));
9896
}
9997
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import assert from 'node:assert/strict';
2+
import test from 'node:test';
3+
import { main } from '../src/index.ts';
4+
import baseCounters from './helpers/baseCounters.ts';
5+
import { createOptions } from './helpers/create-options.ts';
6+
import { resolve } from './helpers/resolve.ts';
7+
8+
const cwd = resolve('fixtures/package-entry-points-root-dir');
9+
10+
test('Resolve package entry points with tsconfig rootDir: "."', async () => {
11+
const options = await createOptions({ cwd });
12+
const { counters, issues, configurationHints } = await main(options);
13+
14+
assert.deepEqual(issues.files, {});
15+
assert.deepEqual(configurationHints, []);
16+
17+
assert.deepEqual(counters, {
18+
...baseCounters,
19+
processed: 2,
20+
total: 2,
21+
});
22+
});

packages/knip/test/subpath-imports-outdir.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ const cwd = resolve('fixtures/subpath-imports-outdir');
99

1010
test('Resolve subpath imports to outDir with rootDir="."', async () => {
1111
const options = await createOptions({ cwd });
12-
const { counters } = await main(options);
12+
const { counters, configurationHints } = await main(options);
13+
14+
assert.equal(configurationHints.length, 1);
15+
assert.equal(configurationHints[0].type, 'entry-redundant');
1316

1417
assert.deepEqual(counters, {
1518
...baseCounters,
16-
processed: 5,
17-
total: 5,
19+
processed: 3,
20+
total: 3,
1821
});
1922
});

0 commit comments

Comments
 (0)