tirth8205/code-review-graph · error · ValueError

empty JSON document

Error message

empty JSON document

What it means

Raised by _find_value when the token list produced by the JSONC tokenizer is empty — there is no JSON content at all to search. The removal machinery refuses to operate on a document with zero tokens, since there is nothing to locate or delete.

Source

Thrown at code_review_graph/uninstall.py:315

        raise ValueError("expected JSON array")
    elements: list[_Element] = []
    cursor = index + 1
    while cursor < len(tokens) and tokens[cursor].kind != "]":
        if tokens[cursor].kind == ",":  # trailing comma
            cursor += 1
            continue
        value_end = _skip_value(tokens, cursor)
        comma_index = value_end if (
            value_end < len(tokens) and tokens[value_end].kind == ","
        ) else None
        elements.append(_Element(cursor, value_end, comma_index))
        cursor = value_end + 1 if comma_index is not None else value_end
    return elements


def _find_value(tokens: Sequence[_Token], path: Sequence[str | int]) -> int:
    if not tokens:
        raise ValueError("empty JSON document")
    current = 0
    for component in path:
        if isinstance(component, str):
            member = next(
                (item for item in _object_members(tokens, current) if item.key == component),
                None,
            )
            if member is None:
                raise KeyError(component)
            current = member.value_index
        else:
            elements = _array_elements(tokens, current)
            if component < 0 or component >= len(elements):
                raise IndexError(component)
            current = elements[component].value_index
    return current

View on GitHub (pinned to b58668751a)

Solutions

  1. Check the file content is non-empty and contains actual JSON before invoking removal
  2. If only comments remain, treat the file as already-cleaned and skip removal (optionally delete the file if that's the uninstall policy)
  3. Verify the correct file path is being read
  4. Guard with a token/parse pre-check and treat empty documents as a no-op

Example fix

# before
_remove_jsonc_paths("" , [("key",)])  # -> ValueError: empty JSON document

# after
if not text.strip():
    return  # nothing to remove
_remove_jsonc_paths(text, [("key",)])
Defensive patterns

Strategy: type-guard

Validate before calling

def has_json_content(text: str) -> bool:
    import re
    stripped = re.sub(r"//[^\n]*|/\*.*?\*/", "", text, flags=re.S).strip()
    return len(stripped) > 0

Type guard

def is_editable_jsonc(text: str) -> bool:
    import re
    return bool(re.sub(r"//[^\n]*|/\*.*?\*/", "", text, flags=re.S).strip())

Try / catch

try:
    _remove_jsonc_paths(text, paths)
except ValueError as e:
    if 'empty JSON document' in str(e):
        return  # nothing to do

Prevention

When it happens

Trigger: Calling the removal API on an empty string, a file containing only whitespace, or a file consisting solely of comments (which the tokenizer strips). Also possible if the wrong file path was read (empty/blank content).

Common situations: Config file was emptied by a previous failed uninstall or manual cleanup; file contains only a comment header; code read the wrong path (missing file handled elsewhere, blank file not); race where another process truncated the file.

Related errors


AI-assisted analysis of tirth8205/code-review-graph@b58668751a (2026-08-28). Data as JSON: /api/errors/cb251db2ae4a935f. Report an issue: GitHub.