Adding a language
Ix parses source in core-ingestion/. Most
languages use a tree-sitter grammar and a query that names what to extract. Formats
with no useful grammar (YAML, SQL, Markdown and the rest) have a small hand-written parser instead. This page covers
both. Set up the repository first as described in Contributing.
The files to change
Section titled “The files to change”For a language parsed with a tree-sitter grammar:
core-ingestion/package.json: add the grammar package. Put it underdependenciesonly if it installs on every platform Ix supports. Otherwise put it underoptionalDependencies(see Optional grammars).core-ingestion/src/languages.ts: add a member to theSupportedLanguagesenum, and map each file extension to it inEXT_MAP. A file recognized by its whole name rather than its extension (likeMakefile) goes inFILE_NAME_MAP.ix-cli/src/cli/supported-extensions.ts: add the same extensions toSUPPORTED_EXTENSIONS(and whole file names toSUPPORTED_FILE_NAMES). This is the listix mapandix watchwalk, so a file type missing here is never parsed.extension-parity.test.tsfails if the two lists differ.core-ingestion/src/index.ts: load the grammar and add it toGRAMMAR_MAP. If the language has built-in functions that should not become call edges, add them tobuiltinsForLanguage.core-ingestion/src/queries.ts: write the query (see Writing the query) and add it toLANGUAGE_QUERIES.- Tests: see Testing.
- Docs: give the new enum value a display label in
LABELSinix-cli/scripts/check-language-docs.mjs, then runnode scripts/check-language-docs.mjs --writefromix-cli/. That regenerates the language lists inREADME.mdand on the Supported languages page and updates every language count.
For a format with a hand-written parser, skip steps 1, 4 and 5. Write a parse<Format>File(filePath, source)
function in core-ingestion/src/index.ts that returns a FileParseResult, add it to HAND_PARSERS, and map the
format to '' in LANGUAGE_QUERIES. The format then appears under “Config and data formats” in the generated lists.
Add a row for it to the format table on the Supported languages page saying what it
extracts. The check fails until that row exists.
SupportedLanguages keys several Record types, including LANGUAGE_QUERIES and the smoke test’s samples. Run
npm run typecheck in core-ingestion/ and ix-cli/ after adding the member; it lists the maps that still need an
entry.
Writing the query
Section titled “Writing the query”A query is a set of tree-sitter S-expression patterns. The capture names say what each match is:
| Capture | Meaning |
|---|---|
@definition.<kind> with @name |
A symbol. <kind> is class, interface, function, method, struct, enum, trait, module, const, type, macro and others; DEFINITION_KIND_MAP in index.ts maps each one to a graph node kind. |
@call with @call.name |
A call. The name is matched against the definitions to make a CALLS edge. |
@import with @import.source |
An import. The source is the module or path being imported. |
@heritage with @heritage.class and @heritage.extends or @heritage.implements |
Inheritance. |
LUA_QUERIES in queries.ts is a short, complete example to start from. To find the node types and field names a
grammar produces, parse a sample file and print the tree, for example with the tree-sitter CLI’s tree-sitter parse
or with parser.parse(source).rootNode.toString() in a Node script. A query that names a node type or field the grammar doesn’t have fails to compile, and then every
file in the language goes unparsed. queries.compile.test.ts catches that.
Optional grammars
Section titled “Optional grammars”Grammars are native modules. Some have no prebuilt binary for every OS and CPU, so a required dependency would break
npm install on those machines. Those grammars are optionalDependencies and load through one of two helpers in
index.ts:
tryLoadGrammar(language, pkg)for CommonJS packages, andtryImportGrammar(language, pkg)for packages that ship ES module bindings.
Both return null instead of throwing when the grammar can’t load, and record why. Add the grammar to GRAMMAR_MAP
only when it loaded, as the existing entries do: ...(Lua ? { [SupportedLanguages.Lua]: Lua } : {}). On a machine
without it, files in that language are counted as unparsed, and ix map and ix doctor name the grammar and the
reason. Read the comment at the top of index.ts before choosing between the bare package name and a path to its
binding: it explains which package shapes make Node print a deprecation warning.
Add an optional grammar to OPTIONAL_GRAMMAR_PACKAGES in core-ingestion/src/__tests__/helpers/grammars.ts, or a
required one to REQUIRED_GRAMMARS in the same file. The tests use that list to decide whether the grammar has to
load on the machine they run on.
Testing
Section titled “Testing”Run the core-ingestion tests with cd core-ingestion && npm test, and the CLI tests with cd ix-cli && npm test.
For a new language, add:
core-ingestion/src/__tests__/queries.<language>.test.ts: parse small snippets withparseFileand check the entities and relationships you expect, by name and kind. The existingqueries.*.test.tsfiles show the pattern.- A sample in
languages.smoke.test.ts: a few lines of real source that must yield at least one entity. It catches a grammar upgrade that renames a node type. - A probe path in
queries.compile.test.ts, for a language with a query.
When the extractor version changes
Section titled “When the extractor version changes”Each ingest patch is stamped with the extractor version (extractorName() in core-ingestion/src/patch-builder.ts).
ix map re-parses only files that changed since the last run, so a file that did not change keeps what the old parser
extracted until the extractor version changes. When it changes, the next ix map re-ingests every file once.
- Adding a new language or extension normally needs no change: those files were never in the graph, so the next
ix mapingests them as new files. - Changing what an existing parser or query extracts from files already in the graph (new kinds, renamed nodes, new edges) needs one, or existing graphs keep the old output for unchanged files.
The extractor version is the maintainers’ decision. Don’t change it in your pull request. Say in the description that the change alters existing output and needs a new extractor version.
CI enforces this. core-ingestion/test-fixtures/extractor-output.json records a digest of the golden patch output and
the lockfile version of every grammar under the current extractor version. If either changes while the version stays
the same, patchBuilder.golden.test.ts fails, and refreshing its snapshots with -u does not get past it. A maintainer
bumps the version and then rewrites the record with
IX_UPDATE_EXTRACTOR_OUTPUT=1 npx vitest run src/__tests__/patchBuilder.golden.test.ts in core-ingestion/.
