{"record":{"id":"9e18120a16a2f753","repo":"facebook/relay","slug":"parse-error","errorCode":null,"errorMessage":"Parse error: {:?}","messagePattern":"Parse error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/crates/prettier-format/src/lib.rs","lineNumber":29,"sourceCode":"//! and JavaScript files containing graphql template literals using\n//! the Rust prettier-style printing utilities.\n\nuse common::SourceLocationKey;\nuse extract_graphql::JavaScriptSourceFeature;\nuse extract_graphql::extract;\nuse graphql_syntax::parse_document;\nuse graphql_text_printer::prettier_print_document;\n\n/// Format a GraphQL source string using prettier-compatible formatting.\n///\n/// Parses the input as a GraphQL executable document and returns\n/// the formatted output.\npub fn format_graphql_source(\n    source: &str,\n    source_location: SourceLocationKey,\n) -> anyhow::Result<String> {\n    let document = parse_document(source, source_location)\n        .map_err(|errors| anyhow::anyhow!(\"Parse error: {:?}\", errors))?;\n    let leading_comments = extract_leading_comments(source);\n    let formatted = prettier_print_document(&document);\n    if leading_comments.is_empty() {\n        Ok(formatted)\n    } else {\n        Ok(format!(\"{}\\n{}\", leading_comments, formatted))\n    }\n}\n\n/// Extract comment and blank lines from the beginning of a GraphQL source.\n///\n/// Returns everything up to (and including) the last line of the leading\n/// comment block. A \"leading comment block\" is a contiguous run of lines\n/// starting from the top of the file where every line is either a comment\n/// (starts with `#`) or blank/whitespace-only.\nfn extract_leading_comments(source: &str) -> &str {\n    let mut end = 0;\n    for line in source.lines() {","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/facebook/relay/blob/668b1b85e06261aa3b58dabfc51f8b5524a70955/compiler/crates/prettier-format/src/lib.rs#L11-L47","documentation":"format_graphql_source in prettier-format parses the GraphQL source with parse_document; on parse errors it formats the error list with {:?} into an anyhow error. It means the GraphQL source string (e.g. embedded in a JS template literal) is not syntactically valid GraphQL and cannot be formatted.","triggerScenarios":"Calling format_graphql_source(source, source_location) — from format_js_file on Relay's graphql`` template contents, or in tests/main — where parse_document returns syntax errors: unbalanced braces, invalid selection syntax, truncated documents from bad extraction.","commonSituations":"Prettier plugin (format_js_file) formatting JS files whose graphql`` literals were mangled by earlier transforms; template literals with JS interpolation ${...} inside GraphQL text that the parser can't handle; truncated extraction from minified code.","solutions":["Decode the Debug-printed errors in the message; they identify the invalid token/span in the source.","Fix the GraphQL syntax inside the template literal / source string.","If the literal contains interpolation, confirm the extraction step handles it or skip formatting that literal.","Report the file and location via the passed SourceLocationKey when surfacing the error so users can find the broken literal."],"exampleFix":"// before\nformat_graphql_source(\"query { user { name }\", loc)?; // unbalanced brace\n// after\nformat_graphql_source(\"query { user { name } }\", loc)?;","handlingStrategy":"try-catch","validationCode":"// Validate embedded GraphQL before formatting (plugin-level guard)\nfn is_parseable(source: &str, loc: SourceLocationKey) -> bool {\n    parse_document(source, loc).is_ok()\n}","typeGuard":null,"tryCatchPattern":"match format_graphql_source(source, loc) {\n    Ok(out) => out,\n    Err(e) => {\n        // skip unparseable literals instead of failing the whole JS file\n        eprintln!(\"skipping unparseable graphql literal: {e}\");\n        source.to_string()\n    }\n}","preventionTips":["Run GraphQL linting on template literals before prettier passes.","Skip or special-case literals containing JS interpolation.","Keep source_location set so parse errors are traceable to real files."],"tags":["graphql","parse-error","prettier","formatting"],"backgroundTag":"graphql-parse-error","analyzedSha":"668b1b85e06261aa3b58dabfc51f8b5524a70955","analyzedAt":"2026-09-02T19:57:20.783Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}