fix: use lexigraphical sorting in places where it matters (#5587)

This commit is contained in:
Zoltan Kochan
2022-11-04 14:01:22 +02:00
committed by GitHub
parent e19356c32f
commit 2e9790722a
10 changed files with 37 additions and 7 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/find-workspace-packages": patch
"find-packages": patch
"@pnpm/hoist": patch
---
Use deterministic sorting.

View File

@@ -32,6 +32,7 @@
"dependencies": {
"@pnpm/read-project-manifest": "workspace:*",
"@pnpm/types": "workspace:*",
"@pnpm/util.lex-comparator": "1.0.0",
"fast-glob": "^3.2.12",
"p-filter": "^2.1.0"
},

View File

@@ -1,6 +1,7 @@
import path from 'path'
import { readExactProjectManifest } from '@pnpm/read-project-manifest'
import { ProjectManifest } from '@pnpm/types'
import { lexCompare } from '@pnpm/util.lex-comparator'
import fastGlob from 'fast-glob'
import pFilter from 'p-filter'
@@ -48,7 +49,7 @@ export async function findPackages (root: string, opts?: Options): Promise<Proje
paths
.map(manifestPath => path.join(root, manifestPath))
.sort((path1, path2) =>
path.dirname(path1).localeCompare(path.dirname(path2))
lexCompare(path.dirname(path1), path.dirname(path2))
)
),
async manifestPath => {

View File

@@ -32,6 +32,7 @@
"@pnpm/cli-utils": "workspace:*",
"@pnpm/constants": "workspace:*",
"@pnpm/types": "workspace:*",
"@pnpm/util.lex-comparator": "1.0.0",
"find-packages": "workspace:*",
"read-yaml-file": "^2.1.0"
},

View File

@@ -2,6 +2,7 @@ import path from 'path'
import { packageIsInstallable } from '@pnpm/cli-utils'
import { WORKSPACE_MANIFEST_FILENAME } from '@pnpm/constants'
import { Project } from '@pnpm/types'
import { lexCompare } from '@pnpm/util.lex-comparator'
import { findPackages } from 'find-packages'
import readYamlFile from 'read-yaml-file'
@@ -37,7 +38,7 @@ export async function findWorkspacePackagesNoCheck (workspaceRoot: string, opts?
includeRoot: true,
patterns,
})
pkgs.sort((pkg1: { dir: string }, pkg2: { dir: string }) => pkg1.dir.localeCompare(pkg2.dir))
pkgs.sort((pkg1: { dir: string }, pkg2: { dir: string }) => lexCompare(pkg1.dir, pkg2.dir))
return pkgs
}

View File

@@ -47,6 +47,7 @@
"@pnpm/matcher": "workspace:*",
"@pnpm/symlink-dependency": "workspace:*",
"@pnpm/types": "workspace:*",
"@pnpm/util.lex-comparator": "1.0.0",
"dependency-path": "workspace:*",
"ramda": "npm:@pnpm/ramda@0.28.1"
},

View File

@@ -10,6 +10,7 @@ import { logger } from '@pnpm/logger'
import { createMatcher } from '@pnpm/matcher'
import { symlinkDependency } from '@pnpm/symlink-dependency'
import { HoistedDependencies } from '@pnpm/types'
import { lexCompare } from '@pnpm/util.lex-comparator'
import * as dp from 'dependency-path'
const hoistLogger = logger('hoist')
@@ -171,7 +172,7 @@ async function hoistGraph (
// sort by depth and then alphabetically
.sort((a, b) => {
const depthDiff = a.depth - b.depth
return depthDiff === 0 ? a.depPath.localeCompare(b.depPath) : depthDiff
return depthDiff === 0 ? lexCompare(a.depPath, b.depPath) : depthDiff
})
// build the alias map and the id map
.forEach((depNode) => {

View File

@@ -52,6 +52,7 @@
"@pnpm/lockfile-types": "workspace:*",
"@pnpm/merge-lockfile-changes": "workspace:*",
"@pnpm/types": "workspace:*",
"@pnpm/util.lex-comparator": "1.0.0",
"@zkochan/rimraf": "^2.1.2",
"comver-to-semver": "^1.0.0",
"dependency-path": "workspace:*",

View File

@@ -1,3 +1,4 @@
import { lexCompare } from '@pnpm/util.lex-comparator'
import sortKeys from 'sort-keys'
import { LockfileFile } from './write'
@@ -53,10 +54,7 @@ function compareWithPriority (priority: Record<string, number>, left: string, ri
if (leftPriority && rightPriority) return leftPriority - rightPriority
if (leftPriority) return -1
if (rightPriority) return 1
// We want deterministic sorting, so we can't use .localCompare here.
// comparing strings with < and > will produce the same result on each machine.
// An alternative solution could be to use a specific culture for compare, using Intl.Collator
return left < right ? -1 : (left > right ? 1 : 0)
return lexCompare(left, right)
}
export function sortLockfileKeys (lockfile: LockfileFile) {

18
pnpm-lock.yaml generated
View File

@@ -1314,6 +1314,9 @@ importers:
'@pnpm/types':
specifier: workspace:*
version: link:../types
'@pnpm/util.lex-comparator':
specifier: 1.0.0
version: 1.0.0
fast-glob:
specifier: ^3.2.12
version: 3.2.12
@@ -1349,6 +1352,9 @@ importers:
'@pnpm/types':
specifier: workspace:*
version: link:../types
'@pnpm/util.lex-comparator':
specifier: 1.0.0
version: 1.0.0
find-packages:
specifier: workspace:*
version: link:../find-packages
@@ -1726,6 +1732,9 @@ importers:
'@pnpm/types':
specifier: workspace:*
version: link:../types
'@pnpm/util.lex-comparator':
specifier: 1.0.0
version: 1.0.0
dependency-path:
specifier: workspace:*
version: link:../dependency-path
@@ -2020,6 +2029,9 @@ importers:
'@pnpm/types':
specifier: workspace:*
version: link:../types
'@pnpm/util.lex-comparator':
specifier: 1.0.0
version: 1.0.0
'@zkochan/rimraf':
specifier: ^2.1.2
version: 2.1.2
@@ -7836,6 +7848,11 @@ packages:
engines: {node: '>=14.6'}
dev: true
/@pnpm/util.lex-comparator/1.0.0:
resolution: {integrity: sha512-3aBQPHntVgk5AweBWZn+1I/fqZ9krK/w01197aYVkAJQGftb+BVWgEepxY5GChjSW12j52XX+CmfynYZ/p0DFQ==}
engines: {node: '>=12.22.0'}
dev: false
/@pnpm/which-version-is-pinned/4.0.0:
resolution: {integrity: sha512-XqZqdxgX7vqxiMX+REsN6lfZ9TR2FQhhaaClA6ymFAVjhuMzJidDDVibqAx5nlfoSqeBklJEZxYXYnU7LjghlA==}
engines: {node: '>=14.6'}
@@ -16967,6 +16984,7 @@ time:
/@pnpm/registry-mock/3.1.0: '2022-10-13T19:59:17.408Z'
/@pnpm/semver-diff/1.1.0: '2021-11-16T12:40:59.941Z'
/@pnpm/tabtab/0.1.2: '2021-03-05T17:31:19.932Z'
/@pnpm/util.lex-comparator/1.0.0: '2022-11-04T01:03:46.134Z'
/@types/adm-zip/0.4.34: '2021-04-04T18:01:23.318Z'
/@types/archy/0.0.32: '2021-07-06T18:11:33.301Z'
/@types/byline/4.2.33: '2021-07-06T18:22:06.440Z'