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(core): asset hash is different between linux and windows #16945

Merged
merged 3 commits into from
Oct 13, 2021
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
10 changes: 6 additions & 4 deletions packages/@aws-cdk/core/lib/fs/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,26 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions
return hash.digest('hex');

function _processFileOrDirectory(symbolicPath: string, isRootDir: boolean = false, realPath = symbolicPath) {
const relativePath = path.relative(fileOrDirectory, symbolicPath);

if (!isRootDir && ignoreStrategy.ignores(symbolicPath)) {
return;
}

const stat = fs.lstatSync(realPath);

// Use relative path as hash component. Normalize it with forward slashes to ensure
// same hash on Windows and Linux.
const hashComponent = path.relative(fileOrDirectory, symbolicPath).replace(/\\/g, '/');

if (stat.isSymbolicLink()) {
const linkTarget = fs.readlinkSync(realPath);
const resolvedLinkTarget = path.resolve(path.dirname(realPath), linkTarget);
if (shouldFollow(follow, rootDirectory, resolvedLinkTarget)) {
_processFileOrDirectory(symbolicPath, false, resolvedLinkTarget);
} else {
_hashField(hash, `link:${relativePath}`, linkTarget);
_hashField(hash, `link:${hashComponent}`, linkTarget);
}
} else if (stat.isFile()) {
_hashField(hash, `file:${relativePath}`, contentFingerprint(realPath));
_hashField(hash, `file:${hashComponent}`, contentFingerprint(realPath));
} else if (stat.isDirectory()) {
for (const item of fs.readdirSync(realPath).sort()) {
_processFileOrDirectory(path.join(symbolicPath, item), false, path.join(realPath, item));
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/core/test/fs/fs-fingerprint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,22 @@ describe('fs fingerprint', () => {

});
});

test('normalizes relative path', () => {
// Simulate a Windows path.relative()
const originalPathRelative = path.relative;
const pathRelativeSpy = jest.spyOn(path, 'relative').mockImplementation((from: string, to: string): string => {
return originalPathRelative(from, to).replace(/\//g, '\\');
});

const hash1 = FileSystem.fingerprint(path.join(__dirname, 'fixtures', 'test1'));

// Restore Linux behavior
pathRelativeSpy.mockRestore();

const hash2 = FileSystem.fingerprint(path.join(__dirname, 'fixtures', 'test1'));

// Relative paths are normalized
expect(hash1).toEqual(hash2);
});
});