Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9c284b1
Just: introduce scaffolding for common verbs, and apply to rust
redsun82 Jul 4, 2025
1202af1
Just: fix for windows
redsun82 Jul 4, 2025
2dea9da
Just: add `codegen`
redsun82 Jul 4, 2025
9e31fb5
Just: fix and add windows
redsun82 Jul 4, 2025
4768eba
Merge branch 'main' into redsun82/just2
redsun82 Jul 4, 2025
5b9436a
Just: fix swift tests
redsun82 Jul 4, 2025
812fc23
Merge branch 'main' into redsun82/just2
redsun82 Jul 7, 2025
a4acf08
Just: fix ram option for `codeql test run`
redsun82 Jul 7, 2025
d7d7cf9
Just: format `ts` files
redsun82 Jul 7, 2025
6e14111
Just: use `--all-checks` and `--codeql` special flags, and relativize…
redsun82 Jul 7, 2025
cb652f3
Just: add `format` to just directory
redsun82 Jul 7, 2025
fba96c4
Just: add root `lib.just`
redsun82 Jul 7, 2025
c430515
Just: simplify forwarder using `--justfile`
redsun82 Jul 8, 2025
acc7e3f
Just: add `generate` prerequisite to rust ql tests
redsun82 Jul 8, 2025
e8bcbbd
Just: add `language-tests.ts` helper
redsun82 Jul 8, 2025
d987aa6
Merge branch 'main' into redsun82/just2
redsun82 Jul 8, 2025
bb467d4
Just: fix CI build for rust
redsun82 Jul 8, 2025
8ba7efd
Just: fix mono-argument case for argumentless recipes
redsun82 Jul 8, 2025
aa09288
Just: introduce aliases
redsun82 Jul 8, 2025
7f72f87
Just: fix `just rust format` and similar
redsun82 Jul 8, 2025
b8b01ce
Just: rename `_build` to `_build_dist`
redsun82 Jul 8, 2025
0809715
Merge branch 'main' into redsun82/just2
redsun82 Jul 8, 2025
bd003c5
Just: add `_if_not_on_ci_just` helper, and add generation prerequisites
redsun82 Jul 8, 2025
c9cda74
Just: allow mixing different verb implementations
redsun82 Jul 9, 2025
c51f2f8
Merge branch 'main' into redsun82/just2
redsun82 Jul 9, 2025
9267283
Merge branch 'main' into redsun82/just2
redsun82 Jul 10, 2025
103745b
Just: group just invocations in forwarder and improve logging
redsun82 Jul 10, 2025
7e7afba
Merge branch 'main' into redsun82/just2
redsun82 Jul 17, 2025
f2a6503
Just: use bazel directly for dist building
redsun82 Jul 18, 2025
365b2eb
Merge branch 'main' into redsun82/just2
redsun82 Jul 18, 2025
0e3ee6e
Just: reorganize code and revamp formatting
redsun82 Jul 18, 2025
4284d66
Merge branch 'main' into redsun82/just2
redsun82 Jul 18, 2025
f1febac
Merge branch 'main' into redsun82/just2
redsun82 Aug 11, 2025
c67e123
Just: add `README.md`
redsun82 Aug 12, 2025
15e8e48
Just: run prettier on `.ts` files
redsun82 Aug 12, 2025
db83285
Merge branch 'main' into redsun82/just2
redsun82 Aug 12, 2025
9d52a08
Just: add some docs in source
redsun82 Aug 12, 2025
469d09c
Merge branch 'main' into redsun82/just2
redsun82 Aug 14, 2025
0f28502
Merge branch 'main' into redsun82/just2
redsun82 Nov 14, 2025
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
3 changes: 3 additions & 0 deletions java/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import '../lib.just'

build: (_build_dist "java")
6 changes: 6 additions & 0 deletions java/ql/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "../../lib.just"

[no-cd]
format *ARGS=".": (_format_ql ARGS)

consistency_queries := source_dir() / "consistency-queries"
16 changes: 16 additions & 0 deletions java/ql/test/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import "../justfile"

base_flags := """\
CODEQL_EXTRACTOR_KOTLIN_DIAGNOSTIC_LIMIT="\\ " \
"""

all_checks := default_db_checks + """\
--check-undefined-labels \
--check-repeated-labels \
--check-redefined-labels \
--check-use-before-definition \
--consistency-queries=""" + consistency_queries

[no-cd]
test *ARGS=".": (_codeql_test "java" base_flags all_checks ARGS)

4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# see misc/just/README.md for an overview

import 'lib.just'
import 'misc/just/forward.just'
1 change: 1 addition & 0 deletions lib.just
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "misc/just/lib.just"
5 changes: 5 additions & 0 deletions misc/bazel/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import '../just/lib.just'

[no-cd, positional-arguments, no-exit-message]
hello +ARGS:
@echo "hello from bzl" "$@"
5 changes: 5 additions & 0 deletions misc/codegen/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "../just/lib.just"

test *ARGS="": (_bazel "test" "@codeql//misc/codegen/...")

format *ARGS=".": (_format_py ARGS)
44 changes: 44 additions & 0 deletions misc/just/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
This directory contains an infrastructure for [`just`](https://github.com/casey/just)
recipes that can be used throughout this and the internal repository. In particular we
have common verbs (`build`, `test`, `format`, `lint`, `generate`) that individual parts
of the project can implement, and some common functionality that can be used to that
effect.

# Forwarding

The core of the functionality is given by forwarding. The idea is that:

- if you are in the directory where a verb is implemented, you will get that as per
standard `just` behaviour (possibly using fallback).
- if on the other hand you are beneath it, and you run something like
`just test ql/rust/ql/test/{a,b}`, then a forwarder script finds a common justfile
implementing the verb for all the positional arguments passed there, and then retries
calling `just test` from there. So if `test` is implemented beneath that (in that case,
it is in `rust/ql/test`), it uses that recipe.
- even if there isn't a recipe that is common to all the positional arguments, the
forwarder will still group the arguments in batches using the same recipe. So
`just build ql/rust ql/java`, or
`just test ql/rust/ql/test/some/language/test ql/rust/ql/integration-test/some/integration/test`
will also work, with corresponding recipes run sequentially.

Another point is how launching QL tests can be tweaked:

- by default, the corresponding CLI is built from the internal repo (nothing is done if
working in `codeql` standalone), and no additional database or consistency checks are
made
- `--codeql=built` can be passed to skip the build step (if no changes were made to the
CLI/extractors). This is consistent with the same pytest option
- you can add the additional checks that CI does with `--all-checks` or the `+`
abbreviation. These additional checks are configured in justfiles per language, and
correspond to all the additional checks that CI adds (but that a dev might not want to
run by default).

Some caveats:

- passing arguments with spaces generally doesn't work, although setting arguments with
spaces in `justfile`s (for the base arguments) is supported using escaping as in `\\`.
This is a known limitation of just (see
<https://github.com/casey/just/issues/1988>)
- when running different recipes for the same verb, non-positional arguments need to be
supported by all recipes involved. For example, this will work ok for `--learn` or
`--codeql` options in language and integration tests
19 changes: 19 additions & 0 deletions misc/just/build.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Helper build recipes

import "defs.just"

# Build the given language-specific CLI distribution
_build_dist LANGUAGE: _require_semmle_code (_maybe_build_dist LANGUAGE)

# Build the language-specific distribution if we are in an internal repository checkout
# Otherwise, do nothing
[no-exit-message]
_maybe_build_dist LANGUAGE: (_if_in_semmle_code ('cd "$SEMMLE_CODE"; tools/bazel run //language-packs:intree-' + LANGUAGE) '# using codeql from PATH, if any')

# Call bazel. Uses our official bazel wrapper if we are in an internal repository checkout
[no-cd, no-exit-message]
_bazel COMMAND *ARGS: (_if_in_semmle_code 'cd "$SEMMLE_CODE"; tools/bazel' 'bazel' COMMAND ARGS)

# Call sembuild (requires an internal repository checkout)
[no-cd, no-exit-message]
_sembuild *ARGS: (_run_in_semmle_code "./build" ARGS)
159 changes: 159 additions & 0 deletions misc/just/codeql-test-run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import * as child_process from "child_process";
import * as path from "path";
import * as os from "os";
import * as fs from "fs";

const vars = {
just: process.env["JUST_EXECUTABLE"] || "just",
error: process.env["JUST_ERROR"] || "error",
cmd_begin: process.env["CMD_BEGIN"] || "",
cmd_end: process.env["CMD_END"] || "",
semmle_code: process.env["SEMMLE_CODE"],
};

function invoke(
invocation: string[],
options: { cwd?: string; log_prefix?: string } = {},
): number {
const log_prefix =
options.log_prefix && options.log_prefix !== ""
? `${options.log_prefix} `
: "";
console.log(
`${vars.cmd_begin}${log_prefix}${invocation.join(" ")}${vars.cmd_end}`,
);
try {
child_process.execFileSync(invocation[0], invocation.slice(1), {
stdio: "inherit",
cwd: options.cwd,
});
} catch (error) {
return 1;
}
return 0;
}

type Args = {
tests: string[];
flags: string[];
env: string[];
codeql: string;
all: boolean;
};

const old_console_error = console.error;

console.error = (message: string) => {
old_console_error(vars.error + message);
};

function parseArgs(args: Args, argv: string) {
argv.split(/(?<!\\) /)
.map((arg) => arg.replace("\\ ", " "))
.forEach((arg) => {
if (arg.startsWith("--codeql=")) {
args.codeql = arg.split("=")[1];
} else if (arg === "+" || arg === "--all-checks") {
args.all = true;
} else if (arg.startsWith("-")) {
args.flags.push(arg);
} else if (/^[A-Z_][A-Z_0-9]*=.*$/.test(arg)) {
args.env.push(arg);
} else if (arg !== "") {
args.tests.push(arg);
}
});
}

function codeqlTestRun(argv: string[]): number {
const [language, base_args, all_args, extra_args] = argv;
const ram_per_thread = process.platform === "linux" ? 3000 : 2048;
const cpus = os.cpus().length;
let args: Args = {
tests: [],
flags: [`--ram=${ram_per_thread * cpus}`, `-j${cpus}`],
env: [],
codeql: vars.semmle_code ? "build" : "host",
all: false,
};
parseArgs(args, base_args);
parseArgs(args, extra_args);
if (args.all) {
parseArgs(args, all_args);
}
if (
!vars.semmle_code &&
(args.codeql === "build" || args.codeql === "built")
) {
console.error(
"Using `--codeql=build` or `--codeql=built` requires working with the internal repository",
);
return 1;
}
if (args.tests.length === 0) {
args.tests.push(".");
}
if (args.codeql === "build") {
if (
invoke([vars.just, language, "build"], {
cwd: vars.semmle_code,
}) !== 0
) {
return 1;
}
}
if (args.codeql !== "host") {
// disable the default implicit config file, but keep an explicit one
// this is the same behavior wrt to `--codeql` as the integration test runner
process.env["CODEQL_CONFIG_FILE"] ||= ".";
}
// Set and unset environment variables
args.env.forEach((envVar) => {
const [key, value] = envVar.split("=", 2);
if (key) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
} else {
console.error(`Invalid environment variable assignment: ${envVar}`);
process.exit(1);
}
});
let codeql;
function check_codeql() {
if (!fs.existsSync(codeql)) {
console.error(`CodeQL executable not found: ${codeql}`);
process.exit(1);
}
}
if (args.codeql === "built" || args.codeql === "build") {
codeql = path.join(
vars.semmle_code!,
"target",
"intree",
`codeql-${language}`,
"codeql",
);
check_codeql();
} else if (args.codeql === "host") {
codeql = "codeql";
} else {
codeql = args.codeql;
check_codeql();
}
if (fs.lstatSync(codeql).isDirectory()) {
codeql = path.join(codeql, "codeql");
if (process.platform === "win32") {
codeql += ".exe";
}
check_codeql();
}

return invoke([codeql, "test", "run", ...args.flags, "--", ...args.tests], {
log_prefix: args.env.join(" "),
});
}

process.exit(codeqlTestRun(process.argv.slice(2)));
58 changes: 58 additions & 0 deletions misc/just/defs.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import? '../../../semmle-code.just' # internal repo just file, if present
import 'semmle-code-stub.just'

set fallback
set allow-duplicate-recipes
set allow-duplicate-variables
set unstable

export PATH_SEP := if os() == "windows" { ";" } else { ":" }
export JUST_EXECUTABLE := just_executable()

error := style("error") + "error" + NORMAL + ": "
cmd_sep := "\n#--------------------------------------------------------\n"
export CMD_BEGIN := style("command") + cmd_sep
export CMD_END := cmd_sep + NORMAL
export JUST_ERROR := error

tsx := "npx [email protected]"

default_db_checks := """\
--check-databases \
--check-diff-informed \
--fail-on-trap-errors \
"""

[no-exit-message]
@_require_semmle_code:
{{ if SEMMLE_CODE == "" { '''
echo "''' + error + ''' running this recipe requires doing so from an internal repository checkout" >&2
exit 1
''' } else { "" } }}

[no-cd]
_run +ARGS:
{{ cmd_sep }}{{ ARGS }}{{ cmd_sep }}

[no-cd]
_run_in DIR +ARGS:
{{ cmd_sep }}cd "{{ DIR }}"; {{ ARGS }}{{ cmd_sep }}

[no-cd]
_run_in_semmle_code +ARGS: _require_semmle_code (_run_in "$SEMMLE_CODE" ARGS)

[no-cd, positional-arguments, no-exit-message]
@_just +ARGS:
echo "-> just $@"
"{{ JUST_EXECUTABLE }}" "$@"

[no-cd, positional-arguments]
@_if_not_on_ci_just +ARGS:
if [ "${GITHUB_ACTIONS:-}" != "true" ]; then \
echo "-> just $@"; \
"$JUST_EXECUTABLE" "$@"; \
fi

[no-cd, no-exit-message]
_if_in_semmle_code THEN ELSE *ARGS:
{{ cmd_sep }}{{ if SEMMLE_CODE != "" { THEN } else { ELSE } }} {{ ARGS }}{{ cmd_sep }}
20 changes: 20 additions & 0 deletions misc/just/format.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import "build.just"

[no-cd, no-exit-message]
_format_ql +ARGS: (_maybe_build_dist "nolang") (
_if_in_semmle_code
'"$SEMMLE_CODE/target/intree/codeql-nolang/codeql"'
'codeql'
("query format --in-place -v $(find " + ARGS + " -type f -name '*.ql' -or -name '*.qll')")
)

[no-cd, no-exit-message]
_format_py *ARGS=".": (_if_in_semmle_code "uv run black" "black" ARGS)

[no-cd, no-exit-message]
_format_cpp *ARGS=".": (
_if_in_semmle_code
"uv run clang-format"
"clang-format"
"-i --verbose $(find " + ARGS + " -type f -name '*.h' -or -name '*.cpp')"
)
Loading
Loading