Skip to content

fix: sort discovered locales for deterministic generator output#792

Open
KuceraMartin wants to merge 1 commit into
codingcommons:mainfrom
KuceraMartin:fix/sort-discovered-locales
Open

fix: sort discovered locales for deterministic generator output#792
KuceraMartin wants to merge 1 commit into
codingcommons:mainfrom
KuceraMartin:fix/sort-discovered-locales

Conversation

@KuceraMartin
Copy link
Copy Markdown

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time.
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint

Problem

getAllLocales (in packages/shared/src/file.utils.mts) returns the locale
folders in the order fs.readdir happens to yield them. That order is
filesystem-dependent and is not guaranteed to be stable across platforms
or even across runs on the same machine (e.g. APFS on macOS).

Because this order flows directly into the generated output, the Locales
union type and the locales array get reordered whenever types are
regenerated — even when no translations changed. For projects that commit
their generated i18n-types.ts / i18n-util.ts, this produces recurring,
noisy diffs like:

 export type Locales =
+	| 'cs'
 	| 'en'
-	| 'cs'
 	| 'sk'

Fix

Sort the discovered locale folder names so the generated output is
deterministic regardless of the order returned by fs.readdir. This is a
single .sort() on the resulting string[].

Tests

Added packages/generator/test/get-all-locales.test.ts, which feeds
getAllLocales a mocked filesystem returning locale folders in an unsorted
order and asserts the result is sorted and independent of the input order.
The test fails on main and passes with this change.

pnpm test (generator suite, 103 tests) and pnpm lint pass locally.

`getAllLocales` returned locale folders in the order `fs.readdir` yielded
them, which is filesystem-dependent and not stable across platforms or
runs. This caused the generated `Locales` type and `locales` array to be
reordered on regeneration even when nothing changed, producing noisy diffs.

Sort the discovered locales so the output is deterministic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant