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

fix(@angular-devkit/build-angular): Set chunk names explicitly #25511

Merged
merged 1 commit into from
Jul 14, 2023

Conversation

gultyayev
Copy link
Contributor

@gultyayev gultyayev commented Jul 7, 2023

PR Checklist

Please check to confirm your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Currently, chunk names are determined by ESBuild. Some libraries may resolve to a name that is not in a format of chunk.hash.mjs. This causes Jest to run over the lib files and fail as it contains no test suite.

Fixes: #25189

What is the new behavior?

The change sets chunk name pattern explicitly so that all chunks are exluded by pattern from running tests over.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I didn't figure out how this could be covered by tests. I did run a local build & installation and verified on a sample project that @azure/msal-angular no longer creates a "StandardController" file. Instead, it's named as chunk... and hence is excluded from tests.

@GinMitch
Copy link

GinMitch commented Jul 7, 2023

This seems like a good idea, but can you please provide some context?

@gultyayev
Copy link
Contributor Author

This seems like a good idea, but can you please provide some context?

Mostly, the issue comes down to ESBuild generating chunk names as chunk.hash.mjs in 99% of the cases. Sometimes, however it generates a chunk with a different name. For me it was StandardController.mjs for imported @azure/msal-angular library (v3.0.0-alpha.0).

When running tests with Jest, the builder is using pattern **/*.mjs to literally pick up all the mjs files. It does have exclusion for chunk files. Because of that, when running tests with Jest it fails, because StandardController.mjs is included to run tests over and it doesn't have any.

First idea was to simply change the pattern to .spec.mjs, but as it was highlighted by @dgp1130 users may change the tests name pattern. Therefore, it's easier to make all chunks follow the name pattern.

For build code it doesn't make any difference. For tests it makes sure that all chunks have the same name pattern and are excluded from the tests.

@dgp1130 dgp1130 requested review from dgp1130 and clydin July 7, 2023 22:17
@dgp1130 dgp1130 added the target: patch This PR is targeted for the next patch release label Jul 7, 2023
Copy link
Collaborator

@dgp1130 dgp1130 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gultyayev for looking into this, I haven't had a chance to dig in yet. I think this is probably a reasonable solution, though it looks like one of the tests is failing, as it is expecting the app to print the lazy loaded file's name which is no longer true. Maybe we could search for a more generic "rebuild done" string like "Application bundle generation complete"?

Also making this change in builder-context.ts means that it will apply to all ESBuild executions, not just Jest. I think that's probably ok, but I wonder if there might be some unintended consequences from it. @clydin, do you have any concerns with that?

@gultyayev gultyayev force-pushed the fix-tests branch 2 times, most recently from 570ce59 to 4c14f0c Compare July 8, 2023 11:38
@gultyayev
Copy link
Contributor Author

Also making this change in builder-context.ts means that it will apply to all ESBuild executions, not just Jest.

TBH I can't think of any possible shortcomings from this. The names are always generated and hence cannot be relied on. Furthermore, current behavior is to create chunks with chunk-hsh.mjs name. So, technically we are fixing those limited cases when the name of the chunk is different.

It's also possible to pass this change for Jest builder only. I didn't locate the place though. Not sure if it would make much sense to do.

Copy link
Collaborator

@alan-agius4 alan-agius4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of setting the option in the builder context which only contains infrastructure related options. It would be better to follow the existing pattern for similar options and set it in

function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): BuildOptions {
or even better

@alan-agius4 alan-agius4 added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews target: minor This PR is targeted for the next minor release target: patch This PR is targeted for the next patch release and removed target: patch This PR is targeted for the next patch release target: minor This PR is targeted for the next minor release labels Jul 8, 2023
@gultyayev
Copy link
Contributor Author

Instead of setting the option in the builder context which only contains infrastructure related options. It would be better to follow the existing pattern for similar options and set it in

function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): BuildOptions {

or even better

Did the first suggested change. The 2nd one doesn't work.

@gultyayev
Copy link
Contributor Author

One concern that may be is that chunk names become obscure. Current ng build outputs lazy loaded chunks as component-name.component-[hash].js. With the change it becomes just chunk-[hash] which may be undesirable.

@dgp1130
Copy link
Collaborator

dgp1130 commented Jul 13, 2023

@gultyayev, thanks for your patience. Using chunk-[hash] everywhere should be fine. It does make chunks a little harder to debug, however there is no general 1:1 correlation between chunks and source files, so preserving the name is maybe setting a bad expectation in the first place.

Just the one comment from @clydin about making sure a chunk is created on the rebuild and this should be good to go.

Explicitly set chunk name pattern to exclude them from Jest run
@clydin clydin removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jul 14, 2023
@dgp1130 dgp1130 added the action: merge The PR is ready for merge by the caretaker label Jul 14, 2023
@dgp1130
Copy link
Collaborator

dgp1130 commented Jul 14, 2023

Thanks! This looks good to merge.

@clydin clydin added target: minor This PR is targeted for the next minor release and removed target: patch This PR is targeted for the next patch release labels Jul 14, 2023
@gultyayev
Copy link
Contributor Author

Thank you for guidance.

@clydin clydin merged commit 5048f6e into angular:main Jul 14, 2023
23 checks passed
@gultyayev gultyayev deleted the fix-tests branch July 18, 2023 19:13
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@angular-devkit/build-angular:jest creates invalid test suite
5 participants