Skip to content

Commit

Permalink
fix(cache): Store cache with sorted object keys (#17607)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Sep 4, 2022
1 parent b23b886 commit 5dc1eb3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/util/cache/repository/impl/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import is from '@sindresorhus/is';
import hasha from 'hasha';
import { GlobalConfig } from '../../../../config/global';
import { logger } from '../../../../logger';
import { safeStringify } from '../../../stringify';
import {
CACHE_REVISION,
isValidRev10,
Expand Down Expand Up @@ -106,7 +107,7 @@ export abstract class RepoCacheBase implements RepoCache {
}

async save(): Promise<void> {
const jsonStr = JSON.stringify(this.data);
const jsonStr = safeStringify(this.data);
const hash = await hasha.async(jsonStr);
if (hash === this.oldHash) {
return;
Expand All @@ -133,10 +134,10 @@ export abstract class RepoCacheBase implements RepoCache {
}

isModified(): boolean | undefined {
const jsonStr = JSON.stringify(this.data);
if (!this.oldHash) {
return undefined;
}
const jsonStr = safeStringify(this.data);
return hasha(jsonStr) !== this.oldHash;
}
}
27 changes: 27 additions & 0 deletions lib/util/cache/repository/impl/local.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,31 @@ describe('util/cache/repository/impl/local', () => {

expect(fs.outputCacheFile).not.toHaveBeenCalledWith();
});

it('does not write cache when only key order has changed', async () => {
const oldCacheRecord = await createCacheRecord({
configFileName: 'renovate.json',
semanticCommits: 'enabled',
});
const cacheType = 'protocol://domain/path';
fs.readCacheFile.mockResolvedValueOnce(JSON.stringify(oldCacheRecord));
const localRepoCache = CacheFactory.get(
'some/repo',
'0123456789abcdef',
cacheType
);

await localRepoCache.load();
const data = localRepoCache.getData();
delete data.configFileName;
delete data.semanticCommits;
data.semanticCommits = 'enabled';
data.configFileName = 'renovate.json';

expect(localRepoCache.isModified()).toBeFalse();

await localRepoCache.save();

expect(fs.outputCacheFile).not.toHaveBeenCalledWith();
});
});
8 changes: 2 additions & 6 deletions lib/util/clone.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { configure } from 'safe-stable-stringify';

const stringify = configure({
deterministic: false,
});
import { quickStringify } from './stringify';

export function clone<T>(input: T | null = null): T {
return JSON.parse(stringify(input));
return JSON.parse(quickStringify(input));
}
9 changes: 9 additions & 0 deletions lib/util/stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { configure } from 'safe-stable-stringify';

export const quickStringify = configure({
deterministic: false,
});

export const safeStringify = configure({
deterministic: true,
});

0 comments on commit 5dc1eb3

Please sign in to comment.