Skip to content

Preserve locale during brew startup - #23332

Merged
MikeMcQuaid merged 1 commit into
mainfrom
improve-locale-startup
Jul 28, 2026
Merged

Preserve locale during brew startup#23332
MikeMcQuaid merged 1 commit into
mainfrom
improve-locale-startup

Conversation

@MikeMcQuaid

Copy link
Copy Markdown
Member
  • Validate the locale charmap instead of trusting its name.
  • Reuse filtered Linux locale settings to avoid locale -a.

  • Have you followed our Contributing guidelines?
  • Have you checked for other open Pull Requests for the same change?
  • Have you explained what your changes do? Performance claims (e.g. "this is faster") must include Hyperfine benchmarks.
  • Have you explained why you'd like these changes included, not just what they do?
  • For bug fixes, have you given step-by-step brew commands to reproduce the bug?
  • Have you written new tests (excluding integration tests)? Here's an example.
  • Have you successfully run brew lgtm (style, typechecking and tests) locally?

  • AI was used to generate or assist with generating this PR.

OpenAI Codex GPT 5.6 Sol xhigh with local review and (macOS) testing


Copilot AI review requested due to automatic review settings July 28, 2026 08:57
@MikeMcQuaid
MikeMcQuaid enabled auto-merge July 28, 2026 08:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts Homebrew’s early startup environment handling to better preserve a user’s locale (while still forcing a safe UTF-8 fallback when needed), and to avoid expensive locale enumeration on Linux by reusing values already captured during bin/brew’s environment filtering.

Changes:

  • Update setup-locale to validate the active locale via locale charmap and avoid locale -a when the filtered locale values are already UTF-8.
  • Add bash-level specs covering setup-locale behavior (macOS charmap validation and Linux variable restoration).
  • Preserve LC_ALL/LC_CTYPE through bin/brew filtering by copying them into HOMEBREW_* variables.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
Library/Homebrew/utils/os.sh Restores locale from filtered copies and enforces UTF-8 by validating locale charmap.
Library/Homebrew/test/bash_spec.rb Adds tests for setup-locale behavior on macOS and Linux paths.
bin/brew Copies LC_ALL and LC_CTYPE into HOMEBREW_* before environment filtering.
Comments suppressed due to low confidence (2)

Library/Homebrew/utils/os.sh:110

  • On macOS, setup-locale never restores LANG/LC_* from the HOMEBREW_LANG/HOMEBREW_LC_* copies that bin/brew now exports, so after environment filtering the locale remains unset and the macOS branch will always force LC_ALL=en_US.UTF-8. This prevents preserving a valid user UTF-8 locale during startup and makes the new LC_ALL/LC_CTYPE exports in bin/brew ineffective on macOS.
  if [[ -z "${HOMEBREW_MACOS}" ]]
  then
    [[ -z "${HOMEBREW_LANG:-}" ]] || export LANG="${HOMEBREW_LANG}"
    [[ -z "${HOMEBREW_LC_CTYPE:-}" ]] || export LC_CTYPE="${HOMEBREW_LC_CTYPE}"
    [[ -z "${HOMEBREW_LC_ALL:-}" ]] || export LC_ALL="${HOMEBREW_LC_ALL}"

Library/Homebrew/utils/os.sh:120

  • locale charmap can print warnings to stderr when the locale variables are invalid (the broken-locale scenario this code is trying to handle). Because setup-locale runs during brew startup, those warnings can leak into user output. Suppressing stderr for the charmap probe keeps startup quiet while still letting you detect non-UTF-8.
    if [[ -z "${LC_ALL:-${LC_CTYPE:-${LANG:-}}}" ]] || [[ "$(locale charmap)" != "UTF-8" ]]
    then
      export LC_ALL="en_US.UTF-8"
    fi
  else

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Library/Homebrew/utils/os.sh Outdated
Comment thread Library/Homebrew/test/bash_spec.rb
- Validate the locale charmap instead of trusting its name.
- Reuse filtered Linux locale settings to avoid `locale -a`.
@MikeMcQuaid
MikeMcQuaid force-pushed the improve-locale-startup branch from 98dc8ab to 059785d Compare July 28, 2026 11:03
@MikeMcQuaid
MikeMcQuaid added this pull request to the merge queue Jul 28, 2026
Merged via the queue into main with commit 97b2643 Jul 28, 2026
42 checks passed
@MikeMcQuaid
MikeMcQuaid deleted the improve-locale-startup branch July 28, 2026 11:45
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.

3 participants