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(assets): invalid fingerprint when 'exclude' captures root directory name #7719

Merged
merged 2 commits into from
May 6, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export = {
],
},
'SourceBucketNames': [{
'Ref': 'AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3Bucket1A1EC3E9',
'Ref': 'AssetParameters86f8bca4f28a0bcafef0a98fe4cea25c0071aca27401e35cfaecd06313373bcaS3BucketB41AE64D',
}],
'SourceObjectKeys': [{
'Fn::Join': [
Expand All @@ -241,7 +241,7 @@ export = {
'Fn::Split': [
'||',
{
'Ref': 'AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3VersionKeyE46A4824',
'Ref': 'AssetParameters86f8bca4f28a0bcafef0a98fe4cea25c0071aca27401e35cfaecd06313373bcaS3VersionKeyF3CBA38F',
},
],
},
Expand All @@ -254,7 +254,7 @@ export = {
'Fn::Split': [
'||',
{
'Ref': 'AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3VersionKeyE46A4824',
'Ref': 'AssetParameters86f8bca4f28a0bcafef0a98fe4cea25c0071aca27401e35cfaecd06313373bcaS3VersionKeyF3CBA38F',
},
],
},
Expand Down
11 changes: 6 additions & 5 deletions packages/@aws-cdk/core/lib/fs/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions
if (exclude.length) {
_hashField(hash, 'options.exclude', JSON.stringify(exclude));
}
_processFileOrDirectory(fileOrDirectory);
const isDir = fs.statSync(fileOrDirectory).isDirectory();
_processFileOrDirectory(fileOrDirectory, isDir);

return hash.digest('hex');

function _processFileOrDirectory(symbolicPath: string, realPath = symbolicPath) {
if (shouldExclude(exclude, symbolicPath)) {
function _processFileOrDirectory(symbolicPath: string, isRootDir: boolean = false, realPath = symbolicPath) {
if (!isRootDir && shouldExclude(exclude, symbolicPath)) {
return;
}

Expand All @@ -49,15 +50,15 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions
const linkTarget = fs.readlinkSync(realPath);
const resolvedLinkTarget = path.resolve(path.dirname(realPath), linkTarget);
if (shouldFollow(follow, rootDirectory, resolvedLinkTarget)) {
_processFileOrDirectory(symbolicPath, resolvedLinkTarget);
_processFileOrDirectory(symbolicPath, false, resolvedLinkTarget);
} else {
_hashField(hash, `link:${relativePath}`, linkTarget);
}
} else if (stat.isFile()) {
_hashField(hash, `file:${relativePath}`, _contentFingerprint(realPath, stat));
} else if (stat.isDirectory()) {
for (const item of fs.readdirSync(realPath).sort()) {
_processFileOrDirectory(path.join(symbolicPath, item), path.join(realPath, item));
_processFileOrDirectory(path.join(symbolicPath, item), false, path.join(realPath, item));
}
} else {
throw new Error(`Unable to hash ${symbolicPath}: it is neither a file nor a directory`);
Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/core/test/fs/test.fs-fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ export = {
const f1 = FileSystem.fingerprint(dir, options1);
const f2 = FileSystem.fingerprint(dir, options2);

// THEN
test.notDeepEqual(f1, f2);
test.done();
},
'considers negated exclude patterns for fingerprint'(test: Test) {
// GIVEN
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fingerprint-tests'));
const options = {path: dir, exclude: ['**', '!file.txt'], sourcePath: dir};

// WHEN
const f1 = FileSystem.fingerprint(dir, options);
fs.writeFileSync(path.join(dir, 'file.txt'), 'data');
const f2 = FileSystem.fingerprint(dir, options);

// THEN
test.notDeepEqual(f1, f2);
test.done();
Expand Down