biomejs/biome · error
empty file
Error message
empty file
What it means
Panic in the Grit format-on-type handler (grit.rs:460). On a textDocument/onTypeFormatted request, Biome finds the token at the cursor via tree.token_at_offset(offset); TokenAtOffset::None means the document contains no tokens at all (empty file). The arm's comment says 'File is empty, do nothing' but the code panics, so an on-type format request on an empty Grit pattern file crashes this path.
Source
Thrown at crates/biome_service/src/file_handlers/grit.rs:460
settings: &SettingsWithEditor,
offset: TextSize,
workspace_db: WorkspaceDb,
) -> Result<Printed, WorkspaceError> {
let options = resolve_format_options(biome_path, document_file_source, settings, &workspace_db);
let tree = parse.syntax(&workspace_db);
let range = tree.text_range();
if offset < range.start() || offset > range.end() {
return Err(WorkspaceError::FormatError(FormatError::RangeError {
input: TextRange::at(offset, TextSize::from(0)),
tree: range,
}));
}
let token = match tree.token_at_offset(offset) {
// File is empty, do nothing
TokenAtOffset::None => panic!("empty file"),
TokenAtOffset::Single(token) => token,
// The cursor should be right after the closing character that was just typed,
// select the previous token as the correct one
TokenAtOffset::Between(token, _) => token,
};
if token.text_trimmed_range().end() != offset {
return Ok(format_on_type_noop(offset));
}
if !matches_on_type_char(token.text_trimmed()) {
return Ok(format_on_type_noop(offset));
}
let root_node = match token.parent() {
Some(node) => node,
None => panic!("found a token with no parent"),
};View on GitHub (pinned to 405dedb0ff)
Solutions
- Update Biome and report the panic upstream if it persists (https://github.com/biomejs/biome/issues)
- Do not send textDocument/onTypeFormatted for empty documents from the client
- In a fork: TokenAtOffset::None => return Ok(format_on_type_noop(offset))
Example fix
// before
TokenAtOffset::None => panic!("empty file"),
// after
TokenAtOffset::None => return Ok(format_on_type_noop(offset)), Defensive patterns
Strategy: validation
Validate before calling
// LSP client (TypeScript) - skip on-type formatting for empty Grit files if (document.getText().length === 0) return;
Type guard
const isEmptyDocument = (document: TextDocument): boolean => document.getText().length === 0;
Prevention
- Send onTypeFormatted only for documents containing at least one character
- Do not fire on-type format from document-open hooks on empty files
- Watch Biome releases for the empty-file no-op fix
When it happens
Trigger: An LSP client sends onTypeFormatted for a zero-length .grit document: offset 0 passes the range guard over tree range 0..0, token_at_offset(0) returns None, and the panic fires.
Common situations: Editors firing on-type formatting on newly created empty .grit files; Grit authoring integrations that send format requests on document open.
Related errors
AI-assisted analysis of biomejs/biome@405dedb0ff (2026-08-20).
Data as JSON: /api/errors/195649c59a551d8a.
Report an issue: GitHub.