angular-cli: Generated angular.json file does not correctly lower case the application name

šŸž Bug report

Command (mark with an x)

  • [x ] new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • xi18n
  • run
  • config
  • help
  • version
  • doc

Is this a regression?

Iā€™m not certain whether this is a regression. I donā€™t recall seeing the issue. No one had reported it to me previously. But that doesnā€™t mean it didnā€™t exist.

Description

When you create a new Angular project using ng new with an upper case or mixed case name, the Angular CLI correctly converts the name to lower case for the keys in the package.json file. It does not convert the name to lower case for the keys in the angular.json file. See the ā€œMinimal Reproductionā€ for screen shots.

šŸ”¬ Minimal Reproduction

ng new Hello-World

Resulting package.json file correctly lower cases the project name: image

Resulting angular.json file does not lower case the project name, resulting in an error (see below): image

Notice the resulting error: image

šŸ”„ Exception or Error





Property Hello-World is not allowed

šŸŒ Your Environment




Angular CLI: 9.1.3
Node: 12.16.1
OS: win32 x64

Angular: 9.1.4
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.3
@angular-devkit/build-angular     0.901.3
@angular-devkit/build-optimizer   0.901.3
@angular-devkit/build-webpack     0.901.3
@angular-devkit/core              9.1.3
@angular-devkit/schematics        9.1.3
@angular/cli                      9.1.3
@ngtools/webpack                  9.1.3
@schematics/angular               9.1.3
@schematics/update                0.901.3
rxjs                              6.5.5
typescript                        3.8.3
webpack                           4.42.0

Anything else relevant?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 18 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I am claiming this issue. I have started to work on the fix.

Thank you.

Iā€™d like to see #2 as it is more friendly for beginners and for those used to being able to use mixed case project names as they can now.

Two alternatives for more consistent behavior would be:

  1. Error on invalid characters which do not match the JSON schema regex
  2. Transform both - the project name and the package name to lower case

The second alternative would be more implicit, but also less surprising for folks used to the partial name transform we do right now only for the package name.

@Jefiozie, project names are valid both in camel case or kebeb case. Therefore weā€™d need to update the above mention RegeExp.

When it comes to project directories, I am inclined towards always dasherizing the project folder name to align with other schematics where the folder name is always dasherized, that being said Itā€™s not a biggy, as long as the schema validation is fixed.

ng g c myCool
CREATE src/app/my-cool/my-cool.component.scss (0 bytes)
CREATE src/app/my-cool/my-cool.component.html (22 bytes)
CREATE src/app/my-cool/my-cool.component.spec.ts (627 bytes)
CREATE src/app/my-cool/my-cool.component.ts (279 bytes)
UPDATE src/app/app.module.ts (474 bytes)

Personally I never use a - in my names and just use fooBox for my package names and project names

This is so also personal preference, I always use kebab casing when it comes to file and directory naming.

Hi all,

Hello-World is a perfectly valid project name. The problem here is that the schema used by the IDE is not accounting for this. https://github.com/angular/angular-cli/blob/a98c71dbf241499532c49216795c13d40c92015b/packages/angular/cli/lib/config/schema.json#L30

The above schema also doesnā€™t account for other valid project names such as @foo/bar.

The above doesnā€™t match the actual RegExp used to valid project names https://github.com/angular/angular-cli/blob/a98c71dbf241499532c49216795c13d40c92015b/packages/schematics/angular/utility/validation.ts#L33

Also there seems to be a misalignment between the library and application schematic, where the library folder is always dasherized, while in the application itā€™s not.

ng g lib fooBar
CREATE projects/foo-bar/README.md (996 bytes)
...
ng g app fooBox
CREATE projects/fooBox/.browserslistrc (853 bytes)
...

@Splaktar Thank you for the pointers. I am leveraging strings.dasherize() method to transform options.name as lower casing the name wont be sufficed as camelcase is not supported as well. This will match the name as same as in package.json.

In addition to the change in schematics/angular/application/index.ts i was validating if the name transformation needs to be applied for the corresponding individual rules ng-new, app-shell etc. Which i have not found the answers why i should not make the changes in rules.

The application schematic adds the project to the workspace using options.name https://github.com/angular/angular-cli/blob/1d2e7bba07e1477fe3a551314ab93ad7aaa63420/packages/schematics/angular/application/index.ts#L290-L294

Thereā€™s a validateProjectName() function, but it doesnā€™t change the name.

You may want to apply the appropriate string transform function to options.name just after validateProjectName() is called in the default function since options.name feeds into so many other configurations.