From c7a9c71c072cea3d06813c18688e73a973f412ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Wed, 28 May 2025 09:45:32 +0200 Subject: [PATCH] fix(linter): handle `ng-package.json` file with no `lib.entryFile` in `@nx/enforce-module-boundaries` rule (#31360) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Current Behavior When an `ng-package.json` file of an Angular library secondary entry point does not specify `lib.entryFile`, the `@nx/enforce-module-boundaries` rule throws an error. The `ng-package.json` file of an Angular secondary entry point can be as simple as `{}`, but it would cause the rule to throw an error. ## Expected Behavior The `@nx/enforce-module-boundaries` rule should correctly handle an `ng-package.json` file of an Angular library secondary entry point that does not specify `lib.entryFile`. The property should [default to `src/public_api.ts`](https://github.com/ng-packagr/ng-packagr/blob/22a7ba1979f117a12901dca195187948c1fd022d/src/ng-entrypoint.schema.json#L20). Co-authored-by: Miroslav Jonaš --- .../src/utils/runtime-lint-utils.spec.ts | 68 +++++++++++++++---- .../src/utils/runtime-lint-utils.ts | 3 +- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts b/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts index 97dfd5f759..2d40f2bf1c 100644 --- a/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts +++ b/packages/eslint-plugin/src/utils/runtime-lint-utils.spec.ts @@ -448,21 +448,20 @@ describe('is terminal run', () => { }); describe('isAngularSecondaryEntrypoint', () => { - beforeEach(() => { - const tsConfig = { - compilerOptions: { - baseUrl: '.', - resolveJsonModule: true, - paths: { - '@project/standard': ['libs/standard/src/index.ts'], - '@project/standard/secondary': [ - 'libs/standard/secondary/src/index.ts', - ], - '@project/features': ['libs/features/index.ts'], - '@project/features/*': ['libs/features/*/random/folder/api.ts'], - }, + const tsConfig = { + compilerOptions: { + baseUrl: '.', + resolveJsonModule: true, + paths: { + '@project/standard': ['libs/standard/src/index.ts'], + '@project/standard/secondary': ['libs/standard/secondary/src/index.ts'], + '@project/features': ['libs/features/index.ts'], + '@project/features/*': ['libs/features/*/random/folder/api.ts'], }, - }; + }, + }; + + beforeEach(() => { const fsJson = { 'tsconfig.base.json': JSON.stringify(tsConfig), 'libs/standard/package.json': '{ "version": "0.0.0" }', @@ -549,6 +548,47 @@ describe('isAngularSecondaryEntrypoint', () => { ) ).toBe(true); }); + + it('should handle secondary entry points with no entry file defined in ng-package.json', () => { + vol.writeFileSync('/root/libs/standard/secondary/ng-package.json', '{}'); + vol.renameSync( + '/root/libs/standard/secondary/src/index.ts', + '/root/libs/standard/secondary/src/public_api.ts' + ); + const updatedTsConfig = { + ...tsConfig, + compilerOptions: { + ...tsConfig.compilerOptions, + paths: { + ...tsConfig.compilerOptions.paths, + '@project/standard/secondary': [ + 'libs/standard/secondary/src/public_api.ts', + ], + }, + }, + }; + vol.writeFileSync( + '/root/tsconfig.base.json', + JSON.stringify(updatedTsConfig) + ); + + // main + expect( + belongsToDifferentEntryPoint( + '@project/standard', + 'libs/standard/secondary/src/subfolder/index.ts', + 'libs/standard' + ) + ).toBe(true); + // secondary + expect( + belongsToDifferentEntryPoint( + '@project/standard/secondary', + 'libs/standard/src/subfolder/index.ts', + 'libs/standard' + ) + ).toBe(true); + }); }); describe('hasNoneOfTheseTags', () => { diff --git a/packages/eslint-plugin/src/utils/runtime-lint-utils.ts b/packages/eslint-plugin/src/utils/runtime-lint-utils.ts index ce960836ba..f5b38fddc7 100644 --- a/packages/eslint-plugin/src/utils/runtime-lint-utils.ts +++ b/packages/eslint-plugin/src/utils/runtime-lint-utils.ts @@ -556,7 +556,8 @@ function getEntryPoint(file: string, projectRoot: string): string { ); if (ngPackageContent) { // https://github.com/ng-packagr/ng-packagr/blob/23c718d04eea85e015b4c261310b7bd0c39e5311/src/ng-package.schema.json#L54 - const entryFile = parseJson(ngPackageContent)?.lib?.entryFile; + const entryFile = + parseJson(ngPackageContent)?.lib?.entryFile ?? 'src/public_api.ts'; return joinPathFragments(parent, entryFile); } parent = joinPathFragments(parent, '../');