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(toolkit): avoid EMFILE and preserve mode when zipping #3428

Merged
merged 2 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions packages/aws-cdk/lib/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import glob = require('glob');
import path = require('path');

export function zipDirectory(directory: string, outputFile: string): Promise<void> {
return new Promise((ok, fail) => {
return new Promise(async (ok, fail) => {
// The below options are needed to support following symlinks when building zip files:
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - follow: This will follow symlinks and copy the files within.
Expand All @@ -24,12 +24,16 @@ export function zipDirectory(directory: string, outputFile: string): Promise<voi
archive.on('error', fail);
archive.pipe(output);

files.forEach(file => { // Append files serially to ensure file order
archive.append(fs.createReadStream(path.join(directory, file)), {
// Append files serially to ensure file order
for (const file of files) {
const fullPath = path.join(directory, file);
const [data, stat] = await Promise.all([fs.readFile(fullPath), fs.stat(fullPath)]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again... I am concerned about memory usage. Can we throttle this some how? Instead of reading all files at once, can we read them one by one or in batches?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The files are now read and appended one by one, this line just reads and "stats" in parallel a single file.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this. It is currently crashing our build when zip files are being generated.

archive.append(data, {
name: file,
date: new Date('1980-01-01T00:00:00.000Z'), // reset dates to get the same hash for the same content
mode: stat.mode,
});
});
}

archive.finalize();

Expand Down
Empty file.
6 changes: 6 additions & 0 deletions packages/aws-cdk/test/test.archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export = {
test.equal(dates[0], '1980-01-01T00:00:00.000Z', 'Dates are not reset');
test.equal(new Set(dates).size, 1, 'Dates are not equal');

// check that mode is preserved
const stat = await fs.stat(path.join(extractDir, 'executable.txt'));
// tslint:disable-next-line:no-bitwise
const isExec = (stat.mode & fs.constants.S_IXUSR) || (stat.mode & fs.constants.S_IXGRP) || (stat.mode & fs.constants.S_IXOTH);
test.ok(isExec, 'File is not executable');

await fs.remove(stagingDir);
await fs.remove(extractDir);
test.done();
Expand Down