Skip to content

intermittent TypeError: NodeClass is not a constructor when getting tree nodes #196

Description

@verhovsky

To reproduce this:

cd /tmp
mkdir tree-sitter-nodeclass-repro
cd tree-sitter-nodeclass-repro/
npm init -y
npm install tree-sitter tree-sitter-bash tree-sitter-python

and then create this main.js

const Parser = require('tree-sitter');
const Bash = require('tree-sitter-bash');
const Python = require('tree-sitter-python');

const parser = new Parser();

// parser.setLanguage(Bash);
parser.setLanguage(Python);
const sourceCode = `0`;

const tree = parser.parse(sourceCode);
let i = 0;
try {
  for (;; i++) {
    for (const n of tree.rootNode.namedChildren) {}
  }
} catch (e) {
  console.log(i + " iterations");
  console.log(e);
  process.exit();
}

Then run it

while true; do
  timeout 1 node main.js
done

If the error doesn't happen after about a second, it doesn't seem to happen ever (I left node main.js running for 10 minutes). It can take a few tries, but eventually you will see this error

769 iterations
TypeError: NodeClass is not a constructor
    at unmarshalNode (/private/tmp/tree-sitter-nodeclass-repro/node_modules/tree-sitter/index.js:783:18)
    at Tree.get (/private/tmp/tree-sitter-nodeclass-repro/node_modules/tree-sitter/index.js:25:14)
    at Object.<anonymous> (/private/tmp/tree-sitter-nodeclass-repro/main.js:15:26)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49

It happens when accessing .children as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions