Skip to content

Commit 079263b

Browse files
committed
Improve global handling of config files
1 parent 06583fa commit 079263b

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

packages/knip/src/WorkspaceWorker.ts

+31-33
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ type WorkspaceManagerOptions = {
4949
isStrict: boolean;
5050
isCache: boolean;
5151
cacheLocation: string;
52-
allConfigFilePaths: Set<string>;
53-
allConfigFilesMap: Map<string, Map<PluginName, Set<string>>>;
52+
configFilesMap: Map<string, Map<PluginName, Set<string>>>;
5453
};
5554

5655
type CacheItem = { resolveEntryPaths?: Input[]; resolveConfig?: Input[]; resolveFromAST?: Input[] };
@@ -91,8 +90,7 @@ export class WorkspaceWorker {
9190

9291
cache: CacheConsultant<CacheItem>;
9392

94-
allConfigFilePaths: Set<string>;
95-
allConfigFilesMap: Map<string, Map<PluginName, Set<string>>>;
93+
configFilesMap: Map<string, Map<PluginName, Set<string>>>;
9694

9795
constructor({
9896
name,
@@ -112,8 +110,7 @@ export class WorkspaceWorker {
112110
getSourceFile,
113111
isCache,
114112
cacheLocation,
115-
allConfigFilePaths,
116-
allConfigFilesMap,
113+
configFilesMap,
117114
}: WorkspaceManagerOptions) {
118115
this.name = name;
119116
this.dir = dir;
@@ -127,8 +124,7 @@ export class WorkspaceWorker {
127124
this.negatedWorkspacePatterns = negatedWorkspacePatterns;
128125
this.ignoredWorkspacePatterns = ignoredWorkspacePatterns;
129126
this.enabledPluginsInAncestors = enabledPluginsInAncestors;
130-
this.allConfigFilePaths = allConfigFilePaths;
131-
this.allConfigFilesMap = allConfigFilesMap;
127+
this.configFilesMap = configFilesMap;
132128

133129
this.getReferencedInternalFilePath = getReferencedInternalFilePath;
134130
this.findWorkspaceByFilePath = findWorkspaceByFilePath;
@@ -260,7 +256,7 @@ export class WorkspaceWorker {
260256
}
261257

262258
public async runPlugins() {
263-
const name = this.name;
259+
const wsName = this.name;
264260
const cwd = this.dir;
265261
const rootCwd = this.cwd;
266262
const manifest = this.manifest;
@@ -296,17 +292,19 @@ export class WorkspaceWorker {
296292
}
297293
};
298294

295+
const configFilesMap = this.configFilesMap;
296+
const configFiles = this.configFilesMap.get(wsName);
297+
299298
const handleConfigInput = (pluginName: PluginName, input: ConfigInput) => {
300299
const configFilePath = this.getReferencedInternalFilePath(input);
301300
if (configFilePath) {
302301
const workspace = this.findWorkspaceByFilePath(configFilePath);
303302
if (workspace) {
304303
// We can only handle root → child transfers, otherwise add to and run in current workspace
305304
const name = this.name === ROOT_WORKSPACE_NAME ? workspace.name : this.name;
306-
const files = this.allConfigFilesMap;
307-
if (!files.has(name)) files.set(name, new Map());
308-
if (!files.get(name)?.has(pluginName)) files.get(name)?.set(pluginName, new Set());
309-
files.get(name)?.get(pluginName)?.add(configFilePath);
305+
if (!configFilesMap.has(name)) configFilesMap.set(name, new Map());
306+
if (!configFilesMap.get(name)?.has(pluginName)) configFilesMap.get(name)?.set(pluginName, new Set());
307+
configFilesMap.get(name)?.get(pluginName)?.add(configFilePath);
310308
}
311309
}
312310
};
@@ -329,15 +327,6 @@ export class WorkspaceWorker {
329327
const label = 'config file';
330328
const configFilePaths = await _glob({ patterns, cwd: rootCwd, dir: cwd, gitignore: false, label });
331329

332-
const remainingConfigFilePaths = configFilePaths.filter(filePath => !this.allConfigFilePaths.has(filePath));
333-
for (const filePath of remainingConfigFilePaths) {
334-
if (basename(filePath) !== 'package.json') {
335-
this.allConfigFilePaths.add(filePath);
336-
addInput(toEntry(filePath));
337-
addInput(toConfig(pluginName, filePath));
338-
}
339-
}
340-
341330
const options = {
342331
...baseScriptOptions,
343332
config,
@@ -349,13 +338,18 @@ export class WorkspaceWorker {
349338

350339
if (config.entry) {
351340
const toInput = isProduction && plugin.production && plugin.production.length > 0 ? toProductionEntry : toEntry;
352-
for (const input of config.entry.map(id => toInput(id))) addInput(input);
353-
} else if ((!plugin.resolveEntryPaths && !plugin.resolveFromAST) || remainingConfigFilePaths.length === 0) {
354-
if (plugin.entry) for (const input of plugin.entry.map(id => toEntry(id))) addInput(input);
355-
if (plugin.production) for (const input of plugin.production.map(id => toProductionEntry(id))) addInput(input);
341+
for (const id of config.entry) addInput(toInput(id));
342+
} else if (
343+
(!plugin.resolveEntryPaths && !plugin.resolveFromAST) ||
344+
(configFilePaths.length === 0 &&
345+
(!this.configFilesMap.get(wsName)?.get(pluginName) ||
346+
this.configFilesMap.get(wsName)?.get(pluginName)?.size === 0))
347+
) {
348+
if (plugin.entry) for (const id of plugin.entry) addInput(toEntry(id));
349+
if (plugin.production) for (const id of plugin.production) addInput(toProductionEntry(id));
356350
}
357351

358-
for (const configFilePath of remainingConfigFilePaths) {
352+
for (const configFilePath of configFilePaths) {
359353
const isManifest = basename(configFilePath) === 'package.json';
360354
const fd = isManifest ? undefined : this.cache.getFileDescriptor(configFilePath);
361355

@@ -375,10 +369,11 @@ export class WorkspaceWorker {
375369
configFileName: basename(configFilePath),
376370
};
377371

372+
const seen = this.configFilesMap.get(wsName)?.get(pluginName)?.has(configFilePath);
378373
const cache: CacheItem = {};
379374
let loadedConfig: unknown;
380375

381-
if (plugin.resolveEntryPaths) {
376+
if (plugin.resolveEntryPaths && !seen) {
382377
if (!loadedConfig) loadedConfig = await loadConfigForPlugin(configFilePath, plugin, resolveOpts, pluginName);
383378
if (loadedConfig) {
384379
const inputs = await plugin.resolveEntryPaths(loadedConfig, resolveOpts);
@@ -387,7 +382,7 @@ export class WorkspaceWorker {
387382
}
388383
}
389384

390-
if (plugin.resolveConfig) {
385+
if (plugin.resolveConfig && !seen) {
391386
if (!loadedConfig) loadedConfig = await loadConfigForPlugin(configFilePath, plugin, resolveOpts, pluginName);
392387
if (loadedConfig) {
393388
const inputs = await plugin.resolveConfig(loadedConfig, resolveOpts);
@@ -410,6 +405,11 @@ export class WorkspaceWorker {
410405
}
411406
}
412407

408+
if (basename(configFilePath) !== 'package.json') {
409+
addInput(toEntry(configFilePath));
410+
addInput(toConfig(pluginName, configFilePath));
411+
}
412+
413413
if (!isManifest && fd?.changed && fd.meta) fd.meta.data = cache;
414414
}
415415

@@ -422,8 +422,6 @@ export class WorkspaceWorker {
422422
const enabledPluginTitles = this.enabledPlugins.map(name => Plugins[name].title);
423423
debugLogObject(this.name, 'Enabled plugins', enabledPluginTitles);
424424

425-
const configFiles = this.allConfigFilesMap.get(name);
426-
427425
for (const pluginName of this.enabledPlugins) {
428426
const patterns = [...this.getConfigurationFilePatterns(pluginName), ...(configFiles?.get(pluginName) ?? [])];
429427
configFiles?.delete(pluginName);
@@ -433,7 +431,7 @@ export class WorkspaceWorker {
433431

434432
{
435433
// Handle config files added from root or current workspace recursively
436-
const configFiles = this.allConfigFilesMap.get(name);
434+
const configFiles = this.configFilesMap.get(wsName);
437435
if (configFiles) {
438436
do {
439437
for (const [pluginName, dependencies] of configFiles.entries()) {
@@ -445,7 +443,7 @@ export class WorkspaceWorker {
445443
}
446444
}
447445

448-
debugLogArray(name, 'Plugin dependencies', () => compact(inputs.map(toDebugString)));
446+
debugLogArray(wsName, 'Plugin dependencies', () => compact(inputs.map(toDebugString)));
449447

450448
return inputs;
451449
}

packages/knip/src/graph/build.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ export async function build({
7878
tsConfigFile,
7979
workspaces,
8080
}: BuildOptions) {
81-
// Handle config files only once across workspaces workers
82-
const allConfigFilePaths = new Set<string>();
83-
const allConfigFilesMap = new Map<string, Map<PluginName, Set<string>>>();
81+
const configFilesMap = new Map<string, Map<PluginName, Set<string>>>();
8482

8583
const enabledPluginsStore = new Map<string, string[]>();
8684

@@ -137,8 +135,7 @@ export async function build({
137135
getSourceFile: (filePath: string) => principal.backend.fileManager.getSourceFile(filePath),
138136
isCache,
139137
cacheLocation,
140-
allConfigFilePaths,
141-
allConfigFilesMap,
138+
configFilesMap,
142139
});
143140

144141
await worker.init();

0 commit comments

Comments
 (0)