-
Notifications
You must be signed in to change notification settings - Fork 159
feat: warn about legacy experiments.cache config under rspack 2 #1399
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
Open
dannyhw
wants to merge
4
commits into
feat/rspack-2-types-test-infra
Choose a base branch
from
feat/rspack-2-cache-warning
base: feat/rspack-2-types-test-infra
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d5c8a3c
feat: warn about legacy experiments.cache config under rspack 2
dannyhw c90a677
fix: only warn about legacy cache config when persistent caching is off
dannyhw 614bdfe
fix: warn when ignored experiments.cache options are not migrated to …
dannyhw 625c696
fix: satisfy CacheStorageOptions in cache warning tests
dannyhw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@callstack/repack": patch | ||
| --- | ||
|
|
||
| Show a one-time warning when a legacy `experiments.cache` value is detected under Rspack 2. Rspack 2 moved persistent cache configuration to the top-level `cache` option and silently ignores the legacy key, so persistent caching would be lost without any signal. The config is left untouched - migrate the value to the top-level `cache` option in your Rspack config. |
203 changes: 203 additions & 0 deletions
203
packages/repack/src/commands/common/__tests__/warnLegacyRspackCacheConfig.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| describe('warnLegacyRspackCacheConfig', () => { | ||
| let warnSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| // the helper keeps module-level once-only state - re-evaluate it per test | ||
| jest.resetModules(); | ||
| warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| warnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| const loadHelper = () => { | ||
| // require instead of import so jest.resetModules() re-evaluates the module | ||
| const module: typeof import( | ||
| '../warnLegacyRspackCacheConfig.js' | ||
| ) = require('../warnLegacyRspackCacheConfig.js'); | ||
| return module.warnLegacyRspackCacheConfig; | ||
| }; | ||
|
|
||
| it('does not warn when no config uses the legacy experiments.cache key', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| warnLegacyRspackCacheConfig([ | ||
| {}, | ||
| { cache: true }, | ||
| { cache: { type: 'persistent' } }, | ||
| { experiments: {} }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('warns when a config has experiments.cache and no top-level cache', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| warnLegacyRspackCacheConfig([ | ||
| { experiments: { cache: { type: 'persistent' } } }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy.mock.calls[0][0]).toMatch(/experiments\.cache/); | ||
| expect(warnSpy.mock.calls[0][0]).toMatch(/top-level 'cache'/); | ||
| }); | ||
|
|
||
| it('warns for the typical Rspack 1 shape (cache: true + experiments.cache)', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| // under Rspack 2 `cache: true` enables the memory cache only, | ||
| // so persistent caching is still not in effect | ||
| warnLegacyRspackCacheConfig([ | ||
| { cache: true, experiments: { cache: { type: 'persistent' } } }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('warns when top-level cache is memory-only next to the legacy key', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| warnLegacyRspackCacheConfig([ | ||
| { | ||
| cache: { type: 'memory' }, | ||
| experiments: { cache: { type: 'persistent' } }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not warn for a migrated config that only left the inert legacy key behind', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| // persistent caching IS enabled and the legacy key carries no options | ||
| // beyond `type` - there is nothing Rspack 2 could drop | ||
| warnLegacyRspackCacheConfig([ | ||
| { | ||
| cache: { | ||
| type: 'persistent', | ||
| storage: { type: 'filesystem', directory: '/custom' }, | ||
| }, | ||
| experiments: { cache: { type: 'persistent' } }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not warn when the legacy key is deep-equal to the top-level cache', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| // every option under the legacy key is mirrored at the top level, | ||
| // so ignoring the legacy key changes nothing | ||
| warnLegacyRspackCacheConfig([ | ||
| { | ||
| cache: { | ||
| type: 'persistent', | ||
| storage: { type: 'filesystem', directory: '/custom' }, | ||
| buildDependencies: ['/project/rspack.config.mjs'], | ||
| }, | ||
| experiments: { | ||
| cache: { | ||
| type: 'persistent', | ||
| storage: { type: 'filesystem', directory: '/custom' }, | ||
| buildDependencies: ['/project/rspack.config.mjs'], | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('warns softly for a partial migration that leaves options under the legacy key', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| // persistent caching IS enabled, but the storage directory only lives | ||
| // under the legacy key - Rspack 2 drops it and uses the default location | ||
| warnLegacyRspackCacheConfig([ | ||
| { | ||
| cache: { type: 'persistent' }, | ||
| experiments: { | ||
| cache: { | ||
| type: 'persistent', | ||
| storage: { type: 'filesystem', directory: '/custom' }, | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy.mock.calls[0][0]).toMatch(/experiments\.cache/); | ||
| expect(warnSpy.mock.calls[0][0]).toMatch(/not applied/); | ||
| // caching IS enabled - the soft warning must not claim otherwise | ||
| expect(warnSpy.mock.calls[0][0]).not.toMatch(/NOT enabled/); | ||
| }); | ||
|
|
||
| it('warns strongly when legacy options come without a top-level persistent cache', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| warnLegacyRspackCacheConfig([ | ||
| { | ||
| experiments: { | ||
| cache: { | ||
| type: 'persistent', | ||
| storage: { type: 'filesystem', directory: '/custom' }, | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy.mock.calls[0][0]).toMatch(/NOT enabled/); | ||
| }); | ||
|
|
||
| it('prefers the strong warning when configs trigger both tiers', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| warnLegacyRspackCacheConfig([ | ||
| { | ||
| cache: { type: 'persistent' }, | ||
| experiments: { | ||
| cache: { | ||
| type: 'persistent', | ||
| storage: { type: 'filesystem', directory: '/custom' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { experiments: { cache: { type: 'persistent' } } }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy.mock.calls[0][0]).toMatch(/NOT enabled/); | ||
| }); | ||
|
|
||
| it('warns when any config of a multi-config array is misconfigured', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| warnLegacyRspackCacheConfig([ | ||
| { | ||
| cache: { type: 'persistent' }, | ||
| experiments: { cache: { type: 'persistent' } }, | ||
| }, | ||
| { experiments: { cache: { type: 'persistent' } } }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('warns only once across repeated calls', () => { | ||
| const warnLegacyRspackCacheConfig = loadHelper(); | ||
|
|
||
| warnLegacyRspackCacheConfig([ | ||
| { experiments: { cache: { type: 'persistent' } } }, | ||
| ]); | ||
| warnLegacyRspackCacheConfig([ | ||
| { experiments: { cache: { type: 'persistent' } } }, | ||
| ]); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/repack/src/commands/common/warnLegacyRspackCacheConfig.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import * as colorette from 'colorette'; | ||
| import type { RspackConfigurationWithLegacyCache } from './resetPersistentCache.js'; | ||
|
|
||
| let warningDisplayed = false; | ||
|
|
||
| function hasPersistentCacheEnabled(config: RspackConfigurationWithLegacyCache) { | ||
| return typeof config.cache === 'object' && config.cache.type === 'persistent'; | ||
| } | ||
|
|
||
| function isPlainObject(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
| } | ||
|
|
||
| function isDeepEqual(a: unknown, b: unknown): boolean { | ||
| if (a === b) return true; | ||
| if (Array.isArray(a) && Array.isArray(b)) { | ||
| return ( | ||
| a.length === b.length && a.every((item, i) => isDeepEqual(item, b[i])) | ||
| ); | ||
| } | ||
| if (isPlainObject(a) && isPlainObject(b)) { | ||
| const aKeys = Object.keys(a); | ||
| const bKeys = Object.keys(b); | ||
| return ( | ||
| aKeys.length === bKeys.length && | ||
| aKeys.every((key) => key in b && isDeepEqual(a[key], b[key])) | ||
| ); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * A legacy `experiments.cache` value carries extra information when it is an | ||
| * object with options beyond `type` that are not mirrored by the top-level | ||
| * `cache` value - Rspack 2 drops those options and runs with defaults. | ||
| * Booleans, a bare `{ type: 'persistent' }` and a value deep-equal to the | ||
| * top-level `cache` are inert leftovers of a completed migration. | ||
| */ | ||
| function legacyCacheCarriesExtraOptions( | ||
| config: RspackConfigurationWithLegacyCache | ||
| ) { | ||
| const legacyCache = config.experiments?.cache; | ||
| if (!isPlainObject(legacyCache)) return false; | ||
| const extraKeys = Object.keys(legacyCache).filter((key) => key !== 'type'); | ||
| if (extraKeys.length === 0) return false; | ||
| return !isDeepEqual(legacyCache, config.cache); | ||
| } | ||
|
|
||
| /** | ||
| * Rspack 2 moved the persistent cache configuration from `experiments.cache` | ||
| * to the top-level `cache` option and silently ignores the legacy key | ||
| * (validation is loose) - users migrating a Rspack 1 config would lose | ||
| * persistent caching without any signal. | ||
| * | ||
| * Warn (once) when running Rspack 2 with a legacy `experiments.cache` value: | ||
| * - without a top-level persistent `cache` option, persistent caching is NOT | ||
| * in effect - warn that it is disabled. | ||
| * - with a top-level persistent `cache` option, caching IS enabled, but any | ||
| * options that only live under the legacy key are silently dropped - warn | ||
| * (more softly) that they are not applied. A truly inert leftover (a | ||
| * boolean, a bare `{ type: 'persistent' }` or a value deep-equal to the | ||
| * top-level `cache`) stays silent. | ||
| * | ||
| * The config is left untouched - migrating it is the user's move, and | ||
| * mutating it here would make Re.Pack behave differently from bare Rspack | ||
| * given the same config. | ||
| */ | ||
| export function warnLegacyRspackCacheConfig( | ||
| configs: RspackConfigurationWithLegacyCache[] | ||
| ) { | ||
| if (warningDisplayed) return; | ||
| const cachingDisabled = configs.some( | ||
| (config) => | ||
| config.experiments?.cache !== undefined && | ||
| !hasPersistentCacheEnabled(config) | ||
|
dannyhw marked this conversation as resolved.
|
||
| ); | ||
| const optionsDropped = configs.some( | ||
| (config) => | ||
| hasPersistentCacheEnabled(config) && | ||
| legacyCacheCarriesExtraOptions(config) | ||
| ); | ||
| if (!cachingDisabled && !optionsDropped) return; | ||
| warningDisplayed = true; | ||
| if (cachingDisabled) { | ||
| console.warn( | ||
| colorette.yellow( | ||
| "Rspack 2 ignores the legacy 'experiments.cache' option, so persistent " + | ||
| "caching is NOT enabled. Move the value to the top-level 'cache' " + | ||
| 'option in your Rspack config.\n' | ||
| ) | ||
| ); | ||
| } else { | ||
| console.warn( | ||
| colorette.yellow( | ||
| "Rspack 2 ignores the legacy 'experiments.cache' option - options set " + | ||
| "there are not applied. Move them to the top-level 'cache' option " + | ||
| 'in your Rspack config or remove the key.\n' | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.