fix(core): keep all explicit dependencies in the graph (#16576)

This commit is contained in:
Miroslav Jonaš 2023-05-09 17:39:48 +02:00 committed by GitHub
parent 50ad516278
commit ae5cdcb86e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 51 deletions

View File

@ -232,7 +232,7 @@ describe('Linter', () => {
import '@${projScope}/${invalidtaglib}'; import '@${projScope}/${invalidtaglib}';
import '@${projScope}/${validtaglib}'; import '@${projScope}/${validtaglib}';
const s = {loadChildren: '@${projScope}/${lazylib}'}; const s = {loadChildren: '@secondScope/${lazylib}'};
` `
); );

View File

@ -234,8 +234,6 @@ function findAllNpmDeps(
seen, seen,
dependencyPatterns dependencyPatterns
); );
} else {
throw new Error(`Could not find ${dep} in the project graph.`);
} }
} }
} }

View File

@ -40,19 +40,28 @@ export function buildExplicitTypeScriptDependencies(
importExpr, importExpr,
f.file f.file
); );
let targetProjectName;
if (target) { if (target) {
if (!isRoot(source) && isRoot(target)) { if (!isRoot(source) && isRoot(target)) {
// TODO: These edges technically should be allowed but we need to figure out how to separate config files out from root // TODO: These edges technically should be allowed but we need to figure out how to separate config files out from root
return; return;
} }
res.push({ targetProjectName = target;
sourceProjectName: source, } else {
targetProjectName: target, // treat all unknowns as npm packages, they can be eiher
sourceProjectFile: f.file, // - mistyped local import, which has to be fixed manually
type, // - node internals, which should still be tracked as a dependency
}); // - npm packages, which are not yet installed but should be tracked
targetProjectName = `npm:${importExpr}`;
} }
res.push({
sourceProjectName: source,
targetProjectName,
sourceProjectFile: f.file,
type,
});
} }
); );
}); });

View File

@ -33,6 +33,8 @@ describe('ProjectGraphBuilder', () => {
expect(() => expect(() =>
builder.addImplicitDependency('source', 'invalid-target') builder.addImplicitDependency('source', 'invalid-target')
).toThrowError(); ).toThrowError();
// this should not break, but should not exist in resulting dependencies either
builder.addStaticDependency('source', 'invalid-target', 'source/index.ts');
// ignore the self deps // ignore the self deps
builder.addDynamicDependency('source', 'source', 'source/index.ts'); builder.addDynamicDependency('source', 'source', 'source/index.ts');
@ -91,13 +93,6 @@ describe('ProjectGraphBuilder', () => {
'target' 'target'
) )
).toThrowError(); ).toThrowError();
expect(() =>
builder.addExplicitDependency(
'source',
'source/index.ts',
'invalid-target'
)
).toThrowError();
expect(() => expect(() =>
builder.addExplicitDependency( builder.addExplicitDependency(
'source', 'source',

View File

@ -175,41 +175,11 @@ export class ProjectGraphBuilder {
sourceProjectFile: string, sourceProjectFile: string,
targetProjectName: string targetProjectName: string
): void { ): void {
if (sourceProjectName === targetProjectName) { this.addStaticDependency(
return; sourceProjectName,
} targetProjectName,
const source = this.graph.nodes[sourceProjectName]; sourceProjectFile
if (!source) {
throw new Error(`Source project does not exist: ${sourceProjectName}`);
}
if (
!this.graph.nodes[targetProjectName] &&
!this.graph.externalNodes[targetProjectName]
) {
throw new Error(`Target project does not exist: ${targetProjectName}`);
}
const fileData = source.data.files.find(
(f) => f.file === sourceProjectFile
); );
if (!fileData) {
throw new Error(
`Source project ${sourceProjectName} does not have a file: ${sourceProjectFile}`
);
}
if (!fileData.dependencies) {
fileData.dependencies = [];
}
if (!fileData.dependencies.find((t) => t.target === targetProjectName)) {
fileData.dependencies.push({
target: targetProjectName,
source: sourceProjectName,
type: DependencyType.static,
});
}
} }
/** /**
@ -229,6 +199,13 @@ export class ProjectGraphBuilder {
const fileDeps = this.calculateTargetDepsFromFiles(sourceProject); const fileDeps = this.calculateTargetDepsFromFiles(sourceProject);
for (const [targetProject, types] of fileDeps.entries()) { for (const [targetProject, types] of fileDeps.entries()) {
// only add known nodes
if (
!this.graph.nodes[targetProject] &&
!this.graph.externalNodes[targetProject]
) {
continue;
}
for (const type of types.values()) { for (const type of types.values()) {
if ( if (
!alreadySetTargetProjects.has(targetProject) || !alreadySetTargetProjects.has(targetProject) ||
@ -268,7 +245,8 @@ export class ProjectGraphBuilder {
} }
if ( if (
!this.graph.nodes[targetProjectName] && !this.graph.nodes[targetProjectName] &&
!this.graph.externalNodes[targetProjectName] !this.graph.externalNodes[targetProjectName] &&
!sourceProjectFile
) { ) {
throw new Error(`Target project does not exist: ${targetProjectName}`); throw new Error(`Target project does not exist: ${targetProjectName}`);
} }