Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): ENOENT during asset publishing #25869

Merged
merged 6 commits into from
Jun 6, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions packages/cdk-assets/lib/private/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ function writeZipFile(directory: string, outputFile: string): Promise<void> {
}

/**
* Rename the file to the target location, taking into account that we may see EPERM on Windows
* while an Antivirus scanner still has the file open, so retry a couple of times.
* Rename the file to the target location, taking into account:
*
* - that we may see EPERM on Windows while an Antivirus scanner still has the
* file open, so retry a couple of times.
* - this same function may be called in parallel and be interrupted at any point.
*/
async function moveIntoPlace(source: string, target: string, logger: Logger) {
let delay = 100;
let attempts = 5;
while (true) {
try {
if (await pathExists(target)) {
await fs.unlink(target);
}
// 'rename' is guaranteed to overwrite an existing target, as long as it is a file (not a directory)
await fs.rename(source, target);
return;
} catch (e: any) {
Expand All @@ -86,18 +87,6 @@ function sleep(ms: number) {
return new Promise(ok => setTimeout(ok, ms));
}

async function pathExists(x: string) {
try {
await fs.stat(x);
return true;
} catch (e: any) {
if (e.code === 'ENOENT') {
return false;
}
throw e;
}
}

function randomString() {
return Math.random().toString(36).replace(/[^a-z0-9]+/g, '');
}