Skip to content

Commit 2dfd593

Browse files
justinmchaserix0rrr
authored andcommitted
feat(aws-cdk): directory assets follow symlinks (#1318)
When building a nodejs lambda it is really common for dependencies under the `node_modules` folder to actually be symlink'd to other folders on your system. The symlinks will now be followed and the indicated files packaged (instead of the symlinks themselves). Fixes #731
1 parent 3fdbc95 commit 2dfd593

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

packages/aws-cdk/lib/archive.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ export function zipDirectory(directory: string, outputFile: string): Promise<voi
66
return new Promise((ok, fail) => {
77
const output = fs.createWriteStream(outputFile);
88
const archive = archiver('zip');
9-
archive.directory(directory, false);
9+
// The below options are needed to support following symlinks when building zip files:
10+
// - nodir: This will prevent symlinks themselves from being copied into the zip.
11+
// - follow: This will follow symlinks and copy the files within.
12+
const globOptions = {
13+
dot: true,
14+
nodir: true,
15+
follow: true,
16+
cwd: directory
17+
};
18+
archive.glob('**', globOptions);
1019
archive.pipe(output);
1120
archive.finalize();
1221

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../linked
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2

packages/aws-cdk/test/test.archive.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,24 @@ export = {
4343

4444
test.deepEqual(hash1, hash2, 'md5 hash of two zips of the same content are not the same');
4545
test.done();
46+
},
47+
48+
async 'zipDirectory follows symlinks'(test: Test) {
49+
const originalDir = path.join(__dirname, 'test-archive-follow', 'data');
50+
const stagingDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test.archive'));
51+
const zipFile = path.join(stagingDir, 'output.zip');
52+
const extractDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test.archive.follow'));
53+
54+
try {
55+
await zipDirectory(originalDir, zipFile);
56+
await exec(`unzip ${zipFile}`, { cwd: extractDir });
57+
await exec(`diff -bur ${originalDir} ${extractDir}`);
58+
} catch (e) {
59+
test.ok(false, `extracted directory ${extractDir} differs from original ${originalDir}, symlinks not followed.`);
60+
}
61+
62+
await fs.remove(stagingDir);
63+
await fs.remove(extractDir);
64+
test.done();
4665
}
4766
};

0 commit comments

Comments
 (0)