Skip to content

Commit 1ebb13f

Browse files
committed
Preserve extra RUBYOPT in precompile hook
1 parent dd064c3 commit 1ebb13f

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

react_on_rails/lib/generators/react_on_rails/templates/base/base/bin/shakapacker-precompile-hook

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,19 @@ def utf8_subprocess_env(extra = {})
5555
return extra.dup unless [Encoding::US_ASCII, Encoding::ASCII_8BIT].include?(Encoding.find("locale"))
5656

5757
# Merge the UTF-8 widening last so it is authoritative: a caller-supplied key cannot accidentally
58-
# drop the pin (callers pass functional flags like REACT_ON_RAILS_SKIP_VALIDATION here, not locale
59-
# vars). The pin is derived from the parent's own RUBYOPT, so existing non-encoding flags survive.
58+
# drop the pin. The pin is derived from the parent's RUBYOPT plus any caller-supplied RUBYOPT, so
59+
# existing non-encoding flags survive and explicit per-call flags are preserved.
6060
extra.merge(
6161
"LANG" => "C.UTF-8",
6262
"LC_ALL" => "C.UTF-8",
63-
"RUBYOPT" => utf8_widened_rubyopt(ENV.fetch("RUBYOPT", ""))
63+
"RUBYOPT" => utf8_widened_rubyopt(subprocess_rubyopt(extra))
6464
)
6565
end
6666

67+
def subprocess_rubyopt(extra)
68+
[ENV.fetch("RUBYOPT", nil), extra["RUBYOPT"]].compact.join(" ")
69+
end
70+
6771
# Prepend a UTF-8 external-encoding pin to RUBYOPT for the C/POSIX widening case — a portable
6872
# fallback for systems where the C.UTF-8 locale is not installed and LANG/LC_ALL alone would not
6973
# take effect. If RUBYOPT already pins an encoding it is an explicit, deliberate request that we

react_on_rails/spec/react_on_rails/shakapacker_precompile_hook_shared_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,13 @@ def bad_overrides.each_key
283283
end
284284
end
285285

286-
it "keeps the UTF-8 pin even when a caller passes RUBYOPT in extra" do
287-
# The widening is applied last (authoritative), so an extra RUBYOPT key cannot silently drop
288-
# the pin and leave the child on US-ASCII.
286+
it "keeps the UTF-8 pin while preserving caller-supplied RUBYOPT in extra" do
287+
# The widening is applied last (authoritative), and the caller's extra RUBYOPT is folded
288+
# into the widened value rather than silently dropping either set of flags.
289289
with_locale_encoding(Encoding::US_ASCII) do
290290
with_env("RUBYOPT" => "-W0") do
291+
expect(utf8_subprocess_env("RUBYOPT" => "-rbundler/setup")["RUBYOPT"])
292+
.to eq("-EUTF-8 -W0 -rbundler/setup")
291293
expect(utf8_subprocess_env("RUBYOPT" => nil)["RUBYOPT"]).to eq("-EUTF-8 -W0")
292294
end
293295
end

react_on_rails/spec/support/shakapacker_precompile_hook_shared.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,19 @@ def utf8_subprocess_env(extra = {})
6161
return extra.dup unless [Encoding::US_ASCII, Encoding::ASCII_8BIT].include?(Encoding.find("locale"))
6262

6363
# Merge the UTF-8 widening last so it is authoritative: a caller-supplied key cannot accidentally
64-
# drop the pin (callers pass functional flags like REACT_ON_RAILS_SKIP_VALIDATION here, not locale
65-
# vars). The pin is derived from the parent's own RUBYOPT, so existing non-encoding flags survive.
64+
# drop the pin. The pin is derived from the parent's RUBYOPT plus any caller-supplied RUBYOPT, so
65+
# existing non-encoding flags survive and explicit per-call flags are preserved.
6666
extra.merge(
6767
"LANG" => "C.UTF-8",
6868
"LC_ALL" => "C.UTF-8",
69-
"RUBYOPT" => utf8_widened_rubyopt(ENV.fetch("RUBYOPT", ""))
69+
"RUBYOPT" => utf8_widened_rubyopt(subprocess_rubyopt(extra))
7070
)
7171
end
7272

73+
def subprocess_rubyopt(extra)
74+
[ENV.fetch("RUBYOPT", nil), extra["RUBYOPT"]].compact.join(" ")
75+
end
76+
7377
# Prepend a UTF-8 external-encoding pin to RUBYOPT for the C/POSIX widening case — a portable
7478
# fallback for systems where the C.UTF-8 locale is not installed and LANG/LC_ALL alone would not
7579
# take effect. If RUBYOPT already pins an encoding it is an explicit, deliberate request that we

0 commit comments

Comments
 (0)