Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔀 Don't follow symlinks by default #810

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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