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): generate different content hashes for scripts which are changed during the optimization phase #23531

Merged
merged 1 commit into from
Jul 12, 2022

Conversation

martinfrancois
Copy link
Contributor

Instead of generating the content hash based on the content of scripts BEFORE the optimization phase,
the content hash is generated AFTER the optimization phase.

Prevents caching issues where browsers block execution of scripts due to the integrity hash not matching
with the cached script in case of a script being optimized differently than in a previous build,
where it would previously result in the same content hash.

Fixes #22906

Test case is based on the reproduction example as specified in #22906 (thanks!).

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.

Thanks for your contribution.

Couple of small changes.

@alan-agius4 alan-agius4 added the target: patch This PR is targeted for the next patch release label Jul 11, 2022
@martinfrancois
Copy link
Contributor Author

martinfrancois commented Jul 11, 2022

Thanks for your contribution.

Couple of small changes.

@alan-agius4 You're welcome, thanks a lot for the quick and thorough review! I agree with all of your suggestions and addressed all of your comments and pushed the changes, can you please have a look again?

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.

LGTM. one small NIT

Thanks for this.

@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Jul 11, 2022
@angular-robot angular-robot bot requested a review from alan-agius4 July 11, 2022 12:36
@martinfrancois
Copy link
Contributor Author

@alan-agius4 You're welcome, thanks as well for the quick handling! Addressed your nit now and pushed it.

@alan-agius4 alan-agius4 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 11, 2022
@alan-agius4 alan-agius4 removed the action: merge The PR is ready for merge by the caretaker label Jul 11, 2022
@alan-agius4
Copy link
Collaborator

@martinfrancois, it looks like

it('uglifies, uses sourcemaps, and adds hashes', async () => {
host.writeMultipleFiles(scripts);
const { files } = await browserBuild(architect, host, target, {
optimization: true,
sourceMap: true,
outputHashing: 'all',
scripts: getScriptsOption(),
} as {});
const fileNames = Object.keys(files);
const scriptsBundle = fileNames.find((n) => /scripts\.[0-9a-f]{16}\.js/.test(n));
expect(scriptsBundle).toBeTruthy();
expect(await files[scriptsBundle || '']).toMatch('var number=2;');
expect(fileNames.some((n) => /scripts\.[0-9a-f]{16}\.js\.map/.test(n))).toBeTruthy();
expect(fileNames.some((n) => /renamed-script\.[0-9a-f]{16}\.js/.test(n))).toBeTruthy();
expect(fileNames.some((n) => /renamed-script\.[0-9a-f]{16}\.js\.map/.test(n))).toBeTruthy();
expect(fileNames.some((n) => /script\.[0-9a-f]{16}\.js/.test(n))).toBeTruthy();
expect(await files['lazy-script.js']).not.toBeUndefined();
expect(await files['lazy-script.js.map']).not.toBeUndefined();
expect(await files['renamed-lazy-script.js']).not.toBeUndefined();
expect(await files['renamed-lazy-script.js.map']).not.toBeUndefined();
});
is failing.

Let me know if you need any help with this.

@alan-agius4
Copy link
Collaborator

alan-agius4 commented Jul 11, 2022

It looks like the sourcemaps are not renamed. In this case there is also another problem.

This also highlights a bigger problem. Since the sourcemaps are being renamed afterwards the sourcemaps comment would point to an invalid sourcemap file.

I think one way to fix this would be to change the hook to maybe PROCESS_ASSETS_STAGE_DEV_TOOLING or something after PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE.

https://webpack.js.org/api/compilation-hooks/

@martinfrancois
Copy link
Contributor Author

@alan-agius4 I just noticed it as well and came to the same conclusion as you, I tried to run the tests locally but so far I was not able to, when I run them using yarn bazel:test I'm getting the following error (I'm running on macOS, not sure if that should be a problem) and wasn't able to find a solution to it online. Is there a different way of running the tests without Bazel or do you happen to know how to fix the error?

@alan-agius4
Copy link
Collaborator

I do recall @clydin had problems running on Mac M1.

Bazel is to only way to run these tests.

@martinfrancois
Copy link
Contributor Author

@alan-agius4 I'm not using an M1 Mac, just an Intel one, that's the strange part about it... Is there a way to restrict the tests to only test scripts-array_spec.ts or the package it is in, without running the whole test suite?

… for scripts which are changed during the optimization phase

Instead of generating the content hash based on the content of scripts BEFORE the optimization phase,
the content hash is generated AFTER the optimization phase.

Prevents caching issues where browsers block execution of scripts due to the integrity hash not matching
with the cached script in case of a script being optimized differently than in a previous build,
where it would previously result in the same content hash.

Fixes angular#22906
@martinfrancois
Copy link
Contributor Author

martinfrancois commented Jul 12, 2022

@alan-agius4 Your intuition seems to have been correct, changing the hook to PROCESS_ASSETS_STAGE_DEV_TOOLING did the trick, can you have a look again please?
By the way, I was not able to get the tests working on Mac and also not on Windows, they ended up working nicely in Ubuntu 22.04 though.

@martinfrancois
Copy link
Contributor Author

@alan-agius4 Tests are passing now! 🎉

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.

LGTM

@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Jul 12, 2022
@olegomon
Copy link

@martinfrancois thanks for the fix. @alan-agius4 thanks for your quick response on this issue.

@dgp1130 dgp1130 merged commit 357c45e into angular:main Jul 12, 2022
@martinfrancois martinfrancois deleted the issue-22906 branch July 12, 2022 18:28
@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 12, 2022
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: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Output files' filename for scripts hashes are incorrect
4 participants