diff --git a/e2e/linter/src/linter.test.ts b/e2e/linter/src/linter.test.ts index 751e8c0544c4a..304f8eafe9c66 100644 --- a/e2e/linter/src/linter.test.ts +++ b/e2e/linter/src/linter.test.ts @@ -232,7 +232,7 @@ describe('Linter', () => { import '@${projScope}/${invalidtaglib}'; import '@${projScope}/${validtaglib}'; - const s = {loadChildren: '@${projScope}/${lazylib}'}; + const s = {loadChildren: '@secondScope/${lazylib}'}; ` ); diff --git a/packages/nx/src/plugins/js/package-json/create-package-json.ts b/packages/nx/src/plugins/js/package-json/create-package-json.ts index 7ac6a9d5ccdac..954657ff0b813 100644 --- a/packages/nx/src/plugins/js/package-json/create-package-json.ts +++ b/packages/nx/src/plugins/js/package-json/create-package-json.ts @@ -234,8 +234,6 @@ function findAllNpmDeps( seen, dependencyPatterns ); - } else { - throw new Error(`Could not find ${dep} in the project graph.`); } } } diff --git a/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.ts b/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.ts index f2ac0967445f1..385379fb2d8b3 100644 --- a/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.ts +++ b/packages/nx/src/plugins/js/project-graph/build-dependencies/explicit-project-dependencies.ts @@ -40,19 +40,28 @@ export function buildExplicitTypeScriptDependencies( importExpr, f.file ); + let targetProjectName; if (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 return; } - res.push({ - sourceProjectName: source, - targetProjectName: target, - sourceProjectFile: f.file, - type, - }); + targetProjectName = target; + } else { + // treat all unknowns as npm packages, they can be eiher + // - mistyped local import, which has to be fixed manually + // - 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, + }); } ); }); diff --git a/packages/nx/src/project-graph/project-graph-builder.spec.ts b/packages/nx/src/project-graph/project-graph-builder.spec.ts index 1c195d11f139f..5da57d8ae5ce8 100644 --- a/packages/nx/src/project-graph/project-graph-builder.spec.ts +++ b/packages/nx/src/project-graph/project-graph-builder.spec.ts @@ -33,6 +33,8 @@ describe('ProjectGraphBuilder', () => { expect(() => builder.addImplicitDependency('source', 'invalid-target') ).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 builder.addDynamicDependency('source', 'source', 'source/index.ts'); @@ -91,13 +93,6 @@ describe('ProjectGraphBuilder', () => { 'target' ) ).toThrowError(); - expect(() => - builder.addExplicitDependency( - 'source', - 'source/index.ts', - 'invalid-target' - ) - ).toThrowError(); expect(() => builder.addExplicitDependency( 'source', diff --git a/packages/nx/src/project-graph/project-graph-builder.ts b/packages/nx/src/project-graph/project-graph-builder.ts index 36773c22966f3..b651fc8071688 100644 --- a/packages/nx/src/project-graph/project-graph-builder.ts +++ b/packages/nx/src/project-graph/project-graph-builder.ts @@ -175,41 +175,11 @@ export class ProjectGraphBuilder { sourceProjectFile: string, targetProjectName: string ): void { - if (sourceProjectName === targetProjectName) { - return; - } - const source = this.graph.nodes[sourceProjectName]; - 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 + this.addStaticDependency( + sourceProjectName, + targetProjectName, + 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); 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()) { if ( !alreadySetTargetProjects.has(targetProject) || @@ -268,7 +245,8 @@ export class ProjectGraphBuilder { } if ( !this.graph.nodes[targetProjectName] && - !this.graph.externalNodes[targetProjectName] + !this.graph.externalNodes[targetProjectName] && + !sourceProjectFile ) { throw new Error(`Target project does not exist: ${targetProjectName}`); }