Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular Library Generator not working with non simpleName #29508

Open
1 of 4 tasks
NWenzl opened this issue Jan 3, 2025 · 1 comment
Open
1 of 4 tasks

Angular Library Generator not working with non simpleName #29508

NWenzl opened this issue Jan 3, 2025 · 1 comment

Comments

@NWenzl
Copy link

NWenzl commented Jan 3, 2025

Current Behavior

When generating a library with the "@nx/angular:library" generator and using a directory together with simpleName=false the Library Name remains simple, although the documentation suggests the opposite: https://nx.dev/nx-api/angular/generators/library#simplename

nx g @nx/angular:library --directory=libs/path --name=test --standalone=false --simpleName=false --dry-run

Result

CREATE libs/path/project.json
CREATE libs/path/README.md
CREATE libs/path/tsconfig.json
CREATE libs/path/tsconfig.lib.json
CREATE libs/path/src/index.ts
CREATE libs/path/src/lib/test.module.ts
CREATE libs/path/jest.config.ts
CREATE libs/path/src/test-setup.ts
CREATE libs/path/tsconfig.spec.json
CREATE libs/path/.eslintrc.json
UPDATE tsconfig.base.json

Expected Behavior

I would expect the module to have the name "path-test.module.ts".
nx g @nx/angular:library --directory=libs/path --name=test --standalone=false --simpleName=false --dry-run

Result

CREATE libs/path/project.json
CREATE libs/path/README.md
CREATE libs/path/tsconfig.json
CREATE libs/path/tsconfig.lib.json
CREATE libs/path/src/index.ts
CREATE libs/path/src/lib/path-test.module.ts
CREATE libs/path/jest.config.ts
CREATE libs/path/src/test-setup.ts
CREATE libs/path/tsconfig.spec.json
CREATE libs/path/.eslintrc.json
UPDATE tsconfig.base.json

GitHub Repo

No response

Steps to Reproduce

  1. Execute nx g @nx/angular:library --directory=libs/path --name=test --standalone=false --simpleName=false --dry-run

Nx Report

Node           : 22.12.0
OS             : darwin-arm64
Native Target  : aarch64-macos
yarn           : 1.22.19

nx                     : 20.3.0
@nx/js                 : 20.3.0
@nx/jest               : 20.3.0
@nx/eslint             : 20.3.0
@nx/workspace          : 20.3.0
@nx/angular            : 20.3.0
@nx/cypress            : 20.3.0
@nx/devkit             : 20.3.0
@nx/eslint-plugin      : 20.3.0
@nx/module-federation  : 20.3.0
@nx/plugin             : 20.3.0
@nx/storybook          : 20.3.0
@nx/web                : 20.3.0
@nx/webpack            : 20.3.0
typescript             : 5.6.3
---------------------------------------
Community plugins:
@compodoc/compodoc                     : 1.1.18
@ngrx/component-store                  : 19.0.0
@ngrx/effects                          : 19.0.0
@ngrx/entity                           : 19.0.0
@ngrx/operators                        : 19.0.0
@ngrx/router-store                     : 19.0.0
@ngrx/schematics                       : 19.0.0
@ngrx/store                            : 19.0.0
@ngrx/store-devtools                   : 19.0.0
@storybook/angular                     : 8.4.7
---------------------------------------
Local workspace plugins:
         @nw/workspace-plugin

Failure Logs

Package Manager Version

yarn version 1.22.19

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

I debugged the issue and the name is passed to the generator as libFileName:
https://github.com/nrwl/nx/blob/master/packages/angular/src/generators/library/files/ng-module/src/lib/libFileName.module.ts__tpl__

This is passed from createFiles-Method called here:
https://github.com/nrwl/nx/blob/master/packages/angular/src/generators/library/library.ts#L78

The methods utilizes options.libraryOptions.fileName
https://github.com/nrwl/nx/blob/master/packages/angular/src/generators/library/lib/create-files.ts#L35

This is created in the normalizeOptions Method called here
https://github.com/nrwl/nx/blob/master/packages/angular/src/generators/library/lib/create-files.ts#L35

This gets the fileName using a method called determineProjectNameAndRootOptions to determine the the fileName based on the option passed to the schematic:

 const fileName = options.simpleName
    ? projectNames.projectSimpleName
    : projectNames.projectFileName;

https://github.com/nrwl/nx/blob/master/packages/angular/src/generators/library/lib/normalize-options.ts#L45

However, this methods only returns a differing projectFileName when the name starts with an @.

if (name.startsWith('@')) {
    const [_scope, ...rest] = name.split('/');
    projectFileName = rest.join('-');
    projectSimpleName = rest.pop();
  } else {
    projectSimpleName = name;
    projectFileName = name;
  }

https://github.com/nrwl/nx/blob/master/packages/devkit/src/generators/project-name-and-root-utils.ts#L62

This seems to be the root cause, but I don't understand if this is working as designed and I need to change my parameters, but trying different variations (like adding an @ to the name or directory) of the generator did not vary anything.

@NWenzl
Copy link
Author

NWenzl commented Jan 3, 2025

Digging further, I discovered that the following combination of name, directory and importPath works for me:
nx g @nx/angular:library --directory=libs/path/test --name=path-test --importPath=@nw/path/test --standalone=false --simpleName=false.

I found the documentation here as an example for "Nested Folder & Import": https://nx.dev/nx-api/angular/generators/library.

I still don't understand if my previous approach should have worked, so feel free to close this issue if the described behavior is intentional.

Maybe its a documentation issue, since the description for simpleName clearly states that the directory is included in the fileName https://nx.dev/nx-api/angular/generators/library#simplename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant