Skip to content

Commit

Permalink
Try local package specifiers first (enhanced-resolve has trouble)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 13, 2024
1 parent dae3362 commit 89a384e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/knip/src/ConfigurationChief.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class ConfigurationChief {

ignoredWorkspacePatterns: string[] = [];
workspacePackages = new Map<string, Package>();
workspacePackagesByName = new Map<string, Package>();
workspacePackagesByPkgName = new Map<string, Package>();
additionalWorkspaceNames = new Set<string>();
availableWorkspaceNames: string[] = [];
availableWorkspacePkgNames = new Set<string>();
Expand Down Expand Up @@ -227,7 +227,7 @@ export class ConfigurationChief {
const [byName, byPkgName] = await mapWorkspaces(this.cwd, workspaceNames);

this.workspacePackages = byName;
this.workspacePackagesByName = byPkgName;
this.workspacePackagesByPkgName = byPkgName;
this.addRootPackage();

this.availableWorkspaceNames = this.getAvailableWorkspaceNames(byName.keys());
Expand Down Expand Up @@ -258,7 +258,7 @@ export class ConfigurationChief {
manifest: this.manifest,
};
this.workspacePackages.set('.', rootPackage);
this.workspacePackagesByName.set(pkgName, rootPackage);
this.workspacePackagesByPkgName.set(pkgName, rootPackage);
}
}

Expand Down
14 changes: 11 additions & 3 deletions packages/knip/src/util/handle-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ export const getReferencedDependencyHandler =

// Patterns: @local/package/file, self-reference/file, ./node_modules/@scope/pkg/tsconfig.json
if (packageName && specifier !== packageName) {
if (chief.workspacePackagesByName.get(packageName)) {
const filePath = _resolveSync(specifier, dirname(containingFilePath));
if (filePath) return filePath;
const specifierWorkspace = chief.workspacePackagesByPkgName.get(packageName);
if (specifierWorkspace) {
if (specifier.startsWith(packageName)) {
const dir = specifier.replace(new RegExp(`^${packageName}`), `./${specifierWorkspace.name}`);
const resolvedFilePath = _resolveSync(dir, chief.cwd);
if (resolvedFilePath) return resolvedFilePath;
}

const resolvedFilePath = _resolveSync(specifier, dirname(containingFilePath));
if (resolvedFilePath) return resolvedFilePath;

collector.addIssue({
type: 'unresolved',
filePath: containingFilePath,
Expand Down

0 comments on commit 89a384e

Please sign in to comment.