From 8a10c204596aa226844359cc76d27b46e360ec35 Mon Sep 17 00:00:00 2001 From: guru-bharadwaj20 Date: Wed, 2 Dec 2026 12:02:43 +0530 Subject: [PATCH] [webpack5-localization-plugin] Copy cached localizedFiles before use The realContentHash step renames entries of the chunk's localizedFiles map in place. That map was the same object stored in the webpack cache, so a rebuild that read the chunk from the cache got the renamed filenames and failed with "Could not find asset". --- ...ization-cached-files_2026-12-02-12-00.json | 10 ++ .../src/AssetProcessor.ts | 5 +- .../test/LocalizedAsyncCachedRebuild.test.ts | 117 ++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 common/changes/@rushstack/webpack5-localization-plugin/webpack5-localization-cached-files_2026-12-02-12-00.json create mode 100644 webpack/webpack5-localization-plugin/src/test/LocalizedAsyncCachedRebuild.test.ts diff --git a/common/changes/@rushstack/webpack5-localization-plugin/webpack5-localization-cached-files_2026-12-02-12-00.json b/common/changes/@rushstack/webpack5-localization-plugin/webpack5-localization-cached-files_2026-12-02-12-00.json new file mode 100644 index 0000000000..52e28a299f --- /dev/null +++ b/common/changes/@rushstack/webpack5-localization-plugin/webpack5-localization-cached-files_2026-12-02-12-00.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/webpack5-localization-plugin", + "comment": "Fix \"Could not find asset\" errors on rebuilds that reuse cached localized chunks when `realContentHash` is enabled.", + "type": "patch" + } + ], + "packageName": "@rushstack/webpack5-localization-plugin" +} diff --git a/webpack/webpack5-localization-plugin/src/AssetProcessor.ts b/webpack/webpack5-localization-plugin/src/AssetProcessor.ts index 998b62e5cf..b07e3d8eb8 100644 --- a/webpack/webpack5-localization-plugin/src/AssetProcessor.ts +++ b/webpack/webpack5-localization-plugin/src/AssetProcessor.ts @@ -107,7 +107,10 @@ export async function processLocalizedAssetCachedAsync( await cacheItem.storePromise(output); } - const { localizedFiles, processedAssets } = output; + const { processedAssets } = output; + // Copy the map, because the realContentHash step renames its entries in place. + // Without the copy, a later build that reads this item from the cache gets the renamed filenames. + const localizedFiles: Record = { ...output.localizedFiles }; (chunk as ILocalizedWebpackChunk).localizedFiles = localizedFiles; diff --git a/webpack/webpack5-localization-plugin/src/test/LocalizedAsyncCachedRebuild.test.ts b/webpack/webpack5-localization-plugin/src/test/LocalizedAsyncCachedRebuild.test.ts new file mode 100644 index 0000000000..c4020ab5ba --- /dev/null +++ b/webpack/webpack5-localization-plugin/src/test/LocalizedAsyncCachedRebuild.test.ts @@ -0,0 +1,117 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. +// See LICENSE in the project root for license information. + +jest.disableAutomock(); +import { resolve } from 'node:path'; +import { promisify } from 'node:util'; + +import webpack, { type Compiler, type Stats, type StatsError } from 'webpack'; +import { Volume } from 'memfs/lib/volume'; + +import { LocalizationPlugin } from '../LocalizationPlugin'; +import type { ILocalizationPluginOptions } from '../interfaces'; +import { MemFSPlugin } from './MemFSPlugin'; + +interface IBuildResult { + errors: StatsError[] | undefined; + warnings: StatsError[] | undefined; + files: string[]; +} + +describe(LocalizationPlugin.name, () => { + it('Reuses cached localized chunks on a rebuild with realContentHash', async () => { + const memoryFileSystem: Volume = new Volume(); + memoryFileSystem.fromJSON( + { + '/a/package.json': '{ "name": "a", "sideEffects": ["entry.js", "async.js", "asyncLoc.js"] }', + '/a/async.js': `console.log("first");`, + '/a/asyncLoc.js': `import strings from './strings.loc.json'; console.log(strings.test);`, + '/a/entry.js': `import(/* webpackChunkName: 'asyncLoc' */ './asyncLoc');import(/* webpackChunkName: 'async' */ './async');`, + '/a/strings.loc.json': `{"test":{"value":"blah","comment":"A string"}}` + }, + '/' + ); + + const loader: string = resolve(__dirname, '../loaders/locjson-loader.js'); + const options: ILocalizationPluginOptions = { + localizedData: { + defaultLocale: { + localeName: 'LOCALE1' + }, + translatedStrings: { + LOCALE2: { + '/a/strings.loc.json': { + test: 'baz' + } + } + } + }, + realContentHash: true + }; + + const compiler: Compiler = webpack({ + entry: { + main: '/a/entry.js' + }, + output: { + path: '/release', + filename: '[name]-[locale]-[contenthash].js', + chunkFilename: 'chunks/[name]-[locale]-[contenthash].js', + hashSalt: '1' + }, + module: { + rules: [ + { + test: /\.loc.json$/, + use: { + loader + }, + type: 'javascript/esm', + sideEffects: false + } + ] + }, + optimization: { + minimize: false, + moduleIds: 'named' + }, + // Keep the plugin's cache entries between the two runs, as watch mode does + cache: { + type: 'memory' + }, + context: '/', + mode: 'production', + plugins: [new LocalizationPlugin(options), new MemFSPlugin(memoryFileSystem)] + }); + + const runAsync: () => Promise = promisify(compiler.run.bind(compiler)); + + async function buildAsync(): Promise { + const stats: Stats | undefined = await runAsync(); + if (!stats) { + throw new Error(`Expected stats`); + } + + const { errors, warnings } = stats.toJson('errors-warnings'); + const files: string[] = Array.from(stats.compilation.getAssets(), ({ name }) => name).sort(); + return { errors, warnings, files }; + } + + const firstBuild: IBuildResult = await buildAsync(); + expect(firstBuild.errors).toHaveLength(0); + expect(firstBuild.warnings).toHaveLength(0); + + // Change a module outside of the localized chunk, so that the localized chunk is read from the cache + memoryFileSystem.writeFileSync('/a/async.js', `console.log("second");`); + + const secondBuild: IBuildResult = await buildAsync(); + await promisify(compiler.close.bind(compiler))(); + + expect(secondBuild.errors).toHaveLength(0); + expect(secondBuild.warnings).toHaveLength(0); + + const localizedChunkFiles = (files: string[]): string[] => + files.filter((file) => file.startsWith('chunks/asyncLoc-')); + expect(localizedChunkFiles(secondBuild.files)).toEqual(localizedChunkFiles(firstBuild.files)); + }); +});