Skip to content

Remove regexes from compiled code#1197

Merged
ericmj merged 4 commits into
mainfrom
worktree-remove-regexes
Jul 1, 2026
Merged

Remove regexes from compiled code#1197
ericmj merged 4 commits into
mainfrom
worktree-remove-regexes

Conversation

@ericmj

@ericmj ericmj commented Jul 1, 2026

Copy link
Copy Markdown
Member

Regex literals are compiled into beam files at compile time, and the compiled PCRE pattern format is not portable across OTP versions. An archive built on OTP 28.1+ embeds patterns that are reloaded via :re.import/1, which does not exist on OTP <= 28.0, crashing with:

** (UndefinedFunctionError) function :re.import/1 is undefined or private
    (stdlib 7.0) :re.import(...)
    (hex 2.5.0) lib/hex/cooldown.ex:74: Hex.Cooldown.duration_to_seconds/1

(seen in https://github.com/elixir-lang/elixir/actions/runs/28501655392/job/84480157006)

Only two regexes existed in shipped code; both are replaced with plain pattern matching:

  • lib/hex/cooldown.ex: ~r/\A(\d+)(d|w|mo)\z/ becomes Integer.parse/1 with a leading-digit guard. Behavior-identical, verified on 18 edge cases including "+5d", "-5d", "1_0d", whitespace, and non-ASCII digits; characterization tests added.
  • lib/mix/tasks/hex.build.ex: ~r/\W\.DS_Store$/ becomes Path.basename(path) == ".DS_Store". User-supplied :exclude_patterns regexes still work since they are built in the user's project at runtime and never embedded in Hex's beams. Two intentional improvements: a root-level .DS_Store is now excluded too, and files merely ending in .DS_Store are no longer silently dropped.

A new lint test rejects any ~r/Regex./:re. usage under lib/ so regexes do not get reintroduced into the precompiled archive.

ericmj added 4 commits July 1, 2026 16:19
Regex literals are compiled into beam files, and the compiled PCRE
pattern format is not portable across OTP versions. An archive built
on OTP 28.1+ embeds patterns that load via :re.import/1, which does
not exist on OTP <= 28.0, crashing with UndefinedFunctionError.
Keeps the compiled archive free of embedded regex patterns, which are
not portable across OTP versions. User-supplied :exclude_patterns
regexes are unaffected; they are built in the user's project at
runtime and never embedded in Hex's beams.
@ericmj ericmj merged commit a3521ea into main Jul 1, 2026
22 checks passed
@ericmj ericmj deleted the worktree-remove-regexes branch July 1, 2026 23:07
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