facebook/flow · error · Error

Expected File, got ${deserialized.type}

Error message

Expected File, got ${deserialized.type}

What it means

The flow-parser's public parse() API deserializes an AST from the WASM parser and, when the `babel: true` option is set, expects the top-level node to be a Babel-style 'File' node. If the deserialized root has any other type, the parser's internal contract with the WASM output is broken, so it throws rather than returning a malformed AST.

Source

Thrown at packages/flow-parser/oxidized-src/FlowParser.js:347

      // $FlowExpectedError[prop-missing] SyntaxError loc is a parser extension.
      syntaxError.loc = {
        line: flowParseResult_getErrorLine(parseResult),
        column: flowParseResult_getErrorColumn(parseResult),
      };
      throw syntaxError;
    }

    const deserialized = new FlowParserDeserializer(
      programBuffer,
      flowParseResult_getPositionBuffer(parseResult),
      flowParseResult_getPositionBufferSize(parseResult),
      flowParseResult_getStringBuffer(parseResult),
      FlowParserWASM,
      options,
    ).deserialize();
    if (options.babel === true) {
      if (deserialized.type !== 'File') {
        throw new Error(`Expected File, got ${deserialized.type}`);
      }
      return deserialized;
    }
    if (deserialized.type !== 'Program') {
      throw new Error(`Expected Program, got ${deserialized.type}`);
    }
    const ast = deserialized;

    // Wire→ESTree loc/range normalization. The wire format carries
    // `rangeStart` / `rangeEnd` on the loc object; ESTree consumers expect a
    // `range: [start, end]` array on the *node* itself, plus a clean
    // `loc: { source, start, end }`. This walk is the parser-level analog of
    // upstream HermesToESTreeAdapter.transform()'s mandatory range conversion
    // (HermesToESTreeAdapter.js:39 — `node.range = [loc.rangeStart, loc.rangeEnd]`).
    // Lives here (not in src/index.js) so callers that bypass the public
    // hermes-parser-compatibility wrapper (e.g. the wasm fixture runner that
    // tests raw Flow ESTree parity with the OCaml parser) still get the
    // canonical ESTree loc/range shape.

View on GitHub (pinned to 5c86586199)

Solutions

  1. Ensure the flow-parser package is installed intact and a single version: reinstall node_modules so FlowParser.js and FlowParserWASM come from the same build.
  2. Verify the options you pass to parse(); with babel:true the WASM build must emit File roots — check your wrapper/build doesn't strip or alter the root.
  3. If you only need ESTree output, drop babel:true so the 'Program' branch is used instead.

Example fix

// before
const ast = flow.parse(src, { babel: true });
// after (if ESTree Program is what you consume)
const ast = flow.parse(src); // returns Program; no File-type expectation
Defensive patterns

Strategy: type-guard

Validate before calling

const ast = flow.parse(src, { babel: true });
if (ast.type !== 'File') {
  throw new Error('Parser returned non-File root; check flow-parser version integrity');
}

Type guard

const isBabelFile = (n) => n != null && typeof n === 'object' && n.type === 'File';

Try / catch

try {
  const ast = flow.parse(src, { babel: true });
} catch (err) {
  if (String(err.message).startsWith('Expected File')) {
    console.error('flow-parser build mismatch; reinstall node_modules');
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling parse() (parse, the public entry) with options.babel === true while the WASM deserializer returns a root node whose type is not 'File' — e.g. a version mismatch between the JS wrapper and the compiled FlowParserWASM, or corrupted/unexpected wire output.

Common situations: Mixing flow-parser JS code from one version with WASM artifacts from another build; custom builds of the WASM parser; caching deserialized ASTs across parser upgrades.

Understand the failure class

Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.

Related errors


AI-assisted analysis of facebook/flow@5c86586199 (2026-09-08). Data as JSON: /api/errors/c07c65cadeed8e0e. Report an issue: GitHub.