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

syncpack format orders dependencies differently than npm does #206

Open
jenseng opened this issue Apr 18, 2024 · 1 comment
Open

syncpack format orders dependencies differently than npm does #206

jenseng opened this issue Apr 18, 2024 · 1 comment

Comments

@jenseng
Copy link

jenseng commented Apr 18, 2024

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:

  • npm i 5to6-codemod @babel/register
  • Note that @babel/register appears before 5to6-codemod in package.json
  • npx syncpack format
  • Note that @babel/register now appears after 5to6-codemod
  • npm i @babel/types
  • Note that @babel/* again appears before 5to6-codemod
  • npx syncpack lint fails linting

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)

@jenseng jenseng changed the title syncpack format orders dependencies differently than npm does syncpack format orders dependencies differently than npm does Apr 18, 2024
@JamieMason
Copy link
Owner

JamieMason commented Apr 19, 2024

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.

function sortAlphabetically(value: unknown): void {
if (isArray(value)) {
value.sort();
} else if (isObject(value)) {
sortObject(Object.keys(value).sort(), value);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants