From 848a6d8465c5b5cca7820f9d51d880cc1191ee2a Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 1 Apr 2022 17:32:08 +0200 Subject: [PATCH] Default to a UTF-8 locale in Java stub template On non-macOS Unix, without any locale variable set, the OpenJDK defaults to using ASCII rather than UTF-8 as the encoding for file system paths (i.e., the value of the `sun.jnu.encoding` property). --- .../bazel/rules/java/java_stub_template.txt | 7 +++++ .../shell/bazel/unicode_filenames_test.sh | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt index 2aabedcdf942f2..e112c1950e2c51 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt @@ -362,6 +362,13 @@ if [ -z "$CLASSPATH_LIMIT" ]; then is_windows && CLASSPATH_LIMIT=7000 || CLASSPATH_LIMIT=120000 fi +# On non-macOS Unix, without any locale variable set, the OpenJDK defaults to +# using ASCII rather than UTF-8 as the encoding for file system paths (i.e., the +# value of the sun.jnu.encoding property). +if ! is_macos && ! is_windows && [ -z "$LC_ALL" ] && [ -z "$LC_CTYPE" ] && [ -z "$LANG" ]; then + export LC_ALL=C.UTF-8 +fi + if (("${#CLASSPATH}" > ${CLASSPATH_LIMIT})); then export JACOCO_IS_JAR_WRAPPED=1 create_and_run_classpath_jar diff --git a/src/test/shell/bazel/unicode_filenames_test.sh b/src/test/shell/bazel/unicode_filenames_test.sh index 7e38cca62e2bac..c7dbfde888fa98 100755 --- a/src/test/shell/bazel/unicode_filenames_test.sh +++ b/src/test/shell/bazel/unicode_filenames_test.sh @@ -206,4 +206,32 @@ function test_utf8_source_artifact_in_bep() { expect_log '"name":"pkg/srcs/ünïcödë fïlë.txt"' } +function test_utf8_filename_in_java_test() { + touch WORKSPACE + + mkdir pkg + + cat >pkg/BUILD <<'EOF' +java_test( + name = "Test", + srcs = ["Test.java"], + main_class = "Test", + use_testrunner = False, +) +EOF + + cat >pkg/Test.java <<'EOF' +import java.nio.file.Files; +import java.io.IOException; + +class Test { + public static void main(String[] args) throws IOException { + Files.createTempFile("æøå", null); + } +} +EOF + + bazel test //pkg:Test --test_output=errors 2>$TEST_log || fail "Test should pass" +} + run_suite "Tests for handling of Unicode filenames"