Skip to content

Commit

Permalink
[rush] Add cacheHashSalt property
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichon-msft committed Oct 4, 2024
1 parent 4edbbb8 commit f5c875f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Add an optional property `cacheHashSalt` to `build-cache.json` to allow repository maintainers to globally force a hash change in build cache entries.",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
1 change: 1 addition & 0 deletions common/reviews/api/rush-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class ApprovedPackagesPolicy {
// @beta
export class BuildCacheConfiguration {
readonly buildCacheEnabled: boolean;
readonly cacheHashSalt: string | undefined;
cacheWriteEnabled: boolean;
readonly cloudCacheProvider: ICloudBuildCacheProvider | undefined;
static getBuildCacheConfigFilePath(rushConfiguration: RushConfiguration): string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/
// "cacheEntryNamePattern": "[projectName:normalize]-[phaseName:normalize]-[hash]"

/**
* (Optional) Salt to inject during calculation of the cache key. This can be used to invalidate the cache for all projects when the salt changes.
*/
// "cacheHashSalt": "1",

/**
* Use this configuration with "cacheProvider"="azure-blob-storage"
*/
Expand Down
9 changes: 9 additions & 0 deletions libraries/rush-lib/src/api/BuildCacheConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export interface IBaseBuildCacheJson {
* The token parser is in CacheEntryId.ts
*/
cacheEntryNamePattern?: string;
/**
* An optional salt to inject during calculation of the cache key. This can be used to invalidate the cache for all projects when the salt changes.
*/
cacheHashSalt?: string;
}

/**
Expand Down Expand Up @@ -102,6 +106,10 @@ export class BuildCacheConfiguration {
* The provider for interacting with the cloud build cache, if configured.
*/
public readonly cloudCacheProvider: ICloudBuildCacheProvider | undefined;
/**
* An optional salt to inject during calculation of the cache key. This can be used to invalidate the cache for all projects when the salt changes.
*/
public readonly cacheHashSalt: string | undefined;

private constructor({
getCacheEntryId,
Expand All @@ -120,6 +128,7 @@ export class BuildCacheConfiguration {
rushConfiguration: rushConfiguration
});
this.cloudCacheProvider = cloudCacheProvider;
this.cacheHashSalt = buildCacheJson.cacheHashSalt;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export class CacheableOperationPlugin implements IPhasedCommandPlugin {
public apply(hooks: PhasedCommandHooks): void {
const { allowWarningsInSuccessfulBuild, buildCacheConfiguration, cobuildConfiguration } = this._options;

const { cacheHashSalt } = buildCacheConfiguration;

hooks.beforeExecuteOperations.tap(
PLUGIN_NAME,
(
Expand Down Expand Up @@ -143,6 +145,12 @@ export class CacheableOperationPlugin implements IPhasedCommandPlugin {
// of the build cache.
hasher.update(`${RushConstants.buildCacheVersion}`);

if (cacheHashSalt !== undefined) {
// This allows repository owners to force a cache bust by changing the salt.
// A common use case is to invalidate the cache when adding/removing/updating rush plugins that alter the build output.
hasher.update(cacheHashSalt);
}

for (const dependencyHash of dependencyHashes) {
hasher.update(dependencyHash);
}
Expand Down
4 changes: 4 additions & 0 deletions libraries/rush-lib/src/schemas/build-cache.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"type": "string",
"description": "Setting this property overrides the cache entry ID. If this property is set, it must contain a [hash] token. It may also contain one of the following tokens: [projectName], [projectName:normalize], [phaseName], [phaseName:normalize], [phaseName:trimPrefix], [os], and [arch]."
},
"cacheHashSalt": {
"type": "string",
"description": "An optional salt to inject during calculation of the cache key. This can be used to invalidate the cache for all projects when the salt changes."
},
"azureBlobStorageConfiguration": {
"type": "object",
"additionalProperties": false,
Expand Down

0 comments on commit f5c875f

Please sign in to comment.