From 2bca856e00d496a2f2ad54ebfb409f042d4efaf2 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sat, 2 Jul 2022 16:04:42 +0300 Subject: [PATCH] fix(patch): the hash of the patch file should be the same on both Windows and POSIX (#4969) close #4961 --- .changeset/soft-seals-shop.md | 6 ++++++ packages/crypto.base32-hash/package.json | 3 ++- packages/crypto.base32-hash/src/index.ts | 3 ++- packages/crypto.base32-hash/test/index.ts | 11 ++++++++++- packages/crypto.base32-hash/tsconfig.json | 6 +++++- pnpm-lock.yaml | 2 ++ 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 .changeset/soft-seals-shop.md diff --git a/.changeset/soft-seals-shop.md b/.changeset/soft-seals-shop.md new file mode 100644 index 00000000000..2dadec40660 --- /dev/null +++ b/.changeset/soft-seals-shop.md @@ -0,0 +1,6 @@ +--- +"@pnpm/crypto.base32-hash": patch +"pnpm": patch +--- + +The hash of the patch file should be the same on both Windows and POSIX [#4961](https://github.com/pnpm/pnpm/issues/4961). diff --git a/packages/crypto.base32-hash/package.json b/packages/crypto.base32-hash/package.json index 0c027619b43..a2f58d38d80 100644 --- a/packages/crypto.base32-hash/package.json +++ b/packages/crypto.base32-hash/package.json @@ -34,7 +34,8 @@ "rfc4648": "^1.5.1" }, "devDependencies": { - "@pnpm/crypto.base32-hash": "workspace:*" + "@pnpm/crypto.base32-hash": "workspace:*", + "@pnpm/prepare": "workspace:*" }, "funding": "https://opencollective.com/pnpm", "exports": { diff --git a/packages/crypto.base32-hash/src/index.ts b/packages/crypto.base32-hash/src/index.ts index 45fb1af677a..c3340057a05 100644 --- a/packages/crypto.base32-hash/src/index.ts +++ b/packages/crypto.base32-hash/src/index.ts @@ -7,5 +7,6 @@ export function createBase32Hash (str: string): string { } export async function createBase32HashFromFile (file: string): Promise { - return createBase32Hash(await fs.promises.readFile(file, 'utf8')) + const content = await fs.promises.readFile(file, 'utf8') + return createBase32Hash(content.split('\r\n').join('\n')) } diff --git a/packages/crypto.base32-hash/test/index.ts b/packages/crypto.base32-hash/test/index.ts index f77e2fa8d08..44ff7236b97 100644 --- a/packages/crypto.base32-hash/test/index.ts +++ b/packages/crypto.base32-hash/test/index.ts @@ -1,6 +1,15 @@ /// -import { createBase32Hash } from '@pnpm/crypto.base32-hash' +import fs from 'fs' +import { createBase32Hash, createBase32HashFromFile } from '@pnpm/crypto.base32-hash' +import { tempDir } from '@pnpm/prepare' test('createBase32Hash()', () => { expect(createBase32Hash('AAA')).toEqual('4h5p7m7gcttmf65hikljmi4gw4') }) + +test('createBase32HashFromFile normalizes line endings before calculating the hash', async () => { + tempDir() + fs.writeFileSync('win-eol.txt', 'a\r\nb\r\nc') + fs.writeFileSync('posix-eol.txt', 'a\nb\r\nc') + expect(await createBase32HashFromFile('win-eol.txt')).toEqual(await createBase32HashFromFile('posix-eol.txt')) +}) diff --git a/packages/crypto.base32-hash/tsconfig.json b/packages/crypto.base32-hash/tsconfig.json index 41aa731a88e..90cf5d087be 100644 --- a/packages/crypto.base32-hash/tsconfig.json +++ b/packages/crypto.base32-hash/tsconfig.json @@ -8,5 +8,9 @@ "src/**/*.ts", "../../typings/**/*.d.ts" ], - "references": [] + "references": [ + { + "path": "../../privatePackages/prepare" + } + ] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 31adf288441..39789f1cf34 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -613,11 +613,13 @@ importers: packages/crypto.base32-hash: specifiers: '@pnpm/crypto.base32-hash': workspace:* + '@pnpm/prepare': workspace:* rfc4648: ^1.5.1 dependencies: rfc4648: 1.5.2 devDependencies: '@pnpm/crypto.base32-hash': 'link:' + '@pnpm/prepare': link:../../privatePackages/prepare packages/default-reporter: specifiers: