Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat(core-ingestion): Makefile parser via tree-sitter-make (#211)

NKlein77ad4d477ea1a5
diff --git a/core-ingestion/.npmrc b/core-ingestion/.npmrc
new file mode 100644
index 00000000..521a9f7c
--- /dev/null
+++ b/core-ingestion/.npmrc
@@ -0,0 +1 @@
+legacy-peer-deps=true
diff --git a/core-ingestion/package-lock.json b/core-ingestion/package-lock.json
index 8a827bff..8d1c71d7 100644
--- a/core-ingestion/package-lock.json
+++ b/core-ingestion/package-lock.json
@@ -32,6 +32,7 @@
},
"optionalDependencies": {
"tree-sitter-kotlin": "^0.3.8",
+ "tree-sitter-make": "^1.1.1",
"tree-sitter-swift": "^0.6.0"
}
},
@@ -1326,6 +1327,26 @@
"license": "MIT",
"optional": true
},
+ "node_modules/tree-sitter-make": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/tree-sitter-make/-/tree-sitter-make-1.1.1.tgz",
+ "integrity": "sha512-szHESi1wObt07uM5a5mwkMjbLHOcgiS5G2YQjU8O+Tfv9RylQBA0umV25lf6gSOefwTsPBnTJGn4SFFdHAVwMg==",
+ "hasInstallScript": true,
+ "license": "MIT",
+ "optional": true,
+ "dependencies": {
+ "node-addon-api": "^8.3.0",
+ "node-gyp-build": "^4.8.4"
+ },
+ "peerDependencies": {
+ "tree-sitter": "^0.22.1"
+ },
+ "peerDependenciesMeta": {
+ "tree-sitter": {
+ "optional": true
+ }
+ }
+ },
"node_modules/tree-sitter-php": {
"version": "0.23.12",
"resolved": "https://registry.npmjs.org/tree-sitter-php/-/tree-sitter-php-0.23.12.tgz",
diff --git a/core-ingestion/package.json b/core-ingestion/package.json
index 9bc48a7e..52d2c7de 100644
--- a/core-ingestion/package.json
+++ b/core-ingestion/package.json
@@ -33,7 +33,8 @@
},
"optionalDependencies": {
"tree-sitter-kotlin": "^0.3.8",
- "tree-sitter-swift": "^0.6.0"
+ "tree-sitter-swift": "^0.6.0",
+ "tree-sitter-make": "^1.1.1"
},
"devDependencies": {
"@emnapi/core": "1.9.2",
diff --git a/core-ingestion/src/__tests__/queries.makefile.test.ts b/core-ingestion/src/__tests__/queries.makefile.test.ts
new file mode 100644
index 00000000..5acc46cf
--- /dev/null
+++ b/core-ingestion/src/__tests__/queries.makefile.test.ts
@@ -0,0 +1,101 @@
+import { describe, expect, it } from 'vitest';
+
+import { parseFile } from '../index.js';
+
+describe('Makefile queries', () => {
+ it('captures rule targets, variable assignments, and includes', () => {
+ const result = parseFile(
+ '/repo/Makefile',
+ `
+CC := gcc
+CFLAGS = -Wall
+
+all: main.o utils.o
+\t$(CC) $(CFLAGS) -o app main.o utils.o
+
+main.o: main.c
+\t$(CC) -c main.c
+
+include config.mk
+ `,
+ );
+
+ expect(result).not.toBeNull();
+ expect(result!.entities.map(e => e.name)).toEqual(
+ expect.arrayContaining(['CC', 'CFLAGS', 'all', 'main.o']),
+ );
+ expect(result!.relationships).toContainEqual({
+ srcName: 'Makefile',
+ dstName: 'all',
+ predicate: 'CONTAINS',
+ });
+ expect(result!.relationships).toContainEqual({
+ srcName: 'Makefile',
+ dstName: 'CC',
+ predicate: 'CONTAINS',
+ });
+ expect(result!.relationships).toContainEqual({
+ srcName: 'Makefile',
+ dstName: 'config.mk',
+ predicate: 'IMPORTS',
+ });
+ });
+
+ it('does not emit .PHONY as a target entity', () => {
+ const result = parseFile(
+ '/repo/Makefile',
+ `
+.PHONY: all clean
+
+all:
+\t@echo "building"
+
+clean:
+\trm -rf build/
+ `,
+ );
+
+ expect(result).not.toBeNull();
+ expect(result!.entities.map(e => e.name)).not.toContain('.PHONY');
+ expect(result!.entities.map(e => e.name)).toContain('all');
+ expect(result!.entities.map(e => e.name)).toContain('clean');
+ });
+
+ it('does not emit GNU special targets (.SUFFIXES/.DEFAULT/etc.) as entities', () => {
+ const result = parseFile(
+ '/repo/Makefile',
+ `
+.SUFFIXES:
+.DEFAULT:
+.PRECIOUS: app
+.EXPORT_ALL_VARIABLES:
+
+app:
+\t$(CC) -o app main.c
+ `,
+ );
+
+ expect(result).not.toBeNull();
+ const names = result!.entities.map(e => e.name);
+ expect(names).not.toContain('.SUFFIXES');
+ expect(names).not.toContain('.DEFAULT');
+ expect(names).not.toContain('.PRECIOUS');
+ expect(names).not.toContain('.EXPORT_ALL_VARIABLES');
+ expect(names).toContain('app'); // real targets still captured
+ });
+
+ it('captures .mk files by extension', () => {
+ const result = parseFile(
+ '/repo/config.mk',
+ `
+PREFIX := /usr/local
+BINDIR = $(PREFIX)/bin
+ `,
+ );
+
+ expect(result).not.toBeNull();
+ expect(result!.entities.map(e => e.name)).toEqual(
+ expect.arrayContaining(['PREFIX', 'BINDIR']),
+ );
+ });
+});
\ No newline at end of file
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index c52644dd..df4aa259 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -33,6 +33,7 @@ function tryLoadGrammar(pkg: string): any {
}
const Kotlin = tryLoadGrammar('tree-sitter-kotlin');
const Swift = tryLoadGrammar('tree-sitter-swift');
+const Make = tryLoadGrammar('tree-sitter-make');
import { SupportedLanguages, languageFromPath } from './languages.js';
import { LANGUAGE_QUERIES } from './queries.js';
@@ -114,6 +115,7 @@ const GRAMMAR_MAP: Partial<Record<SupportedLanguages, any>> = {
[SupportedLanguages.Scala]: Scala,
...(Kotlin ? { [SupportedLanguages.Kotlin]: Kotlin } : {}),
...(Swift ? { [SupportedLanguages.Swift]: Swift } : {}),
+ ...(Make ? { [SupportedLanguages.Makefile]: Make } : {}),
};
// Capture key prefix → NodeKind string
diff --git a/core-ingestion/src/languages.ts b/core-ingestion/src/languages.ts
index 321f3ed5..660470b0 100644
--- a/core-ingestion/src/languages.ts
+++ b/core-ingestion/src/languages.ts
@@ -19,6 +19,7 @@ export enum SupportedLanguages {
JSON = 'json',
TOML = 'toml',
Markdown = 'markdown',
+ Makefile = 'makefile',
}
const EXT_MAP: Record<string, SupportedLanguages> = {
@@ -54,6 +55,8 @@ const EXT_MAP: Record<string, SupportedLanguages> = {
'.toml': SupportedLanguages.TOML,
'.md': SupportedLanguages.Markdown,
'.markdown': SupportedLanguages.Markdown,
+ '.mk': SupportedLanguages.Makefile,
+ '.makefile': SupportedLanguages.Makefile,
};
export function languageFromPath(filePath: string): SupportedLanguages | null {
@@ -63,6 +66,9 @@ export function languageFromPath(filePath: string): SupportedLanguages | null {
if (lowerFileName === 'dockerfile' || lowerFileName.endsWith('.dockerfile')) {
return SupportedLanguages.Dockerfile;
}
+ if(lowerFileName === 'makefile' || lowerFileName === 'makefile.mk' || lowerFileName.endsWith('.makefile') || lowerFileName.endsWith('.mk') || lowerFileName === 'gnumakefile') {
+ return SupportedLanguages.Makefile;
+ }
const dotIndex = lowerFileName.lastIndexOf('.');
if (dotIndex === -1) return null;
const ext = lowerFileName.slice(dotIndex);
diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.ts
index 08793275..d1fac2e7 100644
--- a/core-ingestion/src/queries.ts
+++ b/core-ingestion/src/queries.ts
@@ -1381,6 +1381,21 @@ export const SCALA_QUERIES = `
(#match? @_qualifier "^[A-Z]")
(#match? @call.name "^[A-Z]"))
`;
+export const MAKEFILE_QUERIES = `
+; ── Rule targets ─────────────────────────────────────────────────────────────
+(rule
+ (targets (word) @name)
+ (#not-match? @name "^\\.[A-Z]")) @definition.function
+
+; ── Variable assignments ──────────────────────────────────────────────────────
+(variable_assignment
+ name: (word) @name) @definition.macro
+
+; ── Include directives ────────────────────────────────────────────────────────
+(include_directive
+ (list (word) @import.name))
+`;
+
export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
[SupportedLanguages.TypeScript]: TYPESCRIPT_QUERIES,
@@ -1403,5 +1418,6 @@ export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
[SupportedLanguages.JSON]: '',
[SupportedLanguages.TOML]: '',
[SupportedLanguages.Markdown]: '',
+ [SupportedLanguages.Makefile]: MAKEFILE_QUERIES,
};
diff --git a/ix-cli/src/cli/__tests__/ingest-discovery.test.ts b/ix-cli/src/cli/__tests__/ingest-discovery.test.ts
index 83867140..596901f2 100644
--- a/ix-cli/src/cli/__tests__/ingest-discovery.test.ts
+++ b/ix-cli/src/cli/__tests__/ingest-discovery.test.ts
@@ -27,6 +27,12 @@ describe('dedupeDiscoveredFilePaths', () => {
it('treats Dockerfile variants as supported source inputs', () => {
expect(isSupportedSourceFile('Dockerfile')).toBe(true);
expect(isSupportedSourceFile('deploy/prod.dockerfile')).toBe(true);
- expect(isSupportedSourceFile('Makefile')).toBe(false);
+ });
+
+ it('treats Makefiles as supported source inputs', () => {
+ expect(isSupportedSourceFile('Makefile')).toBe(true);
+ expect(isSupportedSourceFile('GNUmakefile')).toBe(true);
+ expect(isSupportedSourceFile('build/common.mk')).toBe(true);
+ expect(isSupportedSourceFile('README.txt')).toBe(false);
});
});
diff --git a/ix-cli/src/cli/commands/ingest.ts b/ix-cli/src/cli/commands/ingest.ts
index a9090851..c76ef119 100644
--- a/ix-cli/src/cli/commands/ingest.ts
+++ b/ix-cli/src/cli/commands/ingest.ts
@@ -40,12 +40,15 @@ const SUPPORTED_EXTENSIONS = new Set([
'.json',
'.toml',
'.md', '.markdown',
+ '.mk', '.makefile',
]);
export function isSupportedSourceFile(filePath: string): boolean {
const fileName = nodePath.basename(filePath).toLowerCase();
return fileName === 'dockerfile'
|| fileName.endsWith('.dockerfile')
+ || fileName === 'makefile'
+ || fileName === 'gnumakefile'
|| SUPPORTED_EXTENSIONS.has(nodePath.extname(filePath).toLowerCase());
}