windmill-labs/windmill · error

Failed to parse code

Error message

Failed to parse code

What it means

After set_language succeeds, parse_r_sig_meta calls parser.parse(code, None) to build the R syntax tree. tree-sitter's parse returns Option<Tree> and is None only when the parser could not allocate or the parser is in an invalid state — the code maps that to 'Failed to parse code'. Unlike most parsers, this does NOT fire for syntactically invalid R code (tree-sitter produces an error-containing tree instead).

Source

Thrown at backend/parsers/windmill-parser-r/src/lib.rs:23

use anyhow::anyhow;
use serde_json::Value;
use tree_sitter::Node;
use tree_sitter::Range;
use windmill_parser::json_to_typ;
use windmill_parser::Arg;
use windmill_parser::MainArgSignature;

pub fn parse_r_sig_meta(code: &str) -> anyhow::Result<MainArgSignature> {
    let mut parser = tree_sitter::Parser::new();
    let language = tree_sitter_r::LANGUAGE;
    parser
        .set_language(&language.into())
        .map_err(|e| anyhow!("Error setting R as language: {e}"))?;

    let tree = parser
        .parse(code, None)
        .ok_or(anyhow!("Failed to parse code"))?;
    let root_node = tree.root_node();

    let args = find_main_signature(root_node, code)?;
    let main_sig = MainArgSignature {
        star_args: false,
        star_kwargs: false,
        args: args.unwrap_or_default(),
        has_preprocessor: None,
        auto_kind: None,
        ..Default::default()
    };

    Ok(main_sig)
}

pub fn parse_r_signature(code: &str) -> anyhow::Result<MainArgSignature> {
    Ok(parse_r_sig_meta(code)?)
}

View on GitHub (pinned to e474e8803c)

Solutions

  1. Retry the operation — the failure is typically transient (allocation), unlike a syntax error.
  2. Check memory availability where the backend runs; reduce the size of the R file if it is extraordinarily large.
  3. Verify the tree-sitter-r grammar loads correctly (if error 828 was patched around, this may be the follow-on symptom).
  4. If reproducible on small inputs, file a backend issue with the code snippet — the parser should never return None for normal scripts.
Defensive patterns

Strategy: retry

Validate before calling

// tree-sitter returns None only on allocation/invalid-state failures, so no
// content pre-check helps; guard on size and retry:
function canAttemptRParse(code) {
  if (code.length > 5_000_000) throw new Error('R file too large for signature parsing');
  return true;
}

Try / catch

match parse_r_signature(code) {
    Err(e) if e.to_string().contains("Failed to parse code") => {
        // transient (allocation) — retry once before failing
        parse_r_signature(code)
    }
    other => other,
}

Prevention

When it happens

Trigger: Calling parse_r_signature when the underlying tree-sitter parse call returns None: out-of-memory during tree construction, or a parser left in a bad state after set_language — environmental/resource conditions, not R syntax errors.

Common situations: Parsing under severe memory pressure; extremely large R files exceeding allocation limits; a broken grammar load that set_language silently accepted in some builds.

Understand the failure class

Related errors


AI-assisted analysis of windmill-labs/windmill@e474e8803c (2026-09-03). Data as JSON: /api/errors/ef9153f69cddca80. Report an issue: GitHub.