Skip to content

Codegen: make missing codeql error clearer #19418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions misc/codegen/generators/qlgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import logging
import pathlib
import re
import shutil
import subprocess
import typing
import itertools
Expand Down Expand Up @@ -257,6 +258,15 @@ def format(codeql, files):
if not ql_files:
return
format_cmd = [codeql, "query", "format", "--in-place", "--"] + ql_files
if "/" in codeql or "\\" in codeql:
if not pathlib.Path(codeql).exists():
raise FormatError(f"Provided CodeQL binary `{codeql}` does not exist")
else:
codeql_path = shutil.which(codeql)
if not codeql_path:
raise FormatError(
f"`{codeql}` not found in PATH. Either install it, or pass `-- --codeql-binary` with a full path")
codeql = codeql_path
res = subprocess.run(format_cmd, stderr=subprocess.PIPE, text=True)
if res.returncode:
for line in res.stderr.splitlines():
Expand Down
24 changes: 21 additions & 3 deletions misc/codegen/test/test_qlgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def qlgen_opts(opts):
opts.ql_format = True
opts.root_dir = paths.root_dir
opts.force = False
opts.codeql_binary = "./my_fake_codeql"
pathlib.Path(opts.codeql_binary).touch()
return opts


Expand Down Expand Up @@ -499,7 +501,6 @@ def test_class_dir_imports(generate_import_list):


def test_format(opts, generate, render_manager, run_mock):
opts.codeql_binary = "my_fake_codeql"
run_mock.return_value.stderr = "some\nlines\n"
render_manager.written = [
pathlib.Path("x", "foo.ql"),
Expand All @@ -508,13 +509,12 @@ def test_format(opts, generate, render_manager, run_mock):
]
generate([schema.Class('A')])
assert run_mock.mock_calls == [
mock.call(["my_fake_codeql", "query", "format", "--in-place", "--", "x/foo.ql", "bar.qll"],
mock.call([opts.codeql_binary, "query", "format", "--in-place", "--", "x/foo.ql", "bar.qll"],
stderr=subprocess.PIPE, text=True),
]


def test_format_error(opts, generate, render_manager, run_mock):
opts.codeql_binary = "my_fake_codeql"
run_mock.return_value.stderr = "some\nlines\n"
run_mock.return_value.returncode = 1
render_manager.written = [
Expand All @@ -526,6 +526,24 @@ def test_format_error(opts, generate, render_manager, run_mock):
generate([schema.Class('A')])


def test_format_no_codeql(opts, generate, render_manager, run_mock):
pathlib.Path(opts.codeql_binary).unlink()
render_manager.written = [
pathlib.Path("bar.qll"),
]
with pytest.raises(qlgen.FormatError):
generate([schema.Class('A')])


def test_format_no_codeql_in_path(opts, generate, render_manager, run_mock):
opts.codeql_binary = "my_fake_codeql"
render_manager.written = [
pathlib.Path("bar.qll"),
]
with pytest.raises(qlgen.FormatError):
generate([schema.Class('A')])


@pytest.mark.parametrize("force", [False, True])
def test_manage_parameters(opts, generate, renderer, force):
opts.force = force
Expand Down
Loading