-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Handle when an existing plugin begins to fail with the ne…
…w imported project
- Loading branch information
Showing
9 changed files
with
321 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
packages/nx/src/command-line/init/implementation/check-compatible-with-plugins.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { | ||
AggregateCreateNodesError, | ||
MergeNodesError, | ||
ProjectGraphError, | ||
ProjectsWithNoNameError, | ||
} from '../../../project-graph/error-types'; | ||
import { checkCompatibleWithPlugins } from './check-compatible-with-plugins'; | ||
import { retrieveProjectConfigurations } from '../../../project-graph/utils/retrieve-workspace-files'; | ||
|
||
jest.mock('../../../project-graph/plugins/internal-api', () => ({ | ||
loadNxPlugins: jest.fn().mockReturnValue([[], []]), | ||
})); | ||
jest.mock('../../../project-graph/utils/retrieve-workspace-files', () => ({ | ||
retrieveProjectConfigurations: jest.fn(), | ||
})); | ||
|
||
describe('checkCompatibleWithPlugins', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return empty object if no errors are thrown', async () => { | ||
(retrieveProjectConfigurations as any).mockReturnValueOnce( | ||
Promise.resolve({}) | ||
); | ||
const result = await checkCompatibleWithPlugins(); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should return empty object if error is not ProjectConfigurationsError', async () => { | ||
(retrieveProjectConfigurations as any).mockReturnValueOnce( | ||
Promise.reject(new Error('random error')) | ||
); | ||
const result = await checkCompatibleWithPlugins(); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should return empty object if error is ProjectsWithNoNameError', async () => { | ||
(retrieveProjectConfigurations as any).mockReturnValueOnce( | ||
Promise.reject( | ||
new ProjectGraphError( | ||
[ | ||
new ProjectsWithNoNameError([], { | ||
project1: { root: 'root1' }, | ||
}), | ||
], | ||
undefined, | ||
undefined | ||
) | ||
) | ||
); | ||
const result = await checkCompatibleWithPlugins(); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should return incompatible plugin with excluded files if error is AggregateCreateNodesError', async () => { | ||
(retrieveProjectConfigurations as any).mockReturnValueOnce( | ||
Promise.reject( | ||
new ProjectGraphError( | ||
[ | ||
new AggregateCreateNodesError( | ||
[ | ||
['file1', undefined], | ||
['file2', undefined], | ||
], | ||
[], | ||
0 | ||
), | ||
], | ||
undefined, | ||
undefined | ||
) | ||
) | ||
); | ||
const result = await checkCompatibleWithPlugins(); | ||
expect(result).toEqual({ 0: new Set(['file1', 'file2']) }); | ||
}); | ||
|
||
it('should return true if error is MergeNodesError', async () => { | ||
(retrieveProjectConfigurations as any).mockReturnValueOnce( | ||
Promise.reject( | ||
new ProjectGraphError( | ||
[ | ||
new MergeNodesError({ | ||
file: 'file2', | ||
pluginName: 'plugin2', | ||
error: new Error(), | ||
pluginIndex: 1, | ||
}), | ||
], | ||
undefined, | ||
undefined | ||
) | ||
) | ||
); | ||
const result = await checkCompatibleWithPlugins(); | ||
expect(result).toEqual({ 1: new Set(['file2']) }); | ||
}); | ||
|
||
it('should handle multiple errors', async () => { | ||
(retrieveProjectConfigurations as any).mockReturnValueOnce( | ||
Promise.reject( | ||
new ProjectGraphError( | ||
[ | ||
new ProjectsWithNoNameError([], { | ||
project1: { root: 'root1' }, | ||
}), | ||
new AggregateCreateNodesError([], [], 0), | ||
new AggregateCreateNodesError( | ||
[ | ||
['file1', undefined], | ||
['file2', undefined], | ||
], | ||
[], | ||
0 | ||
), | ||
new MergeNodesError({ | ||
file: 'file2', | ||
pluginName: 'plugin2', | ||
error: new Error(), | ||
pluginIndex: 2, | ||
}), | ||
new AggregateCreateNodesError( | ||
[ | ||
['file3', undefined], | ||
['file4', undefined], | ||
], | ||
[], | ||
2 | ||
), | ||
], | ||
undefined, | ||
undefined | ||
) | ||
) | ||
); | ||
const result = await checkCompatibleWithPlugins(); | ||
expect(result).toEqual({ | ||
0: new Set(['file1', 'file2']), | ||
2: new Set(['file2', 'file3', 'file4']), | ||
}); | ||
}); | ||
}); |
118 changes: 118 additions & 0 deletions
118
packages/nx/src/command-line/init/implementation/check-compatible-with-plugins.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { existsSync } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
import { bold } from 'chalk'; | ||
|
||
import { NxJsonConfiguration } from '../../../config/nx-json'; | ||
import { | ||
isAggregateCreateNodesError, | ||
isMergeNodesError, | ||
ProjectGraphError, | ||
} from '../../../project-graph/error-types'; | ||
import { workspaceRoot } from '../../../utils/workspace-root'; | ||
import { readJsonFile, writeJsonFile } from '../../../utils/fileutils'; | ||
import { output } from '../../../utils/output'; | ||
import { createProjectGraphAsync } from '../../../project-graph/project-graph'; | ||
|
||
/** | ||
* This function checks if the imported project is compatible with the plugins. | ||
* @returns a map of plugin names to files that are incompatible with the plugins | ||
*/ | ||
export async function checkCompatibleWithPlugins(): Promise<{ | ||
[pluginName: string]: Set<string>; | ||
}> { | ||
let pluginToExcludeFiles: { | ||
[pluginIndex: number]: Set<string>; | ||
} = {}; | ||
try { | ||
await createProjectGraphAsync(); | ||
} catch (projectGraphError) { | ||
if (projectGraphError instanceof ProjectGraphError) { | ||
projectGraphError.getErrors()?.forEach((error) => { | ||
const { pluginIndex, excludeFiles } = | ||
findPluginAndFilesWithError(error) ?? {}; | ||
if (pluginIndex === undefined || !excludeFiles?.length) { | ||
return; | ||
} | ||
pluginToExcludeFiles[pluginIndex] ??= new Set(); | ||
excludeFiles.forEach((file) => | ||
pluginToExcludeFiles[pluginIndex].add(file) | ||
); | ||
}); | ||
} | ||
} | ||
return pluginToExcludeFiles; | ||
} | ||
|
||
/** | ||
* This function finds the plugin name and files that caused the error. | ||
* @param error the error to find the plugin name and files for | ||
* @returns pluginName and excludeFiles if found, otherwise undefined | ||
*/ | ||
function findPluginAndFilesWithError( | ||
error: any | ||
): { pluginIndex: number; excludeFiles: string[] } | undefined { | ||
let pluginIndex: number | undefined; | ||
let excludeFiles: string[] = []; | ||
if (isAggregateCreateNodesError(error)) { | ||
pluginIndex = error.pluginIndex; | ||
excludeFiles = error.errors?.map((error) => error?.[0]) ?? []; | ||
} else if (isMergeNodesError(error)) { | ||
pluginIndex = error.pluginIndex; | ||
excludeFiles = [error.file]; | ||
} | ||
excludeFiles = excludeFiles.filter(Boolean); | ||
return { | ||
pluginIndex, | ||
excludeFiles, | ||
}; | ||
} | ||
|
||
/** | ||
* This function updates the plugins in the nx.json file with the given plugin names and files to exclude. | ||
*/ | ||
export function updatePluginsInNxJson( | ||
root: string = workspaceRoot, | ||
pluginToExcludeFiles: { | ||
[pluginIndex: number]: Set<string>; | ||
} | ||
): void { | ||
const nxJsonPath = join(root, 'nx.json'); | ||
if (!existsSync(nxJsonPath)) { | ||
return; | ||
} | ||
let nxJson: NxJsonConfiguration; | ||
try { | ||
nxJson = readJsonFile<NxJsonConfiguration>(nxJsonPath); | ||
} catch { | ||
// If there is an error reading the nx.json file, no need to update it | ||
return; | ||
} | ||
if (!Object.keys(pluginToExcludeFiles)?.length || !nxJson?.plugins?.length) { | ||
return; | ||
} | ||
Object.entries(pluginToExcludeFiles).forEach( | ||
([pluginIndex, excludeFiles]) => { | ||
let plugin = nxJson.plugins[pluginIndex]; | ||
if (!plugin || excludeFiles.size === 0) { | ||
return; | ||
} | ||
if (typeof plugin === 'string') { | ||
plugin = { plugin }; | ||
} | ||
output.warn({ | ||
title: `Incompatible files found for no. ${ | ||
parseInt(pluginIndex) + 1 | ||
} plugin in nx.json`, | ||
bodyLines: [ | ||
`Added the following files to the exclude list for ${plugin.plugin}:`, | ||
...Array.from(excludeFiles).map((file) => ` - ${bold(file)}`), | ||
], | ||
}); | ||
|
||
(plugin.exclude ?? []).forEach((e) => excludeFiles.add(e)); | ||
plugin.exclude = Array.from(excludeFiles); | ||
nxJson.plugins[pluginIndex] = plugin; | ||
} | ||
); | ||
writeJsonFile(nxJsonPath, nxJson); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.