Skip to content

Commit 5c67c84

Browse files
authored
fix: Force sun.jnu.encoding to be UTF-8 in images (#514)
Images pick up `sun.jnu.encoding` from the host build environment, which thus needs to be UTF-8 to be able to access paths with non-ASCII characters in them on Linux. Since Bazel does not inherit `LC_CTYPE` from the host, the JVM would otherwise default to an ASCII locale. Signed-off-by: Fabian Meumertzheim <[email protected]>
1 parent 977aa70 commit 5c67c84

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

internal/native_image/rules.bzl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def _graal_binary_implementation(ctx):
6464
or install a GraalVM `native-image` toolchain.
6565
""")
6666

67+
is_linux = ctx.target_platform_has_constraint(
68+
ctx.attr._linux_constraint[platform_common.ConstraintValueInfo],
69+
)
6770
is_macos = ctx.target_platform_has_constraint(
6871
ctx.attr._macos_constraint[platform_common.ConstraintValueInfo],
6972
)
@@ -103,6 +106,16 @@ def _graal_binary_implementation(ctx):
103106
if ctx.files.data:
104107
direct_inputs.extend(ctx.files.data)
105108

109+
env = native_toolchain.env
110+
111+
# The native image will use the same native encoding (as determined by "sun.jnu.encoding")
112+
# as the build environment, so we need to force a UTF-8 locale. On other platforms, the
113+
# encoding is always UTF-8 (on macOS since JEP 400) or determined by the active code page
114+
# on Windows.
115+
# TODO: Match on the exec platform instead once Graal supports cross-compilation.
116+
if is_linux:
117+
env["LC_CTYPE"] = "C.UTF-8"
118+
106119
# assemble final inputs
107120
inputs = depset(
108121
direct_inputs,
@@ -113,7 +126,7 @@ def _graal_binary_implementation(ctx):
113126
"executable": graal,
114127
"inputs": inputs,
115128
"mnemonic": "NativeImage",
116-
"env": native_toolchain.env,
129+
"env": env,
117130
"execution_requirements": {k: "" for k in native_toolchain.execution_requirements},
118131
"progress_message": "Native Image __target__ (__mode__) %{label}"
119132
.replace("__mode__", _build_action_message(ctx))

0 commit comments

Comments
 (0)