Skip to content

Commit 8fe127b

Browse files
committed
Don't follow symlinks by default
1 parent e31cb49 commit 8fe127b

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

packages/knip/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
288288
ignoreExportsUsedInFile: chief.config.ignoreExportsUsedInFile,
289289
isReportClassMembers,
290290
tags,
291+
workspacePkgNames: chief.availableWorkspacePkgNames,
291292
},
292293
isGitIgnored,
293294
isPackageNameInternalWorkspace,

packages/knip/src/typescript/get-imports-and-exports.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import type { IssueSymbol } from '../types/issues.js';
1010
import { timerify } from '../util/Performance.js';
1111
import { addNsValue, addValue, createImports } from '../util/dependency-graph.js';
1212
import { getPackageNameFromFilePath, isStartsLikePackageName, sanitizeSpecifier } from '../util/modules.js';
13-
import { extname, isInNodeModules } from '../util/path.js';
13+
import { dirname, extname, isInNodeModules } from '../util/path.js';
14+
import { _resolveSyncFollowSymlinks } from '../util/resolve.js';
1415
import { shouldIgnore } from '../util/tag.js';
1516
import type { BoundSourceFile } from './SourceFile.js';
1617
import {
@@ -64,6 +65,7 @@ export type GetImportsAndExportsOptions = {
6465
isReportClassMembers: boolean;
6566
ignoreExportsUsedInFile: IgnoreExportsUsedInFile;
6667
tags: Tags;
68+
workspacePkgNames: Set<string>;
6769
};
6870

6971
interface AddInternalImportOptions extends ImportNode {
@@ -79,7 +81,7 @@ const getImportsAndExports = (
7981
typeChecker: ts.TypeChecker,
8082
options: GetImportsAndExportsOptions
8183
) => {
82-
const { skipTypeOnly, tags, ignoreExportsUsedInFile } = options;
84+
const { skipTypeOnly, tags, ignoreExportsUsedInFile, workspacePkgNames } = options;
8385
const internalImports: ImportMap = new Map();
8486
const externalImports = new Set<string>();
8587
const unresolvedImports = new Set<UnresolvedImport>();
@@ -188,6 +190,9 @@ const getImportsAndExports = (
188190

189191
if (!module.isExternalLibraryImport || !isInNodeModules(filePath)) {
190192
addInternalImport({ ...options, identifier, filePath, isReExport });
193+
} else if (workspacePkgNames.has(getPackageNameFromFilePath(filePath))) {
194+
const fp = _resolveSyncFollowSymlinks(filePath, dirname(filePath));
195+
if (fp) addInternalImport({ ...options, identifier, filePath: fp, isReExport });
191196
}
192197

193198
if (module.isExternalLibraryImport) {

packages/knip/src/util/resolve.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { toPosix } from './path.js';
77
// @ts-ignore error TS2345 (not in latest): Argument of type 'typeof import("node:fs")' is not assignable to parameter of type 'BaseFileSystem'.
88
const fileSystem = new ER.CachedInputFileSystem(fs, 9999999);
99

10-
export const createSyncResolver = (extensions: string[]) => {
10+
export const createSyncResolver = (extensions: string[], symlinks = true) => {
1111
const resolver = ER.create.sync({
1212
fileSystem,
1313
extensions,
14+
symlinks,
1415
conditionNames: ['require', 'import', 'node', 'default'],
1516
});
1617

@@ -22,6 +23,10 @@ export const createSyncResolver = (extensions: string[]) => {
2223
};
2324
};
2425

25-
const resolveSync = createSyncResolver([...DEFAULT_EXTENSIONS, '.json']);
26+
const resolveSync = createSyncResolver([...DEFAULT_EXTENSIONS, '.json'], false);
27+
28+
const resolveSyncFollowSymlinks = createSyncResolver([...DEFAULT_EXTENSIONS, '.json'], true);
2629

2730
export const _resolveSync = timerify(resolveSync);
31+
32+
export const _resolveSyncFollowSymlinks = timerify(resolveSyncFollowSymlinks);

packages/knip/test/module-resolution-non-std-absolute.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ test('Resolve non-standard absolute specifiers', async () => {
1414
});
1515

1616
assert(issues.unlisted['self/index.ts']['other']);
17+
assert(issues.unlisted['self/index.ts']['other/absolute.css']);
18+
assert(issues.unlisted['self/index.ts']['other/absolute.svg']);
1719

1820
assert.deepEqual(counters, {
1921
...baseCounters,
20-
unlisted: 1,
22+
unlisted: 3,
2123
processed: 1,
2224
total: 1,
2325
});

0 commit comments

Comments
 (0)