From c8960b733f63a8cb39f68982b534b8439343b9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Wed, 19 Apr 2023 17:13:10 +0200 Subject: [PATCH] fix(linter): don't confuse buildable libs for secondary ng entry point (#16367) --- .../src/rules/enforce-module-boundaries.ts | 6 ++- .../src/utils/runtime-lint-utils.spec.ts | 41 ++++++++++++++++--- .../src/utils/runtime-lint-utils.ts | 11 +++-- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts index 9da65a2cc84aa..0cc55a70c229c 100644 --- a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts +++ b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts @@ -317,7 +317,11 @@ export default createESLintRule({ if ( !allowCircularSelfDependency && !isRelativePath(imp) && - !isAngularSecondaryEntrypoint(imp, sourceFilePath) + !isAngularSecondaryEntrypoint( + imp, + sourceFilePath, + sourceProject.data.root + ) ) { context.report({ node, diff --git a/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts b/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts index 6952b03cecccd..2f9c20d4ebfa6 100644 --- a/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts +++ b/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts @@ -401,6 +401,7 @@ describe('isAngularSecondaryEntrypoint', () => { ], '@project/features': ['libs/features/src/index.ts'], '@project/features/*': ['libs/features/*/random/folder/api.ts'], + '@project/buildable': ['libs/buildable/src/index.ts'], }, }, }; @@ -421,25 +422,55 @@ describe('isAngularSecondaryEntrypoint', () => { lib: { entryFile: 'random/folder/api.ts' }, }), 'libs/features/secondary/random/folder/api.ts': 'const bla = "foo"', + 'libs/buildable/ng-package.json': JSON.stringify({ + lib: { entryFile: 'src/index.ts' }, + }), }; vol.fromJSON(fsJson, '/root'); }); it('should return true for secondary entrypoints', () => { expect( - isAngularSecondaryEntrypoint('@project/standard', 'apps/app.ts') + isAngularSecondaryEntrypoint( + '@project/standard', + 'apps/app.ts', + 'libs/standard' + ) ).toBe(false); expect( - isAngularSecondaryEntrypoint('@project/standard/secondary', 'apps/app.ts') + isAngularSecondaryEntrypoint( + '@project/standard/secondary', + 'apps/app.ts', + 'libs/standard' + ) ).toBe(true); expect( - isAngularSecondaryEntrypoint('@project/standard/tertiary', 'apps/app.ts') + isAngularSecondaryEntrypoint( + '@project/standard/tertiary', + 'apps/app.ts', + 'libs/standard' + ) ).toBe(true); expect( - isAngularSecondaryEntrypoint('@project/features', 'apps/app.ts') + isAngularSecondaryEntrypoint( + '@project/features', + 'apps/app.ts', + 'libs/features' + ) ).toBe(false); expect( - isAngularSecondaryEntrypoint('@project/features/secondary', 'apps/app.ts') + isAngularSecondaryEntrypoint( + '@project/features/secondary', + 'apps/app.ts', + 'libs/features' + ) ).toBe(true); + expect( + isAngularSecondaryEntrypoint( + '@project/buildable', + 'apps/app.ts', + 'libs/buildable' + ) + ).toBe(false); }); }); diff --git a/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts b/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts index de69104bf72a9..e34403d7f7825 100644 --- a/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts +++ b/packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts @@ -447,7 +447,8 @@ export function groupImports( */ export function isAngularSecondaryEntrypoint( importExpr: string, - filePath: string + filePath: string, + projectRoot: string ): boolean { const resolvedModule = resolveModuleByImport( importExpr, @@ -455,12 +456,14 @@ export function isAngularSecondaryEntrypoint( join(workspaceRoot, getRootTsConfigFileName()) ); - return !!resolvedModule && fileIsSecondaryEntryPoint(resolvedModule); + return ( + !!resolvedModule && fileIsSecondaryEntryPoint(resolvedModule, projectRoot) + ); } -function fileIsSecondaryEntryPoint(file: string): boolean { +function fileIsSecondaryEntryPoint(file: string, projectRoot: string): boolean { let parent = joinPathFragments(file, '../'); - while (parent !== './') { + while (parent !== `${projectRoot}/`) { // we need to find closest existing ng-package.json // in order to determine if the file matches the secondary entry point const ngPackageContent = readFileIfExisting(