JamieMason / syncpack

Consistent dependency versions in large JavaScript Monorepos.
https://jamiemason.github.io/syncpack/
MIT License
1.41k stars 49 forks source link

`syncpack format` orders dependencies differently than npm does #206

Closed jenseng closed 2 months ago

jenseng commented 5 months ago

Description

syncpack format orders dependencies differently than npm does, so package.json can easily become "unformatted" every time you install a new package (npm likes to re-sort things). This is most noticeable if a dependency begins with a number (e.g. 5to6-codemod); syncpack will sort it before any scoped deps (presumably because of this), whereas npm puts scoped deps first (since it uses localeCompare when sorting).

Basic repro:

While you can mostly work around this by running syncpack format in a pre-commit hook, this isn't always feasible (e.g. if you have automated processes that are updating packages ... in that case those also need to explicitly run syncpack format)

Suggested Solution

syncpack should sort dependencies the same way that npm does (using a localeCompare-based sort, rather than a vanilla sort)

JamieMason commented 5 months ago

Great spot @jenseng, thanks a lot.

Syncpack is just doing a completely basic .sort() which I wrongly thought would be enough, thanks for pointing this out.

https://github.com/JamieMason/syncpack/blob/db2b31ccdb1a28fdbe0c42d27ce956ea5c6c543a/src/bin-format/format.ts#L120-L126

JamieMason commented 2 months ago

Released in 12.4.0