Skip to content

Commit b0ee77e

Browse files
author
Ben Keen
committed
Add safety check to Bridge Cache plugin write action
1 parent b9f8849 commit b0ee77e

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
10+
}

rush-plugins/rush-bridge-cache-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Alternatively, the `--bridge-cache-action=read` parameter is useful for tasks su
88

99
## Here be dragons!
1010

11-
The `write` action for plugin assumes that the work for a particular task has already been completed and the build artifacts have been generated on disk. **If you run this command on a package where the command hasn't already been run and the build artifacts are missing or incorrect, you will cache invalid content**. Be careful and beware!
11+
The `write` action for plugin assumes that the work for a particular task has already been completed and the build artifacts have been generated on disk. **If you run this command on a package where the command hasn't already been run and the build artifacts are missing or incorrect, you will cache invalid content**. Be careful and beware! If any defined output folders for a task are not on disk, the write action will skip that package.
1212

1313
The `read` action for this plugin makes no guarantee that the requested operations will have their outputs restored and is purely a best-effort.
1414

rush-plugins/rush-bridge-cache-plugin/src/BridgeCachePlugin.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import { Async } from '@rushstack/node-core-library';
4+
import { Async, FileSystem } from '@rushstack/node-core-library';
55
import { _OperationBuildCache as OperationBuildCache } from '@rushstack/rush-sdk';
66
import type {
77
ICreateOperationsContext,
@@ -123,6 +123,29 @@ export class BridgeCachePlugin implements IRushPlugin {
123123
);
124124
}
125125
} else if (cacheAction === CACHE_ACTION_WRITE) {
126+
// skip this action if any of the defined output folders do not exist on disk
127+
if (
128+
operation.settings?.outputFolderNames &&
129+
operation.settings?.outputFolderNames?.length > 0
130+
) {
131+
const projectFolder = operation.associatedProject?.projectFolder;
132+
const results: { outputFolderName: string; exists: boolean }[] =
133+
operation.settings.outputFolderNames.map((outputFolderName: string) => ({
134+
outputFolderName,
135+
exists: FileSystem.exists(`${projectFolder}/${outputFolderName}`)
136+
}));
137+
138+
if (results.some((folder) => !folder.exists)) {
139+
const missingFolders = results
140+
.filter((folder) => !folder.exists)
141+
.map((folder) => folder.outputFolderName);
142+
terminal.writeErrorLine(
143+
`Operation "${operation.name}": The following output folders do not exist: "${missingFolders.join('", "')}". Skipping cache population.`
144+
);
145+
return;
146+
}
147+
}
148+
126149
const success: boolean = await projectBuildCache.trySetCacheEntryAsync(terminal);
127150
if (success) {
128151
++successCount;

0 commit comments

Comments
 (0)