fix(linter): handle ng-package.json file with no lib.entryFile in @nx/enforce-module-boundaries rule (#31360)

## 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`](22a7ba1979/src/ng-entrypoint.schema.json (L20)).

Co-authored-by: Miroslav Jonaš <missing.manual@gmail.com>
This commit is contained in:
Leosvel Pérez Espinosa 2025-05-28 09:45:32 +02:00 committed by GitHub
parent 2572455c9c
commit c7a9c71c07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 15 deletions

View File

@ -448,21 +448,20 @@ describe('is terminal run', () => {
}); });
describe('isAngularSecondaryEntrypoint', () => { describe('isAngularSecondaryEntrypoint', () => {
beforeEach(() => { const tsConfig = {
const tsConfig = { compilerOptions: {
compilerOptions: { baseUrl: '.',
baseUrl: '.', resolveJsonModule: true,
resolveJsonModule: true, paths: {
paths: { '@project/standard': ['libs/standard/src/index.ts'],
'@project/standard': ['libs/standard/src/index.ts'], '@project/standard/secondary': ['libs/standard/secondary/src/index.ts'],
'@project/standard/secondary': [ '@project/features': ['libs/features/index.ts'],
'libs/standard/secondary/src/index.ts', '@project/features/*': ['libs/features/*/random/folder/api.ts'],
],
'@project/features': ['libs/features/index.ts'],
'@project/features/*': ['libs/features/*/random/folder/api.ts'],
},
}, },
}; },
};
beforeEach(() => {
const fsJson = { const fsJson = {
'tsconfig.base.json': JSON.stringify(tsConfig), 'tsconfig.base.json': JSON.stringify(tsConfig),
'libs/standard/package.json': '{ "version": "0.0.0" }', 'libs/standard/package.json': '{ "version": "0.0.0" }',
@ -549,6 +548,47 @@ describe('isAngularSecondaryEntrypoint', () => {
) )
).toBe(true); ).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', () => { describe('hasNoneOfTheseTags', () => {

View File

@ -556,7 +556,8 @@ function getEntryPoint(file: string, projectRoot: string): string {
); );
if (ngPackageContent) { if (ngPackageContent) {
// https://github.com/ng-packagr/ng-packagr/blob/23c718d04eea85e015b4c261310b7bd0c39e5311/src/ng-package.schema.json#L54 // 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); return joinPathFragments(parent, entryFile);
} }
parent = joinPathFragments(parent, '../'); parent = joinPathFragments(parent, '../');