Skip to content

Commit

Permalink
Don't follow symlinks by default
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 10, 2024
1 parent e31cb49 commit 8fe127b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/knip/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
ignoreExportsUsedInFile: chief.config.ignoreExportsUsedInFile,
isReportClassMembers,
tags,
workspacePkgNames: chief.availableWorkspacePkgNames,
},
isGitIgnored,
isPackageNameInternalWorkspace,
Expand Down
9 changes: 7 additions & 2 deletions packages/knip/src/typescript/get-imports-and-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -64,6 +65,7 @@ export type GetImportsAndExportsOptions = {
isReportClassMembers: boolean;
ignoreExportsUsedInFile: IgnoreExportsUsedInFile;
tags: Tags;
workspacePkgNames: Set<string>;
};

interface AddInternalImportOptions extends ImportNode {
Expand All @@ -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<string>();
const unresolvedImports = new Set<UnresolvedImport>();
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions packages/knip/src/util/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});

Expand All @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 8fe127b

Please sign in to comment.