Skip to content

Commit ce5a619

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

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

packages/knip/src/WorkspaceWorker.ts

+29-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,16 @@ 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 && (!configFiles?.get(pluginName) || configFiles?.get(pluginName)?.size === 0))
345+
) {
346+
if (plugin.entry) for (const id of plugin.entry) addInput(toEntry(id));
347+
if (plugin.production) for (const id of plugin.production) addInput(toProductionEntry(id));
356348
}
357349

358-
for (const configFilePath of remainingConfigFilePaths) {
350+
for (const configFilePath of configFilePaths) {
359351
const isManifest = basename(configFilePath) === 'package.json';
360352
const fd = isManifest ? undefined : this.cache.getFileDescriptor(configFilePath);
361353

@@ -375,10 +367,11 @@ export class WorkspaceWorker {
375367
configFileName: basename(configFilePath),
376368
};
377369

370+
const seen = configFiles?.get(pluginName)?.has(configFilePath);
378371
const cache: CacheItem = {};
379372
let loadedConfig: unknown;
380373

381-
if (plugin.resolveEntryPaths) {
374+
if (plugin.resolveEntryPaths && !seen) {
382375
if (!loadedConfig) loadedConfig = await loadConfigForPlugin(configFilePath, plugin, resolveOpts, pluginName);
383376
if (loadedConfig) {
384377
const inputs = await plugin.resolveEntryPaths(loadedConfig, resolveOpts);
@@ -387,7 +380,7 @@ export class WorkspaceWorker {
387380
}
388381
}
389382

390-
if (plugin.resolveConfig) {
383+
if (plugin.resolveConfig && !seen) {
391384
if (!loadedConfig) loadedConfig = await loadConfigForPlugin(configFilePath, plugin, resolveOpts, pluginName);
392385
if (loadedConfig) {
393386
const inputs = await plugin.resolveConfig(loadedConfig, resolveOpts);
@@ -410,6 +403,11 @@ export class WorkspaceWorker {
410403
}
411404
}
412405

406+
if (basename(configFilePath) !== 'package.json') {
407+
addInput(toEntry(configFilePath));
408+
addInput(toConfig(pluginName, configFilePath));
409+
}
410+
413411
if (!isManifest && fd?.changed && fd.meta) fd.meta.data = cache;
414412
}
415413

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

425-
const configFiles = this.allConfigFilesMap.get(name);
426-
427423
for (const pluginName of this.enabledPlugins) {
428424
const patterns = [...this.getConfigurationFilePatterns(pluginName), ...(configFiles?.get(pluginName) ?? [])];
429425
configFiles?.delete(pluginName);
@@ -433,7 +429,7 @@ export class WorkspaceWorker {
433429

434430
{
435431
// Handle config files added from root or current workspace recursively
436-
const configFiles = this.allConfigFilesMap.get(name);
432+
const configFiles = this.configFilesMap.get(wsName);
437433
if (configFiles) {
438434
do {
439435
for (const [pluginName, dependencies] of configFiles.entries()) {
@@ -445,7 +441,7 @@ export class WorkspaceWorker {
445441
}
446442
}
447443

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

450446
return inputs;
451447
}

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)