{"record":{"id":"1463d83caa571769","repo":"facebook/flow","slug":"failed-to-deserialize-ast","errorCode":null,"errorMessage":"failed to deserialize AST","messagePattern":"failed to deserialize AST","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"rust_port/crates/flow_heap_serialization/src/lib.rs","lineNumber":53,"sourceCode":"    DESERIALIZE_FILE_KEY.with(|k| *k.borrow_mut() = Some(file_key.dupe()));\n}\n\nfn clear_file_key() {\n    DESERIALIZE_FILE_KEY.with(|k| *k.borrow_mut() = None);\n}\n\npub fn serialize_ast(ast: &Program<Loc, Loc>) -> Vec<u8> {\n    let bytes = bincode::serde::encode_to_vec(ast, bincode::config::legacy())\n        .expect(\"failed to serialize AST\");\n    compress(&bytes)\n}\n\npub fn deserialize_ast(file_key: &FileKey, bytes: &[u8]) -> Arc<Program<Loc, Loc>> {\n    let decompressed = decompress(bytes);\n    set_file_key(file_key);\n    let (ast, _): (Program<Loc, Loc>, _) =\n        bincode::serde::decode_from_slice(&decompressed, bincode::config::legacy())\n            .expect(\"failed to deserialize AST\");\n    clear_file_key();\n    Arc::new(ast)\n}\n\npub fn serialize_docblock(docblock: &Docblock) -> Vec<u8> {\n    let bytes = bincode::serde::encode_to_vec(docblock, bincode::config::legacy())\n        .expect(\"failed to serialize Docblock\");\n    compress(&bytes)\n}\n\npub fn deserialize_docblock(file_key: &FileKey, bytes: &[u8]) -> Arc<Docblock> {\n    let decompressed = decompress(bytes);\n    set_file_key(file_key);\n    let (docblock, _): (Docblock, _) =\n        bincode::serde::decode_from_slice(&decompressed, bincode::config::legacy())\n            .expect(\"failed to deserialize Docblock\");\n    clear_file_key();\n    Arc::new(docblock)","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/facebook/flow/blob/f88ac94bcf6992f5d5a158854d94613ebb92c6e6/rust_port/crates/flow_heap_serialization/src/lib.rs#L35-L71","documentation":"Panics when bincode fails to decode the AST Program from already-decompressed bytes. The decoder relies on the thread-local DESERIALIZE_FILE_KEY (set internally just before) to resolve Loc offsets, and the encoding is tied to the exact serialized type layout. Failure therefore means corrupt data or a producer with a different schema/version.","triggerScenarios":"deserialize_ast on heap bytes written by a different flow binary version whose Program/Loc encoding changed; a file that decompresses but was truncated at a record boundary; a file whose recorded file_key differs structurally from what Loc decoding expects.","commonSituations":"Switching between flow builds/versions while reusing a saved state dir; partial writes from killed processes; test fixtures regenerated by an older toolchain.","solutions":["Invalidate the saved state/heap cache so ASTs are re-parsed from source","Ensure the process that produced the bytes and the process consuming them run the same binary version","Write state atomically (temp file + rename) to avoid truncated inputs"],"exampleFix":"// before\nlet ast = flow_heap_serialization::deserialize_ast(&file_key, &bytes);\n\n// after — recover by re-parsing the source file\nlet ast = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {\n    flow_heap_serialization::deserialize_ast(&file_key, &bytes)\n})) {\n    Ok(ast) => ast,\n    Err(_) => {\n        std::fs::remove_file(&cache_path).ok();\n        let ast = parse_source_to_ast(&file_key)?;\n        std::fs::write(&cache_path, flow_heap_serialization::serialize_ast(&ast)).ok();\n        ast\n    }\n};","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {\n    flow_heap_serialization::deserialize_ast(&file_key, &bytes)\n})) {\n    Ok(ast) => ast,\n    Err(_) => { invalidate_and_reparse(&file_key, &cache_path)? }\n}","preventionTips":["Treat every deserialize_* panic as a cache miss: delete the file, recompute, rewrite","Never mix saved states across flow builds; version-stamp cache paths","Regenerate serialized fixtures whenever Program or Loc serialization changes"],"tags":["rust","bincode","serialization","saved-state","ast"],"backgroundTag":"bincode-decode-error","analyzedSha":"f88ac94bcf6992f5d5a158854d94613ebb92c6e6","analyzedAt":"2026-08-20T10:41:37.992Z","schemaVersion":2},"datasetVersion":"2026-08-23T11:17:13.642Z"}