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 1 commit
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
3 changes: 2 additions & 1 deletion packages/@aws-cdk/core/lib/fs/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ 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);
// Normalize relative path with forward slashes to ensure same hash on Windows and Linux
const relativePath = path.relative(fileOrDirectory, symbolicPath).replace(/\\/g, '/');
Copy link
Contributor

Choose a reason for hiding this comment

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

seems like this value is only used as a hash component and not passed into file system operations, correct?

If that is indeed the case, let's rename to hashComponent.


if (!isRootDir && ignoreStrategy.ignores(symbolicPath)) {
return;
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);
});
});