Repository navigation
Description
Summary
A raw NUL (U+0000) inside a string literal is legal Java, but prettier-plugin-java 2.9.0 onwards fails to parse a file containing one. 2.8.1 and earlier parse it fine.
This is the same family as #1046 — the web-tree-sitter grammar being stricter than the JLS — but a different rule: that one was identifiers, this is string literal contents, so #1058 does not cover it.
Reproduction
The literal cannot be pasted into an issue body, so create it with:
python3 -c "open('A.java','wb').write(b'class A {\n String s = \"foo' + bytes([0]) + b'bar\";\n}\n')"
npx prettier --plugin=prettier-plugin-java --write A.javaActual
[error] A.java: Error: Failed to parse: ... (string_literal (string_fragment) (ERROR (UNEXPECTED ...)))
prettier --write exits 2 and leaves the file untouched.
Expected
The file formats, as it does on 2.8.1 and earlier.
Why this is valid Java
JLS 3.10.5 defines a StringCharacter as any InputCharacter except " and \, where InputCharacter excludes only CR and LF. U+0000 is therefore permitted, and javac compiles the file without complaint. We have a test file carrying one that has compiled and passed in CI for four months.
Bisection
Holding prettier at 3.4.2 so the plugin is the only variable, against the same real-world file:
| prettier-plugin-java | result |
|---|---|
| 2.6.7 | parses OK |
| 2.7.4 | parses OK |
| 2.8.0 | parses OK |
| 2.8.1 | parses OK |
| 2.9.7 | Failed to parse |
| 2.10.0 | Failed to parse |
| 2.10.3 | Failed to parse |
Consistent with the parser switch in 2.9.
Impact
The failure is a hard stop rather than a formatting difference: prettier cannot format what it cannot parse, so --write cannot repair it and --check fails the commit. For anyone running prettier from a pre-commit hook, upgrading past 2.8.1 blocks commits on any file containing such a literal, pointing at a byte that is invisible in an editor and in a diff.
The workaround on our side is to write the escape \0 instead of the raw byte, which is better style regardless — noting that the six-character unicode escape for zero is not a substitute, since Java resolves unicode escapes before lexing and so reintroduces the raw NUL into the token stream.
This is presumably a tree-sitter-java-orchard issue rather than prettier-java's own code, but filing here as #1046 was, since this is where the regression surfaces.
Activity
jameshearttech commentedon Sep 14, 2026
Correction to the issue body: I attributed this to the Java grammar, and that is wrong.
tree-sitter-java-orchard'sstring_fragmentrule istoken.immediate(prec(1, /[^"\\]+/)), which does not exclude NUL. The grammar is not at fault and no grammar change can fix this.The constraint is a layer down, in tree-sitter's own lexer: it signals end-of-input by setting
lookaheadto\0. A real NUL byte in the source is therefore indistinguishable from EOF to any tree-sitter-generated lexer, independent of what the grammar's character classes say — which is why the failure surfaces as an unexpected-token error at that offset rather than as a rejected character. See tree-sitter/tree-sitter#1482 (end of input signalled to an external scanner as a\0lookahead character) and tree-sitter/tree-sitter#2490 (escape null bytes in generated C code).So this needs a fix, or at minimum a documented limitation, in tree-sitter core rather than in prettier-java or the Java grammar. Leaving it filed here since this is where the regression surfaces for users, same as #1046, but flagging the layer so nobody spends time patching the grammar's regex.
One more correction:
tree-sitter-java-orchardis hosted on Codeberg (grammar-orchard/tree-sitter-java-orchard), not GitHub, for anyone wanting to file there.The bisection, the JLS 3.10.5 point and the reproduction in the issue body are unaffected.
jtkiesel commentedon Sep 18, 2026
@jameshearttech Thank you for reporting this issue! Luckily, I was able to work around the fact that tree-sitter omits the null character when converting our grammar rules to C by using a character class range that explicitly includes
\x00(see here).