Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
feat(parser): add HCL/Terraform parser support (.tf, .tfvars, .hcl) (closes #134) (#257)
Joseph Mikhail1ce2daadd43b
diff --git a/core-ingestion/package.json b/core-ingestion/package.jsonindex a3f2da50..b8064d34 100644--- a/core-ingestion/package.json+++ b/core-ingestion/package.json@@ -33,6 +33,7 @@},"optionalDependencies": {"@davisvaughan/tree-sitter-r": "^1.2.0",+ "@tree-sitter-grammars/tree-sitter-hcl": "^1.2.0","@tree-sitter-grammars/tree-sitter-lua": "^0.2.0","@tree-sitter-grammars/tree-sitter-xml": "^0.7.0","@tree-sitter-grammars/tree-sitter-zig": "^1.1.2",diff --git a/core-ingestion/src/__tests__/queries.hcl.test.ts b/core-ingestion/src/__tests__/queries.hcl.test.tsnew file mode 100644index 00000000..058cdd5e--- /dev/null+++ b/core-ingestion/src/__tests__/queries.hcl.test.ts@@ -0,0 +1,40 @@+import { describe, it, expect } from 'vitest';+import { parseFile } from '../index.js';+import { SupportedLanguages } from '../languages.js';++const h = parseFile('/repo/m.tf', 'variable "x" {}\n');+const describeFn = h && h.language === SupportedLanguages.HCL ? describe : describe.skip;++describeFn('HCL queries', () => {+ it('detects HCL by .tf / .tfvars / .hcl', () => {+ expect(parseFile('/r/main.tf', 'variable "x" {}')!.language).toBe(SupportedLanguages.HCL);+ expect(parseFile('/r/vars.tfvars', 'x = 1')!.language).toBe(SupportedLanguages.HCL);+ });++ it('captures resource/variable/output/module/data blocks by primary name', () => {+ const result = parseFile('/r/main.tf', `+resource "aws_instance" "web" { ami = "ami-123" }+variable "region" { default = "us-east-1" }+output "ip" { value = aws_instance.web.private_ip }+data "aws_ami" "ubuntu" { most_recent = true }+`);+ expect(result).not.toBeNull();+ const names = result!.entities.map(e => e.name);+ expect(names).toEqual(expect.arrayContaining(['web', 'region', 'ip', 'ubuntu']));+ expect(names).not.toContain('aws_instance');+ });++ it('emits IMPORTS for module source', () => {+ const result = parseFile('/r/main.tf', `+module "vpc" { source = "terraform-aws-modules/vpc/aws" }+module "db" { source = "./modules/db" }+`);+ expect(result).not.toBeNull();+ const imports = result!.relationships+ .filter(r => r.predicate === 'IMPORTS')+ .map(r => r.importRaw ?? r.dstName);+ expect(imports).toEqual(+ expect.arrayContaining(['terraform-aws-modules/vpc/aws', './modules/db']),+ );+ });+});diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.tsindex c8011c14..c122b9c0 100644--- a/core-ingestion/src/index.ts+++ b/core-ingestion/src/index.ts@@ -43,6 +43,8 @@ const Html = tryLoadGrammar('tree-sitter-html');// tree-sitter-xml exports { xml, dtd }; we use the xml sub-grammar.const XmlPkg = tryLoadGrammar('@tree-sitter-grammars/tree-sitter-xml');const Xml = XmlPkg?.xml ?? null;+const HclPkg = tryLoadGrammar('@tree-sitter-grammars/tree-sitter-hcl');+const Hcl = HclPkg?.default ?? HclPkg;// tree-sitter-sas uses ESM bindings with top-level await — incompatible with tryLoadGrammar (CJS require)let SAS: any = null;// @ts-ignore@@ -189,6 +191,7 @@ const GRAMMAR_MAP: Partial<Record<SupportedLanguages, any>> = {...(Zig ? { [SupportedLanguages.Zig]: Zig } : {}),...(Html ? { [SupportedLanguages.HTML]: Html } : {}),...(Xml ? { [SupportedLanguages.XML]: Xml } : {}),+ ...(Hcl ? { [SupportedLanguages.HCL]: Hcl } : {}),};// Capture key prefix → NodeKind stringdiff --git a/core-ingestion/src/languages.ts b/core-ingestion/src/languages.tsindex 69dd02f5..5f130e75 100644--- a/core-ingestion/src/languages.ts+++ b/core-ingestion/src/languages.ts@@ -29,6 +29,7 @@ export enum SupportedLanguages {Zig = 'zig',HTML = 'html',XML = 'xml',+ HCL = 'hcl',}const EXT_MAP: Record<string, SupportedLanguages> = {@@ -92,6 +93,9 @@ const EXT_MAP: Record<string, SupportedLanguages> = {'.props': SupportedLanguages.XML,'.targets': SupportedLanguages.XML,'.plist': SupportedLanguages.XML,+ '.tf': SupportedLanguages.HCL,+ '.tfvars': SupportedLanguages.HCL,+ '.hcl': SupportedLanguages.HCL,};export function languageFromPath(filePath: string): SupportedLanguages | null {diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.tsindex df17ea5d..ad87cf27 100644--- a/core-ingestion/src/queries.ts+++ b/core-ingestion/src/queries.ts@@ -1605,6 +1605,19 @@ export const R_QUERIES = `(#eq? @_src "source")) @import`;+// HCL / Terraform. Each labeled block (resource/data/variable/output/module/+// provider) is a definition named by its primary (last) label; a module's+// `source` attribute is an import to the referenced module.+export const HCL_QUERIES = `+((block (identifier) @_t (string_lit (template_literal) @name) . (block_start))+ (#match? @_t "^(resource|data|variable|output|module|provider)$")) @definition.class++((block (identifier) @_t+ (body (attribute (identifier) @_a+ (expression (literal_value (string_lit (template_literal) @import.source))))))+ (#eq? @_t "module") (#eq? @_a "source")) @import+`;+// XML. The useful cross-file signal in XML config is its dependency references,// so the queries extract IMPORTS from the attributes that name another file or// package: href (XInclude), resource (Spring), schemaLocation (XSD), src, and@@ -1754,5 +1767,6 @@ export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {[SupportedLanguages.Zig]: ZIG_QUERIES,[SupportedLanguages.HTML]: HTML_QUERIES,[SupportedLanguages.XML]: XML_QUERIES,+ [SupportedLanguages.HCL]: HCL_QUERIES,};