evanw/esbuild · error · Error

Should be string or array of strings

Error message

Should be string or array of strings

What it means

Thrown by read_string() in scripts/terser-tests.js:285 as the final fallback. read_string() accepts only a SimpleStatement whose body is an AST_String or an AST_Array (of strings). If the statement is not a SimpleStatement, or its body is any other node type (number, regex, call, object literal, etc.), this error fires.

Source

Thrown at scripts/terser-tests.js:285

    }
    throw new Error("Should be boolean");
  }

  function read_string(stat) {
    if (stat.TYPE == "SimpleStatement") {
      var body = stat.body;
      switch (body.TYPE) {
        case "String":
          return body.value;
        case "Array":
          return body.elements.map(function (element) {
            if (element.TYPE !== "String")
              throw new Error("Should be array of strings");
            return element.value;
          }).join("\n");
      }
    }
    throw new Error("Should be string or array of strings");
  }

  function get_one_test(name, block) {
    var test = {
      name: name,
      options: {},
      reminify: true,
    };
    var tw = new U.TreeWalker(function (node, descend) {
      if (node instanceof U.AST_Assign) {
        if (!(node.left instanceof U.AST_SymbolRef)) {
          croak(node);
        }
        var name = node.left.name;
        test[name] = evaluate(node.right);
        return true;
      }
      if (node instanceof U.AST_LabeledStatement) {

View on GitHub (pinned to f6058f8364)

Solutions

  1. Wrap the value in quotes: `expect_exact: "var a=1;";`.
  2. If the value is already text but multi-line, use the array form: `expect_exact: ["line1", "line2"];`.
  3. Move genuinely non-string data to a field whose reader accepts that type (read_boolean for booleans).

Example fix

// before
expect_exact: 42;

// after
expect_exact: "42";
Defensive patterns

Strategy: type-guard

Validate before calling

function isAcceptableStringField(stat) {
  if (stat.TYPE !== 'SimpleStatement') return false;
  const b = stat.body;
  return b instanceof U.AST_String || (b instanceof U.AST_Array && b.elements.every(e => e instanceof U.AST_String));
}

Type guard

const isStringOrStringArray = (stat) =>
  stat.TYPE === 'SimpleStatement' &&
  (stat.body instanceof U.AST_String ||
   (stat.body instanceof U.AST_Array && stat.body.elements.every(e => e.TYPE === 'String')));

Try / catch

try { read_string(stat); }
catch (e) {
  if (/Should be string or array of strings/.test(e.message)) console.error('Use a string literal or array of string literals');
  throw e;
}

Prevention

When it happens

Trigger: Assigning a non-string value to a string field, e.g. `expect_exact: 42;`, `node_version: /v\d+/;`, or `expect: someVar;`. Also hit if the value is wrapped in a way that changes the node type away from a plain SimpleStatement.

Common situations: Contributor uses a number, regex literal, identifier, or function call where the harness expects a string or array of strings (for fields routed through read_string such as expect_exact / node_version).

Related errors


AI-assisted analysis of evanw/esbuild@f6058f8364 (2026-08-09). Data as JSON: /api/errors/e62f0098c26d81d9. Report an issue: GitHub.