Skip to content

Commit bad0a74

Browse files
committed
Store all built-in languages
While we want the CodeQL Action to work with third-party language support, having a list of all built-in languages can help us create better type-level checks to ensure that we don't miss things that we want to customize for each of our built-in languages.
1 parent a26cb68 commit bad0a74

24 files changed

+1495
-1067
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../../tsconfig.json",
3+
"compilerOptions": {
4+
"lib": ["esnext"],
5+
"rootDir": "../../..",
6+
"sourceMap": false,
7+
"noEmit": true,
8+
},
9+
"include": ["./*.ts"],
10+
"exclude": ["node_modules"]
11+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env npx tsx
2+
3+
/**
4+
* Updates src/languages/builtin.json by querying the CodeQL CLI for:
5+
* - Languages that have default queries (via codeql-extractor.yml)
6+
* - Language aliases (via `codeql resolve languages --format=betterjson --extractor-include-aliases`)
7+
*
8+
* Usage:
9+
* npx tsx .github/workflows/script/update-builtin-languages.ts [path-to-codeql]
10+
*
11+
* If no path is given, falls back to "codeql".
12+
*/
13+
14+
import { execFileSync } from "node:child_process";
15+
import * as fs from "node:fs";
16+
import * as path from "node:path";
17+
18+
import * as yaml from "yaml";
19+
20+
const codeqlPath = process.argv[2] || "codeql";
21+
22+
// Step 1: Resolve all language extractor directories.
23+
const resolveJson: Record<string, string[]> = JSON.parse(
24+
execFileSync(
25+
codeqlPath,
26+
["resolve", "languages", "--format=json"],
27+
{ encoding: "utf8" },
28+
),
29+
);
30+
31+
// Step 2: For each language, read codeql-extractor.yml and check default_queries.
32+
const languages: string[] = [];
33+
34+
for (const [language, dirs] of Object.entries(resolveJson)) {
35+
const extractorDir = dirs[0];
36+
const extractorYmlPath = path.join(extractorDir, "codeql-extractor.yml");
37+
38+
if (!fs.existsSync(extractorYmlPath)) {
39+
throw new Error(`Extractor YAML not found for language '${language}' at expected path: ${extractorYmlPath}`);
40+
}
41+
42+
const extractorYml = yaml.parse(fs.readFileSync(extractorYmlPath, "utf8"));
43+
const defaultQueries: unknown[] | undefined = extractorYml.default_queries;
44+
45+
if (Array.isArray(defaultQueries) && defaultQueries.length > 0) {
46+
console.log(` ✅ ${language}: included (default_queries: ${JSON.stringify(defaultQueries)})`);
47+
languages.push(language);
48+
} else {
49+
console.log(` ❌ ${language}: excluded (no default queries)`);
50+
}
51+
}
52+
53+
languages.sort();
54+
55+
// Step 3: Resolve aliases, filtered to only those targeting included languages.
56+
const betterjsonOutput = JSON.parse(
57+
execFileSync(
58+
codeqlPath,
59+
["resolve", "languages", "--format=betterjson", "--extractor-include-aliases"],
60+
{ encoding: "utf8" },
61+
),
62+
);
63+
64+
const languageSet = new Set(languages);
65+
const aliases: Record<string, string> = Object.fromEntries(
66+
Object.entries((betterjsonOutput.aliases ?? {}) as Record<string, string>)
67+
.filter(([, target]) => languageSet.has(target))
68+
.sort(([a], [b]) => a.localeCompare(b)),
69+
);
70+
71+
// Step 4: Write builtin.json.
72+
const outputPath = path.join(
73+
__dirname,
74+
"..",
75+
"..",
76+
"..",
77+
"src",
78+
"languages",
79+
"builtin.json",
80+
);
81+
82+
const content = JSON.stringify({ languages, aliases }, null, 2) + "\n";
83+
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
84+
fs.writeFileSync(outputPath, content);
85+
86+
console.log(`\nWrote ${outputPath}`);
87+
console.log(` Languages: ${languages.join(", ")}`);
88+
console.log(` Aliases: ${Object.keys(aliases).join(", ")}`);

.github/workflows/update-bundle.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,10 @@ jobs:
6363
with:
6464
tools: https://github.com/github/codeql-action/releases/download/${{ github.event.release.tag_name }}/codeql-bundle-linux64.tar.gz
6565

66-
- name: Update language aliases
66+
- name: Update built-in languages
67+
run: npx tsx .github/workflows/script/update-builtin-languages.ts "$CODEQL_PATH"
6768
env:
6869
CODEQL_PATH: ${{ steps.setup-codeql.outputs.codeql-path }}
69-
run: |
70-
"$CODEQL_PATH" resolve languages --format=betterjson --extractor-include-aliases \
71-
| jq -S '.aliases // {}' \
72-
> src/known-language-aliases.json
7370

7471
- name: Bump Action minor version if new CodeQL minor version series
7572
id: bump-action-version

lib/analyze-action-post.js

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/analyze-action.js

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/autobuild-action.js

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action-post.js

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/resolve-environment-action.js

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setup-codeql-action.js

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)