Skip to content

Write native configuration files as UTF-8 - #36972

Closed
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/filenativeconfigwriter-utf8
Closed

junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/filenativeconfigwriter-utf8

Conversation

@junhyeong9812

Copy link
Copy Markdown
Contributor

Overview

A small consistency/hardening change: make FileNativeConfigurationWriter always write the GraalVM native-image configuration files (reachability-metadata.json, etc.) as UTF-8.

This is a minor edge-case fix rather than a critical bug; the trigger is narrow (see below), so please feel free to decline if you consider it not worth the change.

Problem

writeTo uses a plain FileWriter:

try (FileWriter out = new FileWriter(file)) {
    writer.accept(createJsonWriter(out));
}

FileWriter without an explicit charset encodes using the JVM platform default charset, which is not UTF-8 on every supported configuration (for example a Windows JVM, where the default is a code-page charset such as windows-1252 prior to JDK 18 / JEP 400). BasicJsonWriter passes non-ASCII characters through unescaped, so the on-disk encoding is decided entirely by the writer's charset. GraalVM expects these files to be UTF-8, so on a non-UTF-8 platform non-ASCII characters in resource patterns or bundle names could be written with the wrong encoding.

The conditions are narrow (a non-UTF-8 default charset combined with non-ASCII content), so this is more of a hardening/consistency improvement than a frequently hit bug.

Fix

Specify StandardCharsets.UTF_8 explicitly:

try (FileWriter out = new FileWriter(file, StandardCharsets.UTF_8)) {
    writer.accept(createJsonWriter(out));
}

This is consistent with the explicit UTF-8 usage already present in the aot.generate package.

A test writes a resource pattern containing a non-ASCII character and asserts that the raw file bytes contain its UTF-8 byte sequence. Note that on a JVM whose default charset is already UTF-8 (such as the CI environment) the test passes both before and after the change; it serves as a guard on non-UTF-8 platforms and documents the intended encoding. The correctness of the fix itself rests on the FileWriter(File, Charset) contract rather than on reproducing a non-UTF-8 default in CI.

FileNativeConfigurationWriter wrote native-image configuration files
using a plain FileWriter, which encodes with the JVM platform default
charset. On a non-UTF-8 platform (for example a Windows JVM, where the
default charset is not UTF-8 prior to JDK 18) non-ASCII characters in
resource patterns or bundle names were written with the wrong encoding,
while GraalVM expects the configuration files to be UTF-8.

Specify StandardCharsets.UTF_8 explicitly so the files are always
written as UTF-8, consistent with the UTF-8 usage already present in
the aot.generate package.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 27, 2026
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing labels Jun 27, 2026
@sbrannen sbrannen self-assigned this Jun 27, 2026
@sbrannen sbrannen added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 27, 2026
@sbrannen sbrannen added this to the 7.0.9 milestone Jun 27, 2026
sbrannen added a commit that referenced this pull request Jun 27, 2026
@sbrannen sbrannen closed this in 872b1ad Jun 27, 2026
@sbrannen

Copy link
Copy Markdown
Member

Good catch! 👍

This has been merged into 7.0.x and main.

Thanks

@junhyeong9812

junhyeong9812 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the quick review and merge, @sbrannen. Glad the single root-cause fix did the job. I really appreciate your time and feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants