From 8fe127b08a43ee007c199ee87bbcf5b5756b77f5 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Thu, 10 Oct 2024 19:39:23 +0200 Subject: [PATCH] Don't follow symlinks by default --- packages/knip/src/index.ts | 1 + packages/knip/src/typescript/get-imports-and-exports.ts | 9 +++++++-- packages/knip/src/util/resolve.ts | 9 +++++++-- .../knip/test/module-resolution-non-std-absolute.test.ts | 4 +++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/knip/src/index.ts b/packages/knip/src/index.ts index e958b2839..70866c466 100644 --- a/packages/knip/src/index.ts +++ b/packages/knip/src/index.ts @@ -288,6 +288,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => { ignoreExportsUsedInFile: chief.config.ignoreExportsUsedInFile, isReportClassMembers, tags, + workspacePkgNames: chief.availableWorkspacePkgNames, }, isGitIgnored, isPackageNameInternalWorkspace, diff --git a/packages/knip/src/typescript/get-imports-and-exports.ts b/packages/knip/src/typescript/get-imports-and-exports.ts index b0fc28d49..c3c3b6d19 100644 --- a/packages/knip/src/typescript/get-imports-and-exports.ts +++ b/packages/knip/src/typescript/get-imports-and-exports.ts @@ -10,7 +10,8 @@ import type { IssueSymbol } from '../types/issues.js'; import { timerify } from '../util/Performance.js'; import { addNsValue, addValue, createImports } from '../util/dependency-graph.js'; import { getPackageNameFromFilePath, isStartsLikePackageName, sanitizeSpecifier } from '../util/modules.js'; -import { extname, isInNodeModules } from '../util/path.js'; +import { dirname, extname, isInNodeModules } from '../util/path.js'; +import { _resolveSyncFollowSymlinks } from '../util/resolve.js'; import { shouldIgnore } from '../util/tag.js'; import type { BoundSourceFile } from './SourceFile.js'; import { @@ -64,6 +65,7 @@ export type GetImportsAndExportsOptions = { isReportClassMembers: boolean; ignoreExportsUsedInFile: IgnoreExportsUsedInFile; tags: Tags; + workspacePkgNames: Set; }; interface AddInternalImportOptions extends ImportNode { @@ -79,7 +81,7 @@ const getImportsAndExports = ( typeChecker: ts.TypeChecker, options: GetImportsAndExportsOptions ) => { - const { skipTypeOnly, tags, ignoreExportsUsedInFile } = options; + const { skipTypeOnly, tags, ignoreExportsUsedInFile, workspacePkgNames } = options; const internalImports: ImportMap = new Map(); const externalImports = new Set(); const unresolvedImports = new Set(); @@ -188,6 +190,9 @@ const getImportsAndExports = ( if (!module.isExternalLibraryImport || !isInNodeModules(filePath)) { addInternalImport({ ...options, identifier, filePath, isReExport }); + } else if (workspacePkgNames.has(getPackageNameFromFilePath(filePath))) { + const fp = _resolveSyncFollowSymlinks(filePath, dirname(filePath)); + if (fp) addInternalImport({ ...options, identifier, filePath: fp, isReExport }); } if (module.isExternalLibraryImport) { diff --git a/packages/knip/src/util/resolve.ts b/packages/knip/src/util/resolve.ts index cad887bfd..d8fc3ff41 100644 --- a/packages/knip/src/util/resolve.ts +++ b/packages/knip/src/util/resolve.ts @@ -7,10 +7,11 @@ import { toPosix } from './path.js'; // @ts-ignore error TS2345 (not in latest): Argument of type 'typeof import("node:fs")' is not assignable to parameter of type 'BaseFileSystem'. const fileSystem = new ER.CachedInputFileSystem(fs, 9999999); -export const createSyncResolver = (extensions: string[]) => { +export const createSyncResolver = (extensions: string[], symlinks = true) => { const resolver = ER.create.sync({ fileSystem, extensions, + symlinks, conditionNames: ['require', 'import', 'node', 'default'], }); @@ -22,6 +23,10 @@ export const createSyncResolver = (extensions: string[]) => { }; }; -const resolveSync = createSyncResolver([...DEFAULT_EXTENSIONS, '.json']); +const resolveSync = createSyncResolver([...DEFAULT_EXTENSIONS, '.json'], false); + +const resolveSyncFollowSymlinks = createSyncResolver([...DEFAULT_EXTENSIONS, '.json'], true); export const _resolveSync = timerify(resolveSync); + +export const _resolveSyncFollowSymlinks = timerify(resolveSyncFollowSymlinks); diff --git a/packages/knip/test/module-resolution-non-std-absolute.test.ts b/packages/knip/test/module-resolution-non-std-absolute.test.ts index c5e70a92d..cded9c004 100644 --- a/packages/knip/test/module-resolution-non-std-absolute.test.ts +++ b/packages/knip/test/module-resolution-non-std-absolute.test.ts @@ -14,10 +14,12 @@ test('Resolve non-standard absolute specifiers', async () => { }); assert(issues.unlisted['self/index.ts']['other']); + assert(issues.unlisted['self/index.ts']['other/absolute.css']); + assert(issues.unlisted['self/index.ts']['other/absolute.svg']); assert.deepEqual(counters, { ...baseCounters, - unlisted: 1, + unlisted: 3, processed: 1, total: 1, });