Skip to content

Commit 1568065

Browse files
committed
prevent parallel builds on the same template
1 parent ffd94b4 commit 1568065

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

packages/core/src/shared/sam/build.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import {
2525
getSamCliPathAndVersion,
2626
getTerminalFromError,
2727
isDotnetRuntime,
28+
registerTemplateBuild,
29+
throwIfTemplateIsBeingBuilt,
30+
unregisterTemplateBuild,
2831
updateRecentResponse,
2932
} from './utils'
3033
import { getConfigFileUri, validateSamBuildConfig } from './config'
@@ -207,6 +210,9 @@ export async function runBuild(arg?: TreeNode): Promise<SamBuildResult> {
207210
throw new CancellationError('user')
208211
}
209212

213+
throwIfTemplateIsBeingBuilt(params.template.uri.path)
214+
await registerTemplateBuild(params.template.uri.path)
215+
210216
const projectRoot = params.projectRoot
211217

212218
const defaultFlags: string[] = ['--cached', '--parallel', '--save-params', '--use-container']
@@ -242,10 +248,13 @@ export async function runBuild(arg?: TreeNode): Promise<SamBuildResult> {
242248
// Run SAM build in Terminal
243249
await runInTerminal(buildProcess, 'build')
244250

251+
await unregisterTemplateBuild(params.template.uri.path)
252+
245253
return {
246254
isSuccess: true,
247255
}
248256
} catch (error) {
257+
await unregisterTemplateBuild(params.template.uri.path)
249258
throw ToolkitError.chain(error, 'Failed to build SAM template', {
250259
details: { terminal: getTerminalFromError(error), ...resolveBuildArgConflict(buildFlags) },
251260
code: getErrorCode(error),

packages/core/src/shared/sam/utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,35 @@ export async function updateRecentResponse(
108108
getLogger().warn(`sam: unable to save response at key "${key}": %s`, err)
109109
}
110110
}
111+
112+
const buildProcessMementoRootKey = 'samcli.build.processes'
113+
114+
/**
115+
* Returns true if there's an ongoing build process for the provided template, false otherwise
116+
* @Param templatePath The path to the template.yaml file
117+
*/
118+
function isBuildInProgress(templatePath: string): boolean {
119+
return getRecentResponse(buildProcessMementoRootKey, 'global', templatePath) !== undefined
120+
}
121+
122+
/**
123+
* Throws an error if there's a build in progress for the provided template
124+
* @Param templatePath The path to the template.yaml file
125+
*/
126+
export function throwIfTemplateIsBeingBuilt(templatePath: string) {
127+
if (isBuildInProgress(templatePath)) {
128+
throw new ToolkitError('Build in progress', { code: 'BuildInProgress' })
129+
}
130+
}
131+
132+
export async function registerTemplateBuild(templatePath: string) {
133+
await updateRecentResponse(buildProcessMementoRootKey, 'global', templatePath, 'true')
134+
}
135+
136+
export async function unregisterTemplateBuild(templatePath: string) {
137+
await updateRecentResponse(buildProcessMementoRootKey, 'global', templatePath, undefined)
138+
}
139+
111140
export function getSamCliErrorMessage(stderr: string): string {
112141
// Split the stderr string by newline, filter out empty lines, and get the last line
113142
const lines = stderr

0 commit comments

Comments
 (0)