diff --git a/packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/prepare-source.ts b/packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/prepare-source.ts index 7d97c7638..3a753aeaf 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/prepare-source.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/prepare-source.ts @@ -49,7 +49,7 @@ export class ExecutionEnvironment implements AsyncDisposable { * up when this object is disposed. * * If `markSuccessful()` is called, the writer lock is converted to a reader lock - * and temporary directories will not be cleaned up anymore. + * and temporary directories will not be cleaned up by this object anymore. */ public static async create(services: ToolkitServices, options: ExecutionEnvironmentOptions) { let outDirIsTemporary = false; @@ -79,16 +79,14 @@ export class ExecutionEnvironment implements AsyncDisposable { } /** - * Should the outdir be disposed of. + * The directory the cloud assembly will be written to. */ - public get shouldDisposeOutDir(): boolean { - return this.shouldClean; - } + public readonly outdir: string; /** - * The directory the cloud assembly will be written to. + * Whether `outdir` was created as a temporary directory for this execution. */ - public readonly outdir: string; + public readonly outDirIsTemporary: boolean; private readonly options: Required; private readonly ioHelper: IoHelper; @@ -109,6 +107,7 @@ export class ExecutionEnvironment implements AsyncDisposable { this.sdkProvider = services.sdkProvider; this.debugFn = (msg: string) => this.ioHelper.defaults.debug(msg); this.lock = lock; + this.outDirIsTemporary = outDirIsTemporary; this.shouldClean = outDirIsTemporary; this.outdir = options.outdir; this.options = options; @@ -117,7 +116,7 @@ export class ExecutionEnvironment implements AsyncDisposable { public async [Symbol.asyncDispose]() { await this.lock?.release(); - if (this.shouldDisposeOutDir) { + if (this.shouldClean) { await fs.rm(this.outdir, { recursive: true, force: true }); } } diff --git a/packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/source-builder.ts b/packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/source-builder.ts index 905144263..71db4bbf0 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/source-builder.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/source-builder.ts @@ -658,7 +658,7 @@ export abstract class CloudAssemblySourceBuilder { }); const success = await execution.markSuccessful(); - const deleteOnDispose = options.disposeOutdir ?? execution.shouldDisposeOutDir; + const deleteOnDispose = options.disposeOutdir ?? execution.outDirIsTemporary; return new ReadableCloudAssembly(asm, success.readLock, { deleteOnDispose }); } finally { await cleanupContextOverflow(); diff --git a/packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts b/packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts index 81fd6ab8d..46aaa8f0a 100644 --- a/packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts +++ b/packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts @@ -120,6 +120,92 @@ describe('fromAssemblyBuilder', () => { expect(await fs.pathExists(asm.cloudAssembly.directory)).toBeFalsy(); }); + test('disposes temporary outdir by default when outdir is omitted', async () => { + // GIVEN + const builder = builderFunctionFromFixture('two-empty-stacks'); + const cx = await toolkit.fromAssemblyBuilder(builder, { + resolveDefaultEnvironment: false, + }); + + // WHEN + const asm = await toolkit.synth(cx); + const outdir = asm.cloudAssembly.directory; + expect(await fs.pathExists(outdir)).toBeTruthy(); + + try { + await asm.dispose(); + + // THEN + expect(await fs.pathExists(outdir)).toBeFalsy(); + } finally { + await fs.remove(outdir); + } + }); + + test('preserves temporary outdir when disposeOutdir is false', async () => { + // GIVEN + const builder = builderFunctionFromFixture('two-empty-stacks'); + const cx = await toolkit.fromAssemblyBuilder(builder, { + disposeOutdir: false, + resolveDefaultEnvironment: false, + }); + + // WHEN + const asm = await toolkit.synth(cx); + const outdir = asm.cloudAssembly.directory; + + try { + await asm.dispose(); + + // THEN + expect(await fs.pathExists(outdir)).toBeTruthy(); + } finally { + await fs.remove(outdir); + } + }); + + test('preserves explicit outdir by default when disposeOutdir is omitted', async () => { + // GIVEN + await using synthDir = autoCleanOutDir(); + const explicitDir = path.join(synthDir.dir, 'explicit.dir'); + const builder = builderFunctionFromFixture('two-empty-stacks'); + const cx = await toolkit.fromAssemblyBuilder(builder, { + outdir: explicitDir, + resolveDefaultEnvironment: false, + }); + + // WHEN + const asm = await toolkit.synth(cx); + await asm.dispose(); + + // THEN + expect(await fs.pathExists(explicitDir)).toBeTruthy(); + }); + + test('cleans up temporary outdir when builder fails', async () => { + // GIVEN + let tempOutdir: string | undefined; + const cx = await toolkit.fromAssemblyBuilder(async (props) => { + tempOutdir = props.outdir; + throw new Error('builder failure'); + }, { + resolveDefaultEnvironment: false, + }); + + // WHEN + try { + await expect(cx.produce()).rejects.toThrow(/Assembly builder failed/); + + // THEN + expect(tempOutdir).toBeDefined(); + expect(await fs.pathExists(tempOutdir!)).toBeFalsy(); + } finally { + if (tempOutdir) { + await fs.remove(tempOutdir); + } + } + }); + test('fromAssemblyBuilder can successfully loop', async () => { // GIVEN const provideContextValues = jest.spyOn(contextproviders, 'provideContextValues').mockImplementation(async (