Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

big-code-analysis

This documentation is also available in Japanese.

big-code-analysis is a Rust library and command line tool (bca) that computes maintainability metrics from source code in many programming languages. It parses with tree-sitter, an incremental parsing library with a grammar per language, so it needs no compiler or language runtime.

This book is task-oriented: metric definitions, the command reference, CI recipes, and guides for the Rust crate and the Python bindings. The source code is on GitHub, and issues and feature requests go to the issue tracker.

Supported platforms

big-code-analysis runs on Linux, macOS, and Windows. The GitHub releases page carries prebuilt binaries for all three.

API docs

The complete Rustdoc reference for the crate (every public type, trait, and function, including the feature-gated vcs module) lives on docs.rs, generated straight from the source.

For task-oriented guides on embedding the crate (quick start, in-memory analysis, walking FuncSpace results, and error handling), see Using as a Library.

For the PyO3 bindings (pip install big-code-analysis, batch processing, flat-record iteration, SARIF output, and async patterns), see Python Bindings.

License

  • The vendored grammar crates (tree-sitter-ccomment, tree-sitter-mozcpp, tree-sitter-mozjs, tree-sitter-preproc, tree-sitter-tcl) are released under the MIT license.

  • big-code-analysis, big-code-analysis-ast, big-code-analysis-cli, big-code-analysis-web, and big-code-analysis-py are released under the Mozilla Public License v2.0.

Supported Languages

This is the list of programming languages big-code-analysis parses. Each entry is a LANG variant (defined by the mk_langs! invocation in big-code-analysis-ast/src/langs.rs) and is gated behind the matching per-language Cargo feature documented in Per-language Cargo features.

  • Bash
  • C
  • C/C++
  • C#
  • Elixir
  • Go
  • Groovy
  • Irules
  • Java
  • JavaScript
  • Kotlin
  • Lua
  • Mozcpp
  • Mozjs
  • Objective-C
  • Perl
  • Php
  • Python
  • Ruby
  • Rust
  • Tcl
  • Tsx
  • Typescript

Every variant’s slug is its LANG::name, lowercase and punctuation-free so it round-trips through FromStr (C# reports csharp). Some entries are variants of a shared grammar pipeline:

  • JavaScript (the upstream tree-sitter-javascript grammar) is the default for .js, .mjs, .cjs, and .jsx files. Mozjs is the Mozilla / SpiderMonkey fork, opt-in: it owns only the .jsm (Firefox module) extension and reports the slug mozjs. The two are metric-equivalent on ordinary JavaScript.
  • Tsx is Typescript (upstream tree-sitter-typescript) with JSX syntax enabled and reports the distinct slug tsx.
  • Since #721 C has its own variant C (slug c, upstream tree-sitter-c), owning .c and the c emacs mode; the C/C++ variant (slug cpp, upstream tree-sitter-cpp since #720) keeps .cpp / .cc / .h and the rest. .h deliberately stays on Cpp: a C++ header through the C grammar ERROR-cascades on class / template, whereas a C header through the C++ grammar only trips on C++-keyword identifiers.
  • The Mozilla/Gecko C++ dialect is the opt-in Mozcpp variant (slug mozcpp), which owns no file extensions and is selected only by name, exactly as Mozjs relates to JavaScript.
  • Since #724 Objective-C (slug objc, upstream tree-sitter-objc) owns .m and the objc / objective-c emacs modes. Objective-C++ (.mm) stays on Cpp. These files mix Objective-C and C++, but tree-sitter-objc cannot parse C++ templates, namespaces, or ::. The C++ grammar struggles only with the Objective-C portions, so it gives the better result, using the same trade-off as .h. Metrics for those Objective-C portions remain approximate.

K&R definitions with a wrapped return type

A pre-ANSI (K&R) function definition opens no function space under C or Objective-C when its return type wraps the declarator; int *, char **, struct S *, and a static pointer return all reproduce it:

int *krptr(a, b) int a; int b; { if (a) { return 0; } return 1; }

tree-sitter-c 0.24.2 builds its old-style function definition from a declarator nested inside the old-style declarator, so an outer pointer_declarator is unreachable and the parser prefers a plain declaration that swallows the first parameter declaration, orphaning the body as a bare compound_statement. No ERROR node is produced: the parse silently succeeds wrongly, and there is no dispatch arm missing on our side.

The consequence is that the body’s decisions are charged to the file’s unit space: the line above reports cyclomatic.sum 2 and nom.functions 0, a file-level count no function-level row accounts for. A K&R definition whose return type does not wrap the declarator (int krplain(a, b) int a; int b; { … }) is unaffected, as is any ANSI definition. C/C++ and Mozcpp open no space for either K&R form, because tree-sitter-cpp has no rule for the syntax at all. This is an upstream grammar limitation, tracked here as #1209.

Internal helper variants

The following LANG variants are internal helpers, not user-facing languages. They belong to the C-family analysis pipeline (they ride every C-family Cargo feature: cpp, c, and mozcpp) and are not selected directly when analysing source files:

  • Ccomment: C/C++ comments.
  • Preproc: C/C++ preprocessor macros.

Note: Since #720 the Mozilla/Gecko C++ dialect is exposed as the Mozcpp LANG variant (backed by the vendored bca-tree-sitter-mozcpp crate, pulled in by the opt-in mozcpp feature). It is a public, name-selectable language that owns no file extensions. Unlike the Ccomment / Preproc helpers above, it is not internal.

Supported Code Metrics

This chapter describes every metric that big-code-analysis computes. Each section starts from the original research paper, walks through the algorithm, and explains both how the metric was originally meant to be used and how the industry uses it today. If you are new to software metrics, read the sections in order: the later metrics (the Maintainability Index in particular) are built on the earlier ones (Halstead, Cyclomatic, LOC).

Three framing notes:

  • A metric is a measurement, not a verdict. Every number on this page summarises a structural property of source code. None of them measures correctness, productivity, or developer skill. The first question for any metric is “compared with what?”: the same module a month ago, this module versus its siblings, this codebase versus an industry baseline. Absolute thresholds are rough heuristics.
  • Most metrics here are computed at three scopes: per function / method, per class or unit-like space, and per file. The underlying tree-sitter parser produces a tree of “spaces” (functions, closures, classes, namespaces, and so on) and every metric is rolled up through that tree.
  • Object-oriented metrics only fire on object-oriented constructs. WMC, NPA, and NPM report 0 on a Rust file that has no impl blocks or on a Python module without classes; that is the correct answer, not a bug.

Index

MetricMeasuresFirst defined by
ABCSize as <Assignments, Branches, Conditions>Fitzpatrick, 1997
Cognitive ComplexityHow hard a function is to readCampbell / SonarSource, 2017
Cyclomatic Complexity (CC)Independent paths through a functionMcCabe, 1976
HalsteadVocabulary-based size, difficulty, effort, bugsHalstead, 1977
Lines of Code (SLOC, PLOC, LLOC, CLOC, BLANK, DLOC)Raw, physical, logical, comment, blank, and documentation line countsConte, Dunsmore & Shen, 1986
Maintainability Index (MI)Composite maintainability scoreOman & Hagemeister, 1992; Coleman et al., 1994
NArgsNumber of arguments per functionfolk metric
NExitsNumber of exit points per functionstructured-programming literature
NOMNumber of methods and closuresLorenz & Kidd, 1994
NPANumber of public attributesLorenz & Kidd, 1994
NPMNumber of public methodsLorenz & Kidd, 1994
TokensTree-sitter leaf-token count (size proxy)Lizard tool, Terry Yin
WMCSum of cyclomatic complexity across a class’s methodsChidamber & Kemerer, 1994

ABC

The ABC metric measures the size of a piece of code as a three-dimensional vector. Each component counts one kind of operation:

  • Assignments: anything that stores a value into a variable, including compound assignments (+=, ++) and explicit initialisation.
  • Branches: function and method calls. Despite the name, this is not the count of conditional jumps; it is the number of points where control branches out to other code.
  • Conditions: boolean tests. These are comparison operators (==, !=, <=, >=, <, >), ternary operators (?), and the fixed keyword set (else, case, try, catch). The default / wildcard arm is not counted in any language (see the per-language deviations below). The short-circuit logical operators && and || are not counted on their own; instead, each non-comparison operand of a && / || chain contributes one condition via Fitzpatrick’s “unary conditional expression” rule. The next subsection walks through the rules, the per-language deviations, and worked examples.

Jerry Fitzpatrick introduced the metric in the 1997 C++ Report article Applying the ABC metric to C, C++ and Java. The current canonical specification, including the rules for what counts as an A, B, or C in modern languages, is maintained on Fitzpatrick’s Software Renovation site.

Counting rules

Fitzpatrick’s paper enumerates the rules in three figures: Figure 2 (C), Figure 3 (C++, which extends Figure 2), and Figure 4 (Java). The tables below summarise the paper’s rule set, with each row attributed to the figure that introduces it. Big-code-analysis implements them per language, with the exceptions listed under Per-language deviations and these paper rows that no language implements today: the delete operator, the goto / labeled break / labeled continue jumps, the preprocessor #else / #elif, and the C++ initializing constructor invocation (S s(1);). Each scores zero.

Assignments

RuleCounted as AFirst defined in
Plain assignment (=)one per occurrenceFigure 2 (C)
Compound assignment (+=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=)one per occurrenceFigure 2 (C) / Figure 4 (Java)
Java unsigned-right-shift-assign (>>>=)one per occurrenceFigure 4 (Java)
Pre- or post-increment / decrement (++, --)one per occurrenceFigure 2 (C)
Initializing constructor invocationone per occurrenceFigure 3 (C++)

Branches

RuleCounted as BFirst defined in
Function or method callone per call siteFigure 2 (C) / Figure 4 (Java)
new operatorone per occurrenceFigure 3 (C++) / Figure 4 (Java)
delete operatorone per occurrenceFigure 3 (C++)
goto label, break label, continue labelone per occurrenceFigure 2 (C) / Figure 3 (C++) / Figure 4 (Java, labeled break / continue only, since Java has no goto)

Conditions

RuleCounted as CFirst defined in
Comparison operator (==, !=, <=, >=, <, >)one per occurrenceFigure 2, Rule 5
Ternary ? :one per occurrenceFigure 2, Rule 5
else, caseone per occurrenceFigure 2, Rule 5
Preprocessor #else, #elifone per occurrenceFigure 2, Rule 5
try, catchone per occurrenceFigure 3 (C++) / Figure 4 (Java)
Unary conditional expressionone per && / || operand and per if / while / ternary / guard predicate that no other rule already counts (and per !-wrapped argument or return value)Figure 3, Rule 7 / Figure 4, Rule 9

Each language has a role table, a declaration in the source of which tree-sitter node kinds play each syntactic role (/big-code-analysis-ast/src/roles/). Which kinds are a comparison, a short-circuit operator, a ternary or a null-safe operator are its Comparison, ShortCircuit, Ternary and NullSafe rows, and the counting rules in the table above read those rows: a row says what a construct is; the rule says which occurrence pays.

The short-circuit logical operators (&&, ||, and the per-language equivalents: Ruby and / or, Python and / or, Perl and / or, Lua and / or, Tcl && / ||, iRules && / || / and / or) do not contribute a condition on their own. Each non-comparison operand contributes one instead, via the unary-conditional rule. Perl and PHP xor evaluate both operands, so like ^ they are a value: the slot holding one pays a single condition. The paper makes this explicit twice:

  1. Listing 2 annotates (am >= 0 && am <= 0xF) ? '/' : 'C' as accc: one assignment plus three conditions, where the three conditions are the two comparisons (>=, <=) and the ternary (?). The && itself contributes zero.
  2. Rule 7 / Rule 9 instead counts each operand: for if (x || y) printf("test failure\n"); the paper writes “there are two unary conditions since both x and y are tested as conditional expressions”. The || again contributes zero; x and y each contribute one.

Per-language deviations

Per-language impl Abc blocks narrow the paper rule set where the language has no equivalent construct, or where strict literal application would over-count.

LanguageDeviationReason
C, Go, Rusttry / catch omittedNo try/catch keyword in the grammar; error-handling uses errno / Result / Result-like sums.
RubyRescue substitutes for catchRuby’s exception-handling keyword is rescue; the AST node Rescue plays the role of Java’s catch.
All languagesdefault / _ wildcard arm excluded from the condition setFitzpatrick’s Figure 2 lists default, but it falls through unconditionally, so counting it would inflate C on every switch / match regardless of body. big-code-analysis omits it for every language (the Rust _ => and Java default: arms included).
TclChain-operand, bare-truthy and ternary slots wired; argument and return slots are notEach operand of a && / || chain inside expr {…} counts as one condition, so if {$a && $b} reports two. if / elseif / while route their expr {…} predicate, so a bare-truthy if {$a} reports one and if {!$a} likewise, matching C’s if (a) (#1180). A predicate written as a command substitution is a truthy test of that command’s result, so if {[somecmd]} also reports one, and the redundant if {[expr {$a || $b}]} idiom reports three (the chain’s two operands plus the substitution). The argument and return slots remain unrouted: a negation reached only through a standalone expr {…} command still reports zero.
iRulesChain-operand, bare-truthy and ternary slots wired; argument and return slots are notEach operand of a && / || / and / or chain counts as one condition (Rule 9), so if {!$a && !$b} reports two. iRules also recognises the word-form string-match comparators (contains, starts_with, ends_with, equals, matches, and so on) that Tcl lacks (Tcl’s eq / ne / in / ni are shared). Bare-truthy and ternary slot routing matches its Tcl sibling exactly (#1180); see that row for the shared detail, including the argument and return slots that remain unrouted.
Java, Groovy, C#, Rust, Go, JavaScript, TypeScript, TSX, Mozjs, PHP, C, C++, Objective-C, Mozcpp, Python, Perl, Luaif (true) {}, m(!a, !b), return !x count their operand(s)These implementations pass if, while, do-while, argument-list and return expressions through the same condition walker, so the rule applies uniformly across decision-bearing positions. A bare return x continues to report zero: Fitzpatrick treats an identifier in a return slot as a value, not a unary conditional. C# wraps each argument in an argument node, so the walker reads the value after any named-argument label or ref / out / in keyword: M(!a, !b) and M(name: !a, !b) report two, as do the base calls : base(!a, !b) and class S(bool a, bool b) : Base(!a, !b). Before #1537 it read the wrapper instead, and every C# argument reported zero. An indexer argument (o[!a]) is not a call argument and reports zero in C#, as Java’s array index does.
Ternary slots: Java, Groovy, C#, C, C++, Objective-C, Mozcpp, JavaScript, TypeScript, TSX, Mozjs, PHP, Perl, Ruby, Python, Tcl, iRulesa ? !b : !c counts its condition and both branch operandsThe same walker also runs over a ternary’s three operand slots, so a ? !b : !c reports 4 (the ? plus three unary conditions) rather than 1. Tcl and iRules reach the same 4 without grammar fields: their ternary_expr exposes none, and _expr inlines ( … ), so the slots are located relative to the ? and : tokens instead of by index (#1180). Python arrives at the same total by a different route: not operands are counted by the NotOperator rule wherever they appear, so only the condition slot is routed through the walker and (not b) if a else (not c) likewise reports 4. Languages with no ternary (Rust, Go, Kotlin, Lua, Elixir) are unaffected.
for header condition slot: Java, Groovy, C#, C, C++, Objective-C, Mozcpp, JavaScript, TypeScript, TSX, Mozjs, PHP, Go, Perlfor (; a; ) counts one condition; for (;;) counts noneThe loop header’s condition is a decision-bearing slot like an if predicate, so for (; a; ), for (; !a; ) and for (; (a); ) each report one. A comparison-shaped condition (i < n) is already counted by its own operator arm and is not double-counted. Every one of these grammars except Go exposes a condition field on for_statement (Perl’s sits on its C-style for_statement_1), so the slot is read by field name rather than by child index; Go’s field sits on the nested for_clause, and its outer header slot is located structurally (first named child that is neither the body nor a comment). An empty condition counts zero in every language: Fitzpatrick’s rules count conditional operators and unary conditions that are present, and an omitted test is not a decision. Java and Groovy previously scored for (;;) as one vacuously-true condition and no longer do. Tcl, iRules and Bash are command-dispatched grammars whose loop headers carry no boolean-expression slot to route (grammar-dispatch §9) (#1276).
RubyBare-predicate if / unless / while / until (block and modifier forms) count one conditionIdiomatic Ruby favours bare predicates (if flag, x if flag); counting the condition slot keeps ABC conditions at or above Ruby’s cyclomatic decision count (the alignment enforced across the other languages). A comparison (if a == b) or && / || chain in the predicate is counted by its own operator / walker arm and is not double-counted. Each && / || / and / or operand is scored by the same rule (once, unless its own arm already counts it), so a && self, a && Foo::Bar and a && -b score 2 like a && b, wherever the chain is written (#1529).
Python, JavaScript, TypeScript, TSX, Mozjs, C, C++, Objective-C, Mozcpp, Java, C#, Rust, PHP, Perl, Lua, Groovy, Kotlin, Tcl, iRulesA boolean slot pays one condition unless something inside its predicate already counts itEvery if / while / do / for condition, ternary condition, guard and && / || operand scores the way Ruby’s has since #1520: one condition, unless the predicate (seen through parentheses, a negation, a cast or a plain assignment) is a comparison, chain, ternary or type test that its own rule counts. Before #1526 these slots paid only for a predicate from a fixed list of operand kinds, so if (-x), if (x + 1), if (this), if (*p), if (flags & k), if (y = x) and if (sizeof x) each scored 0 against a cyclomatic decision of 1, as did Java’s, C#’s and Rust’s if (a & b) and a boolean switch / match expression. The same rule stops a wrapped comparison paying twice: if ((int)(x > 1)), Python’s if (y := x > 1): and Python’s if not (x > 0): score 1, not 2, as Java’s if (!(x > 0)) always has. Python’s elif, PHP’s elseif, Lua’s elseif and Perl’s elsif now score like Ruby’s elsif and Java’s else if: one for the else plus the predicate slot, so elif b no longer scores one below elif x > 0. Kotlin joined in #1533, so the slot pays for a when it holds, as it does for Java’s switch, C#’s switch expression and Rust’s match: if (when (a) { else -> true }) scores 1 (it scored 0) and if (when (x) { 1 -> true; else -> false }) 2 (it scored 1), its entry paying 1 and the slot 1. Go still pays a slot through an operand list (#1526).
Bashif / elif / while and each non-wildcard case arm count one conditionA Bash predicate is a command, so the branch keyword itself, not an embedded boolean expression, is the condition signal. Each matches a Bash cyclomatic decision; the bare *) case arm (the analogue of default:) is excluded, mirroring the cyclomatic standard count. The arithmetic ternary $(( a ? b : c )) counts one, as the C-family ternary does (#1268). Each comparison inside a test pays one more: ==, !=, the -eq / -z / -f family, and =, which compares in [ … ] / [[ … ]] and assigns in (( … )). The [ … ] connectives -a / -o join two tests as && / || do, and pay nothing, so [ X -a Y ], [ X ] && [ Y ] and [[ X && Y ]] score alike in every decision metric; a bare test X -a Y is all plain words to the grammar and scores none of them (#1536).
Kotlintry counts a condition alongside catchFitzpatrick counts both keywords, and Java / C# / C++ / Groovy already count both; Kotlin previously counted only the catch block.
C#, Java, Rust, Python, Ruby, ElixirA pattern-match guard is a condition slot, scoring one however it is spelledA guard is a branch: the pattern can match while the guard fails. The guard’s expression is scored exactly as an if condition is (one for a call, type test, attribute or bare identifier, one via the operator arm for a comparison), so every spelling agrees, and a compound guard (when a > 1 && b < 2) keeps its sub-structure rather than collapsing to one. Before this, ABC scored whatever operator happened to sit inside, so when x > 2 counted one and the equivalent when IsEven(x) counted none. The spellings, per language: C# when_clause on a switch arm or case section and catch_filter_clause on a catch (#1422); Java 21’s guard on a pattern-switch label, in both the arrow and colon forms; Rust’s match_pattern guard; Python’s case … if g:; Ruby’s if_guard / unless_guard on a case … in arm; Elixir’s when operator on a stab_clause head or a def / defguard head (#1454). The same change made the guard a cyclomatic decision wherever it was not already one (Java, Ruby and Elixir) and excluded Elixir’s typespec when (@spec f(a) :: a when a: integer), which spells the same token as a guard but is type syntax. Groovy and Kotlin are absent because neither pinned grammar has a guard production at all: Kotlin 2.1 guard syntax does not parse, so there is nothing to classify (#1454).
KotlinA subject-less when arm scores through the predicate slot; a subject-ful arm scores per entryA subject-less arm’s condition (when { x > 5 -> … }) is an ordinary boolean expression, compiled as an if predicate, so the comparison inside it is already counted by the operator arms and a blanket per-entry increment double-counted it. A subject-ful arm (when (x) { in 1..2 -> … }) lists a pattern rather than an independent boolean expression, so the implicit subject == pattern is a decision nothing in the source spells and the entry itself pays for it (#1421).
RubyA subject-less case scores each when pattern as an || operand; a subject-ful case scores per clauseA subject-less clause (case; when x > 5) lists ordinary boolean expressions, evaluated like an if predicate, and several patterns (when a, b) are an implicit ||. Each pattern therefore scores once unless its own operator already counted it, so when x > 5 scores 1 like if x > 5, and when a, b scores 2 like if a || b. Before this, a blanket per-clause increment counted a comparison twice. A subject-ful clause (case x; when 1) matches each pattern with an implicit pattern === x that nothing in the source spells, so the clause pays once (#1453).
GoA tagless switch scores each case expression as an || operand; a tagged switch scores per caseA tagless switch (switch { case x > 5: }, with or without an init statement) is switch true: each case expression is an ordinary boolean scored by the same slot an if predicate uses, and a list (case a, b:) is an implicit ||. So case x > 5: scores 1 like if x > 5, and case a, b: scores 2 like if a || b. Before this, a blanket per-case increment counted a comparison twice. A tagged case (switch x { case 1: }) compares with an implicit x == 1 that nothing in the source spells, so the case pays once; type-switch and select cases are unchanged (#1523).
ElixirAn if / unless predicate and each cond clause are condition slots; every other clause construct scores per clause; an if / unless else scores oneAn if predicate scores one condition unless an operator inside it already counts it, as Ruby’s does: if x > 5 and if b each score 1, and if a && b scores 2, its two operands. cond is an if / else if chain written as clauses, so each clause’s condition is scored the same way, and cond do x > 5 -> …; b -> …; true -> … end scores 2, like Go’s tagless switch twin. The unguarded true -> catch-all is cond’s default arm and scores nothing, as in cyclomatic. Before this, the keyword call added one condition on top of its predicate, so if x > 5 scored 2 and cond scored once however many clauses it had (#1527). The same change scores every && / || operand and guard that no operator counts, so a && &f/1 now scores 2, like a && b. The clauses of a case, of a with’s else, of a receive, of a try’s rescue / catch / else, and of a multi-clause fn after its head each score one, through the same gate cyclomatic uses: a bare _ -> catch-all outside a fn scores nothing, like Go’s default: and Rust’s _ =>. A typespec’s function type (@spec f((any -> any)) :: list) parses as a clause too and scores nothing in either metric; cyclomatic counted it before. The keyword forms (case(x, do: (1 -> :a; …)), cond(do: (…)), with’s else:) hold their clauses in the same parenthesised block a typespec does, so they are recognised by the do: / else: / rescue: / catch: key that holds the block, and score exactly as the do … end forms. A clause whose pattern an operator already counts does not pay again, so rescue e in RuntimeError -> scores 1, its in. So a two-clause case scores 2, like Ruby’s subject-ful case and Rust’s match; it scored 1 once per construct. Each pattern <- expr clause of a with scores one as well, because it either matches or falls through to the else, so with {:ok, a} <- g(x) do a end scores 1, like Rust’s if let Ok(a) = g(x). A when guard on that pattern is a guard slot like any other. A for generator’s <- is not a clause and scores nothing. A for … reduce: block’s accumulator clauses score like a multi-clause fn’s: the first is free, so a single acc -> … clause scores nothing (#1535). The else of an if / unless, in the block or the else: keyword form, is Fitzpatrick’s Rule 5 else and scores one, as Ruby’s, Java’s, Python’s and Go’s do (#1531). A bracketed keyword list in last position (if(c, [do: 1, else: 2])) is the same argument as the bare one and scores the same, as does a quoted key ("else": 2); a list anywhere else, or one with a non-pair item, holds no sections. A Kernel.&& / Kernel.|| remote call scores its operands as a && b / a || b does, the piped value of a |> Kernel.||(b) included (#1574), and so do Kernel.and / Kernel.or (#1582). A receive has one free arm, as a case does: its bare _ -> when the do section holds one, otherwise its after clause (the timeout, the path taken when no message clause matched in time), which then scores nothing; beside a bare _ -> the timeout is a path of its own and scores one (#1581).
C++, Objective-C, Mozcpp, Rust, Go, Java, Groovy, C#, Kotlin, TypeScript, TSX, JavaScript, Mozjs, Lua, Perl, Ruby, Bash, ElixirA < or > that is not a comparison is not a conditionEvery one of these grammars spells at least one non-comparison construct with the same bare < / > token a comparison uses, so the comparison rule is gated on the token’s parent. What that excludes, per family: template and generic brackets in C++, Objective-C, Mozcpp, Rust, Go, Java, Groovy, C#, Kotlin, TypeScript and TSX (#1274); JSX tag delimiters in TSX, JavaScript and Mozjs (TypeScript carries the same gate through the shared macro, but the .ts dialect of tree-sitter-typescript has no JSX production, so it has nothing to exclude there); Lua 5.4 variable attributes (local x <const> = 1); a C# comparison-operator overload’s declared name (operator <); Kotlin’s qualified super call (super<A>.g()) (#1297); Perl’s filehandle and lexical-handle readlines (<FH>, <$fh>, but not <STDIN>, which the grammar lexes as one token); Ruby’s superclass clause (class Foo < Bar) and comparison-operator method names (def <(other)), and Bash I/O redirection (cmd > out) (#1280); Elixir’s sigil delimiters (~s<hi>) (#1256). C# additionally excludes the operator of a relational pattern (x is > 0, and the > 5 => arm of a switch expression): the arm or if condition slot that owns the pattern already scores the decision, so counting the operator too charged a relational arm twice what the constant arm 5 => 1 scores (#1383). Its >= / <= spelling is excluded by the same rule through a separate token. The declared name of an operator overload is excluded on every spelling too: C# overloads six comparison operators and gives each a distinct token, so operator <=, operator >=, operator == and operator != join operator < and operator >, which alone were excluded before #1420. C++ and Mozcpp do the same for their seven, operator<=> included, and also exclude the operator a comparison fold spells twice: (0 == ... == a) counts once, like (a == ...) (#1448). An && / || fold applies its operator once as well, and scores what one application of it scores: (true && ... && a) what true && a does, and (... && a), whose unwritten side is one more operand, what a && a does (#1533). Their ISO alternative tokens not_eq, not, and and or count exactly as !=, !, && and || do (#1470). The exclusion is on the operator alone, not on the test that encloses it: since #1461 the is test itself is a condition wherever it is written, so bool b = x is > 0; and return x is > 0; each score 1, level with the x > 0 they are sugar for, and the same 1 a when n is > 5 guard has scored since #1422 made the guard a condition slot. Counting the pattern’s operator as well would make a relational arm worth twice the constant arm 5 => 1. PHP, Python, Tcl and iRules emit a bare < / > from no non-comparison production; C carries the same gate as its C-family siblings although, having no templates, it has nothing to exclude. The gate is a claim about the grammar’s productions, not about every parse: where a grammar resolves a generic call into nested binary_expression nodes, as tree-sitter-kotlin-ng does for id<Int>(a), no polarity can exclude it (#1394). The exclusion is ABC’s alone: Halstead counts vocabulary rather than decisions, so bca ops still bills every one of these brackets as an operator (a type-enclosing pair as one <> since #1559 and #1567), and the two metrics are meant to disagree here (#1395); see the Halstead delimiter rule.
Java, Groovy, C#, TypeScript, TSXA ? used as type syntax is not a ternaryIn each of these grammars the ternary ? and the type-syntax ? are the same anonymous token, so the ternary rule above is gated on the token’s parent. Java and Groovy exclude the wildcard bound List<? extends T> (#1274); C# excludes the nullable type int? x and the constraint where T : class?; TypeScript and TSX exclude optional parameters, properties, methods, class fields and tuple elements, and conditional types (T extends U ? X : Y, which the type checker resolves and erases before runtime, so it is no more a branch than the < / > already excluded) (#1275). Safe navigation is untouched: C#’s a?.b shares the same token and still counts, while the other languages spell theirs as a distinct one.
C#, Java, Groovy, Kotlin, Ruby, Elixir, PerlA relational operator scores by use; a value-bearing operand scores in a boolean slotFive of these grammars spell at least one relational construct as its own production rather than as a binary expression with an operator token: C#’s x is int and x is null, Java’s and Groovy’s x instanceof T, Groovy’s a in l, Kotlin’s a is T and a in 1..2, and Ruby’s one-line a in Integer. Having no token to count, each was reached only through the language’s terminal-operand set, which the walker consults inside an if / while / ternary / &&-operand slot and nowhere else, so var b = x == 1; scored 1 while var b = x is int; scored 0. Fitzpatrick’s Rule 5 counts a relational operator wherever it appears, so each now counts wherever it appears and has left the operand set; being in both would score it twice. Groovy’s spaceship <=> counts on the same rule, as it already did in Ruby, PHP, C++ and Mozcpp, although its result is an integer rather than a boolean: what Rule 5 measures is the comparison, not its type. Elixir reached the same 0 by the other route: its membership and type tests (a in [1, 2], rescue e in RuntimeError) do carry an operator token, and simply had no arm matching it. They count by use on the same rule, gated on the token’s parent so that an operator merely named (&in/2) stays excluded alongside < and >, and, since #1531, alongside ==, ===, !=, !==, <= and >=, so &==/2 and Kernel.==(a, b) score nothing. Perl’s bare pattern match (my $r = /^#/;, or m{^#}) matches the implicit $_ with no operator token at all, and scores by use on the same rule, level with $x =~ /^#/ (#1467). Two patterns are excluded: the one bound by =~ / !~, whose operator already counts, and the one split takes first (split /,/, $s), which is a delimiter rather than a match. A substitution or transliteration (s///, tr///, y///) edits its string and yields a count, so it is no test by use in either spelling: bare on $_ or bound as $x =~ s/a/b/, it scores nothing outside a slot, and inside one the slot pays for it once (#1540). The converse still holds for a construct whose value fills the slot: a cast, a Go type assertion, a Rust matches! / cfg! macro and a Python walrus are all operands and count only where a slot reads them as a predicate (#1461).
PerlA comparison’s right operand is not a condition, although the grammar parses it as onetree-sitter-perl 1.1.2 lets a chaining comparison (<, >, <=, >=, lt, gt, le, ge, ==, !=, eq, ne) take everything of lower precedence to its right as its operand, so $a > $b ? 1 : 2 parses as $a > ($b ? 1 : 2) and $a > $b && $c as $a > ($b && $c). The ternary’s condition slot and the chain’s first operand then held $b, which scored as a condition of its own: one above the JavaScript twin for each misparsed comparison. The walker recognises the shape and leaves that operand to the comparison, so both spellings score what the parenthesised ($a > $b) ? 1 : 2 and ($a > $b) && $c do. An explicit $a > ($b ? 1 : 2) is unaffected. <=>, cmp, =~ and !~ do not chain and parse correctly. The workaround goes when the grammar is fixed upstream (#1273).
PerlAn auto-quoted hash key is a string, not a callPerl quotes a bareword before => and alone in a hash subscript, a leading - included, so (name => 1), new(name => $x), (-name => 1) and $h{-name} hold the strings "name" and "-name". tree-sitter-perl 1.1.2 parses each word as a call, which scored a branch, and each -name as a negation, which billed a Halstead - operator and the operand name. Each now scores as its quoted twin ('name' => 1) does, and Halstead bills the key as one operand spelled as written: name, -name. A package-qualified word (Foo::Bar => 1), which => does not quote, a word with arguments (name() => 1) and a bareword value ((a => foo)) keep their call. bca find --type call reports the same calls ABC counts (#1580). Nor does nexits count a die or exit key as an exit (#1585). The workaround goes when the grammar is fixed upstream (#1571, #1572).

Where a constructor call lands

Fitzpatrick’s branch rule is “function invocation or object creation”, so object construction and constructor delegation count as branches even though they are not call sites. They do not appear in bca find --type call, which reports invocations only; see Semantic filters.

Which space a constructor call is charged to depends on how it is spelled, and C# 12 and Kotlin both let you spell it two ways:

class Sub(int x) : Base(x) { }            // charged to the class space
class Classic : Base {
    public Classic(int x) : base(x) { }   // charged to the constructor
}

A primary constructor’s function space is its parameter list (see Primary constructors) and nothing more. Its superclass call sits beside that list in the class header (C#’s base_list, Kotlin’s delegation_specifiers), outside every member, so no function space encloses it and the branch belongs to the class. The classic spelling puts the same call inside a constructor declaration, whose function space does enclose it.

File-level abc.branches is identical either way, so nothing shifts at the aggregate. Per-function figures do: branches_max, branches_average, and a per-space bca check --threshold abc=N all see the primary-constructor form on a class row and never on a function row. A codebase migrating to primary constructors will therefore see its per-function ABC distribution move without any behaviour change. That is the intended reading: the constructor’s space holds its parameters, and the call to the base is not among them.

Worked example

Consider this C function:

char digit_or_C(int am) {
    char c;
    if (am >= 0 && am <= 0xF) {
        c = '/';
    } else {
        c = 'C';
    }
    return c;
}

Walking the function body:

Token / constructComponentWhy
am >= 0C += 1Comparison (Rule 5, >=)
am <= 0xFC += 1Comparison (Rule 5, <=)
&&(none)Logical operator; does not contribute on its own.
if/elseC += 1else keyword (Rule 5)
c = '/'A += 1Assignment (Rule 1)
c = 'C'A += 1Assignment (Rule 1)

Total: <A,B,C> = <2, 0, 3>, magnitude √13 ≈ 3.61.

If the same body is rewritten with a unary conditional:

if (am_in_range || force_letter) {
    c = 'C';
}

the walker counts am_in_range and force_letter once each (Rule 7 / 9 unary conditional). The || operator itself contributes zero. This matches Fitzpatrick’s Rule 7 / 9 worked example in the paper: if (x || y) printf("test failure\n"); “there are two unary conditions since both x and y are tested as conditional expressions.”

Comparison with other ABC tools

The project follows Fitzpatrick’s original paper for && / ||: the operator does not count; each non-comparison operand counts once as a unary conditional. This deviates from RuboCop’s Metrics/AbcSize (which counts and / or directly) and matches StepicOrg/abcmeter and eoinnoble/python-abc. Operator-counting choices cause ABC results to differ across tools on the same source.

Algorithm

The metric walk passes syntax-tree nodes to the language’s Abc implementation. It counts assignments, calls and conditions, inspecting child expressions where a construct requires it. The four headline values are:

  • the three components themselves, assignments, branches, conditions;
  • the magnitude |<A,B,C>| = √(A² + B² + C²), which is the way Fitzpatrick recommends summarising the vector as a single number.

The full serialised output (src/metrics/abc.rs) emits these four together with value (the per-space magnitude the CLI thresholds against, which equals magnitude at a leaf space), the per-component averages (assignments_average, branches_average, conditions_average), and per-component *_min / *_max at the file scope, for fourteen fields total. The metric is specialised per language in src/metrics/abc/<lang>.rs, matching on the node kinds generated into big-code-analysis-ast/src/languages/language_*.rs.

How to read it

ABC is a size metric, not a complexity metric: a long, dull function with no decisions still scores high if it does many assignments. Fitzpatrick’s original recommendation was to use the magnitude as a relative ruler: rank a file’s functions by ABC magnitude and look at the top decile.

In practice the Ruby community adopted ABC most widely; the rubocop linter and the flog tool both default to threshold-based warnings. A Ruby method with an ABC magnitude over about 17 is conventionally a refactoring candidate; over 30 is considered hard to maintain. Those thresholds are language-specific. Expect higher values in C++ and Java, which use explicit getter/setter assignments more often.

Cognitive Complexity

G. Ann Campbell at SonarSource introduced Cognitive Complexity in the 2017 white paper Cognitive Complexity: A new way of measuring understandability and the follow-up IEEE TechDebt 2018 paper Cognitive Complexity: An Overview and Evaluation. The white paper’s link always serves the latest revision, and the rules have been reworded since 2017: v1.5 made the multi-level break / continue increment explicit. This chapter and the source cite it at v1.7, 29 August 2023, where Appendix B’s §B1 lists the increments, §B2 the structures that raise the nesting level, and §B3 the ones that pay a nesting increment.

The metric was designed as a replacement for Cyclomatic Complexity in code-quality tooling. Campbell’s argument is that cyclomatic complexity measures how hard code is to test, not how hard it is to understand: a 1024-arm switch statement scores the same as a deeply nested chain of ifs that perform identical logic, yet a human reader finds the nested code much harder to follow.

Algorithm

Cognitive Complexity starts at zero and applies three rules as it walks the tree:

  1. Ignore “shorthand” control flow. Constructs that route to a single block (a top-level if with no nesting, an else without conditions of its own, the head of a for, a ?: ternary) add a baseline +1 each, but they do not punish you for the pattern.
  2. Penalise breaks in linear flow. Conditionals, switches, loops, catch clauses and labeled jumps add increments under the rules below. A try block alone adds nothing. big-code-analysis does not score the recursive-call increment in any language: it performs no call resolution, so a recursive call costs nothing (see the per-language deviations below).
  3. Punish nesting. Every time control flow appears inside an already-nested block, the metric adds an extra +1 per level of nesting. An if inside a for inside an outer if inside a method scores 1 + 2 + 3 = 6, where a flat sequence of the same three constructs would have scored 1 + 1 + 1 = 3.

Sequences of identical boolean operators (a && b && c) score +1 for the whole run, on the grounds that a chain of &&s is no harder to read than a single &&. Switching operators (a && b || c) is where the cognitive load jumps, so the second operator earns its own +1. “Identical” means the same logical operation, not the same token: where a language spells conjunction and disjunction both ways (C++, Ruby, PHP, Perl, Elixir, iRules), a && b and c is one sequence. Precedence still decides what nests where, so Ruby’s a || b and c is (a || b) and c and scores two. xor, like ^, is no sequence.

The null-coalescing operators are not sequences either, or anything else: the paper’s “Ignore shorthand” section (v1.7, page 6) sets them aside as a one-line spelling of if (x == null) …, and so does big-code-analysis. ?? in the JavaScript family, C# and PHP, the Elvis ?: of Kotlin and Groovy, Perl’s defined-or //, the compound ??= / //= and every safe navigation (?., &., ?->) score nothing. Until #1800 the coalescing operators were folded into the boolean sequence for parity with cyclomatic complexity, which still counts each of them as a decision.

big-code-analysis exports the per-space structural score as value along with the subtree sum and a per-function average, min and max; the aggregate population section below says exactly which spaces those three read. The implementation is in src/metrics/cognitive.rs.

How to read it

A Cognitive Complexity of 0 means the function is purely linear: no branches, no loops. SonarSource’s tooling defaults to flagging functions above 15 as “too complex”, and Campbell’s recommendation in the white paper is that a function should rarely exceed about 25. Unlike Cyclomatic Complexity, the metric scales smoothly: deeply nested code scores well above flat code with the same number of decisions.

The common use is refactoring guidance during code review: because the metric penalises nesting specifically, it flags the kind of function that benefits from an early-return or “extract method” refactor. SonarLint’s IDE plugins (IntelliJ, VS Code, Visual Studio, Eclipse) all surface it as the headline complexity number on hover, and several language servers and code-review platforms outside the Sonar ecosystem have since adopted it.

Per-language deviations

What follows is where the implementation and the paper’s Appendix B part, or where the paper has nothing to say. Null coalescing is not on this list since #1800: it follows the paper now (see the paragraph above). The numbered break N / continue N of PHP and Bash is listed below only because the paper’s §B1 names it and a reader may not expect a level to count as a label.

  • No language scores the recursion increment (B1): the metric walk resolves no calls, so a function calling itself costs nothing beyond its explicit control flow.

  • Ruby’s redo and retry add +1 each. §B1’s jump bullet names only goto and a break / continue to a label or number; these two are scored by analogy with it, as unstructured jumps with no structured spelling.

  • Elixir does not score jump statements either. Elixir control flow (if / unless / cond / case / with / receive) is built from macro-shaped Call nodes rather than dedicated grammar productions, and the language has no break / continue / goto. The implementation therefore scores only the nesting-bearing constructs it can identify and omits the unstructured-jump (B1) increments that the SonarSource specification adds for languages that expose those shapes syntactically. A with expression’s else clauses act like case arms. The with expression already contributes the nesting increment, so its else adds nothing, just as Ruby’s case/else does. An if / unless / try else still adds +1. The keyword forms (else:, rescue:, catch:, bare or bracketed) score as their do … end blocks do, and Kernel.||(a, b) is a boolean sequence like a || b (#1574), as Kernel.or(a, b) is like a or b (#1582). A receive is a case over the mailbox and pays +nesting as one; its after timeout adds nothing, and a receive holding only an after is a timed wait that scores nothing (#1581).

  • For every language with a syntactic function-definition node, a nested function (a local function, or a method on a local or inner class) resets the nesting counter to zero at its boundary and adds a function-depth surcharge, so control flow inside it is scored against the nested function’s own depth rather than the enclosing function’s nesting. Byte-equivalent constructs therefore score identically across languages.

  • A lambda or closure (x -> …, |x| …, lambda x: …, a Ruby block, an Objective-C block) is not a function boundary in that sense. It adds a surcharge on top of the enclosing nesting instead of replacing it, so a decision inside a lambda written inside an if is charged for both. Which kinds are a closure, and which open a function boundary, are each language’s Closure and FuncOpener rows in its role table (see Conditions above for what a role table is); a kind can be on both (Elixir’s fn … end opens a space and is a closure), and the surcharge rule reads the Closure row.

  • That surcharge stops at the next function boundary. A function declared inside a closure body opens a fresh lexical scope, so it does not inherit the closure’s surcharge: fn g inside a || { … } scores what it would score outside one. Until #1187 only the JavaScript family applied this, so the same body scored differently in Rust, Java, C++, PHP and C# depending on whether something two levels up happened to be a closure. Python cannot express the shape at all: a def is a statement and a lambda body is a single expression.

  • PHP and Bash score break N / continue N, the only non-default jump argument either has, at a flat +1 for any explicit level, break 1 included — §B1’s break NUMBER / continue NUMBER, and what sonar-php does. A bare break / continue adds nothing. Before #1765 the numeric form scored +0 in both as a structured loop-level exit; Bash’s parses as a plain command, so it is read by its leading word.

  • Rust’s let-else (let Some(a) = y else { continue };) postdates the specification. It is scored as the if let it replaces: +1 plus the current nesting, with everything under it, the else block included, one level deeper. Its else token does not pay again. A for holding it scores 3, the same as one holding if let Some(a) = y { … }. Before #1548 the let-else paid a flat +1 through its else token whatever its depth, so that for scored 2.

  • Python charges a boolean operator an extra +1 for each enclosing lambda, on top of the +1 the boolean sequence itself earns. No other language does this. Only the outermost operator inside a given lambda body pays the surcharge, and the walk that counts those lambdas stops at the nearest enclosing expression_list. (It also stops at if, for and while, but a lambda body is a single expression, so no lambda ever sits above one of those statements and those three arms never change a count.) The sequence increments themselves still follow the operator-switch rule above, so a mixed chain inside one lambda pays two of them plus a single surcharge. Measured:

    Python sourcecognitive.sum
    g = a and b1
    g = lambda y: y and a2
    g = lambda y: y and a and b2
    g = lambda y: y and a or b3
    g = lambda x: (lambda y: y and a)3
    k = lambda q: (yield a and b, c)1
    def f(a): g = lambda x: x0

    The last two rows are the boundaries. The parenthesised yield puts an expression_list between the operator and the lambda, ending the walk before it reaches one, so only the fundamental +1 is left; and a lambda containing no boolean operator costs nothing by itself.

    Campbell gives a boolean sequence a fundamental increment and no nesting increment, so this is an addition to the specification rather than an implementation of it. Issue #1150 reviewed the rule and kept it deliberately. Python’s boolean-operator cost is not comparable with another language’s score for the same code.

Cyclomatic Complexity (CC)

Cyclomatic Complexity is the original software complexity metric. Thomas J. McCabe introduced it in 1976 in A Complexity Measure (IEEE Transactions on Software Engineering, SE-2(4), pages 308–320).

McCabe’s idea was to apply graph theory to the control-flow graph of a function. If you draw every basic block as a node and every jump between blocks as an edge, the cyclomatic number of that graph is

M = E − N + 2P

where E is the number of edges, N the number of nodes, and P the number of connected components. M counts linearly independent control-flow paths. Basis-path testing uses this count; branch coverage alone can require fewer tests.

Algorithm

big-code-analysis does not build a control-flow graph. Instead it uses the equivalent, much cheaper formulation McCabe proved in the 1976 paper for structured programs:

Cyclomatic Complexity = 1 + (number of decision points)

A “decision point” is any node where control can branch:

  • if, else if, ternary ?:
  • case / when arms in switch / match / select
  • while, do … while, every variant of for
  • exception-handler catch clauses
  • short-circuit boolean operators && and ||, including their word spellings (and / or in C++, Python, Ruby, Lua, Perl, PHP, Elixir and iRules)

A && or || counts only where it applies the operator. C++ also spells the token in rvalue references (int&& x), the ref-qualifier (void f() &&), overload names (operator&&) and requires-clause constraints, and none of those is a decision (#1525). Nothing inside a requires clause or a requires expression is a decision, written with parentheses or without (#1533); cognitive complexity and ABC conditions skip it too, though a call in it is still an ABC branch. Any other compile-time expression still counts, as if constexpr does: static_assert(A && B), and a concept body concept C = A && B.

The per-language Cyclomatic trait, in src/metrics/cyclomatic.rs, asks each tree-sitter node “are you a decision?” and increments the counter. The metric is rolled up per function and per file; per-class aggregation across method bodies is provided separately by WMC below.

Aggregate population

Every space carries its own value, a subtree sum, and a per-function average, min and max. The three per-function fields read one population: the spaces of kind function in the subtree, named functions, methods, and every closure form that opens a space of its own (a Rust |x| …, a JavaScript arrow function, a Java anonymous-class method, a Kotlin property accessor or init block, a static {} initializer). average is the mean of their values, min and max their extremes, so min <= average <= max holds on every space. A subtree with no function space reports average 0.0, min 0, max 0.

Two things are deliberately not in that population:

  • Containers and the file unit. Each still contributes its own value — base 1 for cyclomatic, its module-level decisions for all three — to sum, which is what MI and WMC read, but not to the per-function fields. Before #1284 the numerator and the extrema folded every space while the divisor counted functions only, so a file holding one branch-free function reported cyclomatic average 2.0 against min 1, max 1, and the unit’s own 0 pinned file-level cognitive min at 0 whatever the functions scored.
  • Closures that open no space. A Python lambda is counted by NOM but has no space of its own: its decisions are part of the enclosing function’s value, so that function is the one population member they belong to. Before #1284 cognitive and NExits divided by nom’s total instead, and a function holding one lambda could report average 7.0 against min 14, max 14.

The same three fields on cognitive and nexits follow this rule; sum on all three keeps its all-spaces meaning. NArgs does not follow it: its extrema fold every space and its averages divide by the function and closure counts of NOM, so closure_args_average can fall below closure_args_min (#1809). The remaining metrics’ *_average / *_min / *_max fields are per-space averages over every space in the subtree, as their sections say.

Modified cyclomatic

big-code-analysis also reports a modified variant that collapses all case / match / when arms inside a single switch statement into one decision point, regardless of how many arms it has. This undercounts big dispatch tables in a way that often matches developer intuition better than the strict McCabe definition: a 30-arm enum dispatch reads as one decision, not thirty. The convention is not original to this project; it echoes the -m mode of Terry Yin’s lizard tool, where many readers will first have seen it. Both numbers are exported side by side; pick one and be consistent.

Only alternatives collapse. A sequence of tests is still one decision per test in both variants. Elixir’s with is the case where the two meet: each pattern <- expr clause is a separate test, scored once in both variants, as Rust’s if let and ? are. The clauses of its else are alternatives, so modified scores them once, and only when one of them is a decision: else _ -> 0 selects nothing, like a plain else, and is free in both variants (#1535). Rust’s let-else (let Ok(a) = g(x) else { return 0 };) is one such test too. It binds or diverges, so it scores one in both variants, as its if let twin does. Before #1542 it scored nothing.

Elixir’s receive is a case over the mailbox, and modified scores its clauses once, as it does a case’s. Like a case it has one free arm: its bare _ -> when the do section holds one, otherwise its after clause (the timeout, the path taken when no clause matched in time). So receive do {:a, y} -> y after 10 -> :t end scores one decision in standard, as the two-clause case does, and receive do {:a, y} -> y; _ -> 0 after 10 -> :t end two: beside the catch-all the timeout is a path of its own. A receive holding only an after is a timed wait and scores nothing at all (#1581).

Counting Rust’s ? operator

By default Rust’s ? operator (the try_expression grammar node) adds +1 to both standard and modified cyclomatic, matching upstream rust-code-analysis: ? is an early-return branch. When cyclomatic is used as a maintainability gate, this can over-penalise linear but fallible code that threads a handful of ? through a happy path. You can opt out so ? is treated as linear error propagation:

  • Library: MetricsOptions::default().with_count_cyclomatic_try(false).
  • CLI: --cyclomatic-count-try=false (or the deprecated --no-cyclomatic-try alias), or cyclomatic_count_try = false in bca.toml (the CLI value overrides the key in either direction).
  • A repo gate: set cyclomatic_count_try = false in the auto-discovered bca.toml. This project’s own make self-scan does exactly this, with no flag or env var. Toggling the policy shifts cyclomatic values, so regenerate .bca-baseline.toml in the same change.

The default is unchanged (? keeps counting), so published metric values are preserved. The toggle is Rust-only; no other language emits try_expression.

How to read it

McCabe’s original recommendation, repeated in the 1976 paper and preserved by NIST’s Structured Testing report (Special Publication 500-235, 1996), is to treat 10 as the upper bound for a single function: above that, the number of test cases needed for branch coverage grows uncomfortably large.

Cyclomatic complexity is used for three things:

  1. Defect prediction. Complexity correlates, imperfectly, with the probability of a function containing a bug, and most static-analysis tools flag high-CC functions as risky.
  2. Test planning. CC measures the size of a basis-path test set, not the minimum test count for branch coverage.
  3. Refactor triage. Cyclomatic Complexity is the headline “complexity” number in almost every code-quality dashboard, often as a tie-breaker between two functions that look similar in length.

The metric’s known blind spot is that it treats every decision as equal weight. A 30-arm switch over an enum and a function with two nested ifs each containing nested ifs both score around 30, even though they read very differently. Cognitive Complexity (above) was designed to fix exactly that.

Halstead

The Halstead suite is the oldest size-and-effort metric family on this page. Maurice H. Halstead introduced it in his 1977 book Elements of Software Science (Elsevier, ISBN 0-444-00205-7); the Wikipedia page on Halstead complexity measures summarises the formulas. Halstead’s aim was a quantitative, empirical science of software, in the same way that physics is the empirical science of matter.

The four base counts

Halstead reduces a program to its tokens, then partitions them into two categories:

  • Operators, anything that does something: keywords (if, return, while), arithmetic and logical operators, assignment, function-call syntax, punctuation that controls flow.
  • Operands, anything that is something: identifiers and literals.

From these you derive four base counts:

JSON keySymbolMeaning
unique_operatorsn1number of distinct operators
unique_operandsn2number of distinct operands
total_operatorsN1total count of operator occurrences
total_operandsN2total count of operand occurrences

The serialized output uses the descriptive JSON key column; the derived-metric formulas below use Halstead’s classic n1/N1/n2/N2 notation for the same four counts.

big-code-analysis records these four numbers in src/metrics/halstead.rs per function and per file. The per-language trait classifies tokens as operator vs. operand on a token-by-token basis. Punctuation counts as an operator: a bracket pair is one operator, billed once at its opener and listed by bca ops as (), [] or {}, and ,, ;, . and : count once per occurrence. Closing brackets, comments and a literal’s own delimiters count as nothing, which is why the Halstead totals are not the same as the Tokens count.

The following rules define which syntax contributes to Halstead’s vocabulary. Context matters because grammars reuse punctuation for operators, literal delimiters and other syntax:

  • A literal’s own delimiters are not operators. A JavaScript regex, a Groovy slashy string, a C++ raw string, a Tcl braced word and a Perl or Ruby pattern each contribute one operand, the literal, and no operator for the punctuation around it. Otherwise the score would move with the author’s choice of delimiter, which says nothing about the code. The same holds for punctuation the grammar spells inside a literal: Perl’s bare /[a]#/ bills no [] or #, matching m/[a]#/, whose text the grammar does not split into tokens (#1549). Tcl and iRules need one extra step to honour this, because their grammars spell a braced literal and a braced script body the same way. {a b} is a block or a quoted value depending on the command it is passed to, so the classifier reads that command’s name. eval {…}, uplevel, after, time, an on or trap handler clause written outside a try, and Tcl’s for and switch take scripts and keep their {} operator, as does every construct the grammar models with a node of its own (proc, if, while, foreach, catch, try, namespace, an iRules when handler). Every other command (lappend, puts, list, and any user-defined proc) is taken to receive a value, so its braces score no operator.

    Recognising the command is only half of it, because most of those signatures mix the two roles. A braced word in a slot the command’s documented syntax reserves for a value scores no operator either, however script-like the command around it: after’s delay (after {100} {puts hi} scores the one {} its script earns, the same as after 100 {puts hi}), time’s iteration count, a braced proc name (proc {my proc} {} {}), a defaulted proc parameter (proc p {a {b {x y}}}), namespace export’s pattern list, namespace ensemble create -map’s dictionary, and a trap or on handler’s error code and variable list. The slots come from the Tcl 8.6 manual pages. The two proc cases are positions the grammar names, so the classifier reads them off the tree; the rest are recorded as per-command signatures in big-code-analysis-ast/src/lang_helpers/tcl_family.rs. Where a command’s argument list does not match the signature (a top-level try … trap … finally parses its whole tail as one trap command, five arguments rather than three), no position in it is read, and every brace stays a block.

    This decides the operator only. The words inside a braced argument are counted either way, because an unrecognised command is as likely to have been handed real code (an oo::class create C {…} body, a tcltest -body {…}) as a list. So a script passed to a command outside the list (a Tk -command {…} callback, a dict for body) gives up one {} occurrence per block and nothing else, with one visible consequence: a space whose only blocks are of that kind has no operator left, and because Halstead’s difficulty multiplies by the operator count, its effort reads 0.0 rather than slightly low. A proc keeps its own proc keyword and body brace, so this reaches top-level script fragments, not functions.

    The consequence for the operand column is an asymmetry, and it is the contract rather than an open defect: a braced value scores one operand where the grammar names the command (set x {a b} → the operand {a b}) and one per word where only the command name would (lappend x {a b} → the operands a and b). Both ways of removing it were weighed and neither is available. Deciding from the contents (no substitution, no nested block, therefore a list) was built and measured: on a file holding an oo::class body, a tcltest -body and an apply lambda it took n2 from 25 to 15 and N2 from 32 to 15, each of those three collapsing into a single operand. And listing the value-taking commands instead needs an open set where the script-taking one is closed, since every user proc belongs to it, so the list would be incomplete by construction and would make three behaviours where there are two.

  • A string-interpolation opener is not an operator. "{$x}" in PHP, "#{x}" in Ruby and Elixir, "${x}" in Kotlin and Groovy, $"{x}" in C#, f"{x}" in Python and Java 21’s STR."\{x}" all count the interpolated expression’s own operators and nothing for the opener itself, and the wrapping literal is not an operand either. Before #1703, Java counted the wrapping string template as an operand as well as the interpolated x. The : that opens a Python format spec (f"{x:>10}") counts nothing either, because the spec after it is string content (#1486). Nor does the self-documenting = of f"{x=}", which prints the expression’s text and assigns nothing (#1551). C#’s format : and alignment , ($"{x,8:F2}") count nothing for the same reason (#1573), but the alignment is an expression and keeps its own vocabulary: 8 is an operand, and {x,-w} counts - and w. A Python literal width (f"{x:>10}") is format-spec text and counts nothing.

  • A delimiter around syntax is an operator, whatever it delimits. The rule above is about a literal’s own punctuation, and it stops there. Halstead counts the vocabulary a program is written in and has no notion of a decision. Halstead uses the same < vocabulary entry for comparisons, JSX tag delimiters, Lua <const> attributes, C# operator < declarations and Perl <FH> readlines, and bca ops reports every one of them. This is the deliberate counterpart to the ABC deviation that excludes the same tokens from conditions: ABC asks whether a branch is taken, and these are not branches; Halstead asks what alphabet the source uses, and these are part of it. Expect the two metrics to disagree here. The same reading bills Python’s (, [, {, ; and block, slice, dict and annotation :, and Rust’s annotation, field, bound and label :, which is a separate operator from ::. Both languages left these out until #1486, so their Halstead scores rose with it.

  • A declaration keyword is an operator. Python’s class is billed like def since #1550, as Ruby, Kotlin, C# and PHP bill their class. Java, Groovy, the JavaScript family, C, C++ and Objective-C followed in #1552 with their type-declaration keywords: class, struct, union, enum, interface, record, trait, @interface, @implementation and @protocol. The class tail of Java’s C.class is an operand, not this keyword (see the class literal below). Groovy’s var bills as its def does since #1704, once per local declaration however many declarators follow it, and so does the var of a field (var field = 1), which the grammar reads as a type name (#1706), or of a parameter, a closure parameter, a resource, a catch parameter or a final var local. A var used as a name stays an operand (def var = 3, [var: 1]), or bills nothing where the grammar cannot parse the statement (var = 4). Kotlin’s interface followed in #1556. #1557 added C++’s namespace, template, typename, typedef and using (C and Objective-C bill typedef too), so template <class T> and template <typename T> each bill their keyword and score alike. It also added TypeScript’s type, namespace, module, declare and global. #1561 made TypeScript’s type-level operator keywords operators, as their value-level siblings as, typeof and extends already were: satisfies, keyof, infer, and the is and asserts of a type predicate. #1564 made Kotlin’s typealias, init, where, by, get and set operators, as C# bills its get, set, init and where. The @get: and @set: annotation targets bill nothing, like @field:. The backing field in an accessor is an operand, and the words used as names (val by = 1, x.get(i)) stay operands.

  • A declaration modifier is an operator, keyed by its keyword and never its wrapper node. C#, Kotlin, Java, Groovy, TypeScript and TSX, C, C++, Mozcpp and Objective-C bill every modifier once, as the keyword and never as the node that wraps it: Kotlin’s and Java’s modifiers, TypeScript’s accessibility_modifier and override_modifier, and the C family’s storage_class_specifier, type_qualifier and virtual_specifier. So the const of void f() const and of int * const p each bill once. C# already did; the others followed in #1558 (Kotlin), #1563 (TypeScript), #1565 (Java and Groovy), #1578 (C, C++ and Mozcpp) and #1579 (Objective-C). The exceptions:

    • The same word used as a name stays an operand (val open = 1, let readonly = 1, obj.static, var sealed = 1, int override = 1;). Where Kotlin’s grammar cannot parse the name (open = !open at the start of a statement, val suspend = 1, a parameter named vararg), the word bills nothing.
    • The static of import static and requires static bills nothing, as import does not: Java and Groovy bill no directive keyword (package, import, module, requires, exports), and the word a directive qualifies follows it. C# is the outlier here: it bills using, and so using static in full. Each reading is consistent with the language’s own directive rule, and #1600 kept both rather than move one.
    • C++’s auto is a placeholder type and stays unbilled. Attributes, the MS calling conventions, the Gecko MOZ_* macros and the Apple SDK macros (NS_INLINE, FOUNDATION_EXPORT, IBOutlet) bill nothing.

    The words each language bills:

    • Kotlin: the enum of enum class, data, sealed, the visibility keywords, open, override, suspend, reified, out and the rest.
    • TypeScript and TSX: the member modifiers public, private, protected, override, readonly, abstract, accessor and static. JavaScript and Mozjs bill static too. Their grammars lex static get followed by a newline as one static get token, which bills as one operator, so that spelling scores one entry where static get y() on one line bills static and get.
    • Java and Groovy: the visibility keywords, static, native, strictfp, volatile, sealed and non-sealed, where before #1565 they billed only abstract, final, transient, synchronized and default; plus the permits clause keyword beside extends and implements.
    • C, C++ and Mozcpp: every storage class, function specifier and type qualifier: static, extern, register, inline, thread_local, const, volatile, restrict, _Atomic, constexpr, alignas and their vendor spellings (__inline, __forceinline, __restrict__, __thread, __volatile__, __extension__, …), plus C++’s virtual, mutable, constinit, consteval, explicit, friend, the access labels, override, final and noexcept. C bills its storage class auto. extern "C" bills extern and keeps "C" an operand.
    • Objective-C: C’s list, plus the @public, @private, @protected and @package ivar labels, the @optional and @required protocol sections, the ownership qualifiers (__weak, __strong, __block, …), the nullability qualifiers (nullable, _Nullable, __nullable, …), __kindof, the __covariant and __contravariant variance, and the in, out, inout, bycopy, byref and oneway method-type qualifiers. A property attribute (nonatomic, copy, getter) bills as its word; a getter= or setter= value stays an operand.
  • A type name is an operand. #1557 made every TypeScript type name an operand (x: Foo, interface I, Map<K, V>), as C#, Rust and the C family already did. Java followed in #1560: Foo f, implements I, List<Foo> and each segment of java.util.List bill their names as operands. The inferred type var does not, as C# bills nothing for its own. Java’s primitive types are operators everywhere. Groovy, whose grammar parses a primitive type as a name, bills it the same way since #1568: int, boolean, void and the other primitives are operators keyed by their spelling, as a declared type and as a class literal’s receiver. Groovy reserves these words, and they bill as operators; in the positions Groovy nonetheless allows them as names (x.int, [int: 1]), they stay operands. Java’s boolean is listed as boolean rather than its node kind boolean_type, so both languages report it alike.

  • A class literal bills its parts. Since #1566 a Java class literal bills its parts rather than one operand spelled by its whole text, since JLS §15.8.2 defines it as an expression rather than a literal: in java.util.List.class and Foo[].class each name and the class tail are operands and each . and [] an operator, as Groovy’s Foo.class and Kotlin’s and PHP’s Foo::class bill theirs. The int of int.class and the void of void.class stay operators, as Java bills them everywhere.

  • Angle brackets that enclose types are one bracket pair. The < and > around type arguments and type parameters count as one <> operator, billed at the < as () is at the (, and the > counts nothing (#1559). That covers Vec<u8>, fn f<T>, a turbofish h::<u8>(), for<'a>, use<T> and a qualified path’s <T as Trait>:: or <T>:: in Rust; C++’s template <class T> and std::vector<int>; and the type-argument and type-parameter lists of Java, Groovy, C#, Kotlin, TypeScript and TSX, plus C#’s delegate*<int, void> pointer type and Kotlin’s super<A> (#1567). Objective-C has four generic lists: NSArray<NSString *>, id<NSCopying>, the protocol list in @interface A : NSObject <NSCopying>, and a type passed as a call argument, f(NSArray<NSString *>). A comparison’s < and > and a shift’s << and >> stay separate operators, and a C++ >> that closes two template lists parses as two >, so it bills nothing. The rule follows the parse: where tree-sitter-cpp reads comparisons as a template argument list, as in x = a < b || c > (d), their brackets bill as <> too, since only name lookup tells that from a template call spelled the same. The comparisons Objective-C and C# misread as a call argument’s generic list, g(a<b, c>d) and F(a < b, c > d), keep < and >. The exception is C#’s >= spelling, F(a < b, c >= d), which the grammar parses as an assignment and which still bills <> and =. Go’s generics use square brackets and already billed []. Angle brackets around anything other than types still bill < and >: the JSX, Lua and Perl constructs above. So does a Kotlin generic call with arguments, id<Int>(a), which tree-sitter-kotlin-ng parses as two comparisons (#1394), and a generic inside a Rust macro call, vec![Vec::<u8>::new()], whose token tree tree-sitter-rust leaves unparsed; a >> closing two lists there bills as the shift. Before #1559 each bracket billed under the comparison’s own entry, so Vec<u8> and a < b shared one. Every generic now costs one operator occurrence less, so N1 falls in any code that uses generics. n1 rises by one where a comparison < or > remains beside the new <>, so halstead.effort falls in most files and rises in the rest; on the C++ and Rust corpora it fell in 87% of the files that moved, and MI rose in most of them.

  • An alternative spelling is an operator of its own. C++ spells && equally as and, != as not_eq and so on through all eleven alternative tokens. Each counts once in total_operators, exactly like its symbol, but a && b and c has two entries in unique_operators where a && b && c has one. Operators are distinct by token, and bca ops lists and and && separately, so the metric matches what it reports (#1522).

  • A self-reference is an operand. this, self, $this, super and base name the receiver of a member access, so this.x counts the way p.x does: one . operator and two operands. Java, Groovy, C#, Kotlin (labelled this@Outer included), Python, Rust and PHP all agree. The keyword is an operator only where it declares something, as the operator keyword of an overload does: C#’s indexer public int this[int i], and the wildcard bound ? super T in Java and Groovy (#1380).

  • A keyword’s role follows its text, not its node kind. C#’s grammar parses the bare this, ref, out, in, scoped and readonly of a parameter, and the static / async of a lambda, as a modifier node, a different kind from the one the same keyword has anywhere else. Each keeps the role its text has elsewhere: an extension method’s this Foo f is an operand, the ref in M(ref int a) is the same operator as the ref in the call M(ref x), and static (int x) => x bills the same static as static void M(). A repeated operator keyword adds to total_operators but not to unique_operators (#1418, #1482).

  • Perl’s __END__ and __DATA__ end the program. Neither marker nor the data section after it is an operator or an operand, so two files that differ only in their data section score alike. The grammar holds the marker and every byte after it in the same special_literal node it uses for __FILE__, which until #1644 billed the whole section as one operand spelling its contents, and billed each of __FILE__, __LINE__, __PACKAGE__ and __SUB__ twice, once as that wrapper and once as the keyword inside it. Each constant is now one operand, billed once. The marker and the opaque data leaf are still leaves, so tokens still counts them.

  • JSX element text is an operand. In JavaScript, Mozjs and TSX, the hi of <p>hi</p> counts the way a string’s contents do. It is keyed on its text with leading and trailing whitespace trimmed, so a line of text at two indentation depths is one operand, and the spaces between two tags on one line count as nothing. Whitespace inside a multi-line text is kept. A character reference (&amp;) splits the text into separate operands (a &amp; b is three). Inside an attribute string, the reference is part of that string’s one operand (#1483).

  • An overloaded operator’s name is an operator. The + of operator + was always one, and the names a grammar spells as whole tokens now agree: C#’s operator true / operator false, and C++’s operator[], operator(), operator""_x and operator co_await. The operator keyword is an operator in both languages, as is C++’s co_await in an expression. C++’s [] and () names are tokens of their own, so bca ops lists operator[] beside a subscript’s [] as a second operator, as Ruby does for def [](i) (#1296). C++’s coroutine keywords co_return and co_yield are operators as return is (#1547).

  • Only written tokens count. When tree-sitter cannot parse a construct, its error recovery may insert zero-width tokens the source does not contain: a ;, a ), an identifier. None of them is billed in either half. Before #1546 an inserted identifier became an operand whose text is the empty string. One case is a C++ destructor called through a template name (p->~T<A>()), which tree-sitter-cpp cannot parse. Another is a statement-position typename cast, which made every inserted ; a second ; operator.

  • A named operator is one operand. Elixir names an operator in a capture (&==/2, &and/2) and as the function of a remote call (Kernel.||(a, b)), and its grammar wraps the token in an operator_identifier. That wrapper is the operand, as foo is in &foo/2, so the token inside it adds no operator; &==/2 scores what &foo/2 does. A captured && / || / and / or is no decision either (#1534), but the remote call applies the operator, so Kernel.||(a, b) scores what a || b does in cyclomatic, ABC and cognitive alike, and a user module’s Foo.||(a, b) in none (#1574). Kernel.and(a, b) and Kernel.or(a, b) decide the same way, though their names are plain identifiers with no operator token (#1582). This differs from a C# operator <, whose < has no wrapper and so stays the operator the rule above makes it.

Derived metrics

Halstead then derives a set of formulas. big-code-analysis reports all of the standard ones, plus three less common derivations (estimated_program_length, purity_ratio, level) that are part of the original suite:

vocabulary               n  = n1 + n2
length                   N  = N1 + N2
estimated_program_length N̂  = n1·log2(n1) + n2·log2(n2)
purity_ratio                = N̂ / N
volume                   V  = N · log2(n)                          (bits)
difficulty               D  = (n1 / 2) · (N2 / n2)
level                    L  = 1 / D
effort                   E  = D · V          (elementary mental discriminations)
time                     T  = E / 18                               (seconds)
bugs                     B  = E^(2/3) / 3000 (estimated delivered defects)

The numeric constants come from Halstead’s empirical fits against a heterogeneous corpus of CDC-era programs including FORTRAN, PL/I, and Algol-family languages. The T = E / 18 “Stroud number” comes from psychology instead: Halstead borrowed John Stroud’s estimate that the human mind makes about 18 elementary discriminations per second.

How to read it

Halstead’s original intent was to predict three things about a program before it was written: how big it would be in bits, how long it would take to implement, and how many bugs to expect in deployment. The empirical evidence for the volume and length predictions is reasonable; the time and bugs predictions are more controversial and have been criticised at length, notably in the Purdue technical report Software Science Revisited.

In modern practice the Halstead numbers are used for three things:

  1. As inputs into composite metrics, most importantly the Maintainability Index (next section), which depends on Halstead volume.
  2. As a language-independent size proxy: volume in bits scales smoothly across languages in a way that LOC does not.
  3. For comparative effort budgeting: when two refactoring candidates have similar cyclomatic complexity, the one with the higher Halstead difficulty is the one more likely to introduce regressions.

Lines of Code

This section covers the LOC variants: SLOC, PLOC, LLOC, CLOC, BLANK, and DLOC. “Counting lines” sounds trivial until you have to define exactly what counts. SLOC through BLANK are the de-facto standard breakdown, going back to Samuel Conte, Hubert Dunsmore and Vincent Shen’s 1986 textbook Software Engineering Metrics and Models (Benjamin/Cummings, ISBN 0-8053-2162-4), which codified the distinction between physical and logical lines. The Wikipedia entry on source lines of code summarises that physical-versus-logical distinction.

VariantCounts
SLOCSource Lines Of Code: every line in the file, comments, blanks, and code alike
PLOCPhysical Lines Of Code: non-blank, non-comment-only lines
LLOCLogical Lines Of Code: statement-bearing lines (definitions, assignments, declarations)
CLOCComment Lines Of Code: lines that contain a comment (with or without code on the same line)
BLANKBlank lines: whitespace-only lines
DLOCDocumentation Lines Of Code: rows the language treats as documentation (a doc comment, a Python docstring, a POD block); always a subset of CLOC

Algorithm

big-code-analysis derives every count from a single pass over the tree-sitter syntax tree (see src/metrics/loc.rs). Comments and strings are identified by their AST node type rather than by lexical scanning, so multi-line strings, raw strings, doc comments, and string interpolations are all handled correctly. The per-language Loc trait specifies which node kinds count as a “statement” for LLOC; this is the subtle one, because what counts as a statement is language-defined.

One kind of row has no node to identify it. In Bash, Tcl, iRules, the C family (C, C++, Mozcpp, Objective-C), Ruby and Elixir the lexer splices a line-continuation \ and its newline away, so a row holding only that \ is invisible in the tree. Such rows are found by reading the bytes between nodes. Each one counts as PLOC, the code it continues. The exception is a row that continues a Tcl or iRules comment (# note \), which counts as CLOC; one that continues a C-family // comment is part of that comment’s own node and counts as CLOC with it, and one that continues a row holding only C-family block comments (/* c */ \) counts as what the rest of that line makes it. A row that continues nothing starts a line and counts as what that line becomes: CLOC when the line holds only comment text, blank when the row it joins is empty, PLOC otherwise, as for /* c */ int z = 1;. Python’s grammar keeps the \ as a node, and the same rule applies to it: a Python # comment never continues, so # c \ followed by a bare row joins the row after it, and a \ that ends a bare-string statement counts nothing, the string’s rows being CLOC already, as is a bare row that continues it — unless code precedes the string on that logical line, or the run leads into another statement (; x = 1), where the row is code (#1819); a bare row before any string literal stays blank, because the grammar’s scanner drops that \ (#1825). A bare row inside a literal is part of the literal, which should make it PLOC, but a C-family character literal, a Ruby %w[], regex, quoted symbol or command, and an Elixir string, sigil or quoted atom still leave it blank (#1816). Where CRLF line endings reach the parser unchanged, through the library’s own analyze or Python’s Ast.parse, the Tcl and iRules grammars lex the bare row as a word, so there it counts as PLOC even after a comment (#1802). bca, the web server and the Python bindings’ analyze, analyze_source and Ast.from_path convert line endings to LF first.

Documentation rows are comment rows that are also reported on their own as DLOC (loc.dloc), so CLOC can be read with or without them. Each language’s own documentation tool decides which comments are documentation; there is no single rule for all:

  • Where the grammar marks it, the mark decides: a Rust ///, //!, /** */ or /*! */ comment (tree-sitter’s doc marker child; //// and /**/ carry none), a Groovy groovydoc_comment (but not the empty /**/ that grammar also marks), a Perl POD block (but not one that is nothing but =begin comment and =for comment regions, Perl’s idiom for commenting code out; POD after such a region’s end documents, and makes the block documentation).

  • Where the grammar emits one comment kind for every form, the opening marker decides, as the language’s documentation tool reads it: Doxygen’s /**, /*!, /// and //! (and their after-member < forms) for C, C++ and Objective-C; /** for Javadoc, KDoc, JSDoc and PHPDoc, plus /// for Java’s Markdown doc comments; /// and /** for C# XML docs. The tools disagree at the edges and so does DLOC: /*** is documentation to javac, Kotlin, Groovydoc and tsc (so TypeScript and TSX read it, while plain JavaScript follows JSDoc) but not to Doxygen, JSDoc, Roslyn or PHP; /**/ is documentation nowhere; PHP needs whitespace after /**; and //// is never documentation, which is stricter than Doxygen’s lexer.

  • A doc comment counts only where its tool reads it. A Rust, Java, Kotlin, Groovy, C# or PHP doc comment inside a function or closure body, inside a Java or Groovy initializer, or inside a Kotlin accessor or init block, is an ordinary comment: rustc warns that one on a statement is unused, Roslyn raises CS1587, javadoc and Dokka document declarations only, and an in-body PHPDoc /** @var */ is a type hint. A working note spelled /// … inside such a function therefore stays charged by MI. C, C++ and Objective-C keep in-body doc blocks: Doxygen appends them to the function’s documentation as its in-body description, and HIDE_IN_BODY_DOCS defaults to NO. Two placements inside a function still document: a Rust inner doc comment (//!, /*! */) opening a function’s body is that function’s own documentation, and a doc comment in a signature counts where its tool reads it there (a Doxygen /**< parameter block or a block after [[attributes]], a PHPDoc after #[…] or on a promoted constructor property, a KDoc after an annotation), but not in Java or C#, which bind a doc comment only before a declaration’s first token. JavaScript, TypeScript and TSX keep in-body JSDoc, which documents inner symbols, and Perl keeps POD wherever it sits, as perldoc does, after __END__ or __DATA__ too, where every other non-blank row of the data section counts as PLOC.

  • In Rust, Java, Kotlin and Groovy, a doc comment also has to be attached: it counts only when the declaration after it is one its tool documents. Attributes, annotations, modifiers and blank rows may come between the comment and the declaration. A Rust 2024 safe fn in an extern block counts too, although tree-sitter-rust parses its safe as an error. A doc comment above anything else stays a plain comment row: an import (Rust’s use and extern crate take one), a statement, a macro call, a Rust extern block, a closing brace, or the end of the file. A KDoc nested in an argument or type-parameter list documents nothing. The tools also differ on a run of several doc comments. rustc attaches every outer doc comment above an item, so all of them count. javac and Kotlin keep only the last one; javac’s -Xlint:dangling-doc-comments names the earlier ones, and they do not count. Consecutive /// lines are one Java Markdown doc comment. Groovydoc reads only the comment nearest the declaration, so a plain // comment in between detaches it. A Java doc comment above package counts only in a file with no type after it, which is the shape of package-info.java. Rust’s inner //! is unaffected: it documents the item it sits in.

  • C#, PHP, JavaScript and TypeScript bind doc comments the same way. Each language still follows its own tool:

    • C#: Roslyn keeps the nearest run of doc comments that only blank rows separate, reading past plain comments and directives such as #region to find it. Every other doc comment is CS1587, and so is a doc comment above a namespace, a using directive, or an accessor such as get;.
    • PHP: phpDocumentor gives a class, function, method, property, constant or enum case the last DocBlock before it. A DocBlock above a statement, a use or a namespace counts only as the file DocBlock. phpDocumentor takes the file’s first DocBlock as the file DocBlock when no class, function, constant, interface or trait follows the DocBlocks at the top of the file. It also takes it when exactly two DocBlocks come before the first such element.
    • JavaScript: JSDoc gives a declaration or an assignment the nearest block comment before it, reading past // lines, so a plain /* */ in between detaches a doc comment. A call or another statement takes nothing. A block tagged @file, @fileoverview, @overview or @module, or with a tag that names a symbol (@typedef {Object} T), documents that symbol wherever it sits. @license does not make a block stand alone.
    • TypeScript: tsc keeps the last /** before a declaration, reading past every plain comment, and documents no assignment statement. A doc comment after a decorator, a modifier such as static, or export or declare is inside the declaration and documents nothing, and so does one after code on its own row, except before a parameter. A @module or @packageDocumentation block documents the file, as TypeDoc reads it.

    In a JavaScript or TypeScript function body every doc comment still counts, since JSDoc documents inner symbols.

  • C, C++ and Objective-C follow Doxygen, which keeps every doc block it meets and gives them all to the next entry. A doc block counts when what follows it is something Doxygen documents: a declaration, a definition, a macro, a typedef or using, a namespace, a template, an enumerator, a macro call such as FOO(bar);, or an Objective-C @interface, @property or method. Doxygen reads past blank rows, plain comments, #include, #import, #pragma and an empty ;, and into an include guard or another #if that holds a declaration. A doc block above a forward declaration (struct s;, @class C;), a closing } or @end, or the end of the file stays a plain comment row. A block carrying a structural command documents an entry of its own, so it counts wherever it sits: \file, \mainpage, \page, \defgroup, \addtogroup and \name always, and a command that names its subject, such as \class X or \def M, when it has a name. The doc blocks above a \file block count with it. \brief and \ingroup are not structural. A member-after comment (/**<, ///<) documents the member before it and always counts, as do the blocks in a function body, which Doxygen publishes as the function’s in-body description.

  • In Python, every bare string statement (a string whose value is discarded) is a comment row, but only one where Python reads documentation is DLOC, following PEP 257: the first statement of a module, class or function (the string stored as __doc__, which must be a str literal, never an f-string or bytes), successive strings after any docstring, and an attribute docstring directly after an assignment to a name or a type alias at module or class level, or after an assignment to self.<name> at the top of a class’s __init__, alone or in a tuple of targets, which Sphinx autodoc reads. Sphinx also reads such an assignment inside an if, with, loop or try suite; PEP 257 names the top level only, and DLOC follows the PEP there. PEP 258 also includes successive strings after attribute docstrings. Comments between them do not count as statements. Any other bare string, such as a triple-quoted string wrapping commented-out code in the middle of a function, stays a comment row and is charged by MI like a # comment. This is narrower than radon’s multi, which takes every bare multi-line string. A docstring row that also holds code (def f(): """doc""") is code too. An implicitly concatenated literal ("""doc""" "more") is one string and a bare tuple of strings is none, the same test Halstead applies when it drops a bare string’s operands. A parenthesized string (("""doc""")) is the same string, because CPython drops the parentheses, and its ( and ) rows are its rows. A comment is documentation in Python only in Sphinx’s #: form: consecutive #: lines directly above one of those attribute assignments or aliases, or a #: after the code on its line (MAX = 10 #: in bytes). A plain # line breaks a #: block.

Go, Ruby and Lua have no marker to read, so a comment is documentation by where it sits, decided once per run: the chain of comment siblings on consecutive rows that hold nothing but comments (a trailing x := 1 // t never joins one, and a blank row ends one):

  • Go: a run directly above a declaration is go/doc’s lead comment, where a declaration is the package clause, a func or method, a top-level type / var / const declaration or one of its grouped specs, a struct field, or an interface method; never an import, and never a declaration inside a function body, which go/doc does not read. A directive row such as //go:generate belongs to the group but is not documentation, and is not counted.
  • Ruby: a run directly above a def, def self., class, module, class << Const, a constant or class-variable assignment (MAX = 10, Foo::BAR = 1, @@count = 0), a method alias (alias new old, alias_method :new, :old), an attr or attr_* call or a visibility-wrapped def (private def m), alone or under an if / unless modifier, as YARD attaches it, including the first statement of a class body or of an if, elsif or unless body. In a method body YARD reads only a nested def, and nothing in a singleton class or conditional opened there; no YARD handler claims a case, a begin or a loop, so nothing in their bodies counts anywhere. A shebang, an encoding line or a frozen_string_literal magic comment is dropped from the run, as YARD drops it. Unlike YARD, one blank row between the run and the def is a gap, not a tolerance; a trailing comment on the statement’s own row (MAX = 3 # The maximum.) documents nothing; and a statement in a call’s block (included do, an RSpec describe block, in either do … end or { } form), which YARD never reads, still counts. A block that opens with RDoc’s ## (exactly two #, so a #### banner or a ##### Helpers ##### divider opens nothing) or a YARD @! directive (# @!method, # @!attribute, # @!parse) documents a method with no def to sit above (define_method, a DSL call), so it counts whatever follows it, in a namespace body but never in a method’s.
  • Lua: a run that opens with an LDoc --- row (a row of dashes alone opens too; --- x --- is LDoc’s end marker and does not) and continues over the -- rows below it, or a --[[- long comment. A long comment ends a run, as LDoc’s lexer ends a block there. A block with no text (a row of dashes alone) is a comment, as LDoc discards it. A --- row directly under a line that ends in a trailing -- note is no block at all: LDoc’s lexer keeps a line comment’s newline and gathers consecutive comments into one group, whose first token (the trailing note) decides it. Otherwise a block counts when it is the first block of a file that opens with a comment or a shebang, which LDoc skips (the module’s documentation), when one of its lines opens with an @ tag (-- @param x, LuaLS’s ---@class), or when an item follows it on the next row (the first statement of an if, do or loop body included): a function, an assignment to one name or dotted name (local x = 1, M.a.b = 1; not a bare local x, local a, b = …, local x <const> = … or M[1] = …), a named table field (new = function … end), or a return of a function or a table. Any other block is a comment, as LDoc drops it with a “definition cannot be parsed” warning. Three deviations from LDoc: a tagged block counts even with no item or name to attach to; a blank row between a block and its item is a gap; and a file that opens with code still has its blocks read, where LDoc reads none of them without a module() call. A block inside a function body is a comment: LDoc documents what a module declares, and an in-body --- annotates the code there instead (LuaLS’s ---@type on a local), as PHP’s in-body @var does.

Nothing else counts: an Elixir @doc heredoc is an assignment the compiler stores and is code, and Bash, Tcl and iRules have no documentation syntax.

The counts satisfy a few useful identities:

SLOC = PLOC + BLANK + (lines that are comment-only)
CLOC ≥ (lines that are comment-only)        # CLOC also counts mixed code+comment lines
DLOC ≤ CLOC                                 # every documentation row is a comment row

How to read it

  • SLOC is what most people mean colloquially by “lines of code”. It is the canonical size proxy, but is sensitive to formatting and not portable across language conventions.
  • PLOC strips away the visual noise. It is the measure to gate file length on (loc.ploc), and the size term of the Maintainability Index below.
  • LLOC is the most reliable statement count. It is the right measure if you are budgeting test cases per statement, or comparing the density of a Python file against a Java file.
  • CLOC, combined with PLOC, gives you a comment density: CLOC / PLOC is a rough proxy for how much of the file is documentation versus implementation.
  • BLANK is mostly diagnostic: a file with a very low BLANK proportion is often hard to read.
  • DLOC is the slice of CLOC that is documentation: doc comments, docstrings, POD. CLOC − DLOC is the count of working comments, the numerator of the Maintainability Index’s SEI comment share.

LOC variants are used well beyond raw size. They are the most common input into cost-estimation models (COCOMO and COCOMO II both use KSLOC, thousands of source lines, as their base unit), they feed effort prediction in product-portfolio dashboards, and they are the normalising denominator for almost every other metric: defects per KSLOC, churn per KSLOC, test cases per KSLOC. The weakness is that LOC is easy to game: a 10× difference in coding style can produce a 2× difference in LOC. That is why this chapter has so many other metrics in it.

Maintainability Index (MI)

The Maintainability Index is a composite metric that rolls several of the metrics above into a single number, roughly 0 to 100, meant to be read as “how maintainable is this code?”. Paul Oman and Jack Hagemeister proposed it in their 1992 ICSM paper Metrics for assessing a software system’s maintainability, and Don Coleman, Dan Ash, Bruce Lowther, and Paul Oman refined it in the 1994 IEEE Computer paper Using metrics to evaluate software system maintainability (IEEE Computer 27(8), pages 44-49). Their method was empirical: they collected expert maintainability ratings on a handful of production Hewlett-Packard systems, computed forty candidate metrics on each, and let regression analysis pick the best linear combination. The combination that survived used Halstead volume, cyclomatic complexity, lines of code, and comment density.

big-code-analysis reports the three formulas that have stuck in practice. The three values nest under the mi object as the keys original, sei, and visual_studio (the dotted threshold names mi.original, mi.sei, mi.visual_studio):

mi.original      = 171 − 5.2·ln(HV) − 0.23·CC − 16.2·ln(PLOC)
mi.sei           = mi.original + 50·sin(√(2.4·CM))
mi.visual_studio = max(0, mi.original · 100 / 171)

CM               = (CLOC − DLOC) / (SLOC − DLOC rows that carry no code), in [0, 1]
  • mi.original is the Coleman–Oman formula. It can be negative for pathological files.
  • mi.sei is the Software Engineering Institute’s refinement, which adds a comment-density term. The sin(√(...)) shape was chosen so that some comments help, but adding more after a point does not, and never hurts. CM is the share of working (non-documentation) comment rows among the space’s rows, a ratio in [0, 1]. On that scale the term rises monotonically from 0 with no comment to about 50 (50·sin(√2.4) ≈ 49.99) when every row is a comment, so mi.sei is never below mi.original and equals it in a comment-free space.
  • mi.visual_studio is the linear rescaling Microsoft chose for Visual Studio, where the score is clamped to [0, 100] and shown to developers traffic-light style: green ≥ 20, yellow ≥ 10, red below.

The size term is PLOC, the rows that carry code (#1607). Comment, documentation and blank rows never enter it, so a // TODO, a commented-out block, a Rust /// block, a docstring or a run of blank lines leaves mi.original and mi.visual_studio exactly where they were, with Halstead and cyclomatic unchanged. A row a doc comment or docstring shares with code, as in def f(): """doc""" or int x; /** doc */, is a code row and stays counted.

Documentation also leaves both sides of CM in every language (#1597), so a function’s mi.sei is the same with and without its /// block, its Javadoc comment, its POD or its docstring, which keeps MI consistent with Halstead, where a docstring’s operands are already ignored. Blank rows stay in CM’s denominator: adding one to a space with a working comment dilutes the comment share and lowers mi.sei slightly, while a comment-free space is unmoved.

Two details differ from some published statements of the SEI formula. Its ln matches mi.original’s; releases before #1607 took log2 of the volume and size in mi.sei alone. And CM is a ratio rather than a percentage: on the 0–100 scale the sin term swings between −50 and +50 several times across ordinary comment densities.

Charging documentation

With mi_charges_docs on, mi.sei counts documentation as comment: it takes its comment share as CLOC / SLOC, the inputs that releases without loc.dloc fed that share. Nothing else moves. The size term is PLOC in both modes, so mi.original and mi.visual_studio read the same either way, and since the comment term only rises with CM, charging documentation can only raise mi.sei. It does not reproduce an earlier release’s numbers: those sized every variant on SLOC and took mi.sei on a different scale. It changes no other metric: loc.dloc is reported either way.

  • Library: MetricsOptions::default().with_mi_charges_docs(true).
  • CLI: --mi-charges-docs (or --mi-charges-docs=false to say the default out loud), or mi_charges_docs = true in bca.toml; the CLI value overrides the key in either direction.
  • Python: mi_charges_docs=True on analyze, analyze_source, analyze_batch, analyze_paths and Ast.metrics.
  • The REST server has no request field for it; an embedder sets WebMetricsCfg::with_mi_charges_docs.

The LOC inputs it reads are the current ones, so a Python file whose docstring is an implicit concatenation ("""doc""" "more") or whose bare "a", "b" tuple statement was once a comment row reads a different comment share from a release that classified those rows the other way.

A baseline records which mode wrote it, and bca check --baseline warns when it reads mi.* entries under the other mode. Only mi.sei entries can disagree, and in one direction: a value recorded with documentation charged sits above the default’s, so reading it under the default can fire on untouched code, and a value recorded under the default can cover a real regression under the charged mode. Switching the mode therefore means reviewing any mi.sei threshold and regenerating the baseline in the same change, as for cyclomatic_count_try. bca check --print-effective-config names the mode in force.

A baseline also records the MI formula revision that computed its values (mi_formula in [provenance]; revision 2 since #1607, and an unstamped file reads as revision 1). When a baseline holding any mi.* entry was written under another revision, bca check --baseline warns that its values are not comparable, in place of the mode warning; refresh it with --write-baseline.

Arie van Deursen’s blog post Think Twice Before Using the Maintainability Index collects the historical context and a sharp critique of the metric.

Algorithm

The implementation is purely arithmetic: src/metrics/mi.rs consumes the already-computed Halstead, Cyclomatic, and LOC metrics and applies the three formulas. Because the formulas use the natural log of Halstead volume and of PLOC, MI is undefined for empty files; big-code-analysis returns 0.0 for any space with zero code lines or zero Halstead volume (a comment-only file or a docstring-only module has no volume). That 0.0 is a sentinel, not a score, and it cannot be told apart from a measured 0 or from mi.visual_studio’s clamp: mi::Stats::is_empty() identifies such a space in the library, and bca report’s averages skip it. The JSON value stays 0.0 until 3.0, which is planned to emit null instead (#1621).

How to read it

MI was originally designed as a portfolio-level score: “how much maintenance pain should we expect from this codebase over the next year?”. It is stable across releases of a healthy system and tends to drop measurably before a system enters the “legacy” quadrant.

The common use is the Visual Studio traffic-light rendering: Visual Studio exposes mi.visual_studio through green, yellow and red indicators on a method, which is where many .NET developers meet it, and also why it has attracted much criticism. Treat it as a smoke detector, not a thermostat: a sudden drop is a useful signal, but the absolute number is noisy.

NArgs

NArgs counts the number of arguments declared by a function, method, or closure. The metric has no well-known origin paper; guidance on limiting argument counts appears in Kernighan and Plauger’s The Elements of Programming Style (1974) and Robert C. Martin’s Clean Code (2008), which suggests three arguments as a soft ceiling.

big-code-analysis splits the count by callable kind: every aggregate is reported separately for functions and closures so a Rust file heavy on |…| … closures and a Java file with only methods produce comparable numbers. The serialised output (src/metrics/nargs.rs) is function_args, closure_args, function_args_average, closure_args_average, total, value, average, function_args_min, function_args_max, closure_args_min, closure_args_max. The implementation handles default arguments, variadic arguments, keyword-only arguments, and destructured parameters consistently per language. Comments written inside the parameter list are not parameters, including the C++ idiom that puts one where an unused parameter’s name would go: void f(int /*unused*/) is one argument.

Macro-obscured C-family declarators

C, C++, Mozcpp and Objective-C are parsed without running the preprocessor, so a function-like macro standing where the declared name belongs is still there in the tree. The idiom is the JNI shim:

#define RUN_STATS_METHOD(name) JNICALL Java_org_tensorflow_RunStats_##name

JNIEXPORT jlong RUN_STATS_METHOD(allocate)(JNIEnv *env, jclass clazz) { … }

Read as written, RUN_STATS_METHOD(allocate) is the declarator and (JNIEnv *env, jclass clazz) belongs to the return type: one argument. big-code-analysis reads it the other way and reports two, because neither language lets a function return a function type (C11 6.7.6.3p1, C++ [dcl.fct]): a declarator nested directly inside another one cannot be a declarator chain, so the outer list is the function’s own. Every legitimate function-returning-a-function-pointer form, such as int (*fp(int a, int b))(int c), puts parentheses in between and is unaffected, as is C++ operator().

The space is named for the macro, not the function. There is no other candidate: the real symbol is assembled by ## token pasting and never appears in the source. So several shims in one file share a name, which bca check shows as several rows with the same label at different lines, and which .bca-baseline.toml disambiguates by body hash the way it does an overload set.

What the threshold gate measures

bca check --threshold nargs=N gates each callable on its own parameter list, which is what every comparable tool measures: RuboCop Metrics/ParameterLists, ESLint max-params, Clippy too_many_arguments, lizard, SonarQube S107 and Pylint R0913 all count one callable at a time.

This is not the serialized total. The function_args and closure_args keys above are subtree sums: a function that declares two parameters and contains a three-parameter nested function reports function_args: 5. Before #1196 the gate read that sum, so a three-parameter function with a two-parameter sort comparator was flagged at 5, and the remediation its number implied, fewer parameters, was not the one that would clear it.

The number the gate does read is serialized as value, added in #1236 so a JSON-consuming front-end can reproduce the gate. Until then the Python to_sarif binding had nothing but total to compare and reported breaches bca check did not.

Nothing escapes the narrower rule. In the twelve grammars whose closures open their own space — Rust, JavaScript, TypeScript, TSX, MozJS, C#, Go, PHP, Perl, Ruby, Lua and Elixir — a closure is gated on its own offender row, which is also where its fix belongs. In Python, Java, Kotlin, C++ (and its Mozilla dialect), Groovy and Objective-C a lambda, closure or block opens no space, so its arguments can only be attributed to the enclosing function; there the offender row shows the split:

small: nargs = 8 (1 own + 7 lambda) (limit 5)

The split identifies how many arguments come from the enclosing signature and how many come from lambdas.

Both lists are read from the per-language role tables rather than maintained by hand: a language is in the first when every kind its Closure row owns is also an opener of its FuncOpener row, and in the second when none is (closures_that_open_a_space_are_what_the_book_says in big-code-analysis-ast/src/roles/space_tree_tests.rs pins the two against this paragraph; Bash, C, Tcl and iRules have no closure at all).

Primary constructors

A Kotlin primary constructor and a C# 12 primary constructor — a positional record included — have no body, only a parameter list in the class header:

data class D(val a: Int, val b: Int, val c: Int, val d: Int, val e: Int, val f: Int)
public class C(int a, int b, int c, int d, int e, int f) { }
public record R(int A, int B);

Each opens a function space of its own, named after its class and spanning the list, so it is gated like any other callable: the row above reads D::D: nargs = 6. detekt’s LongParameterList gates constructors the same way, under a threshold of its own and a data-class opt-out that is on by default; bca has neither, so the data class above is reported. Before #1828 the list belonged to no space and counted nowhere, so a six-parameter data class passed --threshold nargs=5. The space also counts as one callable in NOM, NPM and WMC — a written class G() declares a zero-parameter constructor and counts too — and its superclass call stays with the class, as Where a constructor call lands explains. A default spelled as a lambda (class L(val f: (Int) -> Int = { it })) adds that lambda’s parameters to the constructor’s closure_args, exactly as a function’s spaceless lambdas add to its own, so the row’s split shows them.

The space is the list and nothing more, so a suppression marker has to sit inside it — a marker on the line above binds to no function, as for any other callable:

data class Row(
    // bca: suppress(nargs) — a column set, not a signature
    val a: Int, val b: Int, val c: Int, val d: Int, val e: Int, val f: Int,
)

A parameterless class G() opens a space with no code in it, so its mi.* read the 0.0 placeholder that Maintainability Index describes and bca check and bca report skip.

Two things a reader of the rows will meet. A classic constructor beside a primary one shares its name — C#’s H::H twice, one per spelling — and bca check --baseline tells two same-named rows apart by line, so a class written on one line leaves them indistinguishable to the baseline, as two same-line overloads already were. And a Kotlin secondary constructor still reads <anonymous> in every listing, since its node carries no name; only the primary is named after the class.

Record headers

A Java or Groovy record declares its components in its header and, unless the body writes one, gets its canonical constructor from the compiler:

record R(int a, int b, int c, int d, int e, int f) {}
record Point(int x, int y) {}

The components are the constructor’s parameters, and since #1842 the header opens a function space of its own, named after the record and spanning the list — the row above reads R::R: nargs = 6 — exactly as a C# positional record’s does. The space counts as one callable in NOM, NPM and WMC, with the record’s own access (a public record’s constructor is public, and so is a record’s that is a member of an interface, where member types are implicitly public per JLS 9.5; a bare one’s is package-private, per JLS 8.10.4. Groovy follows the reading NPM gives every Groovy member, not the compiler’s, which makes each such constructor public: the header is public on the record’s own public in a class, and in an interface, a trait or an annotation type unless the record is private or protected, as every member there is (NPM)); a component-less record E() {} opens a zero-parameter space with no code in it, whose mi.* read the 0.0 placeholder as class G()’s do. The suppression marker binds inside the parentheses, as for a primary constructor.

The components are counted once, on whichever node declares the canonical constructor:

the record’s body writesthe components belong to
nothing, or only non-canonical constructorsthe header’s space
a compact constructor R { … }the compact constructor’s space
a normal canonical constructor R(int a, int b) { … }that constructor’s space

A non-canonical constructor (R(int a) { this(a, 0); }) is a callable of its own with its own nargs, and does not close the header. The normal canonical constructor is recognised by its name, which must be the record’s, and by its parameter types, not its arity or its parameter names: bca compares each parameter’s declared type with the component’s as spelled by its tokens — comments and whitespace aside, so List</* c */ String> and List<String> agree — and has no symbol table, so List<T> beside a java.util.List<T> component, or List<String> beside a List<@Nullable String> one (a type-use annotation is a token of the type), is one type to the compiler and two spellings here: the constructor is read as non-canonical and the header opens beside it. In Groovy def and var declare no type, so a def a component agrees with a def a or an untyped a parameter. A record and an alternative constructor written on one line carry two R::R rows at one start_line, which bca check --baseline cannot tell apart, as two same-line overloads already were. The previous reading, under #1160, kept a record with no written constructor at 0 and counted only a compact constructor; #1842 revisits it, since the component list is declared code whether or not the constructor is.

Three grammar limitations, all Groovy’s; Java has none of them.

  • A compact constructor does not parse (tree-sitter-groovy#72). In record R(int a) { R { assert a > 0 } } the bare R is an error node and the { … } after it is read as an initializer block of the record. The record writes no constructor that bca can see, so its header opens and reports the right count, but the compact body is not a function of its own: its code is charged to the record’s class space. The members after it stay in the record. Spelled with a modifier, public R { … }, nothing errors: the name is read as a field, so NPA also counts one public attribute the record does not have.
  • A variable-arity component (String... xs) parses with its ... outside the component’s type, so it never matches a written canonical constructor’s String... xs parameter, and the header opens beside that constructor.
  • An untyped component list does not parse. The grammar requires a type on each component, so record R(a, b) recovers as one component (a read as the type, b as the name, the comma in an error node) and the header reports nargs 1 for two components. A written canonical constructor R(a, b) has two untyped parameters, which the one-component header never matches, so the header opens beside it. bca does not infer the count over the error node.

Averages, minimums and maximums

NArgs does not follow the aggregate population rule of cyclomatic, cognitive and NExits. Its extrema and its averages count different things:

  • function_args_min / function_args_max and closure_args_min / closure_args_max fold each space’s own count on that channel, over every space in the subtree. A space that declares no parameters on a channel folds a 0 into that channel’s minimum. The file unit and containers declare none, so their function_args_min is always 0.
  • function_args_average and closure_args_average divide the channel’s argument total by the number of functions or closures NOM counts. average divides the combined total by their sum.

The two disagree in the languages whose lambdas open no space, the second list under What the threshold gate measures. There a lambda’s parameters add to the enclosing function’s own closure_args, one member of the extrema, while NOM counts the lambda as one closure of the divisor. A function holding several such lambdas can report a closure_args_average below its closure_args_min:

def pick(rows):
    a = sorted(rows, key=lambda r: r[0])
    b = filter(lambda r: r[1], a)
    c = map(lambda r: r[2], b)
    return min(c, key=lambda r: r)

pick reports closure_args_min 4, closure_args_max 4 and closure_args_average 1.0: four parameters over four closures, all held by one space.

Languages where it reads 0

A metric that silently reports 0 reads as “no offenders” rather than “not measured”, so know where the count is inert:

  • Bash: correct and permanent. The shell has no formal parameter list; arguments arrive as $1, $2, and so on. It is the only language where every function reads 0.
  • Perl subs without a signature: correct. Signatures (sub add($x, $y)) are counted; a sub that reads its arguments from @_ declares no formal parameters to count.
  • Perl anonymous subs: a gap, and an upstream one. The grammar parses an anonymous sub’s signature inside an error node, so my $f = sub ($x) {…} reads 0 even though it has a signature.
  • Groovy compact constructors: a gap, and an upstream one (tree-sitter-groovy#72). The grammar does not parse record R(int a) { R { … } }: the count is still right, on the header (see Record headers), but the compact body is read as an initializer block of the record, not as a function of its own.

A nargs limit is therefore inert on a Bash codebase and sparse on Perl written before use v5.36. Gate it per language rather than repository-wide; see Choosing thresholds.

How to read it

A function with many arguments is hard to call correctly and harder to test exhaustively, since the test matrix grows roughly exponentially. The classic refactoring advice is the introduce parameter object pattern: when a function takes more than four related arguments, group them into a record / struct / dataclass.

The common use is as a review-blocking lint rule: most linters (Pylint’s R0913, ESLint’s max-params, Checkstyle’s ParameterNumber) flag functions with more than a configurable threshold. NArgs is also a useful component of API-design dashboards: public APIs whose average NArgs has crept upward over time tend to be ones that have accreted “just one more parameter” feature flags.

NExits

NExits counts the number of distinct exit points from a function: every explicit return, every throw / raise, and (in Rust) every ? early-return. A generator’s yield counts in Python, the JavaScript family, C# and PHP, because it hands control back to the caller, and so does C++’s co_yield; C++’s co_return is a return (#1547). The implicit fall-through return at the end of a function is not counted; only explicit exits are (see issue #243).

The metric goes back to the structured-programming literature of the 1970s, where Edsger Dijkstra and others argued that functions should have a single entry and a single exit point (the “SESE” rule). Modern thinking is more nuanced; see Steve McConnell’s Code Complete, 2nd edition (Microsoft Press, 2004), which recommends early returns as a clarity-improving pattern when they reduce nesting.

big-code-analysis walks each function’s syntax tree, identifies the language-specific exit nodes (see the per-language Exit trait in src/metrics/nexits.rs), and reports per-function counts plus file-level sum, average, min, and max; the last three read the function-space population cyclomatic’s do. The serialised field name is nexits, matching the prose acronym used here.

How to read it

Strict SESE coding standards (DO-178C for avionics, MISRA C for embedded automotive) still require an NExits of 1 per function, because multiple exit points complicate certified control-flow analysis. Outside those domains, an NExits of 2-4 is usually a good sign: it almost always means the function uses guard clauses to handle preconditions and then proceeds in a flat body.

A very high NExits, say above 8, is the warning sign. It usually means the function should have been split into several smaller functions, with each “successful branch” becoming its own helper.

NOM

NOM stands for Number Of Methods and counts every function, method, and closure defined inside a given scope (file, class, or namespace). It is one of the first metrics Mark Lorenz and Jeff Kidd introduced in their 1994 book Object-Oriented Software Metrics (Prentice Hall, ISBN 0-13-179292-X), where it is treated as the primary class-size indicator.

big-code-analysis reports the count split by callable kind in src/metrics/nom.rs. The serialised fields are functions, closures, functions_average, closures_average, total, average (overall average across containing spaces), and per-kind functions_min, functions_max, closures_min, closures_max.

The split lets you ask different questions of the same code: a Rust crate with many closures and few functions is typical of iterator-heavy code; a Python module with many functions and few closures is typical of script-style code.

Some constructs that carry executable code are deliberately counted by neither field: Kotlin property accessors (get() / set()) and init blocks, Java and Groovy static { … } initialisers, and JavaScript class static blocks. Each opens a function space, so it has its own complexity scores, bca check can flag it, and it contributes to WMC. But none is a callable you name at a call site, and counting an accessor as a method would make NPM bill the same property once as an attribute and again as a method, skewing the NPA/NPM ratio the OOP metrics exist to report. A Kotlin file of nothing but property accessors therefore reports nom.functions == 0 while still reporting their complexity (#1184).

A constructor is counted as a function wherever its parameters are written, the bodiless kind included: a Kotlin primary constructor, a C# 12 primary constructor or positional record, and a Java or Groovy record’s header where the body writes no canonical constructor each count one, as the classic spelling does (see Primary constructors and Record headers). A compiler-generated constructor with no written parameter list — a class with no constructor at all — counts nothing.

An annotation type’s elements count in Groovy and not in Java. Both compilers make an element an abstract method, but the two grammars parse it differently. Java’s gives the element a node kind of its own, which opens no function space. Groovy’s parses it as a bodiless method, as it does an interface’s abstract method. So @interface Top { int v(); String name(); } reports nom.functions 2, an interface WMC of 2 and a cyclomatic sum of 4 in Groovy, and 0, 0 and 2 in Java. The Groovy values match what a plain interface with the same two methods reports in either language. In both languages NPM counts the two elements as public methods. The Groovy grammar also fails to parse two valid spellings: a modifier before @interface (tree-sitter-groovy#92) and an element’s default value (tree-sitter-groovy#101). The modifier leaves an error node and the counts the annotation type reports without it. A default value is read as an error node or as a field named default, depending on the value: it can add an attribute to NPA, and one written before another element can drop that element from NOM, NPM and WMC.

How to read it

NOM is the input to several other metrics: WMC sums cyclomatic complexity across the same set of methods that NOM counts, and NPM filters that same set down to public methods. As a standalone metric, the Lorenz–Kidd recommendation is ≤ 20 methods per class. The common use is as a God-class detector: a class with NOM in the dozens is almost always doing too much, and is a strong candidate for “extract collaborator” refactoring as documented in Martin Fowler’s Refactoring catalogue entry on Large Class.

NPA

NPA counts the number of public attributes (also called fields, properties, or instance variables) declared by a class or interface. It is part of the metric family introduced by Lorenz and Kidd in Object-Oriented Software Metrics (1994) and was later folded into the MOOD (“Metrics for Object-Oriented Design”) suite proposed by Brito e Abreu and Carapuça (1994).

big-code-analysis splits the count by definition-site kind: classes (concrete types with state) and interfaces (abstract contracts). The serialised output (src/metrics/npa.rs) is class_npa_sum (sum of NPA across all classes), interface_npa_sum (sum across interfaces), class_attributes (sum of all attributes, public or not, across classes), interface_attributes, class_cda (class density of public attributes, an accessibility ratio rather than an average), interface_cda, total, total_attributes, and cda. The per-language Npa trait decides what counts as “public” (Java public, C# public, Rust pub, Python’s “no leading underscore” convention, and so on) and what counts as “attribute” rather than “method”.

Which spaces carry NPA, NPM and WMC

NPA and NPM are emitted on container spaces (class, struct, trait, impl, namespace, interface) and on the whole-file unit root, which carries the roll-up across every container in the file. WMC follows the same rule wherever it is computed at all, which is narrower in two ways set out below. A function space does not carry any of them: a method owns no methods or attributes of its own, so the block would be all zeros. Before big-code-analysis 2.1.0, NPA and NPM did emit that all-zero block on function spaces in C#, JavaScript, MozJS, TypeScript, TSX, PHP and Ruby, and on the Kotlin, Java, Groovy and JS-family accessor / init / static spaces. In the same release Go, Rust, Python, C++, Objective-C and Elixir went the other way: they decided from their own grammar node kinds, so a struct declared inside a function put the block on that function space, while a namespace or a file root with no container at file scope carried none. Both deviations are gone; the space’s kind is now the only input, for every language.

That rule governs the block, not the numbers behind it: the counts roll up through every enclosing space regardless. So a type declared inside a function body is reported by the nearest enclosing container, or by the file root when there is none.

Four things read differently. A Go file’s NPA and NPM live on the unit root and nowhere else, because Go is the one language that emits them without having a container kind in its space tree: type … struct and type … interface open no space of their own. (Bash, C, Lua, Perl, Tcl and iRules have no container kind either, but they emit neither block anywhere, so the question does not arise.)

Neither WMC narrowing moves NPA or NPM, which is why the rule above still holds as stated for those two. The first is language-level: Go emits no wmc block at all, on any space including the unit root, because its flat space model cannot attribute a method to a receiver class. So a Go file carries npa and npm at the root and wmc nowhere. The second is space-kind-level: a namespace space carries npa and npm but no wmc, because a namespace’s member functions are free functions rather than methods of a class, so there is no per-class complexity to weight. That covers every construct mapping to SpaceKind::Namespace (a C++ or Mozcpp namespace, and a Ruby module), not just the C++ spelling. Objective-C has no namespace construct of its own, so the case does not arise there. The class inside the namespace carries all three, and so does the file root.

Finally, the CSV projection is a fixed-column format: it writes the npa.* / npm.* columns on every row regardless of space kind, carrying the real accessor values rather than eliding them.

Thresholds are narrower still: bca check gates npa / npm on container spaces only, never the file root (see Threshold scope). Taken with the paragraph above, that has a direct consequence: since a Go file’s NPA and NPM are only ever reported at the root, no npa or npm threshold can fire on Go source.

How to read it

NPA is a direct measure of encapsulation. Every public attribute is a piece of internal state that callers can read or write without going through a method, which means it is a piece of internal state the class cannot validate or evolve without breaking callers. The canonical guidance, first stated in Bertrand Meyer’s Object-Oriented Software Construction (Prentice Hall, 1988) and known as the Uniform Access Principle, is to keep NPA at or near zero and to expose state through public methods instead.

The common use is API-stability auditing: a public library class whose NPA grows over time accumulates breaking-change liability faster than its public-method surface.

NPM

NPM counts the number of public methods declared by a class or interface. It is the method-side companion to NPA and was again codified by Lorenz and Kidd (1994).

As with NPA, big-code-analysis splits NPM by definition-site kind (classes vs. interfaces). The serialised output (src/metrics/npm.rs) is class_npm_sum (sum of NPM across classes), interface_npm_sum, class_methods (sum of all methods, public or not, across classes), interface_methods, class_coa, interface_coa (operation-accessibility ratios, not averages), total, total_methods, and coa. It follows the same emission rule as NPA (above). The language-specific Npm trait decides what counts as public (for example, Rust’s pub, Python’s leading-underscore convention, C++’s public: section) and folds together regular methods, constructors, and operator overloads as appropriate.

NPM is also one of the inputs into Mark Hitz and Behzad Montazeri’s Class Interface Size metric, and into Chidamber and Kemerer’s Response For a Class (RFC).

Interface members

An interface member is public by default, so NPM counts it unless it narrows its own access:

LanguageNot public in an interface
Javaa private method (JLS 9.4)
Kotlina private function, the one narrowing the compiler accepts there (protected and internal, which it rejects, read the same)
Groovya private member of an interface, trait or annotation type, and a protected one (groovyc accepts that on a nested type only, a member record among them)
C#a member marked private, protected or internal, alone or paired; the static constructor; an explicit implementation of another interface’s member (void IA.M() { }); a partial method that does not say public

C# allows these modifiers on an interface member since C# 8. An internal member is reachable only from its own assembly, so it is not public here, and the same rule reads an accessor: public int X { get; internal set; } has one public method. NPA reads the same rules for Kotlin interface properties, Groovy interface and trait fields, and C# interface fields.

How to read it

NPM is the public interface size. A class with NPM in the dozens is a class with too large an API contract: every public method is something callers can come to depend on, and every change to it is a breaking change. The Lorenz–Kidd guidance is ≤ 20 public methods per class, with anything over 40 considered a strong refactoring candidate. The same rule applies with particular force to interfaces in Java and C#, where the contract is the shape clients pin against.

The common use is as a public-API change tracker for libraries: monitoring NPM at the package level catches accidental expansion of a library’s surface area in the same way that NPA catches accidental exposure of internal fields.

Tokens

Tokens is a per-function and per-file count of the tree-sitter leaf tokens (identifiers, literals, keywords, punctuation), excluding any token whose AST ancestor is a comment node. It is a lexer-driven size proxy intended as a more formatting-resilient alternative to LOC. The same idea is known from Terry Yin’s lizard command-line tool, where many readers will first have seen a token-count metric.

The implementation lives in src/metrics/tokens.rs. Because Tokens counts every leaf, including punctuation that Halstead deliberately skips, the value will not equal Halstead N1 + N2, and because it counts tokens rather than lines it is not equivalent to any LOC variant. Whitespace-only reformatting, renaming a variable, and removing a comment all leave Tokens unchanged. Edits that change the tokens themselves (adding an if, adding optional braces around a single-statement block, or inserting or removing semicolons in a language where they are optional) do change the count.

How to read it

Tokens is less sensitive to formatting than LOC. It is the right size measure when you are normalising another metric across languages or across teams with different style conventions: bugs per KSLOC is sensitive to formatting, while bugs per 1000 tokens is much less so.

The common use is as the defect-density denominator of choice in cross-language research: a 1000-line Java file and a 1000-line Lisp file contain very different amounts of code, but a 1000-token slice of each contains roughly the same amount of information. This makes Tokens useful for machine-learning code-quality models that train across many languages.

WMC

WMC (Weighted Methods per Class) is the first metric in the Chidamber and Kemerer suite, introduced in their 1994 IEEE Transactions on Software Engineering paper A Metrics Suite for Object Oriented Design (volume 20, issue 6, pages 476-493). The CK suite comprises WMC, DIT, NOC, CBO, RFC and LCOM. big-code-analysis currently implements WMC and the simpler size metrics (NOM, NPA, NPM), with the inheritance- and coupling-based ones tracked for future work.

WMC is the sum of the cyclomatic complexity of every method defined in a class. The original paper deliberately left the “weighting” abstract: Chidamber and Kemerer wrote that “if all method complexities are considered to be unity, then WMC = n, the number of methods”. The empirical follow-up literature has settled on cyclomatic complexity as the weight, and that is what big-code-analysis uses.

Algorithm

For each class or interface found by the per-language parser, big-code-analysis sums the standard cyclomatic complexity of every method body inside it (src/metrics/wmc.rs). The file-level serialised output is three fields: class_wmc_sum (sum of WMC across all classes in the file), interface_wmc_sum (sum across interfaces), and total (the two combined). No min/max/average aggregation is emitted at the file scope. To rank individual classes by WMC, use the report subcommand, which surfaces a Type hotspots (top N by WMC) section (see Commands → Report).

How to read it

Chidamber and Kemerer offered three hypotheses about WMC, all of which have been validated repeatedly since:

  1. Higher WMC predicts higher maintenance effort. A class whose methods are individually complex will resist comprehension.
  2. Higher WMC reduces reuse. Classes that do many complicated things are hard to drop into a new context.
  3. Higher WMC suggests broader application-specific behaviour. Such classes tend to be “main loop”-style coordinators rather than reusable building blocks.

The common use is God-class detection: combined with NOM, WMC is one of the clearest signals that a class needs to be split. For a class with high NOM but low WMC, check whether the methods form a cohesive data API before splitting the class. A class with low NOM and high WMC has a few gargantuan methods (split the methods, not the class). A class with both high NOM and high WMC is the classic God class.


Where to go next

  • The Supported Languages chapter lists every supported language and grammar. Metric coverage varies by language because some metric definitions (NPA, NPM, WMC) only make sense in languages with classes.
  • The Supported Change-history (VCS) Metrics chapter covers the complementary family derived from version-control history rather than from the source AST: commit frequency, churn, ownership, and the composite risk and hotspot scores.
  • The Commands → Metrics page documents how to invoke bca metrics to produce the JSON / YAML / TOML / CBOR output for any of these numbers.
  • The Recipes chapter shows end-to-end examples of producing quality reports from these metrics, including pipelining them into dashboards.

Supported Change-history (VCS) Metrics

This chapter describes the change-history metrics: the signals big-code-analysis derives from version-control history rather than from the source AST. Where the Supported Code Metrics chapter measures the shape of the code as it exists today, these metrics measure how that code got there: how often it changes, how much, by how many people, and in what kind of commits. Each section starts from the empirical paper that first connected the signal to defects, walks through how big-code-analysis computes it, and explains how to read the number in practice.

The whole family is computed by the bca vcs command and attached to bca metrics --vcs output; the Commands → Change-history (VCS) metrics page documents the flags, windows, output formats, and caching. This chapter is the why; that page is the how.

Four framing notes:

  • These metrics predict where defects cluster, not whether code is correct. The defect- and vulnerability-prediction literature consistently finds that process signals (how a file has been edited over time) out-predict product signals like size or complexity. Graves and colleagues put it bluntly in 2000: the number of times a module has been changed is a better predictor of its fault count than its length. None of these numbers measures correctness; they rank files by the probability that a bug is hiding in one.
  • Everything is measured over two windows. A single history walk per invocation produces every signal over a long window (default 12mo ≈ 365 days) and a recent window (default 90d). Recent activity is weighted more heavily than old activity throughout, because recency is the strongest single signal in the just-in-time defect-prediction line.
  • The composite scores are ordinal, not cardinal. The risk_score, hotspot_score, and commit-level JIT score are rulers, not thermometers: only relative ranks carry meaning. Rank a repository’s files (or commits) against each other, or against the repository’s own distribution over time. Do not read an absolute magnitude as a probability or compare a raw score across unrelated projects.
  • An absent record is not a zero. An untracked file has no record at all, which is distinct from a tracked file with zero in-window activity. A computed 0.0 (for example, a file that only ever changed alone has zero co-change entropy) is a real measurement.

Index

MetricMeasuresFirst connected to defects by
Commit frequencyHow often a file changesGraves et al., 2000
Code churnHow many lines changeNagappan & Ball, 2005
BurstConcentration of change into the recent windowrecency principle (Graves; JIT line)
Authorship and ownershipHow many hands touch a file, and how concentratedBird et al., 2011; Meneely & Williams, 2009
Fix, security, and revert commitsHistory of corrective changeŚliwerski, Zimmermann & Zeller, 2005
Age and last-modifiedWhen a file was born and last touchedjust-in-time defect-prediction line
Change entropyHow scattered the commits touching a file areHassan, 2009
Co-change entropyHow wide a file’s change-ripple blast radius isarXiv 2504.18511, 2025
Composite risk scoreWeighted roll-up of every signal abovethis project (formula v2)
Hotspot scoreComplexity × recent churnTornhill, 2015
Bus factorKnowledge concentration across a set of filesAvelino et al., 2016
Just-in-time commit scoreDefect-induction risk of one commitKamei et al., 2013

Commit frequency

The simplest change-history signal is how many distinct commits have touched a file. big-code-analysis records it per window as commits_long and commits_recent.

The metric’s standing as a defect predictor comes from Todd Graves, Alan Karr, J. S. Marron, and Harvey Siy’s 2000 IEEE TSE paper Predicting Fault Incidence Using Software Change History. Studying a large telephone-switching system, they found that process measures drawn from the change history predicted fault rates better than any product metric of the code itself, and that the count of prior changes was among the strongest. The intuition is direct: a file nobody touches is a file nobody is breaking.

Algorithm

A single history walk visits each commit reachable from the analysed ref (first-parent only by default; --full-history follows all parent links in the commit graph) and attributes it to every file it modifies. Merge commits are excluded unless --include-merges is passed, and renames are followed by default so a file’s history survives a move — paired the way git pairs them, under the repository’s diff.renameLimit (read through the same trusted-section filter git applies to a repository owned by someone else, so a repository-local value there is ignored). The walk counts distinct commits per file per window, not diffs or hunks, so a commit that touches a file once and a commit that touches it in three hunks both count as one.

How to read it

Commit frequency is a rate, so it is only meaningful relative to a baseline: this file versus its siblings, or this file this quarter versus last. A consistently high count flags a file that is under active feature development or structurally unstable; pairing it with code churn tells the two apart, since heavy churn on few commits is a different story from light churn on many.

Code churn

Code churn is the volume of change: the sum of added and deleted lines touching a file, recorded per window as churn_long and churn_recent.

Churn’s defect-prediction pedigree is Nachiappan Nagappan and Thomas Ball’s 2005 ICSE paper Use of Relative Code Churn Measures to Predict System Defect Density. Their key finding, validated on Windows Server 2003, is that absolute churn is a poor predictor on its own, but churn relative to file size and to the temporal spread of the changes is highly predictive of defect density. big-code-analysis keeps the raw windowed churn as a signal and lets the composite score combine it with size and recency, rather than reporting a single absolute number as a verdict.

Algorithm

For every in-window commit touching a file, the walk adds that diff’s added-plus-deleted line count to the file’s churn total for the window. Because added and deleted lines are summed, a one-line edit counts as two (one deletion, one addition): churn measures activity, not net growth.

How to read it

Churn is the natural numerator for a density ratio. Churn per commit distinguishes steady editing from a few large rewrites; churn against file size recovers Nagappan and Ball’s relative measure. A file whose recent churn is high relative to its long-window churn is changing faster now than its history would predict, which is the burst signal below.

Burst

Burst is the share of a file’s activity concentrated in the recent window: commits_recent / commits_long, reported as a ratio in [0, 1]. A value near 1 means almost all of the file’s commits are recent; a value near 0 means the file was active long ago and has since gone quiet.

The signal follows from the recency principle that runs through the change-history literature. Graves and colleagues found that recent changes weigh more heavily on fault incidence than old ones, and the just-in-time defect-prediction line (see the JIT score) is built on the same observation. A file whose change history is front-loaded into the present is in active flux, and active flux is when defects enter.

How to read it

Burst is a tie-breaker, not a standalone alarm. A high burst on a file with little total history is a young, fast-moving file; a high burst on a file with deep history is an old file that has suddenly woken up, which is often the more interesting case. Read it alongside age to tell the two apart.

Authorship and ownership

Two related signals describe who changes a file. Author counts (authors_long, authors_recent) count the distinct people who touched it in each window. Ownership (ownership_top_share, a ratio in [0, 1]) is the fraction of edits attributable to the single most active author; a low share means diffuse ownership, a high share means one person dominates.

Both trace to empirical work on ownership and security. Christian Bird and colleagues’ 2011 FSE paper Don’t Touch My Code! Examining the Effects of Ownership on Software Quality found, on Windows Vista and 7, that diffuse ownership (many low-expertise contributors, a low top-owner share) predicts both pre-release faults and post-release failures. On the security side, Andrew Meneely and Laurie Williams’ 2009 CCS paper Secure Open Source Collaboration: An Empirical Study of Linus’ Law reported that Red Hat Enterprise Linux 4 files touched by nine or more developers were roughly sixteen times more likely to harbour a vulnerability.

Algorithm

Author identities are canonicalised through the repository .mailmap and counted by lowercased email, so a contributor who commits under two addresses counts once. Co-authored-by: trailers add participants. Bot identities (dependabot[bot], renovate[bot], github-actions[bot], and similar) are excluded by default so automated churn does not inflate the count. ownership_top_share is the top author’s edit count over the file’s total edits in the window.

How to read it

A rising author count on a long-lived file is a knowledge-diffusion signal: more people are now obliged to understand it. The composite risk score folds this in two ways. It scales the author factor by an ownership-dilution term (1 - ownership_top_share), so the same head-count counts for more when ownership is spread thin, and it adds categorical bumps at the six- and nine-developer marks that encode Meneely and Williams’ RHEL4 thresholds. For knowledge concentration across a set of files rather than within one, see the bus factor.

Fix, security, and revert commits

big-code-analysis classifies each long-window commit touching a file by the intent of its message and keeps three counts: bug_fix_commits (messages matching a bug-fix keyword), security_fix_commits (messages matching CVE-####, security, vuln, exploit, sanitize, and similar), and revert_commits (subjects that are a revert or rollback).

Reading a commit’s purpose from its message is the technique introduced by Jacek Śliwerski, Thomas Zimmermann, and Andreas Zeller’s 2005 MSR paper When Do Changes Induce Fixes?, the work now abbreviated SZZ. The premise is that a file’s history of corrective change is itself predictive: a file that has needed many fixes is a file likely to need more. A security fix is a sharper signal than an ordinary bug fix, and a revert marks a change that had to be undone, a localized admission that something went wrong there.

Algorithm

Classification is keyword-based on the commit subject and body (see src/vcs/classify.rs). It is deliberately simple and language- and tracker-agnostic: there is no issue-tracker linkage and no blame back-tracing to the fix-inducing commit, so this is the message-classification half of SZZ, not the full algorithm. The counts are kept over the long window only.

How to read it

A high bug_fix_commits count is a file that keeps needing repair. The weighted composite score combines bug-fix and security-fix counts in a log-scaled term, with security fixes double-weighted, so a file with a history of security fixes ranks above an otherwise-identical file with only ordinary bug fixes. The file-level formula does not use revert_commits; only the just-in-time commit score reads the revert classification. Because the classifier reads messages, its accuracy tracks your project’s commit hygiene: a repository with terse or templated messages will under-count.

Age and last-modified

Two timing signals bound a file’s history. age_days is the number of days since the file’s first in-window commit (capped at the long window); last_modified_days is the number of days since its most recent in-window commit.

These anchor the recency reasoning the rest of the family depends on. A small age_days marks a new file, and newly added code carries elevated risk, an observation from the just-in-time line and from Chromium’s own defect analysis, where freshly added features were disproportionately fault-prone. A small last_modified_days marks a file edited recently, which the windows already weight.

How to read it

age_days is most useful as the new-file trigger it feeds into the risk score: a file first seen inside the recent window earns a small additive bonus, reflecting that brand-new code has had the least chance to be exercised and reviewed. last_modified_days is a staleness read in the other direction: a high-risk file that has not been touched in months is a different maintenance proposition from one being edited today.

Change entropy

Change entropy measures how scattered the commits touching a file are. It is reported per window as change_entropy_long and change_entropy_recent, in bits.

The metric is Ahmed Hassan’s History Complexity Metric from the 2009 ICSE paper Predicting Faults Using the Complexity of Code Changes. The idea adapts Shannon entropy to commits: for each commit, take the distribution of its churn across the files it touched and compute that distribution’s entropy in bits. A commit that touches one file scores 0; a commit that spreads its churn evenly across n files approaches log₂(n). A scattered, cross-cutting commit is harder to reason about than a focused one. Hassan reported that this change-process complexity predicts faults better than prior code-based or change-count models; later work measured file-level change entropy at a Pearson correlation up to 0.54 with defect counts on eight Apache projects (see co-change entropy).

Algorithm

For each commit, big-code-analysis computes the Shannon entropy H of its churn distribution across the touched files (see src/vcs/entropy.rs). Each participating file is then credited its churn share pᵢ·H of that commit (Hassan’s History Complexity Metric), and these shares are summed per file per window. A file repeatedly caught up in diffuse, multi-file changes accumulates high change entropy; a file that only ever changes in tightly focused commits stays low.

How to read it

Change entropy distinguishes a file that changes as part of large, sprawling edits from one that changes in self-contained commits, even when their raw churn is identical. High change entropy is a smell of cross-cutting concern: edits to this file keep dragging in many others. It enters the recent-window risk term additively, complementing rather than restating the churn and commit counts.

Co-change entropy

Co-change entropy measures how wide a file’s change blast radius is: how many different files it tends to change alongside. It is reported per window as cochange_entropy_long and cochange_entropy_recent, in bits.

The signal comes from the 2025 study Co-Change Graph Entropy: A New Process Metric for Defect Prediction (arXiv 2504.18511). Build a weighted graph in which two files share an edge whenever they change in the same commit, the weight being the number of shared commits. A file’s co-change entropy is the Shannon entropy of its edge-weight distribution: low when it always co-changes with the same one partner, high when its changes ripple out across many different files. The study found that adding co-change entropy to change entropy improved the area under the receiver operating characteristic curve (AUROC, a measure of ranking discrimination) in 82.5% of cases over the prior signal set across eight Apache projects.

Algorithm

The walk records, per commit, which in-scope files changed together and accumulates the co-change graph’s edge weights, then computes each file’s edge-weight entropy per window. Bulk-import commits touching more than 1000 files are excluded from the co-change graph, whose edge count grows with the square of commit width, though they still contribute their (linear-cost) change entropy. A computed 0.0 means the file has no co-change neighbours or only one neighbour in the window. It is a real measurement, not a missing record.

How to read it

Where change entropy asks “how scattered are the commits that touch this file?”, co-change entropy asks “how many other files does touching this one drag along?”. A high value flags a file whose edits have unpredictable, far-reaching consequences: a coupling hotspot. The two entropy signals complement each other, and the risk score v2 adds both recent-window terms together.

Composite risk score

The risk score rolls every signal above into a single per-file number, risk_score. It is the headline output of bca vcs and the field its ranked tables sort on. Two formulas are offered, both versioned together by a single risk_score_version (currently 2) so downstream consumers can detect a change.

The weighted formula (default)

The default formula is a log-scaled weighted sum with categorical multiplicative bumps. Counts are passed through ln(1 + x) so that the difference between 10 and 20 commits matters more than the difference between 110 and 120, matching how change activity saturates:

base = 0.30 · ln(1 + churn_recent)
     + 0.25 · ln(1 + commits_recent)
     + 0.15 · ln(1 + commits_long)
     + 0.15 · ln(1 + authors_long) · (1 + dilution)
     + 0.10 · ln(1 + bug_fix_commits + 2 · security_fix_commits)
     + 0.05 · ln(1 + churn_long)
     + 0.10 · change_entropy_recent + 0.05 · cochange_entropy_recent
     + ln(1 + sloc)² / 100

risk_score = base · (1 + dev_bonus + new_file_bonus)

where dilution = 1 - ownership_top_share, the dev_bonus is 0.35 for nine or more long-window authors and 0.15 for six or more, and the new_file_bonus is 0.15 for a file first seen inside the recent window. The weights are grounded term by term in the literature cited throughout this chapter: recent churn and commit frequency carry the most weight (Nagappan & Ball; the JIT line), the author factor is scaled by ownership dilution (Bird et al.) and bumped at the RHEL4 developer-count thresholds (Meneely & Williams), security fixes are double-weighted, and the two recent-window entropy terms enter additively (Hassan; arXiv 2504.18511). The full derivation lives in src/vcs/score.rs.

The size term ln(1 + sloc)² / 100 is a real contributor, not the tie-breaker its position at the end of the sum might suggest: it reaches about 0.85 at 10k SLOC and exceeds 1.0 past roughly 50k SLOC, comparable in magnitude to the churn terms. Large files are only weakly correlated with defects, but the squared-log scaling keeps size a first-class additive signal rather than letting it dominate.

The percentile formula

--risk-formula percentile is the alternative: each signal is re-ranked to its percentile within the analysed set, and the per-file mean of those percentiles becomes the score. This formula replaces fixed weights with relative ranks within the analysed file set. Prediction research recommends relative triggers over fixed absolute thresholds, but this score is meaningful only within one run’s file set.

How to read it

The score is ordinal. Sort a repository’s files by it and look at the top of the list; that is the ranking the score exists to produce. Do not compare a raw risk_score between two repositories, and do not read its magnitude as a defect probability. To watch a single file move over time, use bca vcs trend, which re-anchors the walk at each historical point so the series reflects what the file looked like then.

Hotspot score

The hotspot score is the product of a file’s complexity and its recent churn: hotspot_score = cyclomatic_sum × churn_recent. It is an Option, present only when cyclomatic is among the computed metrics, because it needs both halves. A per-function block uses the space’s own cyclomatic instead of the subtree sum, so a nested function’s complexity is not counted again in every enclosing space.

The metric is the central idea of Adam Tornhill’s 2015 book Your Code as a Crime Scene and the CodeScene tooling built on it. The argument is that complexity on its own is cheap to ignore (a complex file nobody touches costs nothing) and churn on its own is cheap too. The danger is their intersection: code that is both complicated and changing often is where defects concentrate and where developer effort is repeatedly spent. Tornhill observes that a small fraction of a codebase typically accounts for a large majority of its change activity, and the hotspot score is built to find that fraction.

How to read it

Like the risk score, the hotspot product is ordinal: rank files by it, do not read the magnitude. The CLI uses the file-level cyclomatic sum as the complexity axis by convention, but any AST complexity figure serves. The score’s value is its prioritisation: of all the complex files, it surfaces the ones actively being edited, which are both the likeliest to break and the cheapest to refactor while they are already open on someone’s screen.

Bus factor

Where ownership_top_share measures knowledge concentration within a single file, the bus factor (also called the truck factor) measures it across a set of files: the minimum number of developers whose departure would leave more than half of a directory’s files without a knowledgeable maintainer. The broader concept has a Wikipedia summary; big-code-analysis emits it as a vcs_aggregate object covering the whole repository, each top-level directory, and each of its immediate subdirectories.

The estimation method is Guilherme Avelino, Leonardo Passos, Andre Hora, and Marco Tulio Valente’s 2016 ICPC paper A Novel Approach for Estimating Truck Factors. Each developer’s authorship of each file is scored with their Degree-of-Authorship heuristic:

DoA(d, f) = 3.293 + 1.098 · FA + 0.164 · DL − 0.321 · ln(1 + AC)

where FA is first authorship (1 if developer d created file f), DL is d’s number of deliveries (changes) to f, and AC is the changes made by other developers. A developer is an author of a file when their DoA, normalised by the file’s maximum, clears 0.75 (the paper’s threshold).

Algorithm

The truck factor is a greedy removal (see src/vcs/bus_factor.rs): repeatedly drop the developer who authors the most still-covered files, stopping once more than --bus-factor-threshold (default 0.5, per Avelino) of the files are orphaned, and report how many developers were removed. The aggregate covers every in-scope file in one walk, so by_directory entries are computed over all files recursively beneath each directory.

How to read it

A bus factor of 1 means losing one person orphans the set, which is common, and correct, for a repository of mostly single-author files. Treat the number as a planning signal, not a guarantee: it is a heuristic over observed authorship within the long window, so “first authorship” means the earliest commit seen in that window, not necessarily a file’s true creation. Use it to find the directories where knowledge is dangerously concentrated and spread review accordingly.

Just-in-time commit score

Everything above ranks files at a point in time. The just-in-time (JIT) score estimates the defect-induction risk of a single commit. A continuous-integration gate reviews that commit at check-in. bca vcs commit <commit> reports an ordinal risk_score and a per-group contributions breakdown.

The feature groups and their signs are taken from the just-in-time defect-prediction literature, beginning with Yasutaka Kamei and colleagues’ 2013 IEEE TSE paper A Large-Scale Empirical Study of Just-in-Time Quality Assurance and confirmed by the open replications Commit Guru (FSE 2015) and Shane McIntosh and Yasutaka Kamei’s Are Fix-Inducing Changes a Moving Target? (IEEE TSE 2018). big-code-analysis implements a static, rule-based scorer rather than a trained model, so nothing drifts as the project ages.

Algorithm

Five feature groups move the score, each scored against the commit’s first parent (see src/vcs/jit.rs):

GroupFeaturesDirection
Sizelines added / deleted, files touched, diff hunkslarger ⇒ riskier
Diffusiondistinct subsystems and directories, within-commit change entropymore scattered ⇒ riskier
Historythe touched files’ priors (prior changes, distinct authors, fix counts, and their composite risk), measured before the committurbulent history ⇒ riskier
Experiencethe author’s prior commit count (long and recent)more experience ⇒ less risky (this group subtracts)
Purposefix / security-fix / revert classification of the messagefixes add, reverts dampen

The contributions block reports each group’s signed contribution, so a consumer can see why a commit ranked where it did. A merge commit is flagged and scored against its first parent; a root commit and any new files carry zero priors by construction, so the score then leans on size and author experience, exactly as the literature prescribes for changes with no file history. A bare git diff can also be scored (bca vcs commit --diff), but only the size and diffusion groups are computable from a diff, so that path emits a deliberately partial partial_risk_score.

How to read it

Like the file-level score, the JIT score is ordinal: rank commits by it, or compare a commit against the repository’s own commit-score distribution, but do not read the magnitude as a probability. Its intended use is a check-in gate: bca vcs commit HEAD --fail-above <N> exits non-zero when a commit scores at or above a threshold, with the threshold calibrated against your own history rather than treated as an absolute. Any change to the formula bumps a jit_score_version that is independent of the file-level risk_score_version.


Where to go next

Migration: Flag CLI to Subcommand CLI

This page is for users of the old flag-style CLI (one process, many mutually exclusive --action flags) who are moving to the subcommand CLI (bca <verb>). It maps every old invocation to its replacement. The mapping table is the quick reference; the side-by-side examples show each in context.

Why the change

The flag CLI overloaded --output-format with two unrelated meanings: per-file serialization (-O json/yaml/toml/cbor) and a post-walk aggregated report (-O markdown). It needed two clap ArgGroups plus runtime checks to police invalid combinations, and --top / --strip-prefix lived as global flags that only applied to one format. Each further aggregated format (HTML, for example) would have compounded the fragility.

The subcommand CLI fixes the structure: bca metrics and bca ops emit per-file output; bca report <FORMAT> emits an aggregated report; each verb has its own scoped flag set.

Migration mapping

OldNew
--metrics -O markdown (+ --top, --strip-prefix)report markdown
--metrics -O json/yaml/toml/cbormetrics -O json/yaml/toml/cbor
--metrics -O checkstyle/sarif/code-climate/clang-warning/msvc-warningcheck --threshold ... --report-format <fmt> [--output FILE]
--ops -O ...ops -O ...
--dumpdump
--find <NODE>find -t <NODE> [-t <NODE>...]
--count <LIST>count -t <NODE> [-t <NODE>...]
--functionfunctions
--comments [--in-place]strip-comments [--in-place]
--preproc <FILE> <FILE>... (producer)preproc -o <OUT>
--preproc <FILE> (consumer)--preproc-data <FILE> (per subcommand, after the verb)
--list-metrics [MODE]list-metrics [MODE]
--pr (pretty)--pretty (on metrics and ops)
--ls, --le (global)--line-start, --line-end on dump/find (--ls/--le kept as deprecated aliases)
-p, -I, -X, -j, -lscoped to the subcommand; pass them after the verb (-w stays universal)

Side-by-side examples

Aggregated markdown report

# OLD
big-code-analysis-cli \
    --metrics \
    --paths "$PWD" \
    --output-format markdown \
    --jobs $(nproc) \
    --top 20 \
    --strip-prefix "$PWD/"

# NEW
bca \
    report \
    --paths "$PWD" \
    --format markdown \
    --top 20 \
    --strip-prefix "$PWD/"

Per-file metric extraction

# OLD
big-code-analysis-cli --metrics --paths ./src --output-format json --output ./out/

# NEW (per-file tree: --output became --output-dir in 2.0)
bca metrics --paths ./src -O json --output-dir ./out/

Per-file ops extraction

# OLD: big-code-analysis-cli --ops --paths ./src -O json -o ./out/
# NEW: bca ops --paths ./src -O json --output-dir ./out/

AST dump

# OLD: big-code-analysis-cli --dump --paths ./file.rs
# NEW: bca dump --paths ./file.rs

Find / count nodes

# OLD: big-code-analysis-cli --find call_expression --paths ./src
# NEW: bca find --paths ./src -t call_expression

# OLD: big-code-analysis-cli --count if_statement,for_statement --paths ./src
# NEW: bca count --paths ./src -t if_statement -t for_statement

Note: find and count now take one node type per -t / --type flag (repeat it for several) rather than a positional or a comma-separated string; the positional slot holds input paths.

Function spans

# OLD: big-code-analysis-cli --function --paths ./src
# NEW: bca functions --paths ./src

Strip comments

# OLD: big-code-analysis-cli --comments --in-place --paths ./src
# NEW: bca strip-comments --paths ./src --in-place

Preproc data: producer

# OLD
big-code-analysis-cli --metrics --preproc a.h --preproc b.h \
    --paths ./src -o /tmp/p.json

# NEW
bca preproc --paths ./src -o /tmp/p.json

Preproc data: consumer

# OLD
big-code-analysis-cli --metrics --preproc /tmp/p.json \
    --paths ./src -O json -o ./out/

# NEW
bca metrics --paths ./src --preproc-data /tmp/p.json \
    -O json --output-dir ./out/

List metrics

# OLD: big-code-analysis-cli --list-metrics descriptions
# NEW: bca list-metrics descriptions

Migration hint at runtime

If you run a legacy invocation, the CLI prints a hint naming the recognized old flags and their new equivalents before clap’s own error:

$ bca --metrics -O markdown
note: the CLI was restructured into subcommands. See the migration guide for the full mapping:
  https://dekobon.github.io/big-code-analysis/migration.html
  --metrics  ->  bca metrics
  -O markdown  ->  bca report markdown|html [--top N] [--strip-prefix P]
  Run `bca --help` for the new command list.

error: unexpected argument '--metrics' found

Commands

bca is the command-line tool. Each subcommand performs one analysis and takes its own flags. This section covers what every subcommand shares: installation, how flags and input paths are placed, which files the walk visits, and the exit-code contract. The subcommands themselves each have a page: Metrics, Change-history (VCS) metrics, Report, Check, Suppression markers, Nodes, the REST API, and Operating bca-web.

Installation

The bca command-line tool ships as a pip-installable wheel. The distribution name is big-code-analysis-cli and the installed command is bca. The two differ on purpose: the bca name on PyPI belongs to an unrelated project, and big-code-analysis is this project’s importable library bindings.

pip install big-code-analysis-cli   # installs the `bca` command on PATH
bca --version

This puts the compiled bca binary on your PATH the way pip install ruff gives you the ruff command, with no Rust toolchain required. The wheel carries the full all-languages grammar set, so every supported language works out of the box. A single py3-none-<platform> wheel covers every CPython 3.x (and PyPy) on that platform. Prebuilt wheels ship for Linux (manylinux_2_28 x86_64 / aarch64), macOS (x86_64 / arm64), and Windows (x86_64). On any other platform pip falls back to a source build, which needs a Rust toolchain.

This is the binary CLI, distinct from the importable Python bindings (pip install big-code-analysis). The repository README describes the other install paths: Homebrew, .deb / .rpm / .apk packages, prebuilt release archives, and cargo install big-code-analysis-cli.

The wheel build and publish matrix is defined in .github/workflows/python-cli-wheels.yml.

Metrics

Metrics are quantitative measures of source code. They help to:

  • compare programming languages;
  • describe the quality of a codebase;
  • show developers where their code is hardest to handle;
  • surface potential problems early in development.

bca computes its metrics from the source text alone, without running the program; such metrics are called static metrics. See Metrics for the command and Supported Metrics for each metric’s definition.

Nodes

To represent the structure of program code, bca builds an Abstract Syntax Tree (AST). A node is an element of this tree and denotes one syntactic construct of the language. The Nodes commands use them to:

  • print the syntactic structure of a source file;
  • find whether a construct is present in the analyzed code;
  • count the constructs of a given kind;
  • detect syntax errors in the source.

REST API

bca-web runs a server offering a REST API. Clients send source code over HTTP and receive the corresponding metrics as JSON.

Flag placement and input paths

Most subcommands read the input they analyze as a trailing positional path, so the common case reads like every other code tool (tokei, cloc, scc, rg). The exceptions: report and vcs select input with --paths, diff compares two result sets, and init targets a directory via --dir.

bca metrics src/            # analyze the src/ tree
bca check src/ tests/       # gate two subtrees
bca find -t function_item . # find every function in the current tree

Flags are scoped to the subcommand that consumes them and must be written after the subcommand token:

bca metrics --exclude '*.generated.rs' src/   # correct
bca --exclude '*.generated.rs' metrics src/   # ERROR (exit 1)

Only -w / --warnings and --report-skipped are universal and accepted in any position. Every other shared flag lives in a help-grouped section on the subcommands that read it:

  • Input selection: -p / --paths, -I / --include, -X / --exclude, -l / --language, --paths-from, --exclude-from, --no-ignore, --no-skip-generated, --no-config.
  • Walker tuning: -j / --jobs, --exclude-tests, --cyclomatic-count-try, --mi-charges-docs.
  • Preprocessor: --preproc-data.
  • Output: --color.

A flag passed to a subcommand that never consumes it is a hard usage error (exit 1) rather than a silent no-op, so bca vcs commit --exclude-tests and bca list-metrics --paths both error, and bca list-metrics --help does not advertise walker flags.

The -p / --paths flag still works and is unioned with the positional paths, so bca metrics a.rs --paths b.rs walks both. The find and count subcommands take their node kinds via a repeatable -t / --type flag (so the positional slot is free for paths): bca find -t function_item -t struct_item src/.

Skipping generated code

Generated files inflate metrics for code no human will refactor. By default, bca scans the first ~50 lines / 5 KiB of each file for a generated-code marker and skips matches before parsing, so the skipped file pays no tree-sitter parse cost.

Recognized markers (case-insensitive):

  • @generated: a marker emitted by code generators.
  • DO NOT EDIT: a marker used by Go’s generated-code convention and other generators.
  • GENERATED CODE: Lizard’s marker, recognized for compatibility.

A marker phrase that appears only deep in the file body (past the scan window) does not trigger the skip; the detector deliberately looks only at the file header.

The skip applies uniformly to bca metrics, bca report, and the threshold engine.

Flags

  • --no-skip-generated: disable the auto-skip, so every file is parsed.
  • --report-skipped: log skipped (generated): <path> to stderr for each file the detector excludes, so you can audit the exclusions. If a file is wrongly tagged, pass --no-skip-generated; an include filter does not override the detector.

bca check additionally prints a one-line stderr summary whenever the detector dropped files from a gate run (bca: 1 file not checked (1 generated) — pass --report-skipped to list them), and its --strict profile disables the detector outright. A marker is content the branch under test controls, so a pull-request gate should not let it shrink the checked set.

Respecting .gitignore

When a directory is passed to --paths, bca walks it with .gitignore awareness by default. Files matched by any of the following are skipped before parsing:

  • .gitignore files inside the walked tree.
  • .ignore files (the ripgrep / fd convention).
  • .git/info/exclude.
  • The global gitignore (~/.config/git/ignore, or whatever core.excludesFile points at).
  • .gitignore files in ancestor directories of the seed (so bca metrics src/ from a project root picks up the project’s top-level .gitignore).

The walker honors .gitignore even outside a checked-in git repository, so an extracted source tarball with a .gitignore file gets the same treatment as a fresh git clone.

Hidden files (those whose basename starts with .) are filtered during the walk.

Explicit paths bypass the filter

Files passed by name (via --paths, --paths-from, or a trailing positional path) are always analyzed, even when the project’s ignore rules cover them. This makes it safe to run bca metrics --paths-from - from a git diff --name-only pipeline without losing files that happen to match a wildcard ignore rule.

The override is deliberate, and it is the same rule rg and fd apply: a path you named is a direct request. It spans every deny-set the walk consults: .gitignore and friends, -X / --exclude, --exclude-from, a .bcaignore, and a manifest exclude list alike. rg --glob '!x.txt' pattern x.txt searches x.txt for exactly this reason.

Two boundaries on it:

  • -I / --include still applies. The allow-list narrows which named files are analyzed, so bca metrics -I '*.rs' notes.md analyzes nothing. Only the deny-sets are overridden.
  • [check] exclude still applies. The gate-exemption set (--check-exclude / --check-exclude-from, or exclude under [check] in bca.toml) is not a walk filter: it drops violations, not files. It therefore survives an explicit path and reports bca: skipped N violations via [check.exclude] when it fires. A glob written in bca.toml resolves against the manifest’s directory whichever directory you run from (#1164); one passed as --check-exclude resolves against the working directory. See What a relative glob is relative to.

That second point is the one to reach for. A walker exclude shapes what gets analyzed; a check exclude shapes what gets gated. Anything you want kept out of the threshold gate permanently (dev tooling, generated code you still want measured, a subtree under active rewrite) belongs in [check] exclude, because any caller that names paths one at a time bypasses the walker excludes by design. Per-file callers are not hypothetical: that is the shape the agent feedback hooks use on every edit.

When an explicitly named path does override a walker exclude, bca says so on stderr and names the glob:

warning: utils/gate.py matches an exclude pattern (./utils/**) but was named explicitly; analyzing anyway

Path discovery flags

  • --no-ignore: disable .gitignore / .ignore / global-gitignore awareness when expanding directory seeds. bca check counts the analyzable files ignore rules dropped from a gate run in its not-checked summary, lists every dropped entry under --report-skipped (note: skipped (ignored): <path>, note: skipped (ignored directory): <path>), and disables the awareness under its --strict profile, since an ignore file committed in the branch under test otherwise shrinks the checked set.
  • --paths-from <FILE>: read newline-separated input paths from <FILE>, or from stdin when <FILE> is -. Combined as a union with any --paths values. -I globs still apply; -X globs do not reach an entry that names a file directly, per Explicit paths bypass the filter. Blank lines are skipped; # is treated as a path character (not a comment). To pass a file literally named -, write ./-.
  • --exclude-from <FILE>: read newline-separated --exclude glob patterns from <FILE>, or from stdin when <FILE> is -. Patterns are unioned with any inline --exclude / -X values into a single deny-set; order does not matter. .gitignore-style: blank lines and lines whose first non-whitespace character is # are skipped, and a leading UTF-8 BOM is stripped. Convention is a .bcaignore at the repo root, mirroring .gitignore / .dockerignore. To pass a file literally named -, write ./-.

Exit codes

bca follows one exit-code convention across every subcommand, so CI scripts can branch on the process status without inspecting output:

CodeMeaning
0Success.
1Tool error: a bad flag / threshold / glob spec, unreadable input, or a parse failure. This includes usage errors (unknown flag, bad subcommand, a malformed --threshold value rejected by clap). Never a metric signal.
2Metric gate: check thresholds were exceeded, vcs commit --fail-above was breached, or diff / diff-baseline under --exit-code found a non-empty filtered diff.
3–5check --exit-codes=tiered only: tiered violation severity (regression-only / mixed / hard-breach; in tiered mode code 2 means new-only).

Codes 2–5 are gate signals, emitted only by check, vcs commit --fail-above, and diff / diff-baseline under the opt-in --exit-code flag; they report a metric result, not a failure of the tool. Every other subcommand (metrics, ops, report, diff, diff-baseline, exemptions, init, and the rest) exits 0 on success and 1 on error. Code 1 is reserved for tool errors, usage errors included, so a mistyped flag never lands in the gate band and CI can always tell “the gate found a regression” (2–5) from “the tool itself failed” (1).

Unreadable input

Every source-walking subcommand (metrics, ops, report, functions, find, count, dump, exemptions, preproc, strip-comments, check) exits 1 if any selected input file cannot be read: permission denied, or a path that vanished between the walk and the read. This includes init, which uses check to scaffold its baseline, and diff --since. Each failure names the path on stderr as error processing <path>: …, followed by a summary.

The rule is “any read failure”, not “no output at all”, because a missing file is invisible in the result. A partial metrics --output document, a report, or a count looks complete; a diff --since side that lost a file reports it as added or removed rather than as the I/O error it was. The exit code is the only place the failure can be observed, so it has to carry it.

Output streamed during the walk is still emitted, because the check runs after it. A mixed run therefore still shows the files that were read: the stdout trees of metrics / ops / dump / find / functions, and the per-file documents of --output-dir.

Output assembled after the walk is suppressed entirely, because a partial one is indistinguishable from a complete one. That covers the metrics --output / ops --output aggregate document, the report document, count’s tally, preproc’s JSON, and the exemptions report; none of them is printed or written.

If a tree legitimately contains files you cannot open, prune them with --exclude (or --include a narrower set). A file removed by those filters is never opened, so it is never a read failure.

Unlistable directories

A directory the walk cannot list is the same failure one level up, and carries the same exit 1. Its whole subtree drops out of the analysed set before any file is selected, so every count downstream (the metrics document, the count tally, a diff --since side, vcs rank’s ranking) is short by an amount nothing in the output reveals. bca check is the worst case: a gate that reports clean on a tree it could not read is indistinguishable from a gate that passed.

Each unlistable entry warns on stderr as warning: skipping walk entry in …. The walk continues, so one bad directory does not take down the rest of the tree, and the run ends with a summary line and exit 1.

To exempt a directory you knowingly cannot list, name it in an ignore file (.gitignore, .ignore) or narrow --paths so the walk never reaches it. --exclude does not work here, though it is the right answer for an unreadable file: --exclude filters the paths the walk yielded, and a directory that could not be listed yielded none, because the failure happened before the filter could apply.

Two neighbouring cases stay non-fatal by design:

  • A malformed ignore file, or a pattern in one that will not compile. The walker reports these through the same channel and they warn identically, but they describe how the walk was configured rather than files it lost. Only errors carrying an underlying I/O error are counted, so a stray .gitignore typo cannot fail a build.
  • A broken symlink discovered by walking. It is dropped for not being a regular file and never surfaces as an error at all. The walk does not follow links, so it deliberately does not resolve symlinks and has nothing to report. Treating one as fatal would make a stale symlink in a vendored tree a hard CI failure.

An explicitly named path is the exception to that last point, and a pre-existing one: --paths resolves a symlink seed once, and a seed that does not exist (a dangling link or a typo) is its own error, also exit 1.

Unwritable output

The mirror image is the same rule, and it holds for every emission path: a run whose output could not be written exits 1. That covers a per-file document under an unwritable --output-dir, and a full disk on stdout: dump’s banners and trees, find‘s matches, strip-comments’ rewritten source, count’s tally, preproc’s JSON, and the single-document reports from vcs, vcs commit, and vcs trend alike, in every format. A per-file failure is named on stderr and counted in a summary line; output assembled after the walk reports the operating system’s error directly.

The one exemption is a closed downstream pipe: bca dump | head is routine rather than a failure, so BrokenPipe is swallowed and the run still exits 0.

Metrics

bca metrics computes per-file metrics and emits them to stdout, to a single aggregate file (--output), or to a directory of per-file structured files (--output-dir). It is the command for feeding downstream tooling; for a human-readable summary see bca report, and for a pass/fail gate see bca check. The shared walker flags (paths, includes, ignore handling) are described in Commands.

Migrating? This command replaces the pre-restructure --metrics flag. The aggregated report previously selected with -O markdown now lives under bca report, and the CI/IDE offender formats (Checkstyle, SARIF, code-climate, clang-warning, msvc-warning) moved to bca check --report-format <fmt>. See the migration guide.

Display metrics

To compute and display metrics for a given file or directory, run:

bca metrics --paths /path/to/your/file/or/directory
  • --paths (or -p): file or directory to analyze. If a directory is provided, metrics are computed for every supported file it contains. Paths may also be given positionally (bca metrics file.rs dir/).

Explicitly named files must be parseable. When you name a file directly (positionally or via --paths / --paths-from) whose language the tool cannot recognize, bca prints a warning on stderr. If the run produced no output at all it exits 1, the same way a nonexistent explicit path fails; a mixed run that analyzed at least one file still exits 0 with the warning. Pass --language <lang> to force a parser when a file’s extension lies about its contents. Files reached only by walking a directory are skipped silently, since a tree full of READMEs and configs must not be noisy; pass -w to surface those skips too.

A file that is recognized but cannot be read is a stricter case and follows the unreadable-input rule: the run exits 1 even when other files analyzed fine, because a silently shorter metrics document reads as a complete one.

Exporting metrics

bca metrics supports five per-file output formats: CBOR, CSV, JSON, TOML, and YAML. JSON and TOML can also be pretty-printed.

The three top-level output kinds map to three separate commands so each one stays consistent with its data model:

CommandOutputAudience
bca metricsPer-file metric treesDownstream tooling
bca reportAggregated quality dashboardsHumans / PRs
bca checkThreshold-violation reportsCI / IDE

The CI/IDE offender formats (Checkstyle, SARIF, code-climate, clang-warning, msvc-warning) used to live on bca metrics -O <fmt>. They moved to bca check --report-format <fmt> because their input is a list of threshold violations, not the per-file metric tree the other formats carry. See Exporting offender records for the new invocation.

Export command

To export metrics as JSON files:

bca metrics --paths /path/to/your/file/or/directory \
    -O json --output metrics.json
  • -O, --format: output format. Defaults to text, a human-readable colored metric tree printed to stdout; pass --format text to request that default explicitly (for example to override a bca.toml that set a structured format). The structured per-file serializers are cbor, csv, json, toml, and yaml. --output-format is accepted as a deprecated alias and is slated for removal in the next major.
  • -o, --output: a single file holding one aggregate document for the whole run, a top-level array of the per-file results (TOML wraps the array under a files key; CSV concatenates each file’s rows). If omitted, results are printed to stdout.
  • --output-dir: a directory holding one document per input file, named by the input path plus the format extension. Mutually exclusive with --output; passing both is an error.
  • CBOR is binary and so requires a destination (--output or --output-dir). Passing either destination without a structured --format is an error (the default text format streams to stdout and writes no files), so a destination never silently no-ops (#661).
  • --metrics <name,…>: restrict computation to a subset of metrics, comma-separated or repeated (--metrics cyclomatic,cognitive --metrics loc). Names are the canonical ids bca list-metrics prints, the same vocabulary bca check --threshold and bca diff --metric use. Dotted (cyclomatic.modified) and bare loc sub-metric (sloc) spellings are accepted, as is a family name (loc). Each selects its whole family: --metrics sloc prints the full loc block. The Python bindings’ metrics= takes the same names. Derived metrics pull in their dependencies automatically. An unknown name errors with a “did you mean” hint. Omit it to compute every metric (#691).

CSV (spreadsheets and pandas)

bca metrics --paths /path/to/your/code \
    -O csv --output-dir csv-output

The CSV writer emits one row per FuncSpace (function, class, struct, unit, and so on) with the entire metric matrix as columns, a shape a spreadsheet or pandas loads directly. Header order is fixed; CSV_HEADER in src/output/csv.rs is the canonical list. Identity columns come first (path, space_name, space_kind, start_line, end_line), followed by every leaf metric under the same dotted JSON-style names (loc.lloc, halstead.volume, cyclomatic.modified.average), so a single column name addresses the metric in both CSV and JSON.

Empty cells (no value, not 0) signal “not applicable for this space”; for example, the OOP-only metrics (wmc.*, npm.*, npa.*) appear empty for procedural code. RFC 4180 quoting is delegated to the csv crate, so paths and names containing commas, quotes, or newlines round-trip cleanly.

Redirect stdout to collect the CSV output in one file:

bca metrics --paths /path/to/your/code -O csv \
    > metrics.csv

CSV is a per-file format; with --output-dir <dir> each input file produces a <input>.csv mirror under the output directory. With --output <file> every file’s rows are concatenated into one aggregate CSV.

An aggregated HTML report covering the whole walk is available via bca report html. The previous per-file bca metrics -O html writer was removed because it degraded to an unopenable single-file table on real-world repos; CSV is the right shape for flat per-FuncSpace rows.

Pretty print

bca metrics --paths /path/to/your/file/or/directory \
    --pretty -O json

Excluding inline test code

bca metrics --paths /path/to/your/code --exclude-tests

By default, every node in the AST is counted, including inline test items. Rust files following the idiomatic #[cfg(test)] mod tests { ... } layout therefore have headline metrics that mix production and test code together.

Pass --exclude-tests to elide test-only subtrees before any metric is computed. The flag is recognised by every subcommand that walks a source tree (metrics, ops, report, check, dump, find, count, functions, and the rest of the walker group), and currently understands the following Rust attribute shapes:

  • #[test] and #[rstest] / #[test_case] / #[wasm_bindgen_test]
  • #[cfg(test)], #[cfg(all(test, ...))], #[cfg(any(test, ...))]
  • #[tokio::test], #[async_std::test], #[test_log::test], … (any path ending in ::test)
  • #![cfg(test)] on mod items (inner attribute form)

A pruned item takes its whole attribute run with it: every #[…] row above it, any // or /* */ comment among those rows (rustc reads a comment as no token, so the attribute still applies), and the /// doc comments above or among them (rustc reads each as a #[doc] attribute of the same item). A plain comment above the first attribute is not part of the run and stays, as does a //! inner doc comment, which documents the enclosing file or module rather than the item below it.

Languages without a Checker::should_skip_subtree override ignore the flag; only Rust applies the pruning today. The default remains off so existing metric numbers stay byte-identical for users who do not opt in.

To opt a whole project in without repeating the flag, set exclude_tests = true in the repo’s bca.toml manifest. Because --exclude-tests is presence-only (no =false form), the manifest key can only turn pruning on; a CLI --exclude-tests still wins, but the manifest cannot turn it back off.

Pruning lowers the node-counted metrics (cyclomatic, cognitive, Halstead, nom, nargs, and the rest), and loc.sloc drops with them. Unlike node-counted metrics, loc.sloc comes from source spans. The walker subtracts each pruned subtree’s rows from every enclosing span, up to the file. Rows a pruned item shares with retained code or comments are kept, so sloc never falls below the ploc and cloc that survive the prune.

Aggregated report

For a human-readable quality report, use bca report markdown. That command aggregates metrics across all analyzed files and produces per-language hotspot tables.

Listing available metrics

Tooling that drives the CLI can discover the metric catalog at runtime instead of hard-coding it:

bca list-metrics

prints metric names one per line. Pass descriptions for a one-line summary of each metric:

bca list-metrics descriptions

Change-history (VCS) metrics

bca vcs ranks files by change-history risk: signals derived from git history rather than the source AST. It is the project’s one language-agnostic, non-AST metric family. The goal is to surface the files most likely to harbour bugs or vulnerabilities, using the signals the empirical defect- and vulnerability-prediction literature most consistently backs. This page is the command reference; Change-history metrics defines each signal and cites its source.

A single history walk runs once per invocation (never per file) and produces per-file signals over two configurable windows: a long window (default 12mo, about 365 days) and a recent window (default 90d).

Quick start

$ bca vcs --paths src --top 20
Change-history risk (long window 365d, recent 90d, formula v2)
 RANK      RISK  COMMITS rec/long  CHURN rec/long  AUTHORS long  FILE
    1       7.2             68/68     11634/11634             1  src/metrics/cyclomatic.rs
    2       6.9             68/68       7299/7299             1  src/metrics/npa.rs
    ...

With no --format, a human-readable ranked table is printed. Pass --format markdown|html for a rendered report page, or --format json|yaml|toml|cbor|csv for structured output. Unlike bca metrics / bca ops (whose --output-dir is a directory of per-file emissions), a change-history report is a single whole-repo document, so bca vcs --output <file> writes one file (CBOR, being binary, requires --output). The shared --paths / --include / --exclude / --no-ignore filters pick which tracked files to report.

bca vcs exits with an error when run outside a git working tree.

File-type scope

By default bca vcs ranks only the files bca computes metrics for, the same set bca metrics would analyse. High-churn non-source files (CHANGELOG.md, Cargo.lock, generated config) carry no maintainability meaning yet maximise the churn / commit / author signals, so ranking them beside source code is noise. Scoping to files-with-metrics also keeps the standalone ranking aligned with the AST hotspot tables in bca report --vcs.

--file-types <SCOPE> selects the scope:

ValueMeaning
metrics (default)Only files bca has a language/metrics for, by extension
allEvery tracked, non-binary, non-symlink text file
rs,py,toml,…A comma-separated extension allow-list (leading dots optional, case-insensitive)
bca vcs                          # rank source files only (default)
bca vcs --file-types all         # rank every tracked text file
bca vcs --file-types rs,py       # rank only Rust and Python files

The check is extension-only (no file content is read) and ANDs with the --paths / --include / --exclude / --no-ignore filters: a file must pass both to be ranked. Extension-less files (Makefile, Dockerfile, LICENSE) and unknown extensions are out of the metrics scope; a custom list is a literal extension filter, so it can include a non-metrics type like toml. An empty or all-blank custom list is a clear error rather than a scope that silently ranks nothing.

Rendered report page

bca vcs --top 50 --format html --output vcs.html
bca vcs --top 50 --format markdown --output vcs.md

--format html produces a self-contained, sortable page styled like bca report html (click any column header’s label, or Tab to it and press Enter, to re-sort); --format markdown produces the same ranked table as GitHub-Flavored Markdown. Both render every signal column, the same data the structured formats carry. The column set is defined once and shared by both renderers, so they cannot drift.

To fold the ranking into the aggregated quality report instead of a standalone page, pass bca report --vcs, which appends a “Change-history risk” section to report markdown / report html.

Signals

FieldTypeDescription
commits_long / commits_recentu32Distinct commits touching the file in each window
churn_long / churn_recentu64Σ(added + deleted) lines in each window
authors_long / authors_recentu32Distinct canonical author identities in each window
ownership_top_sharef64 ∈ [0,1]Share of edits attributable to the top author (lower = more diluted)
burstf64 ∈ [0,1]commits_recent / commits_long
bug_fix_commitsu32Long-window commits whose message matches a bug-fix keyword
security_fix_commitsu32Long-window commits matching security keywords (CVE-####, security, vuln, exploit, sanitize, …)
revert_commitsu32Long-window commits whose subject is a revert / rollback
age_daysu32Days since the file’s first in-window commit (capped at the long window)
last_modified_daysu32Days since the file’s most recent in-window commit
change_entropy_long / change_entropy_recentf64Change entropy in bits per window (see below)
cochange_entropy_long / cochange_entropy_recentf64Co-change graph entropy in bits per window (see below)
risk_scoref64Composite, formula-versioned (see below); ordinal, not cardinal
hotspot_scoref64?complexity × churn_recent; present only when cyclomatic is among the computed metrics
risk_score_version / vcs_schema_versionu32Forward-compatibility version stamps. Carried once on the report envelope, alongside long_window_days / recent_window_days, not repeated inside each per-file vcs block (issue #635)

Author identities are canonicalised through the repository .mailmap and counted by lowercased email; Co-authored-by: trailers add participants. Bot identities (dependabot[bot], renovate[bot], github-actions[bot], and the like) are excluded by default. Both the built-in bot pattern and a custom --bot-pattern match case-insensitively against substrings of the author name and email. For example, --bot-pattern renovate matches Renovate Bot. Prefix the pattern with (?-i) for case-sensitive matching. Binary files and symlinks are skipped; an untracked file has no record at all (distinct from a tracked file with zero in-window activity).

Change & co-change entropy

Two process-entropy signals (added in risk_score_version 2) capture how a file changes, not just how much:

  • Change entropy (Hassan, 2009, Predicting Faults Using the Complexity of Code Changes). For each commit, the Shannon entropy (in bits) of its churn distribution across the files it touched measures how scattered that change was: a one-file commit is 0; a commit spreading churn evenly across n files approaches log₂(n). Each file is then credited its churn share pᵢ·H of every commit it took part in (Hassan’s History Complexity Metric). Higher means the file is repeatedly caught up in diffuse, cross-cutting changes. Later work (arXiv 2504.18511, below) measured file-level change entropy at a Pearson correlation up to 0.54 with defect counts on eight Apache projects.
  • Co-change graph entropy (arXiv 2504.18511, 2025). Files that change in the same commit are joined by a weighted edge (weight = number of shared commits). A file’s co-change entropy is the Shannon entropy of its edge-weight distribution: low when it always co-changes with the same partner, high when its changes ripple across many different files. Combined with change entropy it improved the area under the receiver operating characteristic curve (AUROC) in 82.5% of cases over the v1 signal set on eight Apache projects.

Both are reported per window. A 0.0 is computed, not missing: the file only ever changed alone (no co-change neighbours, or single-file commits with zero change entropy). Bulk-import commits touching more than 1000 files are excluded from the co-change graph, whose edge count grows O(width²), but still contribute their O(width) change entropy.

Composite risk score

The default weighted formula is a log-scaled weighted sum with categorical multiplicative bumps:

recency_churn  = ln(1 + churn_recent)
recency_count  = ln(1 + commits_recent)
long_count     = ln(1 + commits_long)
long_churn     = ln(1 + churn_long)
author_factor  = ln(1 + authors_long)
dilution       = (1 - ownership_top_share).clamp(0, 1)
fix_factor     = ln(1 + bug_fix_commits + 2 * security_fix_commits)
size_factor    = ln(1 + sloc)^2 / 100              // full coefficient, not a tie-breaker
entropy_factor = 0.10 * change_entropy_recent + 0.05 * cochange_entropy_recent
new_file_bonus = 0.15 if age_days < recent_window_days else 0
dev_bonus      = 0.35 if authors_long >= 9 else 0.15 if authors_long >= 6 else 0

base = 0.30 * recency_churn
     + 0.25 * recency_count
     + 0.15 * long_count
     + 0.15 * author_factor * (1 + dilution)
     + 0.10 * fix_factor
     + 0.05 * long_churn
     + entropy_factor
     + size_factor

risk_score = base * (1 + dev_bonus + new_file_bonus)

The term weights are grounded in the literature: recent churn and commit frequency carry the highest weight (Nagappan & Ball relative churn; just-in-time defect prediction; Firefox NumChanges PD 86); the author factor is scaled by ownership dilution (Avelino DoA / truck-factor; Bird et al.); the categorical developer-count bumps encode the RHEL4 finding that files touched by ≥9 developers were ~16× more likely to harbour a vulnerability; security fixes are double-weighted (Sentence-Level VFC studies; PySecDB); and the recent-window change- and co-change-entropy terms enter additively (Hassan 2009; arXiv 2504.18511). The full derivation lives in src/vcs/score.rs.

The score is ordinal: only relative ranks have meaning. A single risk_score_version (now 2) versions both formulas: any change to the weighted sum or the --risk-formula percentile blend bumps it, and the recent entropy pair joins both.

--risk-formula percentile is an alternative: each signal is re-ranked to its percentile within the analyzed set, then averaged. The literature recommends relative triggers over hard thresholds for cross-project robustness.

Flags

FlagDefaultMeaning
--long-window <DUR>12moLong window (12mo, 2y, 8w, 365d, ISO 8601 P1Y)
--recent-window <DUR>90dRecent window
--top <N>50Show only the top N (0 = all)
--file-types <SCOPE>metricsFiles to rank: metrics, all, or an extension list (rs,py)
--ref <REF>HEADRevision to analyse
--full-historyoffWalk the full DAG (default: first-parent only)
--include-mergesoffInclude merge commits
--no-follow-renamesoffStop following renames (default: follow)
--no-exclude-bots / --bot-pattern <RE>excludeBot-author filtering
--as-of <WHEN>wall clockReference “now” (RFC 3339 / @unix / git date) for reproducible snapshots
--risk-formula {weighted|percentile}weightedComposite formula
--emit-author-detailsoffEmit SHA-256-hashed canonical author IDs
--author-hash-key <KEY>unsetHarden the emitted author digests into a keyed HMAC (see Author-detail privacy); requires --emit-author-details
--include-deletedoffAlso rank files deleted at the target ref
--no-cacheoffSkip the persistent history cache (always walk fresh)
--clear-cacheoffWipe this repo’s cached history before running
--cache-dir <DIR>platform cacheOverride the cache directory

Caching

On large, active repositories, walking the long-window history dominates runtime. Most consecutive CI runs add only a few commits. bca vcs therefore keeps a persistent cache of history walks, keyed by the resolved HEAD SHA and repository identity:

  • On an unchanged tree the prior result is replayed, no history walk.
  • When HEAD has advanced the walk visits only the new commits and splices them onto the cached history.
  • A force-push (the cached head is no longer an ancestor of the new one) falls back to a full walk.

The cache is a pure optimization: a hit is bit-identical to a fresh walk, and the time windows are recomputed against the current moment on every run rather than frozen when the entry was written. An entry is ignored, and the history recomputed, whenever the schema, the score-formula version, the walk-affecting options, the repository’s effective .mailmap, or the git diff configuration that decides how gix diffs (diff.algorithm, core.bigFileThreshold, and a diff.<driver> section’s algorithm, binary, and whether it sets a textconv; a driver’s command never runs here and does not count), the diff attribute bindings that route a path to a diff driver or mark it -diff (the .gitattributes files in the index — committed or staged, never an unstaged edit, which the walk does not read either — and the full content of .git/info/attributes, core.attributesFile or its XDG default, and the system attributes file, so a reorder of two overlapping rules counts as much as an edit, as does a core.ignoreCase flip; each file is read once per run and both the walk and the entry’s stamp come from that one read), or the shallow boundary of a shallow clone (a git fetch --deepen or --unshallow leaves HEAD where it was but reaches commits the entry never saw), or the effective diff.renameLimit the rename pass ran under (read through the same trusted-section filter gix applies to a repository owned by someone else; with --no-follow-renames it is not read at all) differ; in particular changing a window forces a fresh walk. A bca release that adds a term retires every existing entry, so the first bca vcs after such an upgrade re-walks the whole long window once. Nothing deletes an entry that no longer matches, since a run with other options may still use it; --clear-cache removes the repository’s entries, retired ones included. Finalization-only knobs such as --risk-formula, --emit-author-details, --author-hash-key, and --include-deleted are applied on replay, so they reuse the same cached walk; a cached walk even re-finalizes under a different author-hash key without re-walking.

By default the cache lives under $XDG_CACHE_HOME/big-code-analysis/vcs (%LOCALAPPDATA% on Windows, ~/.cache otherwise). Author identities are stored only as their SHA-256 digests, never plaintext, so the cache holds no raw author emails. This is pseudonymization, not anonymization: the digests are recoverable against a candidate email set (see --emit-author-details). The same cache transparently accelerates bca metrics --vcs and bca report --vcs.

# First run primes the cache; the second replays it.
bca vcs --paths .
bca vcs --paths .                 # reuses prior work

bca vcs --no-cache --paths .      # ignore the cache for this run
bca vcs --clear-cache --paths .   # rebuild from scratch
bca vcs --cache-dir /tmp/bca-cache --paths .

The REST (POST /v1/vcs) and Python (vcs.rank) surfaces expose the same behaviour through optional no_cache / cache_dir parameters.

The cache is specific to the file ranking. The trend and commit subcommands, and the /v1/vcs/trend and /v1/vcs/jit endpoints, do not use it, so the cache flags do not apply there: passing --no-cache / --cache-dir alongside a subcommand is a usage error, and the trend endpoint rejects a no_cache / cache_dir field rather than silently ignoring it (issue #961).

In bca metrics

Pass bca metrics --vcs to attach a vcs block (plus a hotspot_score computed from the file’s cyclomatic sum) to each file’s metrics:

$ bca metrics --vcs --paths src/parser.rs --format json
{ "name": "src/parser.rs",
  "metrics": { "cyclomatic": { ... },
    "vcs": { "commits_long": 15, "churn_recent": 211,
             "risk_score": 3.7, "hotspot_score": 7596.0, ... } } }

bca metrics --vcs uses the default windows and weighted formula; for window / formula tuning use bca vcs.

Per-function attribution

bca metrics --vcs-per-function (which implies --vcs) additionally attaches a vcs block to every nested function, method, and class space. It blames each file once with git blame and buckets the surviving lines into the AST function spans, so you can rank the risky function inside a risky file:

$ bca metrics --vcs-per-function --paths src/parser.rs --format json
{ "name": "src/parser.rs",
  "metrics": { "vcs": { "risk_score": 3.7, ... } },   // file-level block
  "spaces": [
    { "name": "parse", "kind": "function",
      "metrics": { "vcs": { "commits_long": 4, "churn_recent": 12,
                            "risk_score": 2.1, "hotspot_score": 144.0 } } } ] }

The per-function block is a current-blame snapshot and is not directly comparable to the file-level block: its churn counts surviving lines whose last touch falls inside the window (not historical added+deleted churn), and ownership is credited per touching commit. A function nobody has changed within the window reports zero counts. Lines whose last touch predates the long window contribute to the function’s size but to none of the windowed counts.

Each nested space’s hotspot_score multiplies its own cyclomatic (cyclomatic.value) by its churn_recent, not its subtree cyclomatic.sum, so a nested function’s complexity is not counted again at every enclosing level; the file-level block keeps the subtree sum. The Python bindings’ vcs_per_function=True reports the same scores.

Limitations. Blame follows file renames (so edits under a former path still attribute), but attributes a line moved between functions to its current position only. A function split into two has no record of its pre-split identity, and a deleted-then-recreated function attributes to the recreating commits. If a file cannot be blamed (untracked, or the rare gix-blame failure on pathologically repetitive content), its per-function blocks are omitted while the file-level block and the AST metrics still emit.

Just-in-time (commit-level) scoring

Where everything above ranks files at a ref, bca vcs commit <commit> scores a single commit for defect-induction risk, the unit a CI gate reviews at check-in. (The subcommand was renamed from bca vcs jit in 2.0; the old jit spelling keeps working as a hidden alias for one release cycle. “Just-in-time (JIT)” stays the literature term, below.) It is a static, rule-based scorer (no trained model, so nothing drifts as the project ages), with the feature groups and signs taken from the just-in-time defect-prediction literature: Kamei et al., A Large-Scale Empirical Study of Just-in-Time Quality Assurance, IEEE TSE 2013, with the open replications Commit Guru (FSE 2015) and McIntosh & Kamei, Are Fix-Inducing Changes a Moving Target? (IEEE TSE 2018).

$ bca vcs commit HEAD --pretty
{
  "jit_schema_version": 3,
  "jit_score_version": 1,
  "source": "commit",
  "risk_score": 4.40,
  "commit": { "id": "5176d3e…", "parent_count": 1, "is_merge": false,
              "purpose": { "is_fix": true, "is_security_fix": false,
                           "is_revert": false } },
  "features": {
    "size":       { "lines_added": 942, "lines_deleted": 60,
                    "files_touched": 19, "hunks": 78 },
    "diffusion":  { "subsystems": 5, "directories": 8, "entropy": 3.48 },
    "history":    { "prior_changes": 275, "prior_distinct_authors": 1,
                    "prior_bug_fix_commits": 237,
                    "prior_security_fix_commits": 21,
                    "file_risk_max": 10.97, "file_risk_mean": 3.87,
                    "new_files": 2 },
    "experience": { "author_prior_commits": 962,
                    "author_recent_commits": 962 }
  },
  "contributions": { "size": 2.74, "diffusion": 0.97, "history": 1.57,
                     "purpose": 0.15, "experience": -1.03 }
}

The five feature groups, and how each moves the score:

GroupFeaturesDirection
Sizelines added / deleted, files touched, diff hunkslarger ⇒ riskier
Diffusiondistinct subsystems & directories, within-commit change entropymore scattered ⇒ riskier
Historythe touched files’ priors (prior changes, distinct authors, bug- and security-fix counts, and the composite risk_score), measured from history before the committurbulent file history ⇒ riskier
Experiencethe author’s prior commit count (long & recent)more experience ⇒ less risky (this group subtracts)
Purposefix / security-fix / revert classification of the messagefixes add, reverts dampen

The contributions block reports each group’s signed contribution to the ordinal risk_score, so a consumer can see why a commit ranked where it did. Like the file-level risk_score, the score is ordinal: rank commits by it, or compare a commit against the repository’s own distribution, but do not read the magnitude as a probability. Any formula change bumps jit_score_version (separate from the file-level risk_score_version).

The commit is scored against its first parent. A merge commit is flagged (is_merge, parent_count ≥ 2) and scored against that first parent. A root commit and any new files carry zero priors by construction; the score then leans on size and author experience, as the literature prescribes for changes with no file history.

The window / --ref / bot / merge / rename flags are shared with the parent bca vcs command; the commit-only flags are the positional <commit> (default HEAD), --format json|yaml|toml|cbor (default json), --output, --pretty, and:

# CI gate: exit 2 when the commit scores at or above the threshold.
bca vcs commit HEAD --fail-above 6.0

--fail-above uses exit code 2 (the same “metric gate” convention as bca check; exit 1 stays reserved for tool errors). Because the score is ordinal, calibrate the threshold against your repository’s own commit-score distribution rather than treating it as an absolute.

Scoring an arbitrary diff (--diff)

bca vcs commit --diff <file> scores a git diff instead of a commit (use --diff - to read the diff from stdin). This suits a pre-commit hook or a code-review bot, where the change exists only as a diff and has not been committed yet.

git diff --cached | bca vcs commit --diff - --pretty

The input must be a git-style unified diff carrying diff --git file headers, as produced by git diff or git format-patch. Plain diff -u / diff -ru output (which has ---/+++ header lines but no diff --git header) parses to zero files, and combined / merge diffs (git diff --cc, with @@@ hunk headers) are rejected as a malformed diff; pipe a regular two-way git diff instead.

A bare diff carries no author, parent, or file history, so only the size and diffusion groups are computable. The output is therefore a deliberately partial report, a distinct shape from a commit report:

$ git diff | bca vcs commit --diff - --pretty
{
  "jit_schema_version": 3,
  "jit_score_version": 1,
  "source": "diff",
  "partial_risk_score": 1.83,
  "size":      { "lines_added": 42, "lines_deleted": 8,
                 "files_touched": 3, "hunks": 6 },
  "diffusion": { "subsystems": 2, "directories": 3, "entropy": 1.46 },
  "contributions": { "size": 1.18, "diffusion": 0.65 }
}

The source field is a permanent "diff" marker, and the history / experience / purpose groups are absent from the report entirely, not present as zero. Zero is a real value (a commit genuinely with no prior history scores those groups at zero); an absent group means “unavailable”, so a consumer can never mistake an unscored group for “low risk”. For the same reason the score field is named partial_risk_score, not risk_score.

A diff-only score is not comparable to a commit score. The partial score sums only size + diffusion; the full commit score for the same change also folds in history, experience, and purpose, and because experience is negatively signed the two are not even ordered (an experienced author can push the full score below the partial one). Rank diffs against other diffs, never against commit scores. --diff and the positional <commit> are mutually exclusive; --fail-above works in both modes (calibrate the diff-mode threshold against your own diff-score distribution).

The parser understands git’s default C-style path quoting (core.quotePath=true), so a diff touching a file with a non-ASCII or spaced name (which git emits as "a/na\303\257ve.txt") is grouped under its decoded path in the diffusion features, not the raw quoted string.

REST and Python parity

The JIT score is also available off the CLI:

  • REST: POST /v1/vcs/jit with { "id", "repo_path", "commit" } returns the commit JitReport JSON, or { "id", "diff" } returns the partial diff report. See Driving the REST API.
  • Python: vcs.commit(repo_path, commit=...) returns the commit report as a dict, and vcs.score_diff(diff) the partial diff report. See Change-history (VCS) metrics.

ML-based JIT models and server-side hook integration remain out of scope.

Historical trend (over time)

A single bca vcs run answers “what is risky now”. bca vcs trend answers “is it getting better or worse”, the question a technical-debt programme acts on, by sampling the metrics at several points in time and emitting a per-file time series.

$ bca vcs --top 20 trend --points 12 --span 24mo --pretty
{
  "trend_schema_version": 1,
  "vcs_schema_version": 2,
  "risk_score_version": 2,
  "long_window_days": 365,
  "recent_window_days": 90,
  "truncated_shallow_clone": false,
  "as_of_points": [ 1700000000, 1705259520, ... ],
  "files": {
    "src/parser.rs": [
      null,                       // did not exist at the oldest point
      { "as_of": 1705259520, "vcs": { "risk_score": 4.1, ... } },
      { "as_of": 1710519040, "vcs": { "risk_score": 6.8, ... } }
    ]
  },
  "deltas": {
    "improved":  [ { "path": "src/old.rs",    "delta": -3.2, ... } ],
    "regressed": [ { "path": "src/parser.rs", "delta":  2.7, ... } ]
  }
}

--points N evenly-spaced samples (inclusive of both endpoints) cover --span DURATION, ending at --as-of (or wall-clock now). as_of_points lists the sample timestamps oldest-first; every file’s array aligns to it 1:1, with a null element marking a point where the file did not exist yet. deltas ranks the files whose risk_score fell the most (improved) and rose the most (regressed) between each file’s earliest and latest present points; --top-deltas trims each list.

Each point re-anchors at the mainline tip that existed at or before that moment; it does not just re-window today’s HEAD tree. That is what makes a file born later show as null at older points rather than leaking its present-day metrics backwards. Files kept in the series are the --top highest-risk by their most-recent sample.

Flags reused from the parent bca vcs command: the window (--long-window / --recent-window), --ref, --file-types, bot / merge / rename toggles, --as-of (the most-recent anchor), and --top. -O accepts json (default), yaml, or cbor; TOML is excluded because an absent point serializes as null, which TOML cannot represent. The point count is bounded (2–120) to keep the per-point history walks tractable on deep histories.

Rename caveat. Renames are followed within each sample’s walk, but a file renamed between two samples appears as two separate path series (its old name, then its new name) rather than one continuous line. Cross-sample rename stitching is a deferred follow-up.

Bus factor (directory & repo level)

Where the per-file ownership_top_share measures concentration within a file, the bus factor (also called the truck factor) measures it across a set of files: the minimum number of developers whose departure would leave more than half of a directory’s files without a knowledgeable maintainer. bca vcs emits it as a top-level vcs_aggregate object alongside the ranked files:

{
  "vcs_aggregate": {
    "bus_factor": {
      "bus_factor_schema_version": 2,
      "coverage_threshold": 0.5,
      "doa_threshold": 0.75,
      "repo": { "bus_factor": 3, "files": 412, "authors": 11 },
      "by_directory": [
        { "directory": "src", "bus_factor": 2, "files": 180, "authors": 7 },
        { "directory": "src/vcs", "bus_factor": 1, "files": 24, "authors": 3 }
      ]
    }
  }
}

Each developer’s authorship of each file is scored with the Avelino Degree-of-Authorship heuristic (Avelino, Passos, Hora & Valente, A Novel Approach for Estimating Truck Factors, ICPC 2016):

DoA(d, f) = 3.293 + 1.098·FA + 0.164·DL − 0.321·ln(1 + AC)

where FA is first authorship (1 if d created f), DL is d’s deliveries (changes) to f, and AC is the changes made by other developers. A developer is an author of f when their DoA, normalised by the file’s maximum, clears 0.75 (the paper’s threshold). The truck factor is then a greedy removal: drop the developer who authors the most still-covered files, repeat until more than --bus-factor-threshold (default 0.5, per Avelino) of the files are orphaned, and report how many were removed. by_directory covers each top-level directory and each of its immediate subdirectories, computed over every file recursively beneath it.

Caveats, by construction:

  • With the default threshold, a repository whose files all share one sole author has a bus factor of 1. Single-author files owned by different developers can produce a higher bus factor. This is the heuristic working as intended, not a bug; treat the number as a planning signal, not a guarantee.
  • Bot identities are filtered (like the per-file signals), and files with no in-window activity carry no authorship and are excluded from the denominator.
  • “First authorship” means the earliest commit observed within the long window, not necessarily a file’s true creation.

The aggregate reflects the whole repository within the file-type scope: one history walk covers every in-scope file, by default the files-with-metrics set (see File-type scope), so --file-types all widens the bus factor to every tracked file. --paths / --include / --exclude scope only the ranked per-file list, not the bus factor. To focus on a subsystem, read its entry in by_directory rather than filtering the walk.

--emit-author-details adds a key_author_ids list to each group: the SHA-256-hashed identities of the removed key developers, in removal order (plaintext identities never leave the process). The aggregate is computed only for the dedicated bca vcs / bca report --vcs reports and the REST / Python endpoints; the per-file bca metrics --vcs injection path does not pay for it.

Author-detail privacy

The key_author_ids digests are a stable pseudonym, not anonymization. Hashing keeps plaintext emails out of the report and the cache and deters casual disclosure, but the hash is not cryptographically irreversible. The pre-image is an email, which is low-entropy and enumerable, and commit histories are public, so anyone with a candidate set of emails can recover which digest belongs to whom by hashing each candidate or with a precomputed email→hash table. This is the same weakness that broke Gravatar’s email hashing.

Treat published key_author_ids (and the per-file author_ids) as pseudonymization that avoids emitting plaintext emails, not as a guarantee that authors cannot be re-identified by a determined attacker. If you need that guarantee, do not publish the digests.

Hardened mode: --author-hash-key

For stronger resistance, pass a secret key with --author-hash-key <KEY> (requires --emit-author-details). The emitted digests then become an HMAC, HMAC-SHA256(key, SHA-256(email)), instead of a bare hash: an attacker without the key can no longer hash a candidate email to recognise its digest, nor use a precomputed email→hash table, because both attacks need the secret key. Pick a high-entropy key and keep it secret; anyone who learns it can re-run the enumeration.

The key is stable: the same key yields the same digests across every report and across a persistent-cache replay, so cross-report correlation and the cache still work. Different keys produce unrelated digests, so two teams sharing histories cannot cross-link authors unless they share the key.

Prefer the BCA_AUTHOR_HASH_KEY environment variable over the flag: a key on the command line is visible to other local users via the process list (ps) and is saved in shell history. The flag takes precedence when both are set:

export BCA_AUTHOR_HASH_KEY="$(cat ~/.config/bca/author-key)"
bca vcs --emit-author-details

What the key does not cover: the on-disk history cache (issue #334) deliberately stores the unkeyed inner SHA-256 digest, because the key is applied at finalization so a cached walk can be re-finalized under any key without re-walking. The cache is local-only and never published, but if your threat model includes an attacker reading your local cache directory, disable the cache (--no-cache) or clear it (--clear-cache). The same key option is available on the REST endpoint (author_hash_key) and in Python (vcs.Options(author_hash_key=…)).

Dogfooding in this repo

This project runs bca vcs on its own source. make vcs prints the ranked table; path selection and the .bcaignore deny-set come from the repo-root bca.toml manifest, the same config make self-scan and make report use, and BCA_VCS_TOP overrides the row cap. The manifest’s [vcs] file_types key sets the default scope (the --file-types CLI flag replaces it when given). On every push to main, the Pages CI job includes the ranking in the published quality report (bca report html --vcs / report markdown --vcs), so reports/index.html shows the change-history risk section beside the AST hotspots. The same job publishes the full top-100 ranking as reports/vcs-report.json for tooling.

REST and Python

  • REST: POST /v1/vcs with a JSON body { "id": "...", "repo_path": "/path/to/repo", ... } returns the ranked report, and POST /v1/vcs/trend (same fields plus points / span / top_deltas) returns the historical time series. See Driving the REST API.
  • Python: big_code_analysis.vcs.rank(repo_path, …) returns the ranked report as a dict, vcs.trend(repo_path, points=…, span=…, …) returns the time series, and analyze(path, vcs=True) attaches a vcs block to a single file’s metrics.

Both POST /v1/vcs and vcs.rank() (through vcs.Options) accept an optional file_types ("metrics" / "all" / "rs,py") to scope which files are ranked, mirroring the CLI --file-types.

Both include the vcs_aggregate bus factor in the result and accept a bus_factor_threshold (in (0, 1)) to tune the coverage fraction.

Report

bca report [--format <FORMAT>] produces an aggregated quality-metrics report across every file walked, for pasting into pull requests, wikis, or issue trackers. For per-file data meant for tooling, use bca metrics; for a pass/fail gate, use bca check.

Two formats are available: markdown (plain text, suited to PR comments) and html (a self-contained dashboard with sortable tables, suited to a build artifact). Pick one with --format / -O (bca report --format html); the default is markdown. The bare positional form (bca report markdown) still works as a deprecated alias and is slated for removal in the next major; prefer --format.

CI integration. For runnable GitHub Actions and GitLab CI recipes that post the Markdown report as a PR/MR comment, see the CI integration recipe.

Migrating? This command replaces the pre-restructure --metrics -O markdown invocation. See the migration guide.

Quick start

Print to stdout:

bca report --paths /path/to/project markdown

Write to a file:

bca report --paths /path/to/project markdown --output report.md

Note: --output must be a file path, not a directory.

Flags

FlagDefaultDescription
--top N20Maximum entries per hotspot table (0 = all).
--strip-prefix PATH(empty)Prefix removed from file paths.
--no-suppress(off)Include functions silenced by in-source suppression markers (raw audit view).
--vcs(off)Append a “Change-history risk” section ranking files by VCS risk (default windows), mirroring bca metrics --vcs. The section ranks the same files-with-metrics as the AST hotspot tables (the metrics file-type scope, #576), so both halves describe one file universe. Ignored with a warning outside a git working tree. See bca vcs.
-o, --output FILE(stdout)Output file. Parent directory must exist.

Suppression markers

By default, bca report markdown|html honours in-source suppression markers: the same // bca: suppress, // bca: suppress-file, and Lizard #lizard forgives comments that bca check and the SARIF emitter respect (see Suppression). A function is omitted from a metric’s hotspot table when that metric is suppressed for it, so the published report agrees with the threshold gate instead of re-surfacing every silenced offender.

Suppression is per-metric: a // bca: suppress(cyclomatic) marker drops the function from the Cyclomatic table only; it still appears in the Cognitive, Halstead, and other tables. A bare // bca: suppress (or // bca: suppress-file) covers every metric.

Pass --no-suppress for the raw audit view that lists every offender regardless of markers. The setting can also be pinned in the bca.toml manifest:

[report]
no_suppress = true

The CLI flag wins in either direction: a bare --no-suppress forces the audit view on, and --no-suppress=false restores the marker-honoring default even when the manifest enabled the audit view. The manifest only fills the value when the CLI left it unset.

Examples

Show only the five worst hotspots per section:

bca report -p src/ markdown --top 5

Strip the workspace root from displayed paths:

bca report -p /home/user/project markdown \
    --strip-prefix /home/user/project/

A typical day-to-day invocation:

bca report \
    --paths "$PWD" \
    markdown \
    --top 20 \
    --strip-prefix "$PWD/"

Report structure

A generated report contains the following sections (each section is omitted when no data exists for it). Every hotspot table includes a Tokens column (Lizard-style leaf-token count, comments excluded) alongside SLOC so two complementary size proxies are visible per row.

  1. Project summary: files analyzed, languages, total SLOC / PLOC / comment counts, function and class counts, comment ratio.
  2. Per-language overview table: one row per language with file count, SLOC, function count, the SLOC-weighted average Maintainability Index (MI), average Cyclomatic Complexity (CC), and average Cognitive Complexity. The MI average is size-weighted and uses the unclamped Visual Studio value, so a language whose files are mostly unmaintainable reads negative instead of saturating at the 0 floor of the per-file MI column.
  3. Per-language hotspot sections (repeated for each language). Every hotspot title follows one template, <Concept> hotspots (top N by <column>), where the truncation clause states the actual --top state (top 20 by CC, or all, by CC for --top 0):
    • Summary: file count, SLOC, PLOC, comment ratio, and Average MI (SLOC-weighted) with a GOOD / MODERATE / LOW rating. The headline is the SLOC-weighted mean of the unclamped Visual Studio MI: large files dominate, and a file whose displayed MI clamps to 0 still contributes its true (often negative) maintainability rather than a misleading 0. A file with no code to measure (an #include-only header, an empty module) is left out of every MI average and of the MI hotspot table, because its MI of 0.0 is a placeholder, not a score; it still counts toward Files and SLOC. A language with no file to measure reads n/a.
    • Actionable Summary: counts of functions exceeding common thresholds (by default CC > 10, cognitive > 15, SLOC > 100, args > 3, Halstead bugs > 1; a manifest [thresholds] table overrides each cutoff). The Actionable Summary appears immediately after the Summary and before the hotspot tables. It counts raw measurements without applying suppression; the section is captioned to say so, naming how many suppressed functions are folded in. When suppression empties a hotspot table whose metric this summary still counts, the table is replaced by a one-line “table omitted: all N matching functions suppressed” note so a summary bullet never points at a missing table.
    • Maintainability Index hotspots (lowest N by MI): files sorted ascending by MI.
    • Cyclomatic complexity hotspots (top N by CC): functions sorted descending by CC, with summary statistics (average, max, counts above 10 and 20).
    • Cognitive complexity hotspots (top N by Cognitive): functions sorted descending by cognitive complexity.
    • Halstead effort hotspots (top N by Effort): functions sorted descending by Halstead effort, including volume and estimated bugs. Effort and Volume render as rounded integers with thousands separators (8,845); full precision lives in JSON/CSV.
    • Function size hotspots (top N by SLOC): functions sorted descending by source lines of code.
    • Many parameters hotspots (top N by Args): functions with more than three parameters, sorted descending.
    • Type hotspots (top N by WMC): types sorted descending by Weighted Methods per Class, with NOM, NPA, and NPM. “Type” covers all six kinds the report counts: class, struct, trait, impl, interface, namespace (the legend’s WMC entry lists them).
    • Exit points hotspots (top N by Exits): functions with more than two exit points, sorted descending. A single return is the baseline, not a hotspot, so the table admits only nexits > 2; when nothing clears the floor the section is omitted.
    • ABC magnitude hotspots (top N by ABC): functions sorted descending by ABC metric magnitude.

Format consistency

The Markdown and HTML renderers share one data model. They use the same summaries, hotspot membership, and captions, so both formats report identical numbers.

Both formats carry a Legend that defines every metric column abbreviation (CC, MI, ABC, WMC, and so on), the global-header stats (PLOC, Comments, Comment ratio), and the Per-language overview’s columns (Files, Functions, Avg MI, Avg CC, Avg Cognitive) — Avg MI in particular, since it is the unclamped, SLOC-weighted average and can read negative where the per-file MI column saturates at 0. It is a ## Legend section in Markdown (its own outline entry, not nested under the last language) and an expanded (<details open>) block in HTML, so it survives print, mobile, and screen readers as well as a hover tooltip. Each entry links to its chapter in the Supported Metrics reference, so a one-line definition can hand the reader the full explanation. The definitions come from the same shared column specs the tooltips use, so the two formats cannot drift.

Both formats close with a provenance footer stating the bca version — followed, for a bca built from a source checkout, by the first twelve digits of its build-inputs digest in parentheses, the same digest bca --version prints in full — the generation date, the seed paths scanned, the per-table --top value, and whether suppression markers were honored, so a detached artifact (a PR comment, a Pages deployment, a file on a ticket) records what it was generated from. The date honors SOURCE_DATE_EPOCH for reproducible builds. The HTML report additionally carries a <meta name="viewport"> tag and wraps every table in a horizontal-scroll container so wide tables stay reachable on mobile and narrow windows, and its table of contents nests each language’s hotspot subsections under a collapsible entry.

Source suppression markers apply per metric to both report hotspot tables and bca check’s offender formats (code-climate, sarif, checkstyle, clang-warning, msvc-warning). Check baselines additionally filter check violations; they do not filter report hotspot tables. The Code Climate, SARIF, and Checkstyle documents are themselves three renderings of one offender set, so they agree by construction; the reports honour the same per-metric suppression decisions.

The single deliberate exception is the Actionable Summary, a whole-codebase health indicator that counts raw measurements regardless of suppression: silencing a function in one metric’s hotspot table does not erase it from that aggregate concern count. Every other figure, including each hotspot table’s caption, reflects the suppression-filtered set. To stop a reader mistaking the two populations for a double-count, each is captioned: the cyclomatic note adds “(excluding suppressed functions)”, and the Actionable Summary names the raw, suppression-ignoring basis of its counts.

HTML format

bca report html emits a single self-contained HTML page covering the same sections as the Markdown report. It is built to be served as a static artifact: inline CSS, inline vanilla JavaScript for click-to-sort on every hotspot table, and no external dependencies (no CDN, no fonts, no template engine). The page renders identically offline.

Write it to a file and open it in any browser:

bca report --paths /path/to/project \
    html --top 10 --output report.html

Every column header is a native button: click its label — or Tab to it and press Enter or Space — to sort that table ascending, and again to toggle descending. Each table sorts independently. Empty cells (where a metric was not measured) sort last in both directions, so “no data” rows never surface at the top of a hotspot. The header follows the WAI-ARIA sortable-table pattern — the label is the <button>, the cell carries aria-sort, and the sort arrow is hidden from assistive technology — so a screen reader announces the control as a button and the column’s current order. The button fills the header cell, so clicking anywhere in it sorts; the ? beside a metric label is the one thing in a header that does not.

Hover any metric column header (SLOC, MI, CC, ABC, WMC, NPA, NPM, Exits, and the rest) for a one-sentence plain-English explanation of the metric. The tooltip is delivered through the native HTML title attribute, so it works offline with no JavaScript. The small ? after a metric’s label is the link to that metric’s chapter in this book.

Because title tooltips are hover-only (invisible in print, on mobile, and to screen readers), the page also ends with a visible, collapsible Legend (<details>) listing every metric column’s one-line definition. Both the tooltips and the legend draw from the same column specs, so a definition cannot say one thing on hover and another in the legend.

Every interpolated string (function name, file path, language label) is HTML-escaped on the way out, so a crafted source path or symbol name cannot inject markup or break out of an attribute value.

Each per-language <section> carries a stable lang-<name> class (lang-rust, lang-python) styled with a low-alpha background tint and matching left border so a multi-language report’s section boundaries are obvious at a glance. Languages without an explicit palette entry fall back to a neutral lang-other tint, and a prefers-color-scheme: dark adapter raises the alpha so contrast holds in both themes.

Metric values of zero

Zero can be a valid measurement or a placeholder for unavailable data, depending on the metric. Each hotspot table applies its own eligibility rule; a measured MI of zero remains eligible. A table with no eligible entries is omitted.

Check

bca check compares metrics against thresholds at each metric’s own scope (function, file, or container) and exits non-zero when any value exceeds its limit. It is the CI integration point: wire it into a build step and a regression in code complexity fails the pipeline before the change lands.

This page documents the command. The first half covers what most runs need: declaring thresholds, exit codes, offender output, baselines, and CI wiring. The second half covers the exemption mechanisms, per-language limits, the soft tier, and the edge cases in how they combine. For runnable pipeline examples see the CI integration recipe, which covers the --report-format matrix, GitHub Actions and .gitlab-ci.yml examples, the baseline ratchet, and the GitLab Code Quality path.

Declaring thresholds

Pass --threshold <metric>=<limit> once per metric (repeatable). Metric names match bca list-metrics; sub-metrics use a dotted form. A value violates its limit only when it exceeds it, so 0 is a valid limit that permits exactly zero.

bca check --paths src/ \
    --threshold cyclomatic=15 \
    --threshold cognitive=20 \
    --threshold loc.lloc=200

Or keep thresholds in the bca.toml manifest, so CI thresholds are versioned alongside the code. A manifest at the repo root is discovered automatically: a bare bca check reads it with no --config flag.

# bca.toml
paths = ["src"]

[thresholds]
cyclomatic = 15
cognitive = 20
"loc.lloc" = 200
"halstead.volume" = 1000
bca check

To merge a separate threshold file on top of the manifest for one run, pass it explicitly with --config; CLI flags and --config values override the manifest for the same metric name, so you can keep a project-wide default and tighten a single metric for a specific run:

bca check --paths src/ --config bca.toml

bca init scaffolds a starting table for you. For where those numbers come from, which ones to override for the language you are gating, and how to pick a different set for agent feedback, legacy triage, or safety-critical work, see Choosing thresholds.

Accepted metric names

Top-level scalar metrics use their list-metrics names directly: cognitive, cyclomatic, nargs, nexits, nom, tokens, abc, wmc, npm, npa. Metric suites with multiple sub-fields use a dotted form:

MetricAccepted threshold names
Cyclomaticcyclomatic, cyclomatic.modified
Halsteadhalstead.volume, halstead.difficulty, halstead.effort, halstead.time, halstead.bugs
Lines of codeloc.sloc, loc.ploc, loc.lloc, loc.cloc, loc.blank, loc.dloc
Maintainability Indexmi.original, mi.sei, mi.visual_studio

An unknown threshold name is a tool error (exit 1), not silently ignored.

Threshold scope

A threshold is checked only against the space kind its metric measures, so a metric’s whole-file or whole-impl aggregate is never mistaken for a per-function limit. Each metric has a fixed scope; there is nothing to configure.

ScopeGated spacesMetrics
Filethe whole-file root onlyloc.sloc, loc.ploc, loc.lloc, loc.cloc, loc.blank, loc.dloc
Functionindividual functions, methods, and closurescognitive, cyclomatic, cyclomatic.modified, halstead.*, mi.*, abc, nargs, nexits, tokens
Containerclasses, structs, traits, impls, namespaces, interfacesnom, wmc, npm, npa

The Function-scoped metrics include the subtree sums (nargs, nexits, tokens, halstead.*). These still roll a function’s own nested closures into its figure, but they are not summed across an entire file or impl. The Container-scoped metrics describe a type’s method set (methods per class, weighted methods, public members), so they gate the container rather than every leaf function. A file whose functions are individually fine therefore never trips an additive limit on the file-wide total, which is the false positive bca: suppress-file markers used to mask.

The bare bca diff --metric spelling of a loc sub-metric is accepted as an alias for its dotted form (sloc for loc.sloc, and so on for ploc/lloc/cloc/blank/dloc), so a name copied from a diff run gates correctly. A bare family head with no single threshold scalar (halstead, mi) is ambiguous and rejected with a “did you mean” hint listing the concrete sub-metrics; pick one, such as halstead.volume.

The two spellings name one metric, so they override each other wherever limits merge: a per-language [thresholds.lang.c] ploc = 100 replaces the global "loc.ploc", and --threshold ploc=100 replaces either. Within a single table, writing both is an error rather than a silent winner. Set the metric once, under whichever spelling you prefer.

Exit codes

CodeMeaning
0All functions within thresholds (or --no-fail set).
2At least one threshold exceeded.
1Tool error (bad arguments, unreadable config or input, unknown metric).

1 is reserved so CI can distinguish a regression (2) from a tool misconfiguration (1).

A gate that could not read all of its input has no verdict to report, so three input problems exit 1 rather than 0: nothing matched --paths / --include / --exclude, any input file that failed to read, and any directory the walk could not list. The last two are the workspace-wide unreadable-input rule. check is not special here; it is where the rule matters most, because a gate reporting clean on a tree it could not read looks the same as a gate that passed. All three checks run before the gate is evaluated. --no-fail does not suppress them, since it suppresses threshold failures rather than broken input, so none of them lets --write-baseline record a partial run.

Tiered exit codes (--exit-codes=tiered)

--exit-codes=tiered (or [check] exit_codes = "tiered" in bca.toml) splits the single violation code 2 by severity so CI can branch on it without parsing the [new] / [regr …] row tags a baseline run prints:

CodeMeaning (tiered mode)
0All functions within thresholds (or --no-fail set).
1Tool error.
2New offenders only (no --baseline entry matched).
3Baseline regressions only (a baselined offender worsened).
4Both new offenders and regressions.
5A --tier=soft violation that also breaches the hard limit.

The tiered codes are opt-in; the default contract above stays 0/1/2. Every fail-state remains non-zero, so exit != 0 → fail wrappers keep working. Only tooling that tests $? -eq 2 explicitly needs to widen to 2-5. --no-fail still forces exit 0. Code 5 is emitted only at the soft tier; at the hard tier every violation is a hard breach by definition, so the 2/3/4 split applies instead.

--exit-codes <default|tiered> takes a value, and the CLI value overrides the [check] exit_codes manifest key in either direction. An invalid exit_codes value is a tool error (1). --print-effective-config reports the resolved exit_codes style. The deprecated --strict-exit-codes flag is a one-cycle alias for --exit-codes tiered (warns; removed at the next major).

Offender output

Every offending (function, metric) pair prints one line to stdout in this stable format:

<path>:<start_line>-<end_line>: <function_name>: <metric> = <value> (limit <limit>)

For example:

src/parser.rs:42-117: parse_expression: cyclomatic = 22 (limit 15)
src/parser.rs:42-117: parse_expression: cognitive = 31 (limit 20)

Lines are sorted by path, then start line, then metric name, so output is deterministic across runs over the same tree.

Which stream

The offender rows are the command’s product, so they go to stdout: bca check | wc -l, | head, | rg -c and 2>/dev/null all reach them.

Everything the run says about itself goes to stderr:

  • the per-file --- summary --- footer,
  • the --- next steps --- remediation block,
  • the GitHub Actions ::error annotations,
  • the bca: skipped N violations via [check.exclude] and bca: filtered N violations via baseline counts,
  • every warning: and error: diagnostic.

One combination inverts this. --report-format <dialect> without --output puts the aggregated SARIF / Checkstyle / Code Climate document on stdout, so the human rows move to stderr rather than corrupting it (the first example counts results with jq):

bca check --report-format sarif | jq '.runs[0].results | length'   # document on stdout
bca check --report-format sarif --output report.sarif | wc -l      # rows back on stdout

The --summary-file digest is a file, not a stream, and appears on neither.

Earlier releases sent the rows to stderr along with everything else, which made | wc -l and 2>/dev/null report an empty offender list, indistinguishable from a clean tree. A pipeline that reads the rows through 2>&1 needs no change; one that captured them with 2>file should now use >file.

Baselines

Adopting thresholds on an existing codebase usually presents a choice between “raise the limit until nothing fires” and “fix every offender before turning the gate on”. A baseline file is the ratchet-down alternative: record today’s offenders, fail only on regressions and new offenders, and shrink the file over time as the team pays down debt.

Baselines complement the suppression markers; they are not a substitute. A suppression says “this function is intentionally exempt forever” and lives in source; a baseline says “this is tech debt we are paying down” and lives in a committed TOML file. bca check applies in-source suppressions first, then [check.exclude] globs, then the baseline filter.

Writing a baseline

bca check --paths src/ \
    --write-baseline .bca-baseline.toml

This walks the tree, captures every threshold violation that would otherwise fail the check, and writes them to the file as sorted TOML. The run exits 0 regardless of offender count, since the point is to capture them.

# bca baseline file. Generated by `bca check --write-baseline`.
# Listed offenders are filtered from threshold checks; a function that
# gets worse than its recorded value still fails. Refresh with
# `--write-baseline` when entries become stale.
version = 6

[provenance]
tier = "hard"

[[entry]]
path = "src/parser.rs"
qualified = "Parser::parse_expression"
metric = "cyclomatic"
value = 22.0

The qualified field is the function’s qualified symbol: the ::-joined chain of enclosing named containers plus the function name. An entry carries a start_line only when another entry shares its (path, qualified, metric) identity, the one case where matching consults a line number. Recording it elsewhere would rewrite the file every time an edit above the function shifted it. With --baseline-fuzzy-match, each entry also carries a body_hash for rename-tolerant matching.

Functions already covered by an in-source suppression marker are excluded. Pass --no-suppress together with --write-baseline to include violations hidden by source markers (the CI-auditor flow); [check.exclude] exemptions still apply.

--write-baseline cannot be combined with --baseline, --report-format, --output, --since, or --changed-only, because the baseline file is the output.

Reading a baseline

bca check --paths src/ \
    --baseline .bca-baseline.toml

A violation is suppressed when both conditions hold:

  • An entry matches by (path, qualified_symbol, metric), independent of line number, or, failing that and with --baseline-fuzzy-match, by body hash. See the Baselines recipe for the full resolution order.
  • The current value is less than or equal to the recorded value.

A function that gets worse than its baseline value still fails. New offenders not listed in the baseline still fail. Improvements pass silently; the entry keeps its older, higher value until the next --write-baseline refresh.

A baseline file that does not exist, is empty, has a missing or unsupported version, or fails to parse is a tool error (exit 1), not a silent zero-match.

Path keys are canonicalised relative to the baseline file’s own directory (the anchor), so --paths ., --paths src/, and --paths "$PWD" produce byte-identical baselines, and a --baseline run matches whichever --paths form generated the file. Switch between them freely without re-running --write-baseline.

Limitations

  • Ambiguous symbols and anonymous functions. Entries key on the qualified symbol, so inserting code above a named function does not re-key it. Two exceptions re-key as “new” on movement: functions sharing a qualified symbol that drift more than --baseline-line-tolerance apart, and anonymous closures or lambdas, whose synthetic symbol embeds the line. Refresh with --write-baseline.
  • OS portability. Paths are stored with forward slashes so a baseline written on one OS matches the same tree on another. Paths that are not valid UTF-8 fall back to a lossy display form (U+FFFD substitution) and may not round-trip exactly.

See the Baselines recipe for the end-to-end adoption flow and CI integration patterns.

Reporting without failing

--no-fail reports offenders as usual but exits 0. Use it while adopting baselines without turning CI red. Other CI tools call this behavior --report-only or --soft-fail; here the flag is spelled --no-fail.

bca check --paths src/ --no-fail

CI example (GitHub Actions)

- name: Check code complexity thresholds
  run: |
    bca check
  # Thresholds and paths come from the auto-discovered `bca.toml`
  # manifest at the repo root. The default non-zero exit fails the
  # step, which is the behaviour wanted here. No extra wiring.

To keep the job green and surface offenders as a build annotation while you reduce the count, swap in --no-fail:

- name: Surface complexity hot spots (non-blocking)
  run: |
    bca check --paths src/ --no-fail

Actionable failure output

When bca check fails, five flags shape the failure stream so a developer skimming a CI log can see what tripped, where in their PR it tripped, and what to do next. Each flag is independent, and all of them auto-detect from GitHub Actions environment variables when present, so the common CI case needs no explicit configuration.

FlagEffectAuto-detect env
--since <ref>Partition per-file footer into “Files in this range” + “Other offenders”BCA_DIFF_BASE, GITHUB_BASE_REF, GITHUB_EVENT_BEFORE
--changed-onlyDrop violations outside the diff scope entirelyRequires a resolvable base (--since or one of the above)
--github-annotations <auto|always|never>Emit ::error file=…::msg workflow commands for inline file annotations (bare flag = always)auto detects GITHUB_ACTIONS == "true"
--summary-file <path|auto|never>Append markdown digest (per-file rollup + breakdown + top-10 offenders); never suppresses itauto detects GITHUB_STEP_SUMMARY
--no-remediationSuppress the trailing --- next steps --- blockBlock emitted on failure unless this flag is passed

The per-violation rows and the per-file rollup footer keep the same content when none of the above are active, so CI tooling that grep-anchors on the legacy text keeps working. See Which stream for where each now lands.

See the CI integration recipe for worked examples, including a GitHub Actions snippet that composes all five flags into one step, and the Baselines recipe for the --write-baseline refresh flow the remediation block links to.

Diff-base auto-detection precedence

When --since is omitted, bca consults environment variables in this order:

  1. BCA_DIFF_BASE: an explicit override for local shells or CI runners other than GitHub Actions.
  2. GITHUB_BASE_REF: set by GitHub Actions on pull_request events. Expanded to origin/<value>; the runner is responsible for the corresponding git fetch (fetch-depth: 0 on actions/checkout).
  3. GITHUB_EVENT_BEFORE: set by GitHub Actions on push events to the SHA at HEAD before the push. The all-zeroes sentinel (force push, brand-new branch) is treated as no signal.

Failing to resolve a base is non-fatal unless --changed-only is passed, in which case the gate dies. Silently suppressing every violation under a misconfigured base is the failure mode this feature exists to prevent. --write-baseline also conflicts with --since / --changed-only: a partial baseline would silently mask every offender outside the diff scope on the next full-tree run.

Exporting offender records

bca check also emits a single CI/IDE document covering every offender in the walk. Pass --report-format <fmt> to pick the shape and --output <file> to write it to disk (stdout if omitted). The --format, -O, and --output-format spellings are accepted as deprecated aliases and will be removed in a future release. These flags do not change the exit-code contract: 0 clean, 2 on any violation (unless --no-fail), 1 on tool error.

When --output is given without --report-format, the format is inferred from the output extension: .sarif selects sarif and .xml selects checkstyle. An extension with no unique format (notably .json, which both sarif and code-climate produce) or no extension at all is a usage error (exit 1) naming --report-format, so an explicit --output is never silently ignored. An explicit --report-format always wins over the extension.

FormatAudience
checkstyleJenkins, SonarQube, GitLab, “warnings plugin” CI
sarifGitHub Code Scanning, modern IDEs / security tooling
code-climateGitLab MR Code Quality widget
clang-warningEditor quickfix parsers, GitHub Actions problem matcher
msvc-warningVisual Studio, VS Code, Windows CI runners

When no offenders exist the writer emits a well-formed but empty document, so CI consumers can ingest clean runs unchanged: an empty runs[].results array for SARIF, an empty JSON array ([]) for Code Climate, no <file> children under the <checkstyle> root for Checkstyle, and zero bytes for the two warning-line formats.

Checkstyle (CI integration)

bca check --paths src/ \
    --threshold cyclomatic=15 \
    --report-format checkstyle \
    --output report.checkstyle.xml

The Checkstyle writer emits a single <checkstyle version="4.3"> document containing one <file> element per source path, each holding one <error> per metric-threshold violation. The schema is the Checkstyle 4.3 XSD that Jenkins and SonarQube’s “Warnings Next Generation” / “Generic Issue” importers consume directly.

SARIF (GitHub Code Scanning)

bca check --paths src/ \
    --threshold cyclomatic=15 \
    --report-format sarif \
    --output report.sarif.json

The SARIF writer emits a single SARIF 2.1.0 JSON document with one runs[] element. Each metric-threshold violation becomes a result under runs[0].results[]; the metric names appearing in the run are deduplicated into runs[0].tool.driver.rules[] with short descriptions.

To upload a SARIF file to GitHub Code Scanning from a workflow:

name: bca-sarif
on: [push, pull_request]
jobs:
  scan:
    runs-on: ubuntu-latest
    permissions:
      security-events: write
    steps:
      - uses: actions/checkout@v4
      - name: Run big-code-analysis
        run: |
          bca check --paths . \
              --report-format sarif \
              --output report.sarif.json \
              --no-fail
      - name: Upload SARIF
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: report.sarif.json

--no-fail keeps the job green so the SARIF upload step still runs when offenders exist; remove it once you want a metric regression to fail the workflow.

GitLab Code Quality (Code Climate JSON)

bca check --paths src/ \
    --threshold cyclomatic=15 \
    --report-format code-climate \
    --output gl-code-quality-report.json

The Code Climate writer emits a single JSON array of issue objects matching GitLab’s strict subset of the upstream Code Climate engine spec: one entry per metric-threshold violation, no byte-order-mark, one trailing newline (empty input renders as []\n).

Each issue carries three fields of note. The check_name is namespaced as big-code-analysis/<metric>. The fingerprint is a stable SHA-256 over path \0 function \0 metric; it ignores line and value, so cosmetic edits still dedup in the MR widget. The severity maps GitLab’s five-level enum from the same severity key bca check ranks its own offenders by: ≤ 1.5 → minor, ≤ 2 → major, ≤ 4 → critical, > 4 → blocker.

For a count the key is value / limit, and a breach of a zero limit is always blocker. For the mi.* family, where lower is worse and the index has no true zero, it is 1 + (limit − value) / unit: one step per maintainability band (17.1 points of mi.original / mi.sei, 10 of mi.visual_studio) below the floor. An mi.original of 0 against a floor of 20 is critical; one of −40 is blocker. The full enum is info/minor/major/critical/blocker; bca never emits info, because a threshold violation always lands at minor or higher.

To wire the artifact into GitLab’s MR Code Quality widget:

code_quality:
  stage: quality
  script:
    - bca check --paths "$CI_PROJECT_DIR"
          --report-format code-climate
          --output gl-code-quality-report.json
          --no-fail
  artifacts:
    when: always
    reports:
      codequality: gl-code-quality-report.json
    paths:
      - gl-code-quality-report.json

See the GitLab Code Quality widget recipe for the full pipeline (combined Code Climate + Checkstyle + Markdown report) and a local jq smoke check.

--no-fail keeps the job green so the Code Quality report still uploads when offenders exist; remove it once you want a metric regression to fail the pipeline.

Clang/GCC warning lines (editor quickfix and CI annotators)

bca check --paths src/ \
    --threshold cyclomatic=15 \
    --report-format clang-warning \
    --output report.txt

The Clang format emits one offender per line in the conventional compiler-warning shape:

path/to/file.rs:42:5: warning: cyclomatic 17 exceeds limit 15 [big-code-analysis-cyclomatic]

This is the format clang -fdiagnostics-format= produces and the shape editor quickfix parsers (VS Code, IntelliJ, Vim) and most CI annotators understand without configuration.

GitHub Actions surfaces the lines as inline annotations on the PR diff through the built-in GCC problem matcher (or any community compiler-problem-matchers action):

name: bca-clang-warnings
on: [push, pull_request]
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Enable GCC problem matcher
        run: echo "::add-matcher::$RUNNER_TOOL_CACHE/problem-matchers/gcc.json"
      - name: Run big-code-analysis
        run: |
          bca check --paths . \
              --report-format clang-warning \
              --no-fail

If your runner does not ship a GCC matcher, fall back to streaming the lines and re-emitting them as ::warning file=...,line=...:: workflow commands.

MSVC warning lines (Visual Studio and Windows CI)

bca check --paths src/ \
    --threshold cyclomatic=15 \
    --report-format msvc-warning \
    --output report.txt

The MSVC format emits one offender per line in Visual Studio’s cl.exe diagnostic shape:

path\to\file.rs(42,5): warning : cyclomatic 17 exceeds limit 15

The space before the colon after warning/error is the MSVC convention. On Windows the path is normalized to \ separators, matching cl.exe output; on other platforms the path is emitted as-is. Visual Studio, VS Code with the C/C++ extension, and Windows CI runners (Azure Pipelines, GitHub Actions on windows-latest) parse these inline without extra configuration.

Silencing violations with suppression markers

In-source comments can silence threshold violations on individual functions or whole files without editing the offending code or excluding it from the walk. The native dialect is bca: suppress / bca: suppress-file; Lizard’s #lizard forgives is recognized as a compatibility shim. See Suppression markers for the full reference and the --no-suppress CI-audit flag.

Exempting whole file categories ([check.exclude])

Some files should be analysed and reported but never gated: test fixtures that intentionally trip cognitive or cyclomatic, generated bindings, macro-dispatch modules whose complexity is structural and will never be “fixed”. Putting these in .bcaignore is too blunt: it removes them from the walk entirely, so bca report loses them too. Baselining them is also wrong, because they are not debt being paid down, and they churn the baseline diff forever.

[check.exclude] is the glob-level middle ground. Matching files are walked, parsed, measured, and shown by bca report, but bca check drops their violations before emitting offenders and before --write-baseline records anything, so the structural exemptions stay out of .bca-baseline.toml.

In bca.toml:

[check]
exclude = [
    "tests/**",
    "big-code-analysis-ast/src/languages/language_*.rs",
    "xtask/**",
]

Or on the command line (--check-exclude is repeatable and unions with --check-exclude-from):

bca check --check-exclude "tests/**" --check-exclude "xtask/**"
bca check --check-exclude-from .bcacheckignore

--check-exclude-from reads a .gitignore-style file (blank lines and # comments skipped); the conventional name is .bcacheckignore, mirroring .bcaignore for the walker. Globs match the path exactly as the walker matched it for --exclude. As a negative filter key, an explicit --check-exclude list unions with the manifest [check] exclude list rather than replacing it. A CLI exemption is added to the project’s, so you cannot accidentally re-gate a path the manifest deliberately exempted. Duplicates collapse; CLI patterns sort first. Pass --no-config to drop the manifest’s exemptions entirely. Positive scope keys like paths / include still replace on the CLI; only the exclude filters merge.

What a relative glob is relative to

The source of a glob determines its root:

SourceA relative glob is resolved against
bca.toml [check] exclude / exclude_fromthe directory holding the bca.toml
--check-exclude / --check-exclude-fromthe directory you ran bca from

So exclude = ["./vendor/**"] in a manifest at the repo root means <repo>/vendor/, whichever directory you invoke bca check from, including from inside vendor/ itself. The same glob passed as --check-exclude './vendor/**' means vendor/ under your shell’s current directory, because that is where you typed it.

This matters most for per-file callers (editor integrations, pre-commit hooks, the agent hooks in Agent feedback), which run bca check <one file> from whatever directory they happen to be in. Their exemptions belong in the manifest, where the anchor is the project’s and not the caller’s.

The walker’s exclude / --exclude pair is split by origin the same way: a --exclude glob is matched against each file’s path relative to the seed you named, and a manifest exclude glob is resolved against the manifest’s own directory, whatever seed you name and wherever you run from. So bca metrics and bca metrics -p sub honour the same manifest exclusions (#1189), and the warning that a named file overrode an exclude resolves each glob at its own root (#1164).

Precedence with the other suppression mechanisms

Most-specific to least, bca check resolves exemptions in this order:

  1. In-source markers (bca: suppress / bca: suppress-file) always win; they apply during the walk, so the function never becomes a violation.
  2. [check.exclude] globs exempt categories of files (tests, generated code).
  3. .bca-baseline.toml lists known offenders being paid down.

--print-effective-config reports the resolved check_exclude globs alongside the other gate inputs.

check_exclude lists the combined CLI and manifest globs. manifest_check_exclude identifies the manifest subset; resolve those globs relative to the reported manifest directory. The walker uses exclude and manifest_exclude in the same way. The manifest-specific keys are absent when the manifest contributes no globs.

manifest_exclude_from and manifest_check_exclude_from follow the same idea for the exclude_from files, with one difference: a command-line --exclude-from replaces the manifest’s file rather than adding to it, so the manifest key is present only when the manifest’s file is the one in effect. The inline glob lists union; the files do not.

Per-language limits ([thresholds.lang.<slug>])

Metric distributions vary by language more than by project: the measured 97.5th-percentile per-function cognitive value runs from 4 in C# to 50 in C. A [thresholds.lang.<slug>] table gives one language its own limits, layered over the project-wide table:

[thresholds]
cognitive = 15
cyclomatic = 15
"loc.ploc" = 600

[thresholds.lang.c]
cognitive = 30
"loc.ploc" = 1200

[thresholds.lang.elixir]
nom = 150
wmc = 300

C is now gated at cognitive = 30 and loc.ploc = 1200 while keeping the project’s cyclomatic = 15; every other language keeps all three. The override is per metric, not a replacement table: a language inherits every limit it does not restate. Which numbers to change, and the two cases where an override is a correction rather than tuning, are in Choosing thresholds.

The key is the canonical language slug, the same vocabulary --language accepts: rust, python, cpp, csharp, objc, tsx, mozcpp, mozjs, and so on. bca check --language nonsense prints the full list. Two rules point in opposite directions here:

  • An unknown slug in the manifest is a tool error (exit 1) with a did-you-mean hint. A typo’d [thresholds.lang.rust-lang] must not silently leave a gate at the project limit while the author believes it was loosened.
  • An unrecognised file language falls through to the global table, as does any language with no override of its own. Nothing is silently ungated. A file whose extension maps to no grammar at all is skipped by the walk before the gate sees it, with a warning if you named it explicitly.

--threshold on the command line stays global and still applies last and absolutely: it overrides the project table and every per-language one, so a limit you type is the limit that runs.

--print-effective-config prints one fully resolved table per overridden language, inherited limits included rather than a diff, so the number that will fire is the number you read:

[thresholds]
cognitive = 15.0
cyclomatic = 15.0

[thresholds.lang.c]
cognitive = 30.0
cyclomatic = 15.0

Per-path limits ([[thresholds.path]])

A [[thresholds.path]] entry re-limits the files its glob matches for the metrics it names, and inherits everything else. It is for a population of files whose shape the project limit misreads — a directory of dispatch tables, a generated layer — and it keeps those files gated, at a limit chosen for them, where a suppression marker would remove them:

[[thresholds.path]]
glob = "src/metrics/{abc,cognitive,cyclomatic,loc}/*.rs"
cyclomatic = 40
"halstead.effort" = 150000

The glob is matched relative to the directory of the config that declared it — the bca.toml, or the --config file — with the engine and anchor [check] exclude uses. Where several entries match one file the last wins, per metric. An unknown metric, a missing or empty glob, and tokens are each a tool error naming the entry. The soft tier derives from the overridden limit, and --threshold still outranks it; the full order is stated once in How the layers compose, and when to reach for an entry rather than a marker is in Per-path overrides.

--print-effective-config prints each entry after the per-language tables, with the metrics it declares at their hard-tier limit — --threshold applied last, the tier not applied — so the printed file replays the gate at the hard tier through --config. That is the whole guarantee. The resolved view carries no [thresholds.soft] table and does not record which limits came from --threshold, so a soft-tier run is not replayed by the printout alone: loading it with the same --tier re-derives a band from the printed hard limits, which matches the original only when the original’s soft configuration is still in scope (the manifest is still discovered, not --no-config) and it used no --threshold (a CLI limit is absolute and unscaled on the original run; reloaded, it is a hard limit the band scales). The reason the printed path limits are hard, not banded, is in Per-path overrides:

[thresholds]
cyclomatic = 15.0

[[thresholds.path]]
glob = "src/metrics/{abc,cognitive,cyclomatic,loc}/*.rs"
cyclomatic = 40.0
"halstead.effort" = 150000.0

Two-tier thresholds (--tier)

--tier <hard|soft|soft=RATIO> selects which threshold tier the gate compares against. hard (the default) uses the [thresholds] table verbatim; soft is an early-warning tier that fires before the hard gate, tightening every limit by RATIO. A bare --tier means soft; soft alone uses the default ratio 0.95; soft=0.90 pins the ratio to 0.90; soft=1.0 disables the blanket scale.

RATIO scales the band, not the number. For most metrics a limit is a ceiling, so tightening it means multiplying: cognitive = 15 with soft=0.9 warns at 13.5.

The lower-is-worse mi.* family is different. Its limit is a floor, a value below it is the violation, and the index has no true zero (mi.original and mi.sei go negative on large functions). So the floor is raised by 1 − RATIO of one maintainability band rather than scaled. One band is 10 points of mi.visual_studio, whose 20/10 cut-offs Visual Studio and radon use, and the same 17.1 points on the 171-point mi.original / mi.sei scale. "mi.original" = 20 with soft=0.9 warns at 20 + 0.1 × 17.1 = 21.71, and a floor of -50 at -48.29. Multiplying a floor would move the warning to 18, under the hard gate, and dividing a negative one would do the same (-50 / 0.9 = -55.6). Either way nothing could reach the soft band first.

Limits must be finite. A count limit must also be non-negative; an mi.original or mi.sei floor may be negative. A limit that cannot fire, or fires on every value, still parses but prints a warning: mi.visual_studio is clamped to [0, 100], so = 0 reads warning: limit mi.visual_studio=0 cannot fire: 0 is the metric's floor; gate mi.original instead, and = 101 warns that it fires on every value.

A [thresholds.soft] table sets per-metric soft limits, each either an absolute number or a "<ratio>x" string that scales the metric’s hard limit:

[thresholds]
cognitive  = 25
cyclomatic = 15
nargs      = 7

[thresholds.soft]
cognitive  = 22       # absolute soft limit
cyclomatic = "0.9x"   # 90% of the hard limit → 13.5
# nargs absent → soft tier inherits the hard limit (no soft band)
bca check --paths src/ --tier=soft

The soft tier is one step of the layering order stated in How the layers compose. Within that step: if a [thresholds.soft] table exists, its overrides merge onto the resolved hard limits and metrics absent from it inherit their hard limit — the blanket RATIO does not apply, since explicit per-metric limits win; otherwise every limit is tightened by the soft RATIO (default 0.95 for a bare soft; soft=1.0 disables scaling). Repeated --threshold name=value flags still apply after it, absolutely.

The step runs once per table — the project’s, each language’s, and each [[thresholds.path]] combination’s — against that table’s own resolved hard limits. There is no [thresholds.lang.<slug>].soft table and none is needed. With [thresholds] cognitive = 15, [thresholds.lang.c] cognitive = 30, and --tier=soft=0.9, C’s soft band is 27, nine tenths of its limit, and the ceiling a C offender is measured against for the exit-5 escalation is 30, not 15. Derive either from the project’s 15 and every C function between 15 and 30 reports “also breaches the hard limit” while sitting inside the limit the project configured for it.

The one combination that can invert the two tiers is an absolute [thresholds.soft] value looser than the hard limit it shadows: [thresholds.soft] cognitive = 12 alongside [thresholds.lang.csharp] cognitive = 4, or an mi.* soft floor below its hard floor. That is a tool error (exit 1) naming the offending table, for the same reason a "<ratio>x" factor above 1 is rejected at parse time: a soft tier that fires after the hard gate is never the intent.

The soft RATIO (and the scale factor in a "<ratio>x" string) must be in (0, 1]. The [check] headroom manifest key supplies the ratio for a bare --tier=soft. The deprecated --headroom <R> flag is a one-cycle alias for --tier=soft=<R> (warns; removed at the next major); it now promotes a hard run to the soft tier. Both tiers ratchet through the same --baseline, and --print-effective-config reports the resolved tier alongside the post-merge limits. See the Local threshold gates recipe for the migration tip and rationale.

Previewing a candidate limit (--explain-threshold)

--explain-threshold <metric>=<limit> reports what a candidate limit would cost at both tiers instead of gating. It is repeatable, takes one candidate per metric, and writes nothing.

$ bca check --explain-threshold nargs=6
nargs: candidate limit 6
  hard tier (limit 6): 60 offenders, 60 already baselined, 0 new
  soft tier (limit 5.7, 0.95x): 135 offenders, 61 already baselined, 74 new
  cluster: 75 of 75 soft-band offenders sit at exactly 6 — the candidate
  limit itself. The soft tier measures distance to the limit, so a limit
  of 6 places them inside the 5.7 band by construction and none of them
  can clear it without real work.

The new column is the figure to weigh: it is how many baseline entries adopting the limit would add. Here the hard tier reads as free and the soft tier costs 74 entries, none of which any amount of tidying can retire. Making that visible is the reason the flag exists.

--threshold nargs=6 cannot tell you this. Its limits are applied last and absolutely, never scaled, so a candidate trialled that way has no soft tier at all. Passing both flags for the same metric is rejected rather than silently resolved.

The soft limit is derived from the candidate exactly as a real run would derive it: a [thresholds.soft] entry for the metric wins, then the --tier=soft=RATIO ratio if one was given, then 0.95. A [thresholds.lang.<slug>] table that overrides the metric keeps its own limit, since a candidate global limit does not reach it, and the report says so on its own line. A ratio-derived soft limit names its arithmetic: 5.7, 0.95x for a count, and for an mi.* floor the additive step, as in mi.original=20 → 20.855, +0.855 (0.05 × 17.1).

Everything else matches the run being predicted: exclude_tests, [check] exclude, in-source suppression markers, --changed-only, and the baseline all apply as usual. The one difference is that baseline-covered offenders are counted rather than dropped, which is what makes the already baselined / new split possible.

The preview replaces the gate, so it never fails: the exit code is 0 unless a tool error (exit 1) stops the run, and a one-line reminder of that goes to stderr. It conflicts with --write-baseline, --print-effective-config, --report-format, --output, and an explicit --summary-file <path> (the auto / never keywords are still accepted), each of which would produce a second, different artifact.

See Choosing thresholds for the rule this flag exists to make visible.

Untrusted-input mode (--strict)

Two default behaviors shrink the set of files the gate looks at, and both read their inputs from the tree under test: the generated-code detector skips any file whose header carries a marker phrase, and the walker honors .gitignore files found inside the walked tree. Both are right for a trusted checkout. In a pull-request gate they are levers the change under review can pull: one added comment line or one .gitignore entry takes a file out of the gate. The CI recipe shows both reproductions.

--strict turns both off in one flag, equivalent to passing --no-skip-generated --no-ignore:

bca check --strict --paths src/

Projects opt in once in bca.toml instead of per workflow:

[check]
strict = true

The key is presence-only, like the top-level exclude_tests: it can turn the profile on, never off, so a CI workflow cannot silently weaken a committed policy (there is no --no-strict).

Even without the profile, bca check reports what it declined to look at. Whenever either mechanism dropped files, a one-line stderr summary gives the counts:

bca: 2 files not checked (1 generated, 1 ignored) — pass --report-skipped to list them

Clean runs print nothing, and the summary changes no exit code. --report-skipped lists each dropped entry with a note: skipped (generated): <path>, note: skipped (ignored): <path>, or note: skipped (ignored directory): <path> line. Under --strict nothing is skipped, so there is nothing to summarize.

The counts come from the walk’s prune points: for every directory the walker entered, the gate checks which immediate children the ignore rules dropped. Only files a parser owns are counted, on both sides. An ignored log file, or a generated Cargo.lock (whose header matches @generated), would have been read and dropped rather than checked, and counting either would make the summary fire on every run. A file named explicitly on the command line bypasses ignore rules and is analyzed, so it is never counted.

Ignored directories are reported only under --report-skipped, as N ignored directories not walked entries, and are never entered. Nearly every checkout has an ignored build tree on disk, and enumerating a target/-sized tree would cost a full second traversal for a million-file count with no signal in it. The ignored tally reflects every source the walker honors (in-tree .gitignore and .ignore files, the global gitignore, and ancestor ignore files), so local and CI counts can differ.

With content sniffing off, generated trees still need an exemption. Give them one the reviewers own, either a committed walker deny-set (--exclude-from .bcaignore) or a [check.exclude] glob, rather than a marker the submitter controls.

Suppression markers

In-source suppression markers silence threshold violations without editing the offending function or excluding the file from the walk. Drop a marker in any comment in the source file and bca check treats the covered metrics as if they were within limits for that scope. Metric computation is unaffected: raw bca metrics output still reports every number. Suppression is a display concern. bca check drops the covered violations from the gate, and bca report markdown|html omits the covered functions from the matching hotspot tables by default (pass bca report --no-suppress for the raw audit view; see report).

Markers exist for the cases where editing the code is not an option: legacy modules awaiting rewrite, accepted exceptions documented in the comment, and migration from Lizard’s #lizard forgives convention. This page is the marker reference: the syntax, the metric names it accepts, where a marker may sit, and the flags that audit or bypass markers.

Native markers (bca:)

The native dialect uses the bca: namespace and the suppress verb, matching the project’s internal “suppression” vocabulary (SuppressionPolicy, FuncSpace::suppressed, --no-suppress). Four forms:

MarkerScopeEffect
bca: suppressEnclosing functionSuppress every metric
bca: suppress(metric, ...)Enclosing functionSuppress only the listed metrics
bca: suppress-fileFileSuppress every metric
bca: suppress-file(metric, ...)FileSuppress only the listed metrics

The two metric-list forms may carry a rationale: free text on the same line, after the marker itself:

#![allow(unused)]
fn main() {
// bca: suppress(nargs) — threaded context, not a god-function
}

No separator is required and none is privileged: — why, - why, : why, // why, and bare prose all read the same. The two bare verbs take no trailing text at all; bca: suppress see #123 is not a marker, whatever punctuation opens the trailing words. See Rationale text for why.

A function-scope marker attaches to the innermost FuncSpace (see the FuncSpace rustdoc) whose source range contains the comment. A function-scope marker outside every function body is silently ignored; for file-wide silencing use the explicit suppress-file verb. A file-scope marker may appear anywhere in the source; there is no “must be in first N lines” rule.

bca: suppress: function-scoped, all metrics (Rust)

#![allow(unused)]
fn main() {
// bca: suppress
fn legacy_dispatch(opcode: u8) -> Action {
    // dense match on every supported opcode
    match opcode { /* ... */ }
}
}

bca: suppress(metric, ...): function-scoped, listed metrics (Python)

def parse_token_stream(tokens):
    # bca: suppress(cognitive)
    # cognitive complexity is intrinsic to this state machine;
    # cyclomatic is still bounded.
    ...

Other thresholds (cyclomatic, halstead, loc, …) still apply.

bca: suppress-file: file-scoped, all metrics (JavaScript)

// bca: suppress-file
// Hand-tuned hot path; do not rewrite to satisfy thresholds.
function transform(input) { /* ... */ }
function validate(input) { /* ... */ }

bca: suppress-file(metric, ...): file-scoped, listed metrics (C++)

/* bca: suppress-file(halstead) */
// Halstead volume is inflated by the generated tables below; every
// other metric is still enforced file-wide.

Prefer a narrower tool first. Thresholds apply at each metric’s defined scope. A file-wide or impl-wide aggregate does not trigger a per-function limit, so muting a file-level halstead / nargs / nexits / nom total is no longer a reason to reach for suppress-file (#969). Use suppress-file when you intend to silence the metric throughout the file. To excuse one irreducibly complex function, use a function-scoped bca: suppress(...) inside it. To grandfather existing offenders without blinding the gate to future regressions, prefer a baseline entry, which keeps firing once a function gets worse than its recorded value.

Lizard compatibility markers

Two Lizard-style markers are recognized verbatim so existing Lizard-instrumented codebases need no rewrites:

Lizard markerScopeEquivalent native marker
#lizard forgivesEnclosing functionbca: suppress
#lizard forgive globalFilebca: suppress-file

The compatibility layer is intentionally narrow: only these two shapes are accepted. Other Lizard directives parse as ordinary comments. Lizard offers no per-metric scoping, so the native form’s bca: suppress(metric, ...) list has no Lizard analogue; every Lizard-style marker silences every metric.

Lizard’s GENERATED CODE marker is not handled here; it is part of the generated-code auto-skip mechanism (see Skipping generated code and the --no-skip-generated flag).

Native vs Lizard side by side

EffectNative formLizard form
Silence every metric for one function// bca: suppress// #lizard forgives
Silence one metric for one function// bca: suppress(cyclomatic)(no equivalent)
Silence every metric for the whole file// bca: suppress-file// #lizard forgive global
Silence one metric for the whole file// bca: suppress-file(halstead)(no equivalent)

Metric identifiers

The identifiers accepted inside bca: suppress(...) and bca: suppress-file(...) are:

abc, cognitive, cyclomatic, halstead, loc, mi, nargs, nexits, nom, npa, npm, wmc.

These match the threshold names and the JSON field names emitted on CodeMetrics, with one deliberate exclusion:

  • nexits is the canonical spelling: bca: suppress(nexits) silences a nexits threshold violation. The legacy exit alias was retired in #555 and is no longer accepted. Spelling it exit is an unknown identifier, which warns and is skipped; the recognized names beside it still suppress (see below).
  • tokens is a threshold-checkable metric (and a CodeMetrics JSON field) but is deliberately absent from the suppression list: a marker cannot turn it off. Treat tokens as a hard resource cap, not a maintainability heuristic.

Silencing a family (for example halstead) covers every sub-metric threshold under it (halstead.volume, halstead.effort, …); suppression vocabulary has no dotted form.

Unknown identifiers in a bca: suppress(...) list emit a stderr warning of the form

warning: path/to/file.rs:42: unknown metric 'no_such_metric' in bca suppression marker; known metrics: abc, cognitive, ...

Only the offending identifier is dropped: bca: suppress(cognitive, exit) still silences cognitive. Skipping can only shrink what a marker covers, so a typo never widens scope to a metric the author did not name. Rejecting the whole marker, which is what earlier releases did, would also discard valid metric names, leaving intended suppressions inactive.

Unknown verbs (anything other than suppress / suppress-file) and bodies that parse to no directive at all (an unbalanced parenthesis, a bare verb followed by any trailing text) produce the same shape of warning, and those do void the marker, since there is nothing left of it to honour. None of these are fatal: a typo in one file does not derail a workspace walk, and a doc comment that merely mentions the syntax does not fail your gate.

Rationale text

Text after a metric list is yours; bca parses past it and does not interpret it. The asymmetry between the two forms is deliberate:

  • After a metric list (bca: suppress(nargs) threaded context) anything goes. The parentheses are a positive signal that this comment is a marker, and the list has already stated the intent unambiguously, so whatever follows is prose.

  • After a bare verb (bca: suppress — irreducible dispatch) there is no trailing text. Nothing in this shape separates a rationale from a sentence about the feature, and no separator can: -, :, //, # and the dashes are exactly the punctuation someone writing // bca: suppress - we removed this marker, see #123 reaches for. Accepting them silences every metric in the enclosing function on the strength of a comment, so the whole shape warns instead.

    To record a reason, name the metrics (bca: suppress(cognitive, cyclomatic) — reason), which is more precise than the All hammer anyway. If All really is what you mean, put the reason on the line above the marker.

Where markers may appear

A marker is recognized inside any source comment, regardless of comment style. The scanner strips the following leading delimiter characters before matching: /, *, !, #, ;, -, and ASCII whitespace. That covers every comment shape bca parses today:

  • C-family line comments: // bca: suppress
  • C-family block comments: /* bca: suppress */
  • Rust inner doc comments: //! bca: suppress and /*! bca: suppress */
  • Python / shell / Ruby / Perl # comments: # bca: suppress
  • Lisp / Lua / SQL line comments: ;; bca: suppress, -- bca: suppress

Function-scope markers attach to the innermost Function-kind FuncSpace whose (start_line..=end_line) range contains the comment’s line. Markers buried in a class or struct body but outside every method are silently ignored; for class-wide silencing use bca: suppress-file or repeat the marker on each method.

File-scope markers are merged into the top-level Unit space and apply to every function in the file regardless of nesting.

Position the marker near the start of the comment. The scanner trims delimiter characters from both ends and then expects bca: (or #lizard) at the very front; markers buried deep in a multi-line block comment will not be recognized.

--no-suppress (CI auditing)

bca check --no-suppress ignores every suppression marker, native and Lizard alike, and reports every threshold violation in the walk. Use it in audit pipelines that need the raw, un-silenced offender list:

bca check --paths src/ --no-suppress

The flag has no effect on metric values themselves: raw bca metrics output always reports every number. bca report markdown|html honours markers in its hotspot tables by default and accepts its own --no-suppress flag for the same raw audit view.

Surfacing suppressed debt (--report-suppressed)

Suppression keeps an offender out of the gate, which also keeps it out of the --report-format document, so a suppressed module disappears from the code-scan report entirely. bca check --report-suppressed puts it back, as suppressed rather than active:

bca check --report-format sarif --no-fail --report-suppressed \
    --tier=soft=0.95 --output bca.sarif

Offenders silenced by an in-source marker or covered by the baseline are emitted into the SARIF document with a SARIF suppressions entry: kind: "inSource" for markers, kind: "external" for the baseline. The suppression never fails the gate (exit code and the human offender rows are unaffected); the suppressions entry lets downstream tooling tell suppressed debt apart from active offenders.

GitHub Code Scanning. GitHub renders a result that carries a suppressions entry as a suppressed (closed) alert, so the debt stays visible on the Security tab without failing the gate. If you only want active offenders to appear, omit --report-suppressed from the upload (this repo’s own Pages workflow does exactly that).

Notes:

  • Use SARIF to export suppressed offenders; other report formats emit only active offenders.
  • Match the tier to your baseline’s provenance (--tier=soft=0.95 for a soft-0.95 baseline) so offenders below the hard limit still appear.
  • Do not combine --report-suppressed with --no-suppress or --write-baseline.

Auditing exemptions (bca exemptions)

--no-suppress shows you the offenders a marker silences, but not the markers themselves; to find every silencer you previously had to diff a --no-suppress run against a normal one. bca exemptions replaces that workaround with a direct listing of everything the bca check gate skips, across all three exemption tiers, in one report:

TierGranularitySource
In-source markersper-function / per-filebca: suppress, #lizard forgives, …
[check.exclude] globsper-glob (categories of files)bca.toml [check] exclude / --check-exclude
Baseline entriesper-(path, symbol, metric).bca-baseline.toml
# List every exemption in the tree (in-source markers honour
# [walker.exclude] just like every other walking command).
bca exemptions --paths src/
# In-source markers (2)
  src/parser.rs:120  bca: suppress       metrics=all  parse_long
  src/lib.rs:1       bca: suppress-file  metrics=halstead  (whole file)

# [check.exclude] globs (1)
  tests/**

# Baseline (.bca-baseline.toml, 1 entry)
  src/markdown_report.rs write_language_section cognitive 29

The surrounding function (for function-scoped markers) gives scope context; file-scoped markers read (whole file), and a function-scoped marker written outside any function, which silences nothing, reads (no enclosing fn) so dead markers are visible.

Formats and section filters

--format markdown emits tables for PR comments; --format json nests all three tiers under a single suppressions envelope for dashboards and jq filtering:

bca exemptions --paths src/ --format json | jq '.suppressions.markers[] | select(.dialect == "lizard")'

In the JSON form an omitted section is null (not requested via a --*-only flag) while a requested-but-empty section is [], so filters can tell the two apart.

The mutually exclusive --markers-only / --excludes-only / --baseline-only flags narrow the report to a single tier for a specialised PR bot, such as one that only comments on newly added in-source markers. The baseline (bca.toml top-level baseline) and [check.exclude] ([check] exclude) inputs default to the same sources bca check reads, so the audit reflects exactly what the gate would skip; override the baseline with --baseline <path>.

The earlier --only-markers / --only-excludes / --only-baseline spellings remain as hidden aliases for one release cycle to keep existing PR-bot invocations working; prefer the --<section>-only forms, which match the diff-baseline section filters.

Unlike bca check, bca exemptions is informational and always exits 0 on success. It is a review surface, not a gate.

See also the Baselines recipe for using bca exemptions alongside bca diff-baseline during PR review.

JSON output

FuncSpace exposes the merged suppression scope as the optional suppressed field in its JSON output. When no marker applies to a space the field is elided so existing snapshot consumers see no change. When a marker fires the field carries one of two shapes:

{ "suppressed": { "kind": "all" } }
{ "suppressed": { "kind": "some", "metrics": ["cognitive", "loc"] } }

kind: all corresponds to a bare marker (bca: suppress, bca: suppress-file, or any Lizard-style marker). kind: some carries the explicit metric list from bca: suppress(...) / bca: suppress-file(...). Both shapes are stable serialization output suitable for dashboards and audit logs.

Migrating from Lizard

The compatibility layer means migration is incremental:

  1. Keep existing #lizard forgives and #lizard forgive global markers during migration; bca check recognizes them.
  2. Replace markers with bca: suppress(...) when you need per-metric scoping; the Lizard form silences every metric.

The project will keep the Lizard compatibility layer indefinitely; there is no removal date.

Reserved syntax

These shapes are reserved for future use and are not parsed today:

  • bca: suppress-next: silence the immediately following declaration rather than the enclosing function. Rejected today as an unknown verb; it will be promoted to first-class behavior in a future release without breaking existing markers.

The reason = "..." argument this section used to reserve is no longer planned. Write the rationale after the metric list instead (see Rationale text), which is the spelling authors already reach for. Inside the parentheses, reason = "..." is still an unknown metric identifier and is skipped with a stderr warning.

Nodes

The find, count, dump, and functions subcommands inspect the Abstract Syntax Tree (AST) that tree-sitter builds for a source file, rather than computing metrics from it. A node is one syntactic construct in that tree, and a node type is the grammar’s name for it (function_item, if_statement). Use these commands to locate constructs, count them, spot parse errors, or see how a grammar shapes a file.

Migrating? The verbs below replace the pre-restructure flag actions (-d, -f, --count, …). See the migration guide.

Error detection

To detect syntactic errors in your code, run:

bca find -t ERROR -I "*.ext" /path/to/your/file/or/directory
  • [PATHS]... / -p, --paths: file or directory to analyze (analyzes all files when given a directory). Paths are given positionally or via --paths; both are unioned. Flags follow the subcommand.
  • -t, --type: the node type to match. Repeat the flag for several types (-t function_item -t struct_item); at least one is required. A string value matches the node-type name exactly (for example function_item). A purely numeric value is instead interpreted as a raw tree-sitter kind_id and matches nodes whose internal symbol id equals that number; for example, -t 65535 matches the ERROR sentinel. Prefer -t ERROR for error detection. The numeric form is an escape hatch for grammar inspection and is unstable: a kind_id is an index into the grammar’s symbol table, so the same number names a different node after a grammar-version bump. Prefer the string form unless you specifically need a kind that has no stable name.
  • -I, --include: glob filter for selecting files by extension (*.js, *.rs). Each -I takes exactly one value, so a following positional path is never swallowed.

Reading find output

find prints a == <path> == banner for each file with a match, then one line per match in source order. The banner is printed even when --line-start / --line-end leave none of the file’s matches to print:

== src/main.rs ==
╰─ {call_expression:256} from (1, 13) to (1, 26) : f(g(1), h(2))
╰─ {call_expression:256} from (1, 15) to (1, 19) : g(1)
╰─ {call_expression:256} from (1, 21) to (1, 25) : h(2)

Each line gives the node type and its kind_id, then two 1-based (row, column) pairs: where the node starts, and the position just past its end. A node that fits on one row ends with its source text and one space; one that spans several rows prints none.

A match nested in another on the same row prints in full, as above, until the nesting passes 64 levels. From the 65th level down, a match prints … in place of each match nested in it, whose own line prints that text:

╰─ {call_expression:256} from (1, 141) to (1, 148) : f(…)
╰─ {call_expression:256} from (1, 143) to (1, 147) : f(1)

This keeps the output linear in the size of the file: printing every level in full made a file of 4,000 nested braces print 16 MB. The … is not escaped, so a … that is in the source reads the same. Real code rarely nests past 64 levels on one row: across the serde, pdf.js and DeepSpeech corpora the deepest is 42, in pdf.js under -t all.

Semantic filters

Six -t/--type values are not node-type names but semantic filters, resolved per language so that one spelling works across every grammar. They take precedence over an identically named node type, so a grammar with a literal call node cannot be matched by name; use its numeric kind_id for that.

  • all: every node, named and anonymous. bca count -t all therefore always reports 100%.
  • function: a named function, method or other callable declaration. Anonymous ones are excluded where the language distinguishes them: in the JavaScript family an arrow function or function expression bound to a name (const add = (a, b) => …) is a function match, while the same expression passed inline as a callback (xs.map(x => x * 2)) is a closure and is not.
  • call: a call site, meaning a function, method or command invocation a reader would navigate to. Object construction (new T(…)) and constructor delegation (C#’s : base(x) and a C# 12 primary constructor’s : Base(x), Java’s super(…), Kotlin’s superclass call) are deliberately excluded. They are counted by the ABC branches axis, whose Fitzpatrick rule is “function invocation or object creation”, a wider question than “where is this called”. Expect abc.branches to exceed a file’s call count in C#, Java, Groovy, Kotlin, C++, JavaScript, TypeScript, PHP, Ruby and Rust; see Where a constructor call lands. Each call matches once. In Perl, where the grammar parses a call’s name as a call of its own, foo(1) matches as the whole call only, and an auto-quoted hash key ((name => 1), $h{-name}), which it parses as a call too, does not match at all.
  • comment: a comment of any of the language’s forms.
  • string: a string literal. Type-annotation keywords that share the literal’s node name (TypeScript’s and PHP’s : string) are excluded.
  • error: a node that is or contains a parse error, so a match covers every ancestor of an ERROR node up to the root as well as the node itself. -t ERROR is the narrower spelling used above: it matches the error nodes alone, by name.

Counting nodes

Count occurrences of one or more node types with the count command:

bca count -t <NODE_TYPE> [-t <NODE_TYPE>...] -I "*.ext" \
    /path/to/your/file/or/directory

Printing the AST

To visualize the AST of a source file, use the dump command. It requires an explicit path, since a whole-tree AST dump is never useful:

bca dump /path/to/your/file/or/directory

Analyzing code portions

To analyze only a specific portion of the code, use the dump subcommand’s --line-start and --line-end options. For example, to print the AST of a single function from line 5 to line 10:

bca dump --line-start 5 --line-end 10 /path/to/your/file/or/directory

These flags are specific to dump and find, so they must follow the subcommand. The short --ls / --le spellings still work as deprecated aliases but are slated for removal in the next major.

Listing functions

For a list of every function or method and its line span, use:

bca functions /path/to/your/file/or/directory

REST API

bca-web serves the big-code-analysis library over HTTP. This page is the reference for a client author: every route, its request and response shape, the error envelope, and the status codes. For running the daemon (flags, environment variables, resource limits, and the trust boundaries to respect before exposing it) see Operating bca-web; for end-to-end curl examples see Driving the REST API.

The source-in-body endpoints remove comments, list function spans, return the tree-sitter syntax tree, and compute metrics. Three further endpoints compute change-history (VCS) metrics over a repository on the server’s filesystem.

Running the server

bca-web --host 127.0.0.1 --port 9090
  • --host: the address to bind (default 127.0.0.1).
  • --port: the TCP port (default 8080).
  • -j: the number of worker threads (optional).
  • --cors: enable CORS for browser-based tooling (off by default).

CORS

By default bca-web emits no CORS headers: a browser script served from a different origin cannot read the API’s responses. This keeps a local bca-web (the default 127.0.0.1 bind) from exposing its repository paths and metrics to any website the operator happens to be visiting.

Pass --cors to opt in. The argument is an explicit, comma-separated allow-list of origins; only those origins receive an Access-Control-Allow-Origin header, and the matched origin is echoed back verbatim (a request from any other origin gets no header and is blocked by the browser):

bca-web --cors https://app.example,https://tools.example

To answer every origin with Access-Control-Allow-Origin: *, pass a literal *:

bca-web --cors '*'

A wide-open * exposes the server’s metrics and repository paths to any origin, so use it only on trusted networks.

When CORS is enabled, a preflight OPTIONS request is answered 204 No Content with Access-Control-Allow-Origin, Access-Control-Allow-Methods (the resource’s own accepted methods, the same set the Allow header advertises), and Access-Control-Allow-Headers (echoing the request’s Access-Control-Request-Headers, or Content-Type, Accept for a bare probe). The API has no authentication or cookies, so Access-Control-Allow-Credentials is never sent.

API Versioning

All endpoints are mounted under a /v1 prefix (for example /v1/metrics). The full route set is /v1/ping, /v1/version, /v1/languages, /v1/ast, /v1/comment, /v1/function, /v1/metrics, /v1/vcs, /v1/vcs/trend, /v1/vcs/jit, and the route index /v1. To discover them programmatically, GET /v1 (see Route index).

The unprefixed paths (/metrics, /comment, /ast, /, …) that earlier 1.x releases served as deprecated aliases were removed in 2.0; requesting one now returns 404. Use the /v1 form everywhere.

Error responses

Errors are reported with an HTTP status code, not inside a 200 body. Every error (on the JSON endpoints, the raw/octet-stream endpoints, and the 415/405/404 fallbacks alike) returns one uniform machine-readable JSON body, so clients parse a single error shape regardless of the success content-type:

{
  "error": "human-readable message",
  "error_kind": "stable_machine_token",
  "id": "echoed-request-id"
}

error is the specific human-readable cause; error_kind is a stable snake_case machine token (unknown_field, unsupported_language, bad_request, parse_timeout, and so on) so clients branch on the cause without string-matching the prose (issue #631). The token vocabulary is closed and governed by STABILITY.md.

The id key is always present. It carries the client-supplied correlation id when the request had one (the JSON endpoints), and an empty string otherwise: the octet-stream / query endpoints carry no id, and the content-type / method / not-found fallbacks, like any request whose body failed to parse before the id was read, have no parsed id to echo.

Status codes:

  • 400 Bad Request: a malformed body or query parameter: invalid JSON, a missing required field, an unrecognised key (the strict deny_unknown_fields parse, including the removed unit flag; see Compute metrics below), or a scope value that is not full / file.
  • 422 Unprocessable Entity: the file_name extension (and content sniffing) maps to no supported language. The route matched and the body parsed; only the submitted entity cannot be processed. The response carries the stable machine token "error": "unsupported_language"; query GET /v1/languages for the supported set. (Before 2.0 this was a 404, indistinguishable from an unknown URL; see issue #634.)
  • 404 Not Found: the URL matches no endpoint.
  • 415 Unsupported Media Type: a known POST endpoint received a Content-Type that is neither application/json nor application/octet-stream (a charset parameter is allowed).
  • 405 Method Not Allowed: a known endpoint was called with the wrong HTTP method. The analysis endpoints accept POST; /v1/ping, /v1/version, /v1/languages, and /v1 accept GET and HEAD.
  • 406 Not Acceptable: the request Accept header named only media types the server cannot produce. The structured analysis endpoints serve application/json, application/yaml, and application/cbor; any other concrete type (application/xml, say) is a 406 carrying the not_acceptable token. See Content negotiation.
  • 413 Payload Too Large: the request body exceeded the server limit.
  • 500 Internal Server Error: metric computation or AST construction failed for an otherwise-valid request, or a /vcs history walk failed on the server side. This also covers a response too deeply nested to serialize (serialize_failed); see Nesting limits below.
  • 503 Service Unavailable: the parse pool is saturated by orphaned (timed-out) tasks; retry later.
  • 504 Gateway Timeout: the parse (or history walk) exceeded the server’s configured deadline.

Nesting limits

Responses are trees, and the server’s serializers recurse through nested response trees, one native stack frame per level. A deeply nested response could overflow the stack, which aborts the whole process and takes every in-flight request with it. So the server refuses to serialize past a fixed depth and answers 500 with the serialize_failed token:

ResponseLimitWhat counts as a level
/metrics128one nested function space (a function inside a function inside a …)
/ast512one AST node

Both limits sit far above anything real source reaches. Across the integration corpus under tests/repositories (TensorFlow, DeepSpeech, serde, and others) the deepest AST measured 188 nodes and the deepest function-space nesting 10 when the limits were set (#1056). The /metrics limit is also more generous than the read side: a JSON document of nested spaces cannot be parsed back past ~61 levels, because serde_json’s own 128-level recursion limit charges two levels per space.

Content negotiation

The structured analysis endpoints (/v1/ast, /v1/comment in its JSON variant, /v1/function, /v1/metrics, /v1/vcs, /v1/vcs/trend, and /v1/vcs/jit) choose their response serialization from the request Accept header, mirroring the CLI’s -O json|yaml|cbor outputs. The same value serializes byte-for-byte identically whether it comes from the CLI or the server.

Accept valueResponse Content-Type
absent, */*, application/*, application/jsonapplication/json
application/yaml (or text/yaml, application/x-yaml)application/yaml
application/cborapplication/cbor
any other concrete type406 Not Acceptable

Rules:

  • JSON is the default. Without an Accept header, the server returns JSON: the same body and Content-Type earlier releases always returned, so existing clients need no change. JSON entries and wildcards (*/*, application/*) make JSON a candidate.
  • q weights are honored. The supported candidate with the highest q weight wins (Accept: application/json;q=0.5, application/yaml;q=0.9 returns YAML), with ties resolved by list order; q=0 refuses a type.
  • Unsupported types are a 406, not a silent JSON fallback. The body is the uniform {error, error_kind, id} envelope with error_kind: "not_acceptable", and the message lists the supported media types.
  • Only structured serializations are offered. TOML and CSV are excluded: TOML is awkward for the deeply nested space tree and CSV is flat and tabular. The error-envelope body is always JSON and the /v1/comment octet-stream variant (raw byte-in / byte-out) is always raw bytes; neither negotiates. The introspection routes (/v1, /v1/version, /v1/languages) return JSON metadata only.
# YAML metrics
curl --silent \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/yaml' \
  --data '{"file_name": "foo.py", "code": "def f():\n    pass\n"}' \
  http://127.0.0.1:8080/v1/metrics

# CBOR metrics (binary; pipe to a decoder)
curl --silent \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/cbor' \
  --data '{"file_name": "foo.py", "code": "def f():\n    pass\n"}' \
  http://127.0.0.1:8080/v1/metrics --output metrics.cbor

Endpoints

1. Ping the server

Checks that the server is running.

Request:

GET http://127.0.0.1:8080/v1/ping

Response:

  • Status code: 200 OK
  • Body: empty.

Use curl -sf http://127.0.0.1:8080/v1/ping && echo ok to script a liveness check; -f makes curl exit non-zero on any HTTP error.

2. Remove comments

Removes comments from the submitted source. The endpoint accepts two Content-Type variants: application/octet-stream for raw byte-in / byte-out, and application/json for a JSON envelope.

Request:

POST http://127.0.0.1:8080/v1/comment

Payload:

{
  "id": "unique-id",
  "file_name": "filename.ext",
  "code": "source code with comments"
}
  • id: a correlation identifier for the request. Optional (issue #645); omitting it defaults to an empty string (treated as “no correlation id”).
  • file_name: the name of the file being analyzed.
  • code: the source code with comments.

Response (JSON variant):

{
  "id": "unique-id",
  "language": "cpp",
  "code": "print"
}

The response envelope reports id, the detected language (the canonical lowercase slug; see Compute metrics below), and the code result key. The code field is a string holding the stripped source: the request code arrived as a JSON string, so the stripped output is guaranteed valid UTF-8 and is handed back as a string, matching the request and every other JSON endpoint. The application/octet-stream variant returns the stripped source as the raw response body (no envelope), which suits binary-faithful round-trips and shell pipelines; its errors still use the uniform JSON error body above.

When the source contains no removable comments, both variants signal the empty result with a 200 status and an empty payload: the JSON variant returns "code": "" (an empty string) and the octet-stream variant returns an empty body. The status code and envelope shape are therefore identical regardless of the requested Content-Type; the octet-stream variant returns an empty 200 body rather than 204 No Content.

3. Retrieve function spans

Returns the line span of every function in the submitted source.

Request:

POST http://127.0.0.1:8080/v1/function

Payload:

{
  "id": "unique-id",
  "file_name": "filename.ext",
  "code": "source code with functions"
}
  • id: a correlation identifier for the request. Optional (issue #645); omitting it defaults to an empty string (treated as “no correlation id”).
  • file_name: the name of the file being analyzed.
  • code: the source code with functions.

Response:

{
  "id": "unique-id",
  "language": "cpp",
  "spans": [
    {
      "name": "function_name",
      "start_line": 1,
      "end_line": 10
    }
  ]
}

The envelope reports id, the detected language slug, and the spans result key. name is null when the parser could not resolve the function’s name from the AST (an anonymous or malformed definition). A null name is the malformed-span signal.

4. Retrieve the AST

Returns bca’s JSON representation of the parsed syntax tree, as a recursive node tree. The dump normalizes some node kinds and flattens some constructs, including string literals.

Request:

POST http://127.0.0.1:8080/v1/ast

Payload:

{
  "id": "unique-id",
  "file_name": "filename.ext",
  "code": "source code to parse",
  "comment": false,
  "span": true
}
  • id: a correlation identifier for the request. Optional; omitting it defaults to an empty string (treated as “no correlation id”).
  • file_name: the name of the file being analyzed.
  • code: the source code to parse.
  • comment: when true, comment nodes are omitted from the tree. Optional; defaults to false.
  • span: when true, each node carries its source span; when false, span is null. Optional; defaults to false.

id, comment, and span are optional and default as noted above (issue #645); file_name and code are required. Unknown keys are rejected with 400 (issue #633).

Response:

{
  "id": "unique-id",
  "language": "rust",
  "root": {
    "type": "source_file",
    "value": "",
    "span": { "start_line": 1, "start_col": 1, "end_line": 2, "end_col": 1,
              "start_byte": 0, "end_byte": 13 },
    "field_name": null,
    "children": [
      {
        "type": "function_item",
        "value": "",
        "span": { "start_line": 1, "start_col": 1, "end_line": 1, "end_col": 13,
                  "start_byte": 0, "end_byte": 12 },
        "field_name": null,
        "children": [
          {
            "type": "identifier",
            "value": "main",
            "span": { "start_line": 1, "start_col": 4, "end_line": 1, "end_col": 8,
                      "start_byte": 3, "end_byte": 7 },
            "field_name": "name",
            "children": []
          }
        ]
      }
    ]
  }
}

The envelope reports id, the detected language slug, and root, the root AST node. Each node carries:

  • type: the tree-sitter grammar node kind (grammar-specific; the language slug tells you which grammar produced it).
  • value: the source text for leaf/named tokens (empty for interior nodes).
  • span: a { start_line, start_col, end_line, end_col, start_byte, end_byte } object (lines and columns 1-based; byte offsets 0-based and half-open, indexing into the submitted code), or null when span was false. These span keys use the *_line vocabulary shared with /function and /metrics (issue #638 renamed the former *_row keys).
  • field_name: the tree-sitter grammar field through which the parent reaches this node (name, left, body, and so on), or null for the root, anonymous tokens, and unfielded children.
  • children: the node’s child nodes, recursively.

Unlike the metric and function endpoints, the AST endpoint reports node coordinates over the exact bytes the client submitted: the source is not EOL-normalised, so spans line up with the client’s own copy (issue #640).

5. Compute metrics

Computes the maintainability metrics for the submitted source.

Request:

POST http://127.0.0.1:8080/v1/metrics

Payload:

{
  "id": "unique-id",
  "file_name": "filename.ext",
  "code": "source code for metrics",
  "scope": "full"
}
  • id: a correlation identifier for the request. Optional (issue #645); omitting it defaults to an empty string (treated as “no correlation id”).
  • file_name: the filename of the source code file.
  • code: the source code to analyze.
  • scope: how much of the space tree to return. full (the default) returns the complete nested space tree: the file-level root plus a recursive spaces list for every function, class, and other unit. file returns only the file-level root with its spaces children cleared. This field replaces the pre-2.0 boolean unit flag (issue #638); sending the old unit key now fails with 400.

The payload is validated strictly: an unrecognised key (a typo, or the removed unit) is rejected with 400 and the uniform JSON error body, naming the offending field (issue #633).

On the application/octet-stream variant, the source is the raw request body and scope is supplied as a query parameter (?file_name=…&scope=full). When the parameter is omitted it defaults to full; an unrecognised value is rejected with 400.

Response:

{
  "id": "unique-id",
  "language": "rust",
  "root": {
    "name": "sample.rs",
    "start_line": 1,
    "end_line": 7,
    "kind": "unit",
    "spaces": [
      {
        "name": "double",
        "start_line": 1,
        "end_line": 7,
        "kind": "function",
        "spaces": [],
        "metrics": {
          "cyclomatic": { "sum": 2, "average": 2.0, "min": 2, "max": 2 },
          "loc": { "sloc": 7, "ploc": 7, "lloc": 1, "cloc": 0, "blank": 0, "dloc": 0 },
          "nom": { "functions": 1, "closures": 0, "total": 1 }
        }
      }
    ],
    "metrics": { "...": "the same metric block, aggregated over the file" }
  }
}

The response envelope reports id, the detected language slug, and root, the single file-level space object (issue #638 renamed this key from the misleading plural spaces). root carries name (the request file_name), the start_line / end_line span, a kind discriminator (unit, function, class, …), its metrics block, and a recursive spaces list of child units. The example above is trimmed: each real metrics block contains every metric family (cyclomatic, cognitive, halstead, loc, nom, nargs, nexits, tokens, mi, abc, and, on the file root and container spaces, wmc, npm, and npa) with the same nested fields the bca CLI emits. When scope is file, root.spaces is an empty list.

The language value is the canonical lowercase slug (rust, cpp, csharp, tsx), the same token the language vocabulary accepts, not a human-pretty display name. Every analysis endpoint (/ast, /comment, /function, /metrics) reports this language field so clients can confirm which grammar was selected.

6. Server and library version

Reports the running server version and the version of the big-code-analysis library it was built against.

Request:

GET http://127.0.0.1:8080/v1/version

Response:

{
  "server": "2.6.0",
  "library": "2.6.0"
}

7. Supported languages

Lists the supported languages and their registered file extensions. The names are the canonical lowercase slugs; the list and extensions are sourced from the library’s language table, never hardcoded.

Request:

GET http://127.0.0.1:8080/v1/languages

Response:

{
  "languages": [
    { "name": "cpp", "extensions": ["cpp", "cc", "hpp", "..."] },
    { "name": "rust", "extensions": ["rs"] }
  ]
}

Like every endpoint, /version and /languages are served only under the /v1 prefix; the unprefixed 1.x aliases were removed in 2.0 (see API Versioning).

8. Route index

Returns a machine-readable index of every registered route (its path, the HTTP methods it accepts, and a one-line description) so clients can discover the API surface without scraping this chapter. The index is generated from the same route table the server registers, so it cannot drift from the live routing.

Request:

GET http://127.0.0.1:8080/v1

Response:

{
  "service": "bca-web",
  "version": "2.6.0",
  "routes": [
    { "path": "/v1", "methods": ["GET", "HEAD"], "description": "This route index." },
    { "path": "/v1/metrics", "methods": ["POST"], "description": "Compute maintainability metrics for the source." }
  ]
}

service is always bca-web; version matches the server field of GET /v1/version. The unprefixed root / that earlier releases served as this endpoint’s alias was removed in 2.0.

Change-history (VCS) metrics

Three endpoints expose the change-history (version-control) metrics, the same numbers bca vcs computes from the CLI. Unlike every other endpoint, these analyse a git repository already present on the server’s filesystem rather than source code carried in the request body: VCS metrics derive from commit history, which has no in-request representation.

Operator warning: repo_path is a trust boundary. The repo_path field is a server-side filesystem path. These endpoints make the server walk any git repository it can read and return that repository’s relative file paths, churn, and author signals. This is materially different from the source-in-body endpoints, which only ever see code the client sends. The optional cache_dir field is a second caller-supplied server-side path that grants a write capability: with caching enabled (the default), the server creates directories and writes JSON cache files under it (<cache_dir>/<repo>/<head_sha>.json), so a caller controlling cache_dir can direct the server to write cache files at any path the server process can write to. (cache_dir is accepted only by /vcs; /vcs/trend and /vcs/jit do not cache.) The endpoint’s filesystem reach is therefore an arbitrary read of any readable git repository and an arbitrary write of cache files under any writable path. Do not expose /vcs, /vcs/trend, or /vcs/jit to untrusted clients without an authorization layer in front of bca-web. The default 127.0.0.1 bind keeps them local. Each walk runs under the same parse-timeout and blocking-pool guard as the analysis endpoints.

All three endpoints are POST-only, accept application/json, echo the request id, and report errors with the uniform {error, error_kind, id} body; its error_kind tokens are the vcs_* family (vcs_not_a_repository, vcs_invalid_window, and so on). A client mistake is a 400: a repo_path that does not exist or is not a git working tree (both carry vcs_not_a_repository), an unresolvable ref/commit, a malformed or non-diff diff, an unusable author_hash_key (vcs_invalid_author_hash_key), or a malformed window / timestamp / formula / file-type / threshold / trend parameter. A failure of the history walk itself is a 500. A nonexistent repo_path is a typo, the most common client error here, so it answers 400 like a path that exists but is not a repository, not a 500 (issue 653).

9. Rank files by risk: /vcs

Walks the repository’s history once and returns its files ranked by a composite risk score (issue #328).

Request:

POST http://127.0.0.1:8080/v1/vcs

Payload:

{
  "id": "unique-id",
  "repo_path": "/srv/repos/my-project"
}

repo_path is required; every other field is optional and defaults to the bca vcs default. id is optional too (issue #645); omitting it defaults to an empty string, echoed back unchanged. The optional fields are:

  • long_window / recent_window: window specs such as 12mo or 90d. Defaults 12mo / 90d.
  • top: keep only the top N files by risk. Absent defaults to 50 (the bca vcs --top default); an explicit 0 returns all files.
  • ref: revision to analyse (default HEAD).
  • risk_formula: weighted (default) or percentile.
  • file_types: metrics (default: only files bca has metrics for), all (every tracked text file), or a comma-separated extension allow-list (rs,py).
  • full_history: walk the full DAG rather than first-parent only.
  • include_merges: include merge commits.
  • follow_renames: follow renames (default true).
  • exclude_bots: exclude bot identities (default true).
  • bot_pattern: override the bot-author exclusion regex.
  • as_of: reference “now” (RFC 3339 / @unix / git date) for snapshots.
  • emit_author_details: emit SHA-256-hashed author identities.
  • author_hash_key: secret key that hardens emit_author_details into a keyed HMAC-SHA256 (requires emit_author_details; an empty key or one without the flag is a 400).
  • include_deleted: include files deleted at the target ref.
  • bus_factor_threshold: bus-factor coverage threshold in (0, 1) (default 0.5).
  • no_cache: disable the persistent change-history cache for this request (default false).
  • cache_dir: override the server-side cache directory.

Response:

{
  "id": "unique-id",
  "vcs_schema_version": 2,
  "risk_score_version": 2,
  "long_window_days": 365,
  "recent_window_days": 90,
  "truncated_shallow_clone": false,
  "vcs_aggregate": { "...": "directory- / repo-level bus factor" },
  "files": [
    {
      "path": "src/main.rs",
      "vcs": {
        "commits_long": 12,
        "commits_recent": 3,
        "churn_long": 540,
        "churn_recent": 80,
        "authors_long": 4,
        "authors_recent": 2,
        "risk_score": 1.42
      }
    }
  ]
}

files is ordered by descending vcs.risk_score. Each entry carries the repository-relative path plus a nested vcs metric block (the same shape bca vcs emits, issue #684): commit and churn counts over the long and recent windows, author counts, ownership share, burst, bug-fix / security-fix / revert counts, age, change and co-change entropy, and the composite risk_score. hotspot_score and the hashed author_ids appear inside that block only when computable / requested. The four constant stamps vcs_schema_version, risk_score_version, long_window_days, and recent_window_days sit once at the top level, never per row (issue #635). vcs_aggregate carries the directory- and repo-level bus factor (issue #332).

10. Historical trend: /vcs/trend

Samples the change-history metrics at several evenly-spaced points in time and returns the per-file time series (issue #333). Its response is a series, not a ranked snapshot, so it is a distinct route from /vcs.

Request:

POST http://127.0.0.1:8080/v1/vcs/trend

Payload: every /vcs field above except the cache controls (no_cache / cache_dir), plus the three below. Trend does not use the persistent cache, so sending either cache field is a 400 (issue #961) rather than a silent no-op.

  • points: number of evenly-spaced sample points (>= 2). Defaults to 12 (the bca vcs trend --points default) when omitted.
  • span: total look-back the points cover (default 12mo).
  • top_deltas: top N files per improving / regressing list. Absent defaults to 10; an explicit 0 returns all.

Response:

{
  "id": "unique-id",
  "trend_schema_version": 1,
  "vcs_schema_version": 2,
  "risk_score_version": 2,
  "long_window_days": 365,
  "recent_window_days": 90,
  "truncated_shallow_clone": false,
  "as_of_points": [1704067200, 1711929600],
  "files": {
    "src/main.rs": [ { "as_of": 1704067200, "vcs": { "risk_score": 1.1 } }, null ]
  },
  "deltas": { "improved": [], "regressed": [] }
}

as_of_points lists the sample timestamps oldest-first. Each file’s array in files aligns to it 1:1, with a null element at a point where the file did not yet exist; each present element is { "as_of": ..., "vcs": { ... } }, with that file’s VCS block nested under vcs at that moment (issue #684). The four constant stamps sit once at the top level, never per point (issue #635). deltas ranks the most-improved and most-regressed files by their risk-score movement across the series.

11. Just-in-time risk: /vcs/jit

Scores the just-in-time risk of a single change: either one commit on a server-side repository, or an arbitrary unified diff carried in the request body (issues #331 / #580). The two modes are mutually exclusive.

Commit mode scores a commit on repo_path:

{
  "id": "unique-id",
  "repo_path": "/srv/repos/my-project",
  "commit": "HEAD"
}

Commit mode also accepts the experience-window knobs long_window, recent_window, full_history, include_merges, follow_renames, and as_of. The response is a full report whose source is "commit" and whose risk_score folds in all five feature groups (size, diffusion, history, experience, purpose):

{
  "id": "unique-id",
  "jit_schema_version": 3,
  "jit_score_version": 1,
  "source": "commit",
  "long_window_days": 365,
  "recent_window_days": 90,
  "risk_score": 0.87,
  "commit": { "id": "…", "parent_count": 1, "is_merge": false, "purpose": {} },
  "features": { "size": {}, "diffusion": {}, "history": {}, "experience": {} },
  "contributions": { "size": 0.4, "diffusion": 0.2, "history": 0.1, "purpose": 0.0, "experience": -0.1 }
}

Diff mode scores an arbitrary unified diff with no repository:

{
  "id": "unique-id",
  "diff": "--- a/x\n+++ b/x\n@@ -1 +1 @@\n-old\n+new\n"
}

A bare diff carries no author, parent, or history, so only the size and diffusion groups are computable. The diff report’s source is "diff" and it reports partial_risk_score, not risk_score, because the missing groups are absent from the body entirely, never present as zero:

{
  "id": "unique-id",
  "jit_schema_version": 3,
  "jit_score_version": 1,
  "source": "diff",
  "partial_risk_score": 0.6,
  "size": {},
  "diffusion": {},
  "contributions": { "size": 0.4, "diffusion": 0.2 }
}

Branch on the source discriminator ("commit" vs "diff") to read the right score field. partial_risk_score sums only the size and diffusion contributions, while a commit’s risk_score also folds in history, purpose, and the negatively signed experience group, so the two are neither ordered nor on the same scale: rank diffs against other diffs, never against commit scores.

Mode conflict. Supplying diff together with any commit-mode field (repo_path, commit, a window, history, rename, or as_of knob) is rejected with a 400 rather than silently honouring the diff and dropping the rest. The two modes answer different, non-comparable questions, so the combination is treated as a client mistake (issue number 632).

Non-diff diff. A diff value that is not a git unified diff (the wrong field, an accidentally-mangled string, arbitrary text) is rejected with a 400 (vcs_invalid_diff) rather than scored as a confident partial_risk_score of 0.0. On a risk-gating endpoint a spurious “zero risk” is the most dangerous failure mode, so non-diff input is a hard error (issue 652). An empty or whitespace-only diff is the one exception: it legitimately means “no changes”, so it still returns a valid 0.0, and a CI step that computed an empty diff gets the zero-risk answer it expects.

Operating bca-web

bca-web is the HTTP daemon that wraps the big-code-analysis library, exposing comment removal, function spans, AST dumps, maintainability metrics, and change-history (VCS) metrics over a REST API. This page is for the operator running the daemon: how to build and start it, which flags and environment variables tune it, and the trust boundaries to respect before exposing it.

Use the REST API reference for request and response fields, and Driving the REST API for complete curl examples. This page covers the process itself and links to those two rather than repeating them.

Build and run

bca-web is the binary of the big-code-analysis-web crate. From a checkout, run it through Cargo:

cargo run -p big-code-analysis-web -- --host 127.0.0.1 --port 8080

To install the binary on your PATH, build a release artifact and copy it out, or install from the crate:

cargo install big-code-analysis-web   # installs the `bca-web` command
bca-web --host 127.0.0.1 --port 8080

bca-web binds the requested address, serves until interrupted, and exits non-zero if it cannot bind the port or hits an I/O error, so a supervisor (systemd, a container orchestrator, or a CI smoke check) sees the failure and can restart or alert.

Building with a subset of languages does not work

The shipped bca-web binary compiles every supported tree-sitter grammar in. The big-code-analysis-web crate pins the library’s all-languages feature set explicitly, so passing --no-default-features or a custom --features list to cargo build -p big-code-analysis-web does not drop grammars from the resulting binary. Dropping a grammar silently from a user-facing daemon would surface as “language X stopped working” at request time rather than as a build error, so the crate forbids it (issue #252).

If you need a reduced grammar set, embed the big-code-analysis library in your own Rust code and select features in your own Cargo.toml. The per-language Cargo features chapter lists every feature with a worked example.

Command-line flags

The full flag set, with defaults:

FlagDefaultPurpose
-j, --num-jobs <N|auto>autoWorker-thread count. auto resolves to the OS-reported effective CPU count.
--host <HOST>127.0.0.1Address to bind.
-p, --port <PORT>8080TCP port.
--parse-timeout-secs <SECS>30Per-parse deadline. 0 disables it.
--cors <ORIGINS>offEnable CORS for a comma-separated origin allow-list.
-h, --helpPrint help and exit.
-V, --versionPrint version and exit.

--num-jobs auto is cgroup-quota- and cpuset-aware on Linux: in a container with a CPU quota it resolves to the quota rather than the host’s physical core count, matching the bca CLI’s --num-jobs. This count sizes the worker pool and the parse-admission semaphore, so it caps how many parses run concurrently. The minimum is 1; 0 is rejected when the flag is parsed.

--parse-timeout-secs bounds how long a single parse may run before the request returns 504 Gateway Timeout. The default of 30 guards against a pathological input wedging a worker indefinitely. Setting it to 0 removes the deadline and, with it, the load-shedding described below; use 0 only when an unbounded parse is acceptable. See the REST API reference for the response body the timeout returns.

CORS is off by default. The CORS section of the reference documents it in full, covering preflight handling, the wildcard form, and the absence of credentials. The short version: pass --cors with an explicit origin allow-list to let browser tooling read responses; omit it to emit no Access-Control-* headers at all.

Environment variables

VariableDefaultPurpose
BCA_MAX_ORPHANED_TASKSmax(num_jobs * 2, 4)Cap on orphaned (timed-out but still-running) parse tasks before new requests are shed with 503.
RUST_LOGinfoLog filter for the tracing subscriber.

RUST_LOG uses the EnvFilter syntax (for example RUST_LOG=big_code_analysis_web=debug). The daemon emits one access-log line per completed request, carrying the method, route, status, and latency.

Resource limits and back-pressure

Two limits protect the daemon from a single client exhausting it.

Request body size. Every endpoint rejects a request body larger than 4 MiB with 413 Payload Too Large. The limit applies uniformly to the JSON and raw-octet-stream content types, so both reject oversized bodies at the same threshold.

Orphaned-task admission control. When a request exceeds --parse-timeout-secs, it returns 504 and its blocking task is asked to stop. The parse and the AST export check for that as they run, so they end soon after the deadline; other analysis already under way, such as a /vcs history walk, runs to completion. Until the task ends it counts as orphaned. To stop sustained pathological input from piling up unbounded background work, new requests are rejected with 503 Service Unavailable once the count of orphaned tasks reaches a soft cap. The cap defaults to max(num_jobs * 2, 4); BCA_MAX_ORPHANED_TASKS overrides it (parsed as an unsigned integer; an invalid or zero value falls back to the default). Setting --parse-timeout-secs 0 disables this mechanism entirely, since with no deadline no task is ever orphaned.

A request’s work is also asked to stop when its connection is reset before the client has closed its sending side. An ordinary close is different: HTTP/1 lets a client close its sending side and still read the response, so the server reads nothing more from that connection, not even a reset that follows, and keeps working on the request until it answers or the deadline passes. With --parse-timeout-secs 0 there is no deadline, so that work runs to completion.

Security and trust boundaries

bca-web has no authentication, authorization, or rate limiting of its own. The defaults are chosen for a local, single-operator deployment; widen them deliberately.

Default bind is loopback. The server binds 127.0.0.1 unless --host says otherwise. Keep it there, or put an authenticating proxy in front, before exposing it to a network. Binding 0.0.0.0 makes every capability below reachable by anyone who can route to the port.

CORS is off by default. With no --cors flag, a browser script from another origin cannot read API responses, so a page the operator happens to visit cannot quietly drive a loopback bca-web. The wildcard form (--cors '*') answers every origin and exposes the server’s metrics and repository paths to any site; use it only on trusted networks. Full semantics are under CORS.

The VCS endpoints read server-side repositories. Unlike the source-in-body endpoints, /v1/vcs, /v1/vcs/trend, and /v1/vcs/jit analyze a git repository already on the server’s filesystem, named by the request’s repo_path. A caller who can reach these endpoints can make the server walk any git repository it can read and learn that repository’s file paths, churn, and author signals. The VCS trust-boundary warning in the reference covers this in full; do not expose these endpoints to untrusted clients without an authorization layer.

The VCS cache directory is a client-controlled write path. The VCS endpoints accept an optional cache_dir field that overrides where the persistent change-history cache is written, defaulting to the platform cache location ($XDG_CACHE_HOME/big-code-analysis/vcs). A caller who can set cache_dir chooses a directory the server process writes into, so an untrusted client could direct cache writes to an attacker-chosen path. This is one more reason the VCS endpoints belong behind an authorization layer, never open to untrusted input.

Recipes

Task-oriented examples for getting work done with bca and bca-web. Each recipe assumes you have built the binaries (cargo build --release) and that bca is on your PATH.

The recipes are grouped by goal:

  • Quality reports: generate Markdown reports for pull requests, dashboards, or wikis, including the C/C++ preprocessor-aware workflow.
  • CI integration: wire bca check and bca report into GitHub Actions and GitLab CI, including the baseline / ratchet pattern and the Code Quality widget path.
  • Baselines: record existing offenders in a .bca-baseline.toml so the gate only fires on new or worsened violations, and audit or diff that baseline during review.
  • Local threshold gates: mirror the CI threshold gate on a developer machine with a two-tier (hard + headroom) Makefile / just / pre-commit pattern, so regressions never reach the pull request.
  • Choosing thresholds: learn where the shipped defaults come from, choose language-specific overrides, and select limits for CI, agent feedback, legacy triage, or safety-critical work.
  • Feeding metrics to an agent: wire bca check into an agentic coding tool’s after-edit feedback loop (Claude Code PostToolUse hook, OpenCode plugin), with the anti-gaming guidance that keeps the loop honest.
  • AST queries: search for syntactic constructs, count node types, dump trees, and detect parse errors.
  • Exporting metric data: emit structured output (JSON / YAML / TOML / CBOR) and consume it from shell pipelines.
  • Driving the REST API: run the HTTP server and call every endpoint with curl.

For a deeper look at any flag the recipes use, see the per-command pages under Commands. For the full list of metrics that show up in these recipes, see Supported Metrics.

Upstream reference. big-code-analysis is a fork of Mozilla’s rust-code-analysis. Recipes that work for the upstream rust-code-analysis-cli binary usually translate directly: replace the binary name and adjust for the subcommand restructure documented in the migration guide.

Quality reports

Recipes for producing aggregated, human-readable Markdown reports.

Wiring reports into CI? See the CI integration recipe for runnable GitHub Actions and GitLab CI examples that post the Markdown report as a PR/MR comment and surface threshold violations through the platform’s native code quality widgets.

Live example reports

big-code-analysis publishes the output of bca report -O markdown --vcs and bca report -O html --vcs against its own source tree on every push to main. Open either to see what the recipes on this page produce on a multi-language Rust + Python codebase:

The wiring that produces them lives in .github/workflows/pages.yml. The same workflow runs the threshold gate; see CI integration for the full pipeline shape.

Generate a project-wide quality report

Run from the project root and write the report to a file:

bca report \
    --paths "$PWD" \
    -O markdown \
    --top 20 \
    --strip-prefix "$PWD/" \
    --output report.md
  • --strip-prefix keeps the file paths short and stable across machines. Without it every row carries the absolute path of the current checkout.
  • --top controls how many rows appear in each hotspot table. 20 is a reasonable default for a PR comment; drop to 5 for a dashboard tile, or pass 0 to list every row.
  • --jobs defaults to the effective CPU count (cgroup-/cpuset-aware on Linux); pass --jobs 1 only to force serial mode for debugging.

Limit the report to specific languages

bca infers language from extension, so the include/exclude globs do the filtering:

bca report \
    --include "*.rs" --include "*.py" \
    --paths "$PWD" \
    -O markdown --output report.md

To exclude vendored or generated trees, layer in --exclude:

bca report \
    --include "*.rs" \
    --exclude "**/target/**" --exclude "**/vendor/**" \
    --paths "$PWD" \
    -O markdown

Flag arity. --include and --exclude take exactly one glob per occurrence; repeat the flag for additional patterns. The = form works the same way: --include="*.rs" --exclude="**/target/**".

Leading ./ is optional. A bare-relative pattern and its ./-prefixed spelling are equivalent: --exclude "vendor/**" matches exactly what --exclude "./vendor/**" does. This holds for every glob surface: --include, --exclude, --exclude-from, .bcaignore, and the [check.exclude] gate-exemption set.

For a stable repo-wide deny-set, keep the patterns in a file at the repo root (a .bcaignore by convention) and load it with --exclude-from. Patterns are unioned with any inline --exclude values; blank lines and #-prefixed comments are skipped:

bca report \
    --paths . \
    --exclude-from .bcaignore \
    -O markdown --output report.md

Show only the worst offenders

For a quick triage view that highlights the top three problems per section:

bca report -p src/ -O markdown --top 3

The report still includes every section, but each table is short enough to scan at a glance.

Compare two revisions

Aggregate reports do not diff revisions on their own. Run the report on each side and diff the Markdown:

work=$(mktemp -d "${TMPDIR:-/tmp}/bca-report-diff.XXXXXX")
git worktree add "$work/before" main
bca report -p "$work/before" -O markdown \
    --strip-prefix "$work/before/" --output "$work/before.md"

bca report -p "$PWD" -O markdown \
    --strip-prefix "$PWD/" --output "$work/after.md"

diff -u "$work/before.md" "$work/after.md" | less

Because both reports use the same --strip-prefix shape, the path columns line up and the diff shows metric changes rather than path noise.

C/C++ preprocessor-aware reports

For macro-heavy C/C++ codebases, feed preprocessor data into the analyzer so conditional compilation is interpreted the way the compiler sees it. The workflow is two steps:

# 1. Build a preprocessor-data JSON from the headers and sources.
bca preproc \
    --paths src/ include/ \
    --output /tmp/preproc.json

# 2. Run the report (or any other command) with that data attached.
bca report \
    --paths src/ \
    --preproc-data /tmp/preproc.json \
    -O markdown --output report.md

Every metric-computing walking subcommand (metrics, ops, functions, report, check, …) accepts --preproc-data. Subcommands that do not consume it (vcs, preproc, list-metrics, diff-baseline) reject it as a usage error.

Step 1’s document is deterministic. The files map is written in sorted path order, and each entry’s direct_includes, indirect_includes, and macros arrays in sorted name order, so two bca preproc runs over an unchanged tree produce byte-identical output, on stdout as well as through --output. Check /tmp/preproc.json into a build cache or diff it between branches without spurious churn, as you can a metrics --output or ops --output document.

You need the sorting rules only when reproducing the output order. The files keys sort by path component, so a/x.h precedes a-b/x.h. Include and macro arrays sort by bytes, so a-b/x.h precedes a/x.h. You do not need to reproduce either sort when reading the document; jq preserves the stored order.

Analyze only files changed in a PR

Pipe a list of changed files into --paths-from - to score just the diff, not the whole tree:

git diff --name-only --diff-filter=AM origin/main...HEAD \
    | bca metrics --paths-from - -O json --output-dir ./out
  • --diff-filter=AM keeps Added and Modified files and drops Deletions, since a file that no longer exists cannot be analyzed.
  • --paths-from - reads newline-separated paths from stdin. A file argument works the same way: --paths-from changed.txt.
  • Paths fed in this way are treated as explicit, so they bypass any .gitignore rule that would have hidden them in a directory walk. Combine with -I '*.py' -I '*.rs' to filter by language (repeat the flag once per glob).

For a PR-scoped Markdown summary, swap metrics for the report pipeline:

git diff --name-only --diff-filter=AM origin/main...HEAD \
    | bca report --paths-from - -O markdown \
        --top 10 --output pr-report.md

A directory walk honors .gitignore automatically, so the recipes earlier on this page need no explicit -X "**/target/**" -X "**/node_modules/**" when your project’s .gitignore already covers those paths. Add --no-ignore to analyze gitignored trees.

CI integration

Recipes for wiring bca into a build pipeline. The bca check command ships every output shape a CI system consumes (Checkstyle, SARIF, GitLab Code Climate JSON, clang/GCC warning lines, MSVC warning lines), plus bca report markdown for humans. This page maps each goal to the subcommand, flags, and platform glue that serve it. Copy-paste snippets come first; the design notes behind them (live worked example, self-scan gate, trust boundary) come after.

Picking outputs

The matrix below maps each common goal to the bca invocation that feeds the corresponding CI surface. The linked sections have the runnable example.

GoalCommand + flags
Hard gate on threshold regressionsbca check (thresholds from the auto-discovered bca.toml)
Ratchet thresholds on an existing codebasebca check --baseline .bca-baseline.toml (‡)
Inline PR annotations (GitHub)bca check … --report-format clang-warning --no-fail + GCC problem matcher
Code Scanning alerts (GitHub)bca check … --report-format sarif --no-fail + github/codeql-action/upload-sarif
Merge-request widget (GitLab Code Quality)bca check … --report-format code-climate --no-fail
Jenkins / SonarQube ingestionbca check … --report-format checkstyle
Human-readable PR/MR comment or downloadablebca report -O markdown --top 20 --strip-prefix "$PWD/"
Machine-readable artifact for dashboardsbca metrics --format json --output-dir ./out

(‡) Recommended adoption path when introducing thresholds on a codebase with existing offenders. See the Baselines recipe for the bootstrap-refresh-retire workflow.

The full reference for bca check’s output formats, exit codes (0 clean, 2 violation, 1 tool error), and threshold config lives in the Check command page. For the Markdown report shape, see the Report command page and the Quality reports recipe.

GitHub Actions

For a reproducible install without a Rust toolchain, use the prebuilt tarball from this repository’s GitHub Releases. It is a single curl | sha256sum | tar and produces byte-identical binaries across runs. Pair it with actions/cache keyed by version so a green-path rerun skips the download entirely:

CLI-artifact schema compatibility. The BCA_VERSION you pin here must support the schema version of every CLI artifact your repo commits, above all .bca-baseline.toml (which carries its own version field) and the bca.toml manifest. A baseline file written by a newer bca (carrying a newer schema version) is not loadable by an older bca, and the gate fails with baseline version N is not supported by this bca. When tracking main or regenerating baselines locally with a newer bca, either re-pin to a release that covers the new schema or switch to a cargo install --git build of bca pointed at the same commit your baseline was written from (see the cargo install alternative below). The compatibility contract is recorded in STABILITY.md.

env:
  BCA_VERSION: "2.6.0"
  BCA_TARGET:  "x86_64-unknown-linux-gnu"
  # sha256 of big-code-analysis-${BCA_VERSION}-${BCA_TARGET}.tar.gz from the
  # release's SHA256SUMS file. Bump together with BCA_VERSION.
  BCA_SHA256:  "53df050fad1e9bc9081fbe89bb216db7631633d4a741f5ea2e9c322ae9cc0864"

steps:
  # Cache key MUST include BCA_SHA256 (and BCA_TARGET). Without the
  # sha256 in the key, rotating the published checksum without bumping
  # the version returns a stale binary on cache hit and silently
  # bypasses the `sha256sum --check` in the install step (which is
  # gated on cache miss). Including BCA_TARGET matters when the same
  # workflow runs against multiple `runs-on`.
  - name: Cache bca binary
    id: bca-cache
    uses: actions/cache@v5
    with:
      path: ~/.local/bin/bca
      key: bca-${{ runner.os }}-${{ env.BCA_TARGET }}-${{ env.BCA_VERSION }}-${{ env.BCA_SHA256 }}

  - name: Install bca from GitHub Releases
    if: steps.bca-cache.outputs.cache-hit != 'true'
    run: |
      set -euo pipefail
      stage="big-code-analysis-${BCA_VERSION}-${BCA_TARGET}"
      tarball="${stage}.tar.gz"
      url="https://github.com/dekobon/big-code-analysis/releases/download/v${BCA_VERSION}/${tarball}"
      mkdir -p "$HOME/.local/bin"
      curl -fsSL --proto '=https' --tlsv1.2 -o "/tmp/${tarball}" "$url"
      echo "${BCA_SHA256}  /tmp/${tarball}" | sha256sum --check --strict -
      tar -xzf "/tmp/${tarball}" -C /tmp
      install -m 0755 "/tmp/${stage}/bca" "$HOME/.local/bin/bca"
      rm -rf "/tmp/${tarball}" "/tmp/${stage}"

  - name: Prepend ~/.local/bin to PATH
    run: echo "$HOME/.local/bin" >> "$GITHUB_PATH"

Available BCA_TARGET values (pick the one that matches runs-on): x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl, aarch64-unknown-linux-gnu, aarch64-unknown-linux-musl, aarch64-apple-darwin, x86_64-pc-windows-msvc, aarch64-pc-windows-msvc. Windows assets use .zip instead of .tar.gz; the bca-web binary ships alongside bca in the same archive.

Alternative: cargo install via prebuilt-aware actions

When a runner cannot reach github.com (air-gapped, custom mirror) but can reach crates.io, the two actions below fall back to cargo install when no prebuilt is published, at the cost of compile time on the cold path. Both pin to the same crates.io release as the GitHub Releases assets, so the CLI-artifact schema compatibility warning applies here unchanged.

If you need a bca ahead of the latest crates.io release (for example, your .bca-baseline.toml is committed at a newer schema than any published bca understands), swap the tool: big-code-analysis-cli@<version> or --version form for cargo install --git https://github.com/dekobon/big-code-analysis --rev <SHA> --locked big-code-analysis-cli against the exact commit the baseline was generated from. This is what the in-tree pages.yml workflow does (against the local checkout via --path). It is a deliberate workaround for bca’s own repo, not a recommended default for downstream adopters.

Use taiki-e/install-action or cargo-binstall.

# Option 1: taiki-e/install-action
- name: Install bca
  uses: taiki-e/install-action@v2
  with:
    tool: big-code-analysis-cli@2.6.0
# Option 2: cargo-binstall
- name: Install cargo-binstall
  uses: cargo-bins/cargo-binstall@main
- name: Install bca
  run: cargo binstall --no-confirm big-code-analysis-cli --version 2.6.0

If either action falls back to compilation, cache the cargo registry and the installed binary so the second run is fast:

- name: Cache cargo registry and bca binary
  uses: actions/cache@v5
  with:
    path: |
      ~/.cargo/registry
      ~/.cargo/git
      ~/.cargo/bin/bca
    # crates.io publishes immutable releases, so a `<version>` key is
    # sufficient here; there is no sha256 to rotate. (The GitHub
    # Releases install path above is different: republished release
    # assets share a version, so its cache key must include the sha256.)
    key: bca-${{ runner.os }}-2.6.0

Pin to a specific version (matching a published big-code-analysis-cli release on crates.io) so reports stay reproducible across runs. A floating install surfaces metric-counting changes as unexplained CI flakes.

Threshold gate, SARIF, and clang-warning matcher

Three recipes live on the Check command page rather than here: the hard threshold gate, the SARIF upload to Code Scanning, and the clang-warning output paired with the GCC problem matcher for inline PR annotations.

Posting the Markdown report as a PR comment

bca report markdown is built for PR/MR comments: a stable header structure, one row per hot spot, and short paths once you pass --strip-prefix. Pair it with marocchino/sticky-pull-request-comment so each push updates a single comment instead of stacking new ones:

name: bca-pr-report
on:
  pull_request:
    branches: [main]
jobs:
  report:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
      - name: Install bca
        uses: taiki-e/install-action@v2
        with:
          tool: big-code-analysis-cli@2.6.0
      - name: Generate report
        run: |
          bca \
            report -O markdown \
            --paths "$PWD" \
            --top 20 \
            --strip-prefix "$PWD/" \
            --output report.md
      - name: Post or update PR comment
        uses: marocchino/sticky-pull-request-comment@v2
        with:
          path: report.md
          header: bca-quality-report

The same Markdown file is suitable for upload as a build artifact (actions/upload-artifact@v7) if you want it downloadable from the workflow run page in addition to the PR comment.

Baseline / ratchet pattern

bca check --baseline is the native ratchet: record today’s offenders in a committed TOML file, fail only on regressions and new offenders, and shrink the file over time. Bootstrap once, commit, then point CI at it:

# Once, on a developer machine. Commit both files.
bca check --paths src/ \
    --write-baseline .bca-baseline.toml
git add bca.toml .bca-baseline.toml

This snippet bootstraps from src/ only, which suits a single-crate library. For a multi-crate workspace, see the live worked example: its .github/workflows/pages.yml scans the entire repo with --exclude-from .bcaignore, a checked-in deny-set covering vendored grammars, generated trees, and tests.

Share the exclude list across workflow, recipe, and bootstrap. Put the deny-set in a single file at the repo root (a .bcaignore by convention, mirroring .gitignore / .dockerignore) and point every bca invocation at it with --exclude-from .bcaignore. Patterns from --exclude-from are unioned with any inline --exclude <GLOB> flags into one deny-set; keep --exclude for one-off excludes. Blank lines and #-prefixed comment lines in the file are skipped. Patterns follow the same ./-prefix convention as --exclude arguments (the walker’s emitted form). Pair edits to .bcaignore with a --write-baseline refresh, because the baseline keys depend on which files the walker visits.

- name: Threshold check with baseline
  run: |
    bca check --paths src/ \
        --baseline .bca-baseline.toml

A regressed function (worse than its baseline value in the metric’s own direction: higher, or lower for the mi.* family) still fails. A new offender not in the baseline still fails. An improved function passes silently and stays in the baseline until the next --write-baseline refresh.

Each surviving violation row starts with a tag that distinguishes a new offender from a known one that has worsened:

  • [new]: no baseline entry for this function / metric.
  • [regr …]: the current value is worse than the recorded baseline, measured in the metric’s own direction (for the lower-is-worse mi.* family that is a drop, still rendered +). The amount comes in two shapes: [regr +N%], a percentage of the record, for counts, and [regr +N.N pts], index points, for mi.*, whose arbitrary zero makes a percentage meaningless. Both start [regr , so rg '\[regr ' matches every regressed row. Special forms: [regr from 0] when a count’s baseline value was zero, [regr +>9999%] when a count’s regression exceeds 100× the baseline, [regr NaN] when the current value is NaN. See Reading the gate output for the details.

The violation rows go to stdout; everything below goes to stderr. See Which stream for the full split.

After the per-violation rows, stderr carries a per-file rollup footer with the format <path>: <count> violations (worst: <metric> = <value> vs limit <limit> at L<start>), sorted by violation count descending. It is meant to be the first thing a reader looks at: which file has the most problems, and which metric is the loudest in that file. Pass --no-summary to suppress the footer for tooling that reads the merged streams.

Actionable failure output

The four sub-sections below turn bca check’s failure output from a wall of offender lines into CI-aware presentations: which files in this PR tripped a threshold (--since / --changed-only), inline file-diff annotations (--github-annotations), a rendered step-summary digest ($GITHUB_STEP_SUMMARY), and a copy-paste-safe remediation block. Each is independent; mix and match per CI surface. A combined worked example is at the end of this group.

Diff-aware mode (--since / --changed-only)

On a PR or push, the developer’s first question is usually which of my files in this change tripped a threshold, not the whole-tree offender list. Two flags answer that:

  • --since <ref> partitions the per-file footer into a “Files in this range:” section (offenders in files touched between <ref> and HEAD) followed by “Other offenders:” (everything else). Per-violation lines are unchanged so existing grep-anchored tooling keeps working.
  • --changed-only drops violations from files outside the touched set entirely. Use it for PR gates that should be terse.
- uses: actions/checkout@v4
  with:
    # `--since origin/<base>` resolves a merge-base. The default
    # `fetch-depth: 1` checkout makes that ref unreachable; `0`
    # pulls the full history so the diff base resolves.
    fetch-depth: 0
- name: Threshold check with diff-aware footer
  run: |
    bca check --paths . --exclude-from .bcaignore \
        --baseline .bca-baseline.toml \
        --since "origin/${{ github.base_ref }}"

When --since is omitted, bca auto-detects the diff base from the environment in this precedence:

  1. BCA_DIFF_BASE: the explicit override. Use this from a local shell or non-GHA CI runner to mimic the auto-detection.
  2. GITHUB_BASE_REF: set by GitHub Actions on pull_request events. Expanded to origin/<value>; the runner is responsible for the corresponding git fetch.
  3. GITHUB_EVENT_BEFORE: set by GitHub Actions on push events to the SHA at HEAD before the push. The all-zeroes SHA (force push, brand-new branch) is treated as no signal.

A failed auto-detection (git missing, ref unresolvable, not a git checkout) is non-fatal without --changed-only: bca prints a warning and falls back to the whole-tree footer. With --changed-only, the same failure is fatal, so a misconfigured CI cannot pass by suppressing every violation.

The “Files in this range:” banner names the resolved base and the signal that produced it, so a CI-log reader can verify the gate latched onto the expected ref:

Files in this range (diff base: origin/main via GITHUB_BASE_REF):
./src/a.rs: 1 violation (worst: cyclomatic = 11 vs limit 2 at L1)

Other offenders:
./src/b.rs: 1 violation (worst: cyclomatic = 11 vs limit 2 at L1)

This is distinct from bca diff-baseline, which diffs baseline files between two on-disk paths and reports added / removed / worsened / improved entries. --since diffs source files between two git refs.

GitHub Actions inline annotations (--github-annotations)

The GHA UI renders ::error file=…,line=…,title=…::msg workflow commands as inline annotations on the file-diff view, which are easier to find than lines in the raw job log. bca check emits one per violation according to the tri-state --github-annotations <auto|always|never>: auto (the default) enables them when $GITHUB_ACTIONS == "true" (set by every GHA workflow step), always forces them on, and never suppresses them even inside a step (useful when a workflow runs bca check twice and wants annotations from only one run). A bare --github-annotations means always.

The annotations are emitted alongside the existing per-violation human stream, not instead of it. To avoid exhausting GitHub’s 10-error-per-step UI quota, annotations are capped at 10 per metric; overflow rolls up to a single ::error::N more <metric> violations not shown line so the count stays visible.

- name: Threshold check with inline annotations
  run: |
    bca check --paths . --exclude-from .bcaignore \
        --baseline .bca-baseline.toml
  # No `--github-annotations` flag needed: auto-enabled in GHA.

Pair this with --since (above) so the annotations point at the files in the PR, not the entire offender list.

Step-summary markdown digest ($GITHUB_STEP_SUMMARY)

GitHub Actions exposes $GITHUB_STEP_SUMMARY, a path to a markdown file that, when populated, renders as the step’s summary view in the job UI. Whenever that env var is set, or when --summary-file <path> is passed explicitly, bca check appends a digest containing the per-file rollup table, a per-metric count breakdown, and the top-10 offenders by severity (ranked the same way as the footer’s worst metric). --summary-file never suppresses the digest even when $GITHUB_STEP_SUMMARY is set.

The digest is bracketed by HTML-comment markers (<!-- bca-step-summary-begin --> / <!-- bca-step-summary-end -->) so a retried step replaces the previous block rather than stacking a new one; three retries converge to exactly one up-to-date digest. Content outside the markers (for example, summaries written by other tools earlier in the same step) is preserved.

- name: Threshold check with step-summary digest
  run: |
    bca check --paths . --exclude-from .bcaignore \
        --baseline .bca-baseline.toml
  # No flag needed: `$GITHUB_STEP_SUMMARY` is set automatically in GHA.

Local users can pipe the digest into any markdown file with --summary-file <path>. Empty input (clean run) still writes a “✓ No threshold violations.” block so the step summary positively confirms the gate ran.

When the gate finds violations, bca check emits a trailing --- next steps --- block on stderr (and inside the step-summary digest from above):

--- next steps ---
* Detailed reports: bca-reports artifact at https://github.com/<owner>/<repo>/actions/runs/<run-id>
* To refresh baseline: bca check --paths . --exclude-from .bcaignore --write-baseline .bca-baseline.toml
* Adoption guide: https://dekobon.github.io/big-code-analysis/recipes/baselines.html

The refresh invocation mirrors every flag the gate ran with that decides what a baseline records, so a first-time reader of a failing CI log can copy-paste it verbatim. That covers the walk scope (--paths, --include, --exclude, --exclude-from, --paths-from), the walker tuning (--language, --exclude-tests, --cyclomatic-count-try, --no-ignore, --no-skip-generated, --no-config), and the gate’s own inputs (--config, --threshold, --tier, --no-suppress, --check-exclude, --check-exclude-from, --baseline-fuzzy-match, --baseline). Flags that change only reporting or the exit code (--jobs, --report-format, --output, --no-fail, --exit-codes) are left off, since none can move an entry into or out of the file. The artifact URL is derived from $GITHUB_REPOSITORY and $GITHUB_RUN_ID when both are present (always true in GHA). Local runs have no upload to point at, so they suggest running bca report for the detailed view instead.

Suppress the block with --no-remediation for tooling that reads the merged streams; a plain bca check | ... pipeline never sees it.

Refresh after focused refactors:

bca check --paths src/ \
    --write-baseline .bca-baseline.toml
git diff .bca-baseline.toml   # expect a shrinking file

Two --write-baseline runs over an unchanged tree produce byte-identical output, so a diff appears only when offenders changed. See the Baselines recipe for the full adoption flow, PR-review heuristics, and the suppression composition rules.

Putting it all together

The four flags above compose. For a PR-gate workflow, the recommended invocation is:

- uses: actions/checkout@v4
  with:
    # `--since origin/<base>` resolves a merge-base. Default
    # `fetch-depth: 1` makes that ref unreachable; `0` pulls the
    # full history so the diff resolves.
    fetch-depth: 0

- name: Threshold gate (diff-aware + GHA UX)
  run: |
    bca check --paths . --exclude-from .bcaignore \
        --baseline .bca-baseline.toml \
        --since "origin/${{ github.base_ref }}"
  # No `--github-annotations` or `--summary-file` flag needed:
  # both auto-enable from `$GITHUB_ACTIONS == "true"` and
  # `$GITHUB_STEP_SUMMARY`. The trailing remediation block is also
  # auto-emitted.

What this gives you on a failing PR:

  1. Per-violation rows on stdout, in the same shape as the legacy gate, so existing grep tooling keeps working.
  2. Per-file rollup footer with Files in this range: (touched in the PR) listed before Other offenders:, so the developer sees their own contributions first.
  3. Inline GHA annotations on the file-diff view, capped at 10 per metric with an overflow rollup.
  4. Step-summary panel with a rendered markdown digest (per-file rollup, per-metric breakdown, top-10 offenders by severity).
  5. Trailing remediation block naming the artifact, printing the exact --write-baseline refresh invocation, and linking to the Baselines recipe.

Knobs:

FlagEffectDefault
--since <ref>Partition footer; auto-detect from env if omittedOff, auto-detect via BCA_DIFF_BASE / GITHUB_BASE_REF / GITHUB_EVENT_BEFORE
--changed-onlyDrop violations outside the diff entirelyOff
--github-annotations <auto|always|never>Emit ::error file=…::msg workflow commands (bare flag = always)auto enables when $GITHUB_ACTIONS == "true"; never opts out
--summary-file <path|auto|never>Append markdown digest; never opts outauto detects $GITHUB_STEP_SUMMARY
--no-remediationSuppress the trailing --- next steps --- blockBlock emitted on failure

Local users running bca check outside GHA see no change in default behaviour: none of the four features auto-enable without an env signal. To preview the GHA experience locally:

summary=$(mktemp "${TMPDIR:-/tmp}/bca-summary.XXXXXX.md")
GITHUB_ACTIONS=true GITHUB_STEP_SUMMARY="$summary" \
  BCA_DIFF_BASE=main \
  bca check --paths . --exclude-from .bcaignore \
      --baseline .bca-baseline.toml
cat "$summary"

For a non-GHA CI (GitLab, Buildkite, Jenkins), set the env vars your runner exposes (or pass the flags explicitly) and the same output paths fire.

Offender-count delta against merge base (stopgap)

For teams who cannot commit a baseline file (for policy reasons, say), a coarser approximation counts <error> elements in two Checkstyle documents, one on the merge base and one on the PR head, and fails when the count grows:

- name: Compute offender deltas vs. merge base
  run: |
    set -euo pipefail
    BASE="$(git merge-base origin/main HEAD)"
    git worktree add /tmp/base "$BASE"

    bca check --paths /tmp/base \
        --report-format checkstyle \
        --output /tmp/base.xml \
        --no-fail
    BASE_COUNT=$(grep -c "<error" /tmp/base.xml || true)

    bca check --paths "$PWD" \
        --report-format checkstyle \
        --output /tmp/head.xml \
        --no-fail
    HEAD_COUNT=$(grep -c "<error" /tmp/head.xml || true)

    echo "Offenders: base=$BASE_COUNT head=$HEAD_COUNT"
    if [ "$HEAD_COUNT" -gt "$BASE_COUNT" ]; then
      echo "::error::Offender count grew from $BASE_COUNT to $HEAD_COUNT"
      exit 1
    fi

This counts violations, not their identity: renaming an offender does not register as a regression, and improving one offender while regressing another nets to zero. The native baseline flow above is strictly more precise and is the recommended approach.

Live worked example

big-code-analysis runs the recipes above against its own source on every push and PR. The workflow source, .github/workflows/pages.yml, exercises the threshold gate, the baseline ratchet, both report formats, and a SARIF upload to GitHub Code Scanning end-to-end against the workspace itself. (The SARIF upload runs on same-repo pushes and PRs only; fork PRs skip it because the upload needs a write-scoped token, exactly as the clippy SARIF job does.) The output sits on GitHub Pages alongside this book:

The bca version quoted in the snippets on this page is the latest published release at the time of writing.

The in-tree workflow installs bca by building it from the current checkout rather than downloading a pinned release. This avoids the CLI-artifact schema-skew failure mode described under Installing bca from a GitHub Release for repos whose .bca-baseline.toml is always written by the same bca that gates it. Downstream adopters tracking a stable release line should stick with the pinned-tarball pattern; only switch to “build from checkout” if you, too, are mutating CLI artifact schemas in lockstep with the binary.

Self-scan threshold gate (local mirror of the CI gate)

CI’s threshold gate fires only after push, which is too late if a refactor silently nudged a metric past its limit. The big-code-analysis repo’s Makefile exposes four targets that mirror the CI gate (the Threshold gate step in .github/workflows/pages.yml) locally and add a second tier at 95% of every limit, so encroachment is caught a commit or two before the hard gate trips:

make self-scan                            # hard gate, 100% of bca.toml thresholds
make self-scan-headroom                   # soft gate, default 95% (BCA_HEADROOM)
make self-scan-write-baseline             # refresh baseline at hard thresholds
make self-scan-write-baseline-headroom    # refresh baseline at soft thresholds

Path selection, the .bcaignore deny-set, the per-function thresholds, the cyclomatic ? policy, and the baseline file all live in the repo-root bca.toml manifest, which bca discovers automatically. The hard tier is exactly what CI runs; expanded, it is a bare check with no path, threshold, or baseline flags, because the manifest supplies them:

cargo run --quiet -p big-code-analysis-cli -- check

That is the dev profile: the release relink costs ~100 s after any edit under src/, and both profiles write a byte-identical baseline, which make self-scan-profile-parity checks per PR. The recipes go through a small wrapper that can also run an already-installed bca when it proves it was built from the checkout; that mechanism and its knobs are repository tooling, documented in the Makefile, not part of the gate an adopter copies.

Both tiers consume the same bca.toml thresholds and the same .bca-baseline.toml; the soft tier runs the hard recipe with every threshold value multiplied by BCA_HEADROOM. Both exit 0 clean, 2 on any threshold violation, and 1 on tool error. The soft tier is a real gate, not advisory, so do not wrap make self-scan-headroom in || true. The two gate targets (self-scan, self-scan-headroom) are wired into make pre-commit, make ci, and .pre-commit-config.yaml; those chains run the hard tier before the soft tier, so a true regression always reports before near-limit headroom. The two write-baseline targets are side-effecting and deliberately not wired in.

BCA_HEADROOM=0.90 make self-scan-headroom widens the band; BCA_HEADROOM=0.99 tightens it to the last 1%. When the soft tier fires, absorb the offender into the baseline with make self-scan-write-baseline-headroom, which records every offender at the scaled thresholds (a strict superset of the hard-tier offenders).

The pattern (hard tier mirroring CI, soft tier as an early-warning band, both ratcheted by the same baseline) is project-agnostic. The Local threshold gates recipe documents the underlying principles, drop-in Makefile / just / package.json skeletons, and the helper script that scales thresholds, so you can adopt the same workflow in your own repo. The generic recipe uses the same BCA_* env-var names as the Makefile above, so overrides like BCA_HEADROOM=0.90 work identically across both.

GitLab CI

Full .gitlab-ci.yml example

The job below installs bca, runs the threshold check producing Code Climate JSON (for the MR Code Quality widget), Checkstyle XML, and a Markdown report, then uploads them as artifacts.

The same CLI-artifact schema-compatibility note from the GitHub Actions section applies here: the BCA_VERSION pin must cover the schema version of every CLI artifact you commit.

stages:
  - quality

variables:
  BCA_VERSION: "2.6.0"  # pin a published big-code-analysis-cli release
  BCA_TARGET:  "x86_64-unknown-linux-gnu"
  # sha256 of big-code-analysis-${BCA_VERSION}-${BCA_TARGET}.tar.gz from
  # the release's SHA256SUMS file. Bump together with BCA_VERSION.
  BCA_SHA256:  "53df050fad1e9bc9081fbe89bb216db7631633d4a741f5ea2e9c322ae9cc0864"

bca-quality:
  stage: quality
  image: debian:stable-slim
  cache:
    # Same key shape as the GitHub Actions snippet: bumping
    # BCA_VERSION invalidates the cache automatically.
    key: "bca-$BCA_VERSION"
    paths:
      - .cache/bca/
  before_script:
    - apt-get update -qq && apt-get install -y --no-install-recommends ca-certificates curl tar
    - |
      set -euo pipefail
      install -d "$CI_PROJECT_DIR/.cache/bca" "$HOME/.local/bin"
      if [ ! -x "$CI_PROJECT_DIR/.cache/bca/bca" ]; then
        stage="big-code-analysis-${BCA_VERSION}-${BCA_TARGET}"
        tarball="${stage}.tar.gz"
        url="https://github.com/dekobon/big-code-analysis/releases/download/v${BCA_VERSION}/${tarball}"
        curl -fsSL --proto '=https' --tlsv1.2 -o "/tmp/${tarball}" "$url"
        echo "${BCA_SHA256}  /tmp/${tarball}" | sha256sum --check --strict -
        tar -xzf "/tmp/${tarball}" -C /tmp
        install -m 0755 "/tmp/${stage}/bca" "$CI_PROJECT_DIR/.cache/bca/bca"
        rm -rf "/tmp/${tarball}" "/tmp/${stage}"
      fi
      install -m 0755 "$CI_PROJECT_DIR/.cache/bca/bca" "$HOME/.local/bin/bca"
      export PATH="$HOME/.local/bin:$PATH"
  script:
    - bca
        check
        --paths "$PWD"
        --report-format code-climate
        --output gl-code-quality-report.json
        --no-fail
    - bca
        check
        --paths "$PWD"
        --report-format checkstyle
        --output bca-checkstyle.xml
        --no-fail
    - bca
        report -O markdown
        --paths "$PWD"
        --top 20
        --strip-prefix "$PWD/"
        --output bca-report.md
    # The threshold gate runs separately so the artifacts above still
    # publish on failure. Exit 2 = at least one threshold exceeded.
    - bca check --paths "$PWD"
  artifacts:
    when: always
    reports:
      codequality: gl-code-quality-report.json
    paths:
      - gl-code-quality-report.json
      - bca-checkstyle.xml
      - bca-report.md

A few notes about the example:

  • The first two bca check … --no-fail invocations collect offenders for the artifacts; the final bca check (no --no-fail) is the pass/fail gate. All three runs use the same threshold config so the artifacts always match the gate decision.
  • artifacts:when: always keeps every artifact downloadable even on a red pipeline, which is when you need them most.
  • artifacts:reports:codequality wires the Code Climate JSON directly into GitLab’s MR Code Quality widget; see the Code Quality widget section below for the field-by-field semantics.

GitLab Code Quality widget

GitLab’s Code Quality view (inline complaints on the MR diff, summary on the MR overview page) consumes Code Climate JSON. bca check emits this via --report-format code-climate, so the integration is a one-liner:

code_quality:
  stage: quality
  script:
    - bca check --paths "$CI_PROJECT_DIR"
          --report-format code-climate
          --output gl-code-quality-report.json
          --no-fail
  artifacts:
    when: always
    reports:
      codequality: gl-code-quality-report.json
    paths:
      - gl-code-quality-report.json

Severity bands are derived from how far each metric breaches its configured threshold: value / limit for a count, and for the maintainability-index family, where lower is worse and the index goes negative, one step per maintainability band below the floor. ≤ 1.5 → minor, ≤ 2 → major, ≤ 4 → critical, > 4 → blocker; bca check’s summary ranks offenders by the same key. The widget deduplicates findings by fingerprint; bca hashes path \0 function \0 metric (no line, no value) so a violation that survives an upstream line-drift edit still collapses into the same widget entry across pipeline runs.

Sanity-check a generated report locally:

jq 'all(.[]; has("description") and has("check_name")
     and has("fingerprint") and has("severity")
     and has("location"))' gl-code-quality-report.json
# → true
jq '[.[] | .severity] | unique' gl-code-quality-report.json
# → a subset of ["info","minor","major","critical","blocker"]

MR-only comment with the Markdown report

To attach the Markdown report as an MR note (the GitLab analogue of the GitHub PR comment recipe), use the project access token and the Notes API:

bca-mr-comment:
  stage: quality
  image: alpine:3
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  needs: ["bca-quality"]
  before_script:
    - apk add --no-cache curl jq
  script:
    - |
      BODY=$(jq -Rs '.' < bca-report.md)
      curl --fail --silent --show-error \
        --request POST \
        --header "PRIVATE-TOKEN: $CI_BCA_BOT_TOKEN" \
        --header "Content-Type: application/json" \
        --data "{\"body\": $BODY}" \
        "$CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes"

CI_BCA_BOT_TOKEN is a project access token with api scope. The job depends on bca-quality so the Markdown artifact is in place before it runs.

Jenkins / SonarQube

Both Jenkins (via the Warnings Next Generation plugin) and SonarQube (via its Generic Issue importer) consume Checkstyle 4.3 XML directly. The same invocation feeds both:

bca check --paths src/ \
    --report-format checkstyle \
    --output report.checkstyle.xml

Wire report.checkstyle.xml into your existing Jenkins Record Issues / SonarQube External Issues step. The Checkstyle writer emits an empty (well-formed) document when there are no offenders, so neither tool needs special-casing for a clean run. See the Check command page for the writer’s schema details.

Generic CI guidance

Applies regardless of provider:

  • Pin bca to a specific version. Both cargo install --version and cargo binstall --version accept the published crate version of big-code-analysis-cli. A floating install surfaces metric-counting changes as unexplained CI flakes. Pin to a version whose CLI-artifact schemas (baseline, thresholds) match the files your repo commits; see the schema-compatibility note in the install section.
  • Use the default CPU-based parallelism unless memory requires fewer workers. --jobs defaults to the effective CPU count and honors available_parallelism() (cgroup-, cpuset- and quota-aware on Linux, OS CPU count on macOS/Windows), so CI runners need not thread --jobs "$(nproc)" through every recipe. --jobs 1 remains a debugging knob, not a default. It is not memory-aware. Each worker holds one whole file at a time and a parse-only run measures 25–70× the source size, so the in-flight term is roughly jobs × 70 × the largest source file in the tree, and --jobs bounds that term only. The destination decides the rest: --output-dir writes each document as it is produced and bca check reduces each file to its violations, so both stay at the in-flight bound. metrics --output <FILE> collects every file’s result and serializes it after the walk, and structured stdout holds finished documents while an earlier file is still being analyzed, which is worst when one large file sorts early. A memory-limited container with many vCPUs should pass an explicit --jobs, and prefer --output-dir to --output <FILE> on a large tree.
  • Always pass --strip-prefix "$PWD/" to bca report markdown so the path column is identical across runners with different workspace paths. Without it the diff between two reports is dominated by /home/runner/work/... vs. /builds/group/project/... noise.
  • Store bca.toml at the repo root, alongside Cargo.toml / pyproject.toml / package.json. bca discovers it automatically, so a bare bca check reads the committed thresholds, paths, and baseline. Treat it as source: review threshold relaxations in code review.
  • Handle the exit-code contract. bca check exits 0 clean, 2 on any threshold violation, 1 on tool error (bad config, unknown metric, unreadable path). Reserving 1 for tool errors lets CI distinguish “a function got too complex” from “the analyzer crashed”. Pass --exit-codes=tiered (or set [check] exit_codes = "tiered" in bca.toml) to split the violation case by severity: 2 new offenders only, 3 regressions only, 4 both, 5 a --tier=soft violation that also breaches the hard limit. The tiered codes are opt-in; the default stays 0/1/2. Every fail-state remains non-zero, so exit != 0 → fail wrappers keep working; only tooling that tests $? -eq 2 explicitly needs to widen to 2-5.
  • Honor in-source suppression markers, audit with --no-suppress. The default bca check honors bca: suppress / bca: suppress-file markers; passing --no-suppress ignores them so auditors see the raw offender list.

The gate’s trust boundary

A pull-request gate analyzes code its author controls, and two of the inputs that decide which files the gate looks at live in that same branch: file contents, which the generated-code detector sniffs, and the .gitignore files the walker honors. Both defaults are right for analyzing a trusted checkout. In a PR gate they are levers the change under review can pull. bca check reports both by default (the summary below) and turns both off under --strict.

A generated-code marker removes a file from the gate

Take a C file whose f() exceeds cyclomatic=2, checked with bca check --no-config --paths . --threshold cyclomatic=2:

stateexit
baseline2 (violation reported)
prepend // @generated as line 10
same file, --no-skip-generated2
marker removed again2

One comment line takes the file out of the gate. Every recognized marker phrase works (@generated, DO NOT EDIT, GENERATED CODE, case-insensitive), and the detector scans the first ~50 lines / 5 KiB, not just line 1. Two forms worth knowing when reviewing a diff:

  • // DO NOT EDIT, see docs/schema.md before changing reads as ordinary housekeeping and passes review without comment.
  • @generated buried on line 4 of a five-line license header still matches; a reviewer scanning for a suspicious first line will not see it.

A committed .gitignore removes a file from the walk

Same gate, with the violator accompanied by one benign sibling file:

stateexit
violator + benign sibling2
+ .gitignore containing violator.c0
same, --no-ignore2

The walker honors ignore files found inside the walked tree, so a .gitignore committed in the branch under test shrinks the checked set. One nuance: when the ignored file is the only analyzable input, the run exits 1 (“no input files matched”) rather than 0, so the clean bypass needs at least one other file to remain.

The gate says what it skipped

bca check prints a one-line stderr summary whenever either mechanism dropped files, so neither bypass is silent:

bca: 2 files not checked (1 generated, 1 ignored) — pass --report-skipped to list them

Clean runs print nothing. --report-skipped adds a note: skipped (generated): <path> / note: skipped (ignored): <path> line per file. The summary changes no exit code: a bypassed run still exits 0, it just says so.

The ignored count covers analyzable files at the directories the walk entered. A whole directory dropped by an ignore rule (a build tree, or an attacker ignoring a source directory outright) is never entered and appears only under --report-skipped, as its own clause (3 ignored directories not walked), because ignored build trees are present in nearly every checkout and would otherwise make the default summary permanent noise. A PR gate that must see whole-directory drops should pass --report-skipped or, better, --strict. See the check page for the exact rules.

--strict turns both mechanisms off

For a gate over untrusted input, turn both mechanisms off rather than auditing stderr:

- name: Threshold gate (PR)
  run: bca check --strict --paths src/

--strict is equivalent to --no-skip-generated --no-ignore; prefer the single flag, since a recipe that remembers one of the two constituents still has one open bypass. Projects opt in once with strict = true under [check] in bca.toml instead of editing every workflow. Genuinely generated trees then belong in an explicit deny-set the reviewers own (the committed .bcaignore pattern), not in content sniffing the submitter controls.

Baselines: ratcheting thresholds on existing code

Introducing thresholds on an existing codebase can flag violations unrelated to the current change. A baseline records accepted violations so the gate fails on new offenders and regressions, and shrinks as the team pays the recorded debt down. This recipe walks through adopting one, reading the gate output it produces, and keeping it fresh; the flag reference is on the check page.

Baselines complement in-source suppression markers; they are not a substitute. Use suppression markers when a function is intentionally complex forever (a parser, a state machine, generated code). Use a baseline when the team intends to pay the debt down. Both can live in the same repo; suppression is checked first.

End-to-end adoption flow

One-shot shortcut: bca init scaffolds a consolidated bca.toml manifest (with paths, exclude_from, baseline, and a [thresholds] table), the .bcaignore it references, and an initial .bca-baseline.toml derived from the current tree in a single command. With the manifest in place, a bare bca check discovers it and gates zero-config; pass --force to overwrite existing files or --no-baseline to skip the walk. The longer recipe below is useful when you want to tune thresholds before bootstrapping the baseline.

1. Pick initial thresholds

Either gut-feel numbers (cyclomatic=15, cognitive=20) or pull them from a bca check --no-fail run over the repo to see the current distribution.

# bca.toml at the repo root, discovered automatically by `bca check`.
paths = ["src"]

[check]
baseline = ".bca-baseline.toml"

[thresholds]
cyclomatic = 15
cognitive = 20
"loc.lloc" = 200

2. Bootstrap the baseline

bca check --write-baseline

A bare --write-baseline (no path) writes to the baseline key from the bca.toml you just created, so the filename lives in exactly one place. Pass an explicit path (--write-baseline <file>) only when you have no manifest baseline to default to; without one, the bare form errors rather than guessing a filename.

Commit both files in the same change:

git add bca.toml .bca-baseline.toml
git commit -m "ci: introduce metric thresholds with baseline"

Path keys in the baseline are stored relative to the baseline file’s own directory (the anchor). --paths ., --paths src/, and --paths "$PWD" produce byte-identical baselines, and --baseline runs match whichever --paths form CI uses. Switch between them freely without re-running --write-baseline.

3. Wire the CI gate

GitHub Actions:

- name: Check code complexity thresholds
  run: |
    bca check
  # `paths`, thresholds, and `baseline` all come from the
  # auto-discovered `bca.toml` manifest at the repo root.

GitLab CI/CD (job excerpt):

threshold-check:
  image: rust:1
  before_script:
    - cargo install --locked big-code-analysis-cli@<VERSION>
  script:
    - bca check

Exit codes: 0 clean, 2 regression or new offender, 1 tool error. See CI integration for the broader matrix of CI surfaces.

4. Refresh the baseline as the team pays debt down

Every few weeks, or after a focused refactor:

cp .bca-baseline.toml .bca-baseline.old.toml
bca check --write-baseline
bca diff-baseline .bca-baseline.old.toml .bca-baseline.toml

A shrinking diff is the goal. Two --write-baseline runs over an unchanged tree produce byte-identical output, so spurious diffs only appear when actual offenders changed.

You do not have to guess when a refresh is due. A gated run whose offenders have measured better than their recorded values says so on stderr (see the stale-entry warning), because the difference between recorded and live is suppression nobody chose.

Before tightening a limit, price it at both tiers

Paying debt down invites tightening the limit that produced it, and the obvious measurement, bca check --threshold <metric>=<candidate>, answers only half the question. A --threshold value is applied last and absolutely, never scaled, so it has no soft tier. A candidate that costs nothing at the hard gate can still put a whole population permanently inside the --tier=soft band, and you will not see it until you have edited bca.toml and run the other gate.

bca check --explain-threshold cognitive=15

Weigh the new column, not the offender count: it is how many baseline entries the change would add, and it is what a reviewer is being asked to approve. A cluster: line means the candidate landed on top of an existing population, and those entries can never be retired; see Tightening a limit onto a cluster.

5. PR-review heuristics

Run bca diff-baseline <old> <new> and read the summary instead of parsing a raw git diff .bca-baseline.toml in your head. It pairs entries on their (path, qualified, metric) identity, so a function that merely drifted up or down the file is not reported as a remove + add, and buckets every real change:

1 added, 1 removed, 2 worsened, 0 improved

## Added
  src/new.rs::shiny        cognitive  = 30

## Removed
  src/gone.rs::old_fn      nargs      = 9

## Worsened
  src/bar.rs::act_on_file  cognitive  60 → 63
  src/foo.rs::do_thing     cognitive  25 → 27

Map the buckets back to the old heuristics:

  • removed (baseline shrank). Debt paid down. No further action.
  • added (baseline grew). Someone added a new offender to the file intentionally. Review the values: was this a deliberate stopgap, or did the author bypass the gate? Either is fine if conscious; the point of committing the file is to make the choice reviewable.
  • worsened (an entry got a higher value). The author re-ran --write-baseline after the function got worse. Treat it the same as added and surface the change in review.
  • improved. A recorded offender got better without dropping out of the baseline. That is a good sign the refactor is working, and the reason to land the refreshed file rather than leave it. Until it is refreshed, the old entry keeps suppressing the offender up to a value the tree no longer produces.

For a PR bot, bca diff-baseline <old> <new> --format markdown emits a fenced block ready to drop into a sticky comment, and the --worsened-only / --added-only filters narrow it to just the regressions reviewers must look at. --format json feeds the same diff to other tooling. The command exits 0 by default, because it informs review rather than gating (the gate is bca check itself); the opt-in --exit-code flag exits 2 on a non-empty filtered diff.

Reading the gate output

A failing bca check --baseline run prefixes each surviving violation with a tag and follows the list with a per-file rollup:

bca: filtered 422 violations via baseline
[regr +60%] src/foo.rs:1-865: <file>: halstead.effort = 1557107.72 (limit 50000)
[new] src/bar.rs:506-747: act_on_file: cognitive = 63 (limit 25)
...

--- summary ---
src/foo.rs: 5 violations (worst: halstead.effort = 1557107.72 vs limit 50000 at L1)
src/bar.rs: 4 violations (worst: cognitive = 63 vs limit 25 at L506)

Tag prefixes:

  • [new]: no baseline entry matched this violation by qualified symbol (within the line tolerance) or, when --baseline-fuzzy-match is set, by body hash. The violation is new since the baseline was written. See Matching for the resolution order.

  • [regr …]: the baseline contains a recorded value and the current value is worse than it. “Worse” is measured in the metric’s own direction, so for the lower-is-worse mi.* family a drop below the recorded value reads + like any other regression. The amount takes one of two shapes, both starting [regr , so rg '\[regr ' matches every regressed row:

    • [regr +N%] for a count (every metric outside mi.*): N is the rise as a percentage of the recorded value, rounded to an integer.
    • [regr +N.N pts] for mi.original, mi.sei, and mi.visual_studio: N.N is the drop in index points, to one decimal. The Maintainability Index has an arbitrary zero and mi.original and mi.sei go negative on large functions, so a percentage of the record means nothing: a drop from −10 to −20 reads [regr +10.0 pts], and a 0.01-point drop from a record of 0.001 reads [regr +0.0 pts] rather than +1069%.

    Special forms:

    • [regr from 0] when a count’s recorded value is 0.0 and a percentage would divide by zero. An mi.* record of zero is an ordinary point and reads in points.
    • [regr +>9999%] caps once a count’s regression exceeds 100× the baseline value. Points are not capped.
    • [regr NaN] when the current metric value is NaN (degenerate Halstead inputs on trivial functions).

Tags only appear when --baseline is passed; without it the line format is byte-identical to the no-baseline default. CI tooling that reads the merged streams can suppress the trailing summary with --no-summary.

The summary footer groups violations by file, cites the single worst metric per file (by severity: value / limit for a count, one step per maintainability band below the floor for mi.*), and sorts rows by violation count descending then path ascending. Use the footer to identify files with the most violations before reading individual offender rows.

Merging two branches

A baseline is generated wholesale, so a textual merge of two branches that both touched it is never right: each side’s values were measured against its own tree, and the merged tree matches neither. Tell git not to try, with one line in .gitattributes:

.bca-baseline.toml -merge

Git then leaves the file conflicted as a whole instead of splicing the two sides together, and the resolution is always the same: regenerate.

bca check --write-baseline
git add .bca-baseline.toml

Unlike a merge=ours driver, -merge needs no per-clone git config, so it works for everyone who clones the repository rather than only for those who already knew to set it up.

6. Retire the baseline

When .bca-baseline.toml contains only version = 6 and no entries, drop the --baseline flag from CI and delete the file. The thresholds now stand on their own.

Tier/headroom provenance

A baseline written with --write-baseline (v5+) records which gate it was written against in a [provenance] table:

version = 6

[provenance]
tier = "soft"
headroom = 0.95
mi_charges_docs = false
mi_formula = 2
  • tier = "hard": written by the hard gate (bca check --write-baseline …); no headroom key.
  • tier = "soft", headroom = <ratio>: written by the soft gate scaled by the soft ratio (bca check --tier=soft=0.95 --write-baseline …).
  • tier = "soft" with no headroom: written by the soft gate driven by a [thresholds.soft] table (per-metric limits, no single ratio).
  • mi_charges_docs: whether the maintainability index charged documentation rows when the file was written (the --mi-charges-docs flag or manifest key; see Charging documentation). Absent from a file written before the key existed, which reads as true: every earlier bca charged documentation.
  • mi_formula: the revision of the maintainability-index formula the file’s mi.* values were computed under (2 since the size term moved to PLOC and mi.sei to a 0–1 comment ratio). Absent from a file written before the key existed, which reads as revision 1.

The provenance is a real TOML table, not a comment, so bca diff-baseline and external tooling can read it. Baselines written by an older bca (v2–v4) carry no provenance and are read without error.

The stricter-than-baseline warning

bca check reduces the baseline’s provenance and the current run’s effective limits to a single strictness scalar (hard → 1.0; soft scaled by h → h; smaller means stricter) and warns when the current run is stricter than the baseline was written against:

warning: this check's effective limits (strictness 0.9) are stricter
than the baseline was written against (strictness 0.95); the baseline
may under-cover and the gate can fire on untouched files. Refresh it at
the matching tier, …

This is the silent-desync the baseline-refresh discipline guards against: a baseline written looser than the current gate may not list every offender the tighter gate produces, so the gate can suddenly fire on files nobody touched.

The warning is directional. It fires only when the current run is stricter. It stays silent in the safe direction: a hard check (strictness 1.0) reading a soft-0.95 baseline sees a superset of its offenders, which is the intended single-baseline setup where make self-scan (hard) and make self-scan-headroom (soft) ratchet through the same .bca-baseline.toml. It also stays silent for equal provenance, for pre-v5 baselines (provenance unknown), and when either side is a [thresholds.soft]-table baseline (no single ratio to compare). To clear a real warning, refresh the baseline at the current tier with the matching --write-baseline recipe.

The MI formula-revision warning

A baseline’s mi.* values are only comparable with a run that computes MI the same way. When the formula itself changes, every recorded value moves for unchanged code, so bca check warns whenever a baseline holding any mi.* entry records a different mi_formula revision from the one the run computes, in either direction:

warning: this baseline records mi.* values under MI formula revision 1;
this run computes revision 2. The recorded values are not comparable
and can misclassify a regression. Review any mi.* threshold and refresh
with `--write-baseline <file>`.

It does not wait for an mi.* offender: it names the stale file on every read, so the record is refreshed before an mi.* gate comes to depend on it. A file written before the key existed reads as revision 1, so the first check after upgrading warns. While it fires, the documentation-mode warning below is not shown: a baseline from another formula is not worth comparing mode by mode. Refresh it with --write-baseline. bca diff-baseline warns the same way when its two files record different revisions and either holds an mi.* entry, since their mi.* deltas then reflect the formula, not the code.

The MI documentation-mode warning

The mi.sei values a baseline records depend on whether documentation rows were charged; mi.original and mi.visual_studio do not. Including documentation rows raises mi.sei. A baseline recorded with documentation included can therefore reject unchanged code when the current run excludes documentation. Reversing the modes can hide a regression. bca check warns in either direction:

warning: this check computes mi.* leaving documentation out
(mi_charges_docs = false) but the baseline was written charging
documentation (mi_charges_docs = true); its mi.sei entries describe a
different comment share (mi.original and mi.visual_studio read the
same in both modes), …

A baseline that records no mode was written by a bca that charged documentation, so it reads as mi_charges_docs = true. Every such file a release wrote also predates the formula stamp, so after upgrading the formula-revision warning above fires instead. The documentation-mode warning stays silent when the modes agree and when no mi.sei value is read against the baseline: a baseline holding no mi.sei entry, or a run with no mi.sei offender, is unaffected by the mode. Clear it by refreshing under the current mode, or by passing the --mi-charges-docs=<bool> the message names to match the baseline.

The stale-entry warning

The ratchet suppresses a violation for as long as it has not worsened past its recorded value, so everything on the improving side classifies alike: a function measuring 5 against a recorded 7 is treated exactly like one still measuring 7. The two points between them are gate headroom nobody chose; the function can grow back to 7 and the gate will not notice.

A gated run reports that drift in one stderr line:

warning: 3 baseline entries improved past the recorded value (worst:
src/spaces/compute.rs::metrics_inner halstead.effort 119147.75 →
116715.61); that gap is gate headroom nobody chose, so refresh with
`--write-baseline`. …

It names one example rather than every entry, because the response to any number of them is the same wholesale refresh. Direction follows the metric: for the lower-is-worse mi.* family the stale direction is a rise above the record. An entry sitting exactly on its recorded value is silent, so a freshly written baseline never warns about itself.

It finds only half of the staleness. A function that stopped breaching its limit altogether produces no violation at all, so it never reaches the baseline matcher and cannot be counted here. Its entry stays in the file, inert and invisible, until someone regenerates.

This repository closes the other half with a scheduled job rather than a gate, and the shape is worth copying. baseline-freshness.yml runs quarterly. It regenerates the baseline with the same --write-baseline recipe that wrote the committed one (the job pins that recipe, so a tier change has to land in both places), diffs the two with bca diff-baseline --exit-code, and files a labelled issue naming what moved. Two details of the job carry its design:

  • It uses diff-baseline rather than git diff, because git diff reports a moved start_line as staleness.
  • It looks up an existing issue by label and title before filing, because a stale baseline persists until someone commits a refresh, so an unconditional file would open one issue per quarter for the same entry. The label alone is shared with other findings; the fixed title is what keeps the comment on the right issue.

Nothing is gated on the result: the tree is fine, the file describing it has aged.

How matching works

Each entry is keyed on (path, qualified_symbol, metric). The qualified symbol is the ::-joined chain of enclosing named containers plus the function name (MyStruct::do_thing, my_namespace::MyClass::method). The top-level file space collapses to <file>. A violation is resolved against the baseline in this order:

  1. Qualified symbol. If exactly one entry shares the violation’s (path, qualified_symbol, metric), it matches regardless of line number, so editing code above a function does not re-key it as [new].

  2. Start-line tolerance. If several entries share that key (two methods named is_valid on different impl blocks the analyzer could not tell apart, overloads, …), the entry whose recorded start_line is closest to the violation, and within --baseline-line-tolerance lines (default 50), wins. Beyond the tolerance the violation is [new].

    This is the only rule that reads a line number, so from schema v6 a start_line is written only for entries in such a group. Everywhere else the field is absent, and the file therefore does not change when an edit above a baselined function moves it. That is what keeps a real baseline change (a value moving) visible in a diff instead of buried in line-number noise.

  3. Body hash (opt-in). With --baseline-fuzzy-match, a violation whose qualified symbol no longer matches is matched against entries with an identical normalised body hash within the same (path, metric). This absorbs a rename that kept the function’s shape (the digest elides the function’s own name and is insensitive to indentation, blank lines, and CRLF). The hash is written into the baseline only when --baseline-fuzzy-match is set, so seed it with one fuzzy --write-baseline to enable fuzzy reads. Configure both keys under [check] in bca.toml as baseline_line_tolerance and baseline_fuzzy_match (the bare top-level spelling is deprecated and warns; see #599).

Anonymous functions (closures, lambdas) have no stable name, so their qualified symbol bakes in the line (outer::<anon@L42>). They therefore re-key as [new] when they move. Symbol matching survives line drift only for named top-level and method-bound functions, which produce the bulk of baseline churn.

When the gate finds violations, bca check emits a trailing --- next steps --- block on stderr (the offender rows themselves go to stdout) and inside the $GITHUB_STEP_SUMMARY digest. The block names the artifact, prints a copy-paste-safe --write-baseline refresh invocation, and links back to this recipe. The refresh invocation mirrors the gate’s resolved --paths / --exclude / --exclude-from / --config / --baseline arguments, so a first-time reader of a failing CI log can refresh the baseline without leaving the page.

Suppress the block with --no-remediation if downstream tooling reads the merged streams and the trailing block confuses it.

Composition with suppression markers

--write-baseline already excludes any function silenced by a bca: suppress or #lizard forgives marker, so the same function does not end up in two places. If a function is intentionally exempt forever, prefer the in-source marker: it lives next to the code, survives refactors, and needs no extra file to commit. Use the baseline only for violations the team intends to fix.

To audit the un-filtered offender set (every violation regardless of suppression or baseline), pass --no-suppress and omit --baseline:

bca check --paths src/ \
    --no-suppress \
    --no-fail

Combined with --write-baseline, --no-suppress records every violation including the ones that suppression markers normally hide.

Auditing every exemption at once

A baseline is one of three ways code escapes the gate; the other two are in-source bca: suppress markers and [check.exclude] globs. bca exemptions lists all three tiers in a single report so a reviewer can see everything bca check is skipping without running three commands:

bca exemptions --paths src/
# In-source markers (2)
  src/parser.rs:120  bca: suppress       metrics=all  parse_long
  ...

# [check.exclude] globs (1)
  tests/**

# Baseline (.bca-baseline.toml, 417 entries)
  src/markdown_report.rs write_language_section cognitive 29
  ...

The baseline section reads the same --baseline / bca.toml [check] baseline source bca check does (or .bca-baseline.toml by default). Use --baseline-only to list just the baselined offenders, --format markdown for a PR comment, or --format json for dashboards. During PR review, pair it with bca diff-baseline <old> <new> (above): the diff shows what changed in the baseline, bca exemptions shows the full current exemption surface. See the Suppression markers page for the complete flag reference.

Limitations

  • Ambiguous symbols. When two functions share a qualified symbol (the analyzer could not resolve distinct containers, or a language permits overloads) and both have drifted beyond --baseline-line-tolerance from their recorded lines, neither disambiguates and the violations surface as [new]. Refresh with --write-baseline, or raise the tolerance.
  • Anonymous functions. Closures and lambdas re-key on movement because their synthetic symbol embeds the line (see How matching works).
  • OS portability. Paths are normalized to forward slashes on write and re-normalized on read, so a baseline generated on Linux matches the same tree on Windows. On Unix and Windows the path key is percent-encoded, so a non-UTF-8 path keeps its identity rather than collapsing onto a lossy display form; the backslash-to-slash normalization is the one deliberate exception.
  • Tightening a threshold. Lowering a limit may newly expose functions that were previously clean. They will not be in the baseline, so CI will fail. This is correct: tightening should expose new offenders. Refresh the baseline if the team chooses to absorb the new entries.

Local threshold gates

Run the threshold gate before committing to catch complexity regressions before CI. This recipe runs the same checker and configuration (the repo-root bca.toml manifest and its .bca-baseline.toml) locally and in CI, with an additional local tier for values approaching their limits. The local gate fires at git commit, alongside cargo fmt --check and cargo clippy -- -D warnings, while the author still has the change in hand.

This recipe captures the pattern big-code-analysis uses on its own source (Makefile’s self-scan* targets, backed by a consolidated bca.toml manifest) and distils it into something you can drop into your own repo’s Makefile, justfile, package.json script, or pre-commit config. The idea is provider-neutral: any threshold checker (bca, ESLint, clippy, SonarLint, Qodana) can be wired the same way.

Principles

Three principles drive the design. They are not specific to bca; they are the same conclusions Sonar reached when it pivoted its default Quality Gate to focus on new code and that the broader ratchet pattern formalises.

  1. Gate locally, mirror CI exactly. The local gate must run the same binary with the same arguments and the same threshold, baseline, and exclude files as CI. If the local gate is “almost what CI runs”, it stops catching regressions the moment one diverges from the other. Running the gate once before pushing is cheap; a red PR-bot ping is not.
  2. Ratchet, do not reset. When you introduce thresholds on an existing codebase, every reasonable limit fires on dozens of pre-existing functions. The realistic adoption path is “absorb today’s offenders into a baseline file, fail only on new or worsening ones, shrink the baseline over time”. This is the same strategy that lets a multi-year codebase introduce strict TypeScript or strict clippy lints without a months-long boil-the-ocean pass. See the Baselines recipe for the bootstrap, CI, refresh, and retire flow.
  3. Warn before you fail. A hard 100% gate fails only once a value exceeds the limit and gives no signal as a function creeps from 80% to 95% to 99% of its threshold. A stricter tier reports values approaching the hard limit. The author still has the file open, the test cases in their head, and the freedom to refactor before the offender hardens into “well, it’s in main now”. Sonar’s “new code” Quality Gate, the GCC -Wall / -Werror split, and clippy’s warn vs. deny lint levels all encode the same insight: a tier between clean and broken is where teams catch drift.

The two tiers

The pattern is two recipes wrapping the same checker, plus two recipes for refreshing the baseline at each tier.

TargetTierThresholdsBaseline-filteredUse case
self-scanhard100% of configyesMirror of CI. Must stay green on every commit.
self-scan-headroomsofttightened by HEADROOMyesEarly-warning band. Fires before the hard tier.
self-scan-write-baselinehard100% of config(write)Absorb today’s hard-tier offenders.
self-scan-write-baseline-headroomsofttightened by HEADROOM(write)Absorb soft-tier offenders when launching or widening the band.

This repository’s own Makefile adds two checks on the baseline file rather than on the code: self-scan-baseline-fresh regenerates to a temporary file and runs bca diff-baseline against the committed one (exit 2 when the file no longer describes the tree — the quarterly freshness job’s question, answerable locally), and self-scan-profile-parity regenerates under both cargo profiles and cmps the results, which is what lets the repo build its gate binary under the fast dev profile. Neither belongs in a generic adopter skeleton; an installed bca has no profile to vary.

The hard tier and the soft tier consume the same [thresholds] table and the same .bca-baseline.toml. The only difference between them is the HEADROOM ratio applied to every threshold value before bca check sees it. The ratio tightens each limit, which for the lower-is-worse mi.* family means raising it rather than lowering it.

Write the shared baseline at the soft tier (self-scan-write-baseline-headroom). A v5 baseline records the tier and headroom it was written against in a [provenance] table, and bca check warns when the current run is stricter than the baseline was written for. A soft-0.95 baseline is a superset of the hard gate’s offenders, so the hard self-scan reads it silently; writing the baseline at the hard tier instead would make the soft self-scan-headroom warn that it is the stricter gate. See Tier/headroom provenance.

A contributor who wants the soft tier to be stricter (to catch encroachment further out) therefore bumps a single environment variable rather than maintaining a parallel soft-threshold file, which drifts out of sync with the hard config the first time anyone forgets to update both.

Two-tier thresholds

bca check --tier <hard|soft|soft=RATIO> selects which tier to gate against. hard (the default) compares against the configured limits verbatim. soft is the early-warning tier, derived from the limits the file’s tables resolved to — the project table, its language table and any matching path entry, in the order How the layers compose states once. If a [thresholds.soft] table exists its overrides are merged on top, and metrics absent from it inherit their hard limit (no soft band); when a soft table is present the blanket RATIO does not apply, because explicit per-metric limits win over the scalar. Otherwise every limit is tightened by the soft RATIO (default 0.95 for a bare soft, so --tier=soft is never a silent no-op; soft=1.0 disables scaling). Repeated --threshold name=value flags apply last, absolutely.

A bare --tier=soft (ratio 0.95) is the zero-config entry point. Use [thresholds.soft] when metrics need different warning bands. The table records each soft limit beside the hard limits:

[thresholds]
cognitive  = 25
cyclomatic = 15
nargs      = 7

[thresholds.soft]
cognitive  = 22       # absolute soft limit
cyclomatic = "0.9x"   # 90% of the hard limit → 13.5
# nargs absent → soft tier inherits the hard limit (no soft band)

Soft limits with integer types read more cleanly as absolute values than as float-scaled ones: prefer nargs = 6 (for a hard nargs = 7) over the 0.95 × 7 = 6.65 a scalar would produce. Use the "<ratio>x" form for the large-valued metrics (halstead.*, loc.*) where an exact integer soft limit is fussy to pick. The scale factor must be in (0, 1]: the soft tier is an early-warning band that fires before the hard gate, never looser than it.

That “tighter, never looser” rule is what fixes the direction of the scaling, and it is not always a multiplication. A mi.* limit is a floor rather than a ceiling (a value below it is the violation), and the index has no true zero, so RATIO raises it by 1 − RATIO of one maintainability band (17.1 points of mi.original) instead: "mi.original" = 20 under --tier=soft=0.9 resolves to 21.71, not 18. The intuition that a soft band is “90% of the limit” is right for every other metric and exactly backwards here.

Both tiers ratchet through the same .bca-baseline.toml (no separate soft baseline file). bca check --print-effective-config --tier=soft prints the resolved limits; paste its [thresholds] output into [thresholds.soft] to migrate from a blanket-ratio band to explicit per-metric limits.

Zero-config: the bca.toml manifest

Rather than thread --paths, --exclude-from, --jobs, --config, --baseline, and --tier=soft=<ratio> through every recipe, drop a bca.toml at the repo root and let bca check discover it:

# bca.toml, discovered automatically at (or above) the working dir.
paths        = ["."]
exclude_from = ".bcaignore"
jobs         = "auto"          # or an integer (was `num_jobs`)

[check]
baseline     = ".bca-baseline.toml"

[thresholds]
cognitive    = 25
cyclomatic   = 15
"halstead.effort" = 50000
nom          = 30
nargs        = 7
nexits       = 5
abc          = 50
wmc          = 60

The headroom key is the soft-tier scale ratio: it only takes effect under --tier=soft, so a bare bca check (hard tier) stays the exact CI mirror regardless of any headroom key. For per-metric soft limits, prefer a [thresholds.soft] table over the scalar; it records the band you chose next to the hard limit instead of leaving it to a runtime multiplier.

With that file in place the four recipes collapse to one flag each:

.PHONY: self-scan self-scan-headroom \
        self-scan-write-baseline self-scan-write-baseline-headroom

self-scan:                          # hard tier (CI mirror)
	bca check
self-scan-headroom:                 # soft tier (early warning)
	bca check --tier=soft=0.95
self-scan-write-baseline:           # absorb hard-tier offenders
	bca check --write-baseline
self-scan-write-baseline-headroom:  # absorb soft-tier offenders
	bca check --tier=soft=0.95 --write-baseline

Discovery and precedence

  • bca climbs from the working directory to the repo root (the directory containing .git) looking for bca.toml; the first match wins. Relative paths inside the manifest resolve against the manifest’s own directory, so a bca.toml above the current directory still points at the right files.
  • Scalars and positive scope keys: CLI wins. Any explicit --baseline, --tier, --jobs, and so on overrides the corresponding manifest key, and the positive scope list keys (paths, include) are replaced by any explicit CLI value (bca check one.rs with manifest paths = ["src"] checks just one.rs). --config <file> merges on top of the manifest [thresholds] table (config keys win on collision), and repeated --threshold name=value flags apply last as absolute limits. The full order, including the per-language and per-path layers, is stated once in How the layers compose; it is shared across --config, --tier, and the manifest.
  • Negative filter keys: CLI unions with the manifest. The exclude list keys (top-level exclude, [check] exclude) are merged, not replaced: a CLI --exclude / --check-exclude is added to the manifest’s deny-set. A command-line filter can therefore never silently un-exclude a directory the project config deliberately skipped (such as vendor/). Duplicates across the two sources collapse; CLI patterns sort first. This mirrors ruff and ESLint’s exclude (replace) vs extend-exclude (add), generalized: targets replace, filters add. --x and --x-from union with each other regardless. Reach for --no-config when you need the manifest excludes gone entirely.
  • --no-config skips discovery entirely, for reproducible fully-explicit invocations that must not pick up repo-level config. bca init also ignores any existing manifest; it scaffolds config rather than consuming it.
  • The top-level include / exclude keys are the global file-filter globs (the --include / --exclude flags) that decide which files are analysed at all. They are distinct from the [check] exclude table (analysed-and-reported but ungated paths; see Exempting whole file categories).
  • A [check] table sets gate-only options. exclude is a glob list whose matching files are analysed and reported but exempt from the threshold gate (and from --write-baseline); exclude_from points at a .gitignore-style file of the same globs (both mirror the --check-exclude / --check-exclude-from flags). exit_codes = "tiered" opts into the finer-grained exit codes (mirrors --exit-codes=tiered; see Exit codes); "default" (the implicit value) keeps the stable 0/1/2 contract. The baseline and headroom keys are gate-only too, so they live here: baseline (the file bca check reads and a bare --write-baseline writes), baseline_line_tolerance, baseline_fuzzy_match, and headroom (the soft-tier scale ratio; mirrors --tier=soft=<R>). The CLI value overrides the table value for each, in either direction.
  • These four keys used to sit at the top level; that spelling is deprecated (#599) and prints a one-time warning. It is honoured for one release cycle, then removed in the next major version. Move baseline, baseline_line_tolerance, baseline_fuzzy_match, and headroom under [check]. When a key is set in both places, the [check] value wins.
  • A [vcs] table sets change-history ranking options for bca vcs. Its file_types key ("metrics", the default, or "all", or a "rs,py"-style extension list) scopes which files are ranked; as a positive scope key, an explicit --file-types CLI flag replaces it (see bca vcs file-type scope).
  • cyclomatic_count_try and exclude_tests are walker-tuning bools that mirror the --cyclomatic-count-try / --exclude-tests flags. exclude_tests = true prunes Rust inline-test subtrees (#[test], #[cfg(test)], …) before metric computation. Both are Rust-only and inert for other grammars. --exclude-tests is presence-only (no =false form), so its manifest key can only turn pruning on: a CLI --exclude-tests wins, but the manifest cannot turn off a key the CLI did not set.
  • mi_charges_docs mirrors --mi-charges-docs: true has mi.sei’s comment share count documentation rows as comments, which can only raise mi.sei; mi.original and mi.visual_studio read the same either way. Off by default, merged like cyclomatic_count_try (the CLI value wins in either direction). A baseline records the mode it was written under and bca check warns when it is read under the other; see Charging documentation.
  • A [thresholds.soft] table sets per-metric soft-tier limits (consumed by --tier=soft; see Two-tier thresholds). Unrecognized keys are ignored with a one-line warning, so you can pre-adopt forthcoming schema additions without breaking older bca builds.
  • bca check --print-effective-config prints the resolved view, including a manifest provenance line, so you can see exactly what the merge produced.

The explicit-flag skeletons below remain fully supported; the manifest is sugar over the same flags, not a replacement. Reach for them when you cannot drop a file at the repo root, or when one CI job needs a different layout than the committed manifest (pair the flags with --no-config).

Skeleton: GNU Make (explicit flags)

The four recipes below are a self-contained drop-in that thread every flag explicitly, the long form of the manifest recipe above. Adjust the BCA variable to point at whatever invocation gives you the checker (a pinned release binary, cargo run --release, an npm or pip wrapper). Adjust PATHS and EXCLUDE_FROM to match your layout.

# --- bca local threshold gates ------------------------------------------
# HARD tier mirrors CI exactly. Both tiers consume the same
# thresholds.toml + .bca-baseline.toml; the soft tier scales every
# threshold by $(BCA_HEADROOM) (default 0.95).
#
# Knobs are namespaced with `BCA_` so they don't collide with anything
# else in your environment. The big-code-analysis repo itself uses the
# manifest form above (a single `bca.toml`) rather than these explicit
# flags; reach for this skeleton when you can't drop a manifest at the
# repo root and must point `--config` at a standalone threshold file.
BCA               := bca
BCA_PATHS         := .
BCA_EXCLUDE_FROM  := .bcaignore
BCA_THRESHOLDS    := thresholds.toml
BCA_BASELINE      := .bca-baseline.toml
BCA_HEADROOM      ?= 0.95

# Common args, factored out so the four recipes stay in lockstep.
# `--jobs` defaults to the OS-reported effective CPU count
# (cgroup-/cpuset-aware on Linux), so no `$(nproc)` plumbing is
# needed. Override with `--jobs N` (or `--jobs 1` to force
# serial mode for debugging).
BCA_BASE_ARGS := --paths $(BCA_PATHS) --exclude-from $(BCA_EXCLUDE_FROM)

.PHONY: self-scan self-scan-headroom \
        self-scan-write-baseline self-scan-write-baseline-headroom

self-scan:
	@echo "bca self-scan (hard gate)..."
	@$(BCA) check $(BCA_BASE_ARGS) \
	  --config $(BCA_THRESHOLDS) \
	  --baseline $(BCA_BASELINE)

# `self-scan-headroom: self-scan` is intentional: under `make -j` Make
# would otherwise run both gates in parallel and the soft tier's scaled
# error message could land before the true regression on the hard tier.
# `--tier=soft=$(BCA_HEADROOM)` scales every config limit before the
# offender comparison: no helper script, no second TOML file.
self-scan-headroom: self-scan
	@echo "bca self-scan (soft gate, BCA_HEADROOM=$(BCA_HEADROOM))..."
	@$(BCA) check $(BCA_BASE_ARGS) \
	  --config $(BCA_THRESHOLDS) \
	  --tier=soft=$(BCA_HEADROOM) \
	  --baseline $(BCA_BASELINE)

self-scan-write-baseline:
	@echo "Refreshing $(BCA_BASELINE) at hard thresholds..."
	@$(BCA) check $(BCA_BASE_ARGS) \
	  --config $(BCA_THRESHOLDS) \
	  --write-baseline $(BCA_BASELINE)

# Soft-tier baseline write. NOTE: this and `self-scan-write-baseline`
# both write `$(BCA_BASELINE)`; never compose them as parallel
# prerequisites of one umbrella target or invoke them with `make -j2`,
# or the two `bca` processes will race on the same file and the
# losing tier's offenders will silently vanish from the baseline.
# Run them sequentially (hard first, then soft) and commit the diff.
self-scan-write-baseline-headroom:
	@echo "Refreshing $(BCA_BASELINE) at soft thresholds (BCA_HEADROOM=$(BCA_HEADROOM))..."
	@$(BCA) check $(BCA_BASE_ARGS) \
	  --config $(BCA_THRESHOLDS) \
	  --tier=soft=$(BCA_HEADROOM) \
	  --write-baseline $(BCA_BASELINE)

The soft tier tightens limits from --config, then applies the shared baseline. It multiplies count ceilings by the ratio (default 0.95 for a bare --tier=soft) and raises mi.* floors by 1 − ratio of one maintainability band. Explicit --threshold values apply last and are not scaled. There is no separate helper script or second TOML file to maintain; the soft tier is the hard-tier invocation plus one flag.

Exit codes

The gate exit codes propagate verbatim from bca check: 0 clean, 2 on any threshold violation (hard or soft), 1 on tool error. The soft tier is a real gate. Never wrap make self-scan-headroom in || true thinking it is advisory; the non-zero exit is the whole point of the encroachment band.

Pass --exit-codes=tiered (or set [check] exit_codes = "tiered") to split the single violation code 2 by severity: 2 new offenders only, 3 regressions only, 4 both, 5 a --tier=soft violation that also breaches the hard limit. The tiered codes are opt-in; the default stays 0/1/2, and every fail-state remains non-zero. Use them when CI needs to route “a new offender appeared” differently from “a baselined offender got worse” without parsing the [new] / [regr …] row tags.

Wiring into pre-commit and CI

Add the soft gate to whatever umbrella target your developers already run before pushing. The hard gate runs as its prerequisite (see the self-scan-headroom: self-scan edge above), so listing only the soft target is enough. That edge also survives make -j, which would otherwise schedule both leaves in parallel and interleave their output:

.PHONY: pre-commit
pre-commit: fmt-check clippy test self-scan-headroom

Ordering matters: the hard tier names a true regression with the 100% limit, not the scaled one. The prerequisite edge enforces that order even under parallel Make.

In CI, run only the hard tier:

- name: Threshold gate
  run: make self-scan

The soft tier is a developer feedback knob, not a release gate. Running it in CI either duplicates the hard tier (when nothing has encroached) or fires noisily on a baseline-absorbed offender that crept upward without crossing 100%. Neither buys you anything CI does not already cover.

The headroom knob

BCA_HEADROOM is a single scalar in (0, 1]. The interesting band is narrow:

BCA_HEADROOMFires when a function reaches…Use case
0.9999% of a count ceilingNarrow warning band.
0.9595% of a count ceiling (default)Default warning band.
0.9090% of a count ceilingWider band, useful immediately after raising a limit, while the new ceiling settles.
1.00100% (parity with hard gate)Sanity check that the two tiers agree.

An mi.* floor is raised by 1 − BCA_HEADROOM of one band instead of scaled, as Two-tier thresholds explains.

Values below ~0.80 turn the soft tier into a second hard tier with arbitrary numbers and stop being useful: every threshold has some function near 80% of it on a real codebase, and the soft tier becomes a permanent baseline-management chore rather than an early-warning signal.

When the soft tier fires

A failed soft gate is a decision point, not a bug report. There are three legitimate resolutions:

  1. Refactor. Same workflow as any other complexity regression: extract a helper, collapse a dispatch arm, split the function. This is the common case, and the soft tier exists to give you the time to do it on the same branch.
  2. Raise the limit. Edit the [thresholds] table (in bca.toml for this repo, or your own threshold file) and leave a why-comment explaining what changed (a new language module, a real algorithmic floor, a re-classified macro). Re-run make self-scan-headroom to confirm the new value covers the offender with room to spare.
  3. Absorb into the baseline. Run make self-scan-write-baseline (hard tier) or make self-scan-write-baseline-headroom (soft tier) when the value is legitimate forever: a parser dispatch arm whose width matches the grammar it covers, a stable state machine, generated code. Commit the diff in .bca-baseline.toml in the same PR as the code that produced it.

Do not pick “raise the limit” silently to make the gate go away. The committed why-comment is the only audit trail the next reader has; without it the bumped limit looks indistinguishable from neglect.

Skeleton: justfile

For projects that prefer just:

# bca local threshold gates. Hard tier mirrors CI; soft tier (headroom)
# is local-only early warning.
bca         := "bca"
paths       := "."
exclude     := ".bcaignore"
thresholds  := "thresholds.toml"
baseline    := ".bca-baseline.toml"
headroom    := env_var_or_default("BCA_HEADROOM", "0.95")

# `--jobs` defaults to the effective CPU count, so the skeleton
# no longer threads `$(nproc)` through `just`. Override
# inline if needed: `just self-scan --jobs 1`.
base_args   := "--paths " + paths + " --exclude-from " + exclude

self-scan:
    {{bca}} check {{base_args}} \
        --config {{thresholds}} --baseline {{baseline}}

self-scan-headroom: self-scan
    {{bca}} check {{base_args}} \
        --config {{thresholds}} --tier=soft={{headroom}} --baseline {{baseline}}

self-scan-write-baseline:
    {{bca}} check {{base_args}} \
        --config {{thresholds}} --write-baseline {{baseline}}

# Like the Make skeleton, never compose this with `self-scan-write-baseline`
# in parallel; they race on the same {{baseline}} file.
self-scan-write-baseline-headroom:
    {{bca}} check {{base_args}} \
        --config {{thresholds}} --tier=soft={{headroom}} --write-baseline {{baseline}}

Skeleton: package.json scripts

For JavaScript projects pulling in bca via npx or a pinned binary. --jobs defaults to the effective CPU count (cgroup-/cpuset-aware on Linux), so the npm tier no longer needs a BCA_NUM_JOBS env var to produce byte-identical bca check invocations as Make / just. Pass --jobs 1 explicitly only when debugging:

{
  "scripts": {
    "self-scan": "bca check --paths . --exclude-from .bcaignore --config thresholds.toml --baseline .bca-baseline.toml",
    "self-scan-headroom": "bca check --paths . --exclude-from .bcaignore --config thresholds.toml --tier=soft=0.95 --baseline .bca-baseline.toml",
    "self-scan-write-baseline": "bca check --paths . --exclude-from .bcaignore --config thresholds.toml --write-baseline .bca-baseline.toml",
    "self-scan-write-baseline-headroom": "bca check --paths . --exclude-from .bcaignore --config thresholds.toml --tier=soft=0.95 --write-baseline .bca-baseline.toml"
  }
}

Because the soft tier is a plain bca check invocation, the npm scripts are byte-identical across shells: no helper script, no python3-vs-py alias to paper over, no env-var-vs-shell-expansion portability traps. To widen the band, edit the literal 0.95 in the script (or wire it through your task runner of choice); the flag parses the same on every platform.

Pair with husky or pre-commit so the same scripts run on git commit.

Skeleton: pre-commit hook

If you use the pre-commit framework (version 3.2.0 or newer; see the version note below), both tiers are local hooks that shell out to make:

- repo: local
  hooks:
    - id: bca-self-scan
      name: bca self-scan (hard gate)
      entry: make self-scan
      language: system
      pass_filenames: false
      stages: [pre-commit]
    - id: bca-self-scan-headroom
      name: bca self-scan-headroom (soft gate)
      entry: make self-scan-headroom
      language: system
      pass_filenames: false
      stages: [pre-commit]

pass_filenames: false is deliberate: bca discovers its own inputs from --paths plus the baseline. Letting pre-commit pass the changed files in would shrink the scan to just those files and miss the cross-file effect of a baseline refresh.

Minimum pre-commit version 3.2.0. The stages: vocabulary was renamed in pre-commit 3.2.0 (2023-03-17): commit became pre-commit, push became pre-push, and so on. Older installs (notably RHEL 8 EPEL, Ubuntu 20.04 default packages, and any .pre-commit-config.yaml pinned to the legacy vocabulary) reject stages: [pre-commit] as an unknown stage name and the hook never registers. If you must support older installations, substitute stages: [commit]; in mixed fleets, pin the framework with pre-commit --version ≥ 3.2.0 in the dev-tooling docs so this contradiction does not surface silently.

Composition with the broader baseline workflow

The four self-scan* targets above are not a replacement for the documented Baselines recipe; they are that recipe, mechanised into developer-machine commands. The same ordering still applies:

  1. Bootstrap once. Write the initial thresholds, write the initial baseline, commit both.
  2. Gate on every commit. Hard tier fails on regression; soft tier fails on encroachment.
  3. Refresh during focused refactors. When a function legitimately moved (someone did pay down debt), regenerate the baseline and review the diff.
  4. Retire when empty. When .bca-baseline.toml shrinks to just version = 6 (the bare schema stamp with no offender entries), drop the --baseline flag and delete the file. The thresholds now stand on their own.

The hard / soft tier split is one instance of a broader pattern. If you have used any of the following, the mental model carries over:

  • Sonar’s Quality Gates focused on new code. Old code is held at its current state; changes must not make things worse. The baseline file is bca’s native form of the “new code” / “leak period” idea.
  • clippy’s warn-vs-deny lint levels. A warn lint surfaces in local builds; the same lint denied with -D warnings fails CI. The two-tier configuration gives you a place to land experimental tighter rules.
  • The ratchet pattern in general migration tooling: record today’s count, fail on increase, lower the ceiling as the count drops. bca check ratchets per-function rather than per-pattern, but the monotonicity guarantee is the same.
  • -Wall + -Werror in C/C++. A first pass with -Wall reveals the noise; promoting to -Werror after the baseline reaches zero is the same retirement step as deleting .bca-baseline.toml once it is empty.

Choosing thresholds

bca check compares each metric against a limit you configure. This page explains where the shipped defaults come from, how to adjust them for the language you are gating, and how to pick a different set for a different job. It is for anyone editing a [thresholds] table, manually or through a coding agent.

If you have not set up a gate yet, start with Local threshold gates for the mechanics and Baselines for absorbing the offenders you already have. This page is about the numbers.

The shipped defaults

bca init scaffolds this table. It is defined once in big-code-analysis-cli/src/default_thresholds.rs. The anchor column cites MISRA and Flog alongside the tools linked inline.

MetricLimitScopeAnchor
cognitive15functionSonarSource default
cyclomatic15functionlizard default; MISRA and NASA safety-critical ceiling
abc40functionbetween RuboCop AbcSize 17 and Flog’s “60 and above is dangerous”
nargs5functionRuboCop ParameterLists 5; Code Climate is stricter at 4
nexits5functionCode Climate return-statements 4
halstead.effort50000functionnone published; percentile-derived
loc.ploc600filenone published; percentile-derived
loc.sloc1200filebloat backstop, not the working limit
nom30containerCode Climate method-count 20; PMD TooManyMethods 10
wmc60containernone published; percentile-derived

Scope matters when you read these. A cognitive limit applies to each function; nom and wmc apply to each class, struct, trait, impl, namespace, or interface; loc.* applies to the whole-file root. bca check will not compare a container’s method count against a per-function limit. See Check for the full scope rules.

Two of these limits need their reasoning spelled out.

loc.ploc and loc.sloc are a pair. PLOC counts physical lines of code with blanks and comments excluded, so it is the working file-size limit: growing a file by documenting a decision costs nothing against it. SLOC counts everything, so it sits far looser and does one job only, which is stopping a file from growing without bound on comment volume while still clearing loc.ploc. Gating file size on SLOC alone charges the same price for a paragraph of rationale as for a new branch, and the observable result is that people delete the rationale.

cyclomatic at 15 rather than McCabe’s 10 is a deliberate loosening. NIST 500-235, the document that made 10 the canonical number, allows raising it for teams with the process to justify it, and observes that limits as high as 15 have been used successfully. Measured against real code, 10 flags roughly twice as many functions as 15 without a corresponding change in what a reviewer would call a problem.

How the defaults were derived

Published thresholds disagree with each other by wide margins. For cyclomatic complexity alone the defaults in common tools span 7 (RuboCop) to 30 (gocyclo), with Checkstyle and PMD at 10, lizard and MISRA at 15, and ESLint at 20. Picking one by authority means picking an authority.

So each limit here is checked against measurement as well. The reference corpus, as measured for #1140, was 43 real-world repositories across 20 languages, cloned at their default branch on 2026-07-31, with test trees, vendored code, and generated files excluded. Measured with bca metrics, that is roughly a quarter of a million function spaces, forty thousand container spaces, and twenty-seven thousand files. Values are binned by the same File, Function, and Container scope bca check applies, so a container’s nom never lands in a per-function distribution.

The design rule is that a default should flag roughly the worst 1% to 3% of spaces in the median language. Below that a limit is inert and gives false comfort. Above it the gate stops being a gate and becomes a style rule that people route around. This is a stricter reading of the benchmark approach in Alves, Ypma, and Visser, who derive risk bands at the 70th, 80th, and 90th percentiles. Their 90th percentile is a useful “worth looking at” line; it is far too noisy to fail a build on.

Two of the previous defaults failed that rule and changed as a result. The original corpus measurement found that nargs = 7 flagged fewer than 1% of functions in the median language, and nothing at all in this project’s own source: a limit that cannot catch anything. That measurement includes nested-closure parameters, which #1196 later removed from nargs; the current own-parameter metric cannot produce a higher count on the same inputs, and the drop is largest for closure-heavy languages, so the conclusion holds. The shipped limit is therefore stricter. cognitive at 25 was inherited from clippy, which is deliberately conservative because it lints rather than gates; 15 is what the metric’s designers chose.

Per-language overrides

Metric distributions vary by language more than by project. The 90th-percentile per-function cognitive value in the reference corpus runs from 0 in C# to 21 in Tcl. Median file loc.ploc runs from 16 in TypeScript to 283 in C, an eighteen-fold spread. A single table cannot fit both ends.

The table below gives the measured 97.5th percentile per language, which is the value a limit would need to flag the worst 2.5% of that language’s code. Read it as calibration data, not as a prescription: a low number means the language’s code is mostly simple, which is a reason you can tighten, not evidence that you should.

Languagecognitivecyclomaticabchalstead.effortloc.ploc
Bash35256044000(thin sample)
C5030601900003500
C++14102554000(thin sample)
C#441344000700
Elixir5720150001500
Go251745890001500
Groovy16132525000450
Java751620000800
JavaScript201535170000(thin sample)
Kotlin1082018000300
Lua35204082000800
Objective-C18123577000(thin sample)
Perl352545410001500
PHP1092526000900
Python20132542000700
Ruby76195000400
Rust882034000900
Tcl45258546000(thin sample)
TypeScript20132560000450
TSX141065130000400

“Thin sample” marks a language whose file count in the corpus is too small to derive a file-size figure from. The per-function figures for Bash, Tcl, JavaScript, and Objective-C rest on the smallest samples in the table and are the ones most worth re-deriving against your own code.

The halstead.effort column comes from a different measurement than the rest of the table. It was re-measured on 2026-10-10, after the Halstead vocabulary changes in #1486 through #1573, on a rebuilt corpus of 41 repositories that is comparable to the original but not identical to it. The C, C++, and Objective-C rows were re-measured on 2026-10-15 to include the declaration modifiers of #1578 and #1579. The repository list and commits are recorded in docs/development/threshold_corpus.md. Several rows moved by a factor of two to five, and almost all of that movement came from the change of repositories, not from the metric: the same 41 repositories measured with the pre-change release differ by at most half (Python) and mostly not at all. Treat any single row as a property of the repositories that represent the language, which is one more reason to re-derive against your own. In this column the thinnest samples are Objective-C, Perl, Bash, and C++, each under 2,500 functions.

What to change, and why

Most languages need no override. The cases that do fall into three groups.

Procedural languages with large dispatch functions. C, Tcl, Bash, Lua, Perl, and Go all run two to three times the cognitive default and above the cyclomatic one. These languages lack exceptions or discourage them, so error handling is inline branching, and they favour long switch-style dispatch over polymorphism. The default cognitive limit flags 5% to 15% of their functions, against 3% in the median language. Raise cognitive first. halstead.effort carries the rest of the excess for C, Go, and Lua; Tcl, Bash, and Perl measure close to the default on it.

In a single-language repository this is the whole [thresholds] table; in a mixed one it belongs under the language it describes, or it drags every other language up with it.

# Gating C. Tcl, Bash, Lua, Perl, and Go share its cognitive and
# cyclomatic shape, but not its effort: Go and Lua measure near 90000,
# and Tcl, Bash, and Perl near the default.
[thresholds.lang.c]
cognitive = 30
cyclomatic = 20
abc = 50
"halstead.effort" = 120000
"loc.ploc" = 1200
"loc.sloc" = 2000

Languages whose module construct is not a class. nom and wmc are Container-scoped, and a container is whatever the grammar calls a class, struct, trait, impl, namespace, or interface. An Elixir defmodule holds dozens of functions by design, so roughly a third of Elixir modules breach nom = 30. That is a scope mismatch, not a code smell. Rust sits at the other end: an impl block is usually a handful of methods, so the default never fires.

# Elixir modules and Rust impls sit at opposite ends of the same metric.
[thresholds.lang.elixir]
nom = 150
wmc = 300

[thresholds.lang.rust]
nom = 20
wmc = 40

Compact, heavily-abstracted languages. Java, Ruby, Kotlin, C#, and Elixir sit far under the defaults on every per-function metric except C#’s halstead.effort. Their 97.5th percentile cyclomatic is 4 to 8. Gating them at 15 means the gate will almost never fire, so tighten if you want it to do work.

Read the C# row with care. Its distribution is dominated by property accessors, which bca counts as functions and which are almost always trivial, so its percentiles sit lower than the code a reviewer would be looking at. The same effect applies more weakly to Java, Kotlin, and Ruby. Prefer re-deriving from your own repository over adopting any of those four rows directly; see Re-deriving for your own codebase.

Metrics that do not apply to every language

nargs reports 0 for every Bash function, and that is correct and permanent: the shell has no formal parameter list, arguments arrive as $1, $2, and so on, and the limit is inert. A nargs limit therefore passes unconditionally on a Bash codebase, which reads as “no offenders” rather than “not measured”. Bash is the only language left in that position.

Perl is counted, but only where the source declares a signature (sub add($x, $y), stable since 5.20 and on by default under use v5.36). A sub without a signature takes its arguments from @_ and has no formal parameter list to measure, so it reads 0, correctly. Perl’s nargs distribution is therefore sparse on codebases predating use v5.36, and one anonymous-sub form still reads 0 because the grammar misparses its signature.

wmc, npm, and npa are only produced for languages with a class-like container. They are absent from Bash, C, Lua, Perl, and Tcl output. Go is a half case: it has no container space at all, so wmc is absent and npm / npa are reported only on the file unit root. bca check gates both on container spaces and never on the root, so no npm or npa limit can fire on Go source; see Which spaces carry NPA, NPM and WMC.

A per-language override cannot fix any of these. Bash nargs is 0 for every function, so no limit (not even 0, which fires only on a value above it) can make the gate say anything. The override mechanism tunes limits; it does not add a measurement the grammar cannot supply.

Applying overrides

Write them into bca.toml as [thresholds.lang.<slug>] tables. Each one layers over the project’s [thresholds] per metric, so a language inherits every limit it does not restate:

[thresholds]
cognitive = 15
cyclomatic = 15
nargs = 5
nom = 30
wmc = 60

# Procedural, dispatch-heavy: raise the two metrics carrying the excess.
[thresholds.lang.c]
cognitive = 30
cyclomatic = 20
"halstead.effort" = 120000
"loc.ploc" = 1200

# A defmodule is not a class; nom and wmc need module-sized limits.
[thresholds.lang.elixir]
nom = 150
wmc = 300

# Compact and heavily abstracted: tighten, or the gate never fires.
[thresholds.lang.csharp]
cognitive = 8
cyclomatic = 8

The key is the canonical language slug, the same vocabulary --language accepts (cpp, csharp, objc, tsx, mozcpp, mozjs); an unknown one is a tool error rather than a silent no-op. A language with no table of its own is gated by [thresholds]. Under --tier=soft each language’s soft band is derived from its own resolved limit, so a language that raises a limit gets a soft band scaled from the raised number rather than the project-wide one. The full reference, including how --print-effective-config renders the resolved tables, is in bca check.

Two things to watch when splitting a table this way. .h is analyzed with the C++ grammar, not the C one, so a C project’s headers are gated by [thresholds.lang.cpp]; see Supported Languages for the extension map. And --threshold on the command line stays global: it overrides every per-language table too, which is what you want for a one-off run and a surprise if you expected it to compose.

The alternative is still available and still worse: keep one table sized for the loosest language and let per-file baselines carry the rest. It is simpler to maintain and weaker, because the stricter languages stop being gated at all.

Per-path overrides

A language table tunes a limit for everything written in that language. Some populations of files are a different shape from the rest of their language, and the shape is a property of the directory, not the grammar: a tree of hand-rolled dispatch tables, a generated binding layer, a parser whose state machine is one function per state. The cyclomatic count of a dispatch table is the length of its construct list, and its Halstead effort is the size of its vocabulary; neither number tells a reader anything a split would improve, and the project limit misreads every function in the directory the same way.

The per-function answer is a suppression marker on each one, and it does not scale. This repository accumulated 38 bca: suppress markers naming cyclomatic and Halstead across its per-language dispatchers, in several wordings and two operand orders, with nothing stating that the directory was exempt as a policy. A [[thresholds.path]] entry states the policy once:

[thresholds]
cyclomatic = 15
"halstead.effort" = 50000

# The per-language dispatchers are exhaustive kind tables: their
# cyclomatic is the construct list and their effort is the vocabulary.
[[thresholds.path]]
glob = "src/metrics/{abc,cognitive,cyclomatic,loc}/*.rs"
cyclomatic = 40
"halstead.effort" = 150000

An entry is an array element, so a manifest can carry several with different globs. Each names a glob and the metrics it re-limits, and inherits every limit it does not restate. The glob is matched against the file’s path relative to the directory of the config that declared it (the bca.toml, or the --config file), with the same engine and the same anchor that [check] exclude uses, so one spelling describes the same files on both surfaces. A file outside that directory is matched by its seed-relative form, exactly as a [check] exclude glob matches it: the path under the --paths seed that produced it, so with bca check -p src an entry declared outside the tree sees src/gen/a.c as gen/a.c, and its src/** does not reach it. Where several entries match one file, the last entry wins, per metric, in declaration order — the manifest’s entries first, then the --config file’s — the rule .gitignore readers already know.

An override is not a suppression. The function stays in bca check’s output and in the baseline with its real value, and the soft tier scales the overridden limit the way it scales any other: at --tier=soft=0.9 the entry above warns at cyclomatic = 36, and the ceiling an offender is measured against for exit 5 is 40, not the project’s 15. The marker takes a function out of the gate; the entry keeps it in, at a limit chosen for its population. That is the whole difference, and it is why --explain-threshold can still price a candidate limit over those files: it lists each entry that declares the metric, with the number it declares, so the count is read against the right denominator. It reports the declaration rather than a per-entry limit because entries overlap — where two match one file, the later one’s number is the limit — and the offender counts it prints are already taken against each file’s own resolved limit.

Three things the parser refuses, each naming the entry by its position in its own file and its glob: a metric name [thresholds] does not know, an entry with no glob or an empty one ("" and ./ would match nothing), and tokens, which is a cap on what the walk will parse rather than a maintainability limit and is not overridable by any layer. --print-effective-config prints every entry under [[thresholds.path]] with the metrics it declares at their hard-tier limit — --threshold applied last, the tier not applied — so the printed file replays the gate at the hard tier through --config. Under --tier soft the printed path limits are the hard limits the soft band is derived from, not the band: the band belongs to a file’s whole match set (a second entry naming only another metric changes the first’s), so no per-entry row can carry it, and a printed band would be scaled a second time on the way back in. A soft-tier run is therefore not replayed by the printout alone — the view echoes no [thresholds.soft] table and cannot mark a --threshold limit as absolute — so the hard tier is the replay contract; see Per-path limits for what a same---tier reload does and does not reproduce.

How the layers compose

This is the one statement of the order; bca check --help and the bca check reference cite it rather than restating it.

  1. [thresholds] — the project table, from bca.toml with --config merged on top.
  2. [thresholds.lang.<slug>] — the file’s language, per metric.
  3. [[thresholds.path]] — every entry whose glob matches the file, in declaration order, per metric; the last match wins.
  4. The tier — [thresholds.soft] or the soft ratio — derived from the limits the three layers above resolved to, so each table’s soft band and hard ceiling come from its own numbers. An absolute [thresholds.soft] limit applies as written to every table, path entries included, so it must sit under each one’s hard limit — an entry that lowers a metric below the soft limit is rejected before the walk, naming the entry.
  5. --threshold name=value — applied last and absolutely, to every table, never scaled.

A limit a --threshold flag names is therefore the limit that runs, for every file, whatever the tables say; and a limit only a path entry names is still computed for every file that entry matches, so the gate it adds can fire.

Per-use-case profiles

The same codebase wants different limits depending on what the gate is for.

Blocking CI gate

This is what the shipped defaults are for. A limit that fails a pull request has to be one the team agrees is a real problem, because the cost of a false positive is a blocked merge and an argument. Pair it with a baseline so the gate fails only on new offenders and regressions, and tighten from there.

Use the shipped defaults unchanged, plus:

[check]
baseline = ".bca-baseline.toml"

Agent feedback loop

When the consumer is a coding agent rather than a human reviewer, a false positive costs one wasted refactor rather than a blocked merge, and the signal arrives while the code is still being written. Tighten toward the published per-function values, and drop the file-size and container limits, which an agent editing one function cannot act on.

[thresholds]
cognitive = 10
cyclomatic = 10
abc = 25
nargs = 4
nexits = 4
"halstead.effort" = 25000

See Feeding metrics to an agent for wiring this into an edit loop, and Suppression markers for telling the agent when complexity is essential rather than accidental.

Legacy audit and triage

When the goal is to rank an unfamiliar codebase rather than to gate it, you want the handful of functions that are worst, not a list of thousands. Set limits at roughly twice the defaults so only extreme outliers surface, run without a baseline, and use bca report markdown rather than bca check.

[thresholds]
cognitive = 40
cyclomatic = 30
abc = 80
"halstead.effort" = 250000
"loc.ploc" = 1500
nom = 60
wmc = 120

Quality reports covers the report output. Adding --vcs ranks by change history as well, which is usually a better triage order than complexity alone: a complex function nobody has touched in three years is not where the bugs are.

Safety-critical and regulated

Standards in this space specify limits directly, and the standard wins over any measurement. MISRA and NASA both cap cyclomatic complexity at 15; the JSF C++ standard allows 20 with a documented exception for large switch statements. McCabe’s original 10 applies where the testing budget supports it, since the number is a bound on the basis-path test count, not a style opinion.

[thresholds]
cognitive = 15
cyclomatic = 10
abc = 30
nargs = 5
nexits = 1
"halstead.effort" = 25000

nexits = 1 encodes the single-exit rule from MISRA C:2023 Rule 15.5. It is contentious outside regulated work, and in most codebases early returns make code simpler rather than harder to follow, so treat it as a compliance setting rather than a general recommendation.

Metrics not gated by default

These are computed and present in every bca metrics output format; bca report markdown|html shows only a subset of them. They are left out of the default table on purpose.

halstead.volume has the most widely-cited threshold of any Halstead measure, the guideline that a function’s volume should stay under 1000. Measured against the corpus it flags about 7% of functions in the median language and 20% in the worst, which makes it useful for ranking and unusable as a gate.

mi.original, mi.sei, and mi.visual_studio are the Maintainability Index family, and they are lower-is-worse: the violation is a value below the limit. Visual Studio’s bands (below 10 poor, 10 to 19 moderate, 20 and above good) apply to its own rescaled 0 to 100 output, which bca reports as mi.visual_studio. mi.original is unbounded above and reaches 167 on the corpus, so the original SEI bands of 65 and 85 do not transfer. The index is also a function of the metrics you are already gating, so gating it too double-counts. See Supported Code Metrics for the formulas.

npm, npa, tokens, and cyclomatic.modified are omitted because each duplicates something already in the table, or because their distributions are dominated by a language idiom rather than by design quality.

Tightening a limit onto a cluster

Converging a limit onto a cluster of existing values is never free while a proportional soft tier is active. Measure a candidate at both tiers before calling it free. The hard-tier reading is the one you naturally take, and it is the one that misleads.

The soft tier measures distance to the limit. A limit chosen to sit exactly on a population’s value therefore maximises soft-tier breach by construction: every function at that value is inside the band the moment the limit lands, and no amount of tidying moves it out, because it is the limit.

This project walked into it. nargs was at 7, and tightening it to 6 looked free: every offender at the tighter limit was already in the baseline, so bca check --threshold nargs=6 reported nothing:

nargs limithard offenderssoft limit (0.95)soft offenders
7 (kept)276.6560
6 (proposed)605.7134

Seventy-four functions sit at exactly 6. A limit of 6 puts all of them 0.3 inside the soft band at once: 74 baseline entries bought for no hard-tier gain, none of which can ever be retired short of rewriting the signatures. The limit stayed at 7; the honest alternative was the real 6 → 5 work. (#1143, #1169.)

Those figures were measured before #1196, when the nargs gate summed a function’s own parameters with every nested closure’s. Most of that population was not parameter-heavy at all: of the 76 functions a limit of 5 would have newly gated, only 17 had six or more parameters of their own, and one had a single parameter plus five contributed by closures in its body. Re-measured on the same tree under own-parameter gating:

nargs limithard offendersnew (unbaselined)soft offendersnew
7 (kept)60160
61603817
5381712999

The cluster trap itself is unchanged and is the reason this section exists: the re-measurement for #1143 found 91 functions sitting at exactly 5, so a limit of 5 still put every one of them permanently inside the soft band. What changed is the size of the real work behind it: signatures rather than mixed cases.

The convergence landed on that evidence. One thing measuring it clarified, which this section previously got wrong: the cluster effect is structural for every integer metric, not a property of landing on a population’s mode. A function sitting exactly at limit L is always above 0.95 x L, so it is always in the soft band. In that measurement the ten functions at exactly 7 were in it before the change, just as the ninety-one at exactly 5 were after. What the limit choice controls is only how many sit there. Read #1143’s objection that way: it was sound because the hard-tier gain was zero, not because 6 was near a mode.

That also means the soft tier carries little information for a small-integer metric, where 0.95 x L and L differ by less than one. It is doing real work for halstead.effort (50000 vs 47500) and almost none for nargs (5 vs 4.75, which flags every compliant function at the ceiling).

bca check --explain-threshold <metric>=<limit> measures both tiers in one walk and names the cluster when it finds one, without touching bca.toml:

$ bca check --explain-threshold nargs=6
nargs: candidate limit 6
  hard tier (limit 6): 60 offenders, 60 already baselined, 0 new
  soft tier (limit 5.7, 0.95x): 135 offenders, 61 already baselined, 74 new
  cluster: 75 of 75 soft-band offenders sit at exactly 6 — the candidate limit itself. …

The trap is not confined to the global table. A per-language override is another way to converge a limit onto a population, and it has the same cliff; --explain-threshold reports a language keeping its own limit on a separate line rather than folding it into the count.

The rule generalises past the soft tier: a limit that lands on a cluster leaves the population with no room, so the next tightening is the one that has to move real code. Prefer a limit in the gap between clusters. See Preview a candidate limit for the flag’s full contract.

Re-deriving for your own codebase

Corpus percentiles are a starting point. Your own distribution is better evidence, and producing it takes one command plus a short script.

bca metrics --no-config -O json -I '*.rs' -X '**/tests/**' . > metrics.jsonl

Each line is one file’s FuncSpace tree. Walk it, keep each space’s own value for the metric you care about, filter by kind to match the metric’s scope ("function", "unit" for loc.*, or a container kind for nom and wmc), and take the 97.5th percentile. Setting a limit there flags your worst 2.5%.

Run two checks afterwards. Count the offenders the limit produces: if it is more than a few percent of your spaces, the gate will be ignored rather than obeyed. Then look at where the values pile up. A natural distribution tails off smoothly, so a cluster of files sitting just under the limit means the limit is shaping the code rather than measuring it, and people are trimming to fit. That happened to this project’s own loc.sloc gate, which is documented in issue #1138.

bca check --write-baseline records today’s offenders so you can adopt a stricter limit without failing on day one. Baselines covers the bootstrap, refresh, and retire flow.

Feeding metrics to an agentic coding tool

This recipe shows how to send maintainability findings to Claude Code or OpenCode after a file-editing tool finishes. It assumes you can run bca check in your project and configure your coding agent. The findings are advice for the agent; the project’s final validation gate still decides when a change is ready.

The hooks use the existing bca check command, which provides:

  • A machine-parseable offender list (per-violation rows on stdout, or --report-format sarif | code-climate | clang-warning | msvc-warning | checkstyle for a structured document).
  • Exit codes that distinguish current findings (2, or 2–5 in tiered mode), a clean check (0), and tool errors (1). The findings cover the whole file, not only the latest edit.
  • Filtering by the project’s policy: baselines, in-source suppression markers, and [check] exclude globs. Put exclusions that must apply to explicitly named files under [check].

After-edit, not keystroke-time

An editor integration can use the Language Server Protocol to update diagnostics while a person types. An agent hook checks the completed file edit. The proposed bca lsp server (#384) and these hooks serve those different uses:

bca lsp (#384)This recipe
ConsumerHuman in an editorAgent in a tool loop
TriggerKeystroke (didChange)Tool call completes (an edit lands)
ReparseIncrementalWhole-file, once per edit
SurfaceMargin diagnosticsText fed back into the model
StatusProposedWorks today with bca check

Use an editor integration for feedback while typing and an after-tool hook for feedback during an agent’s edit loop.

The command every per-tool section below invokes is the same one you would run by hand:

# Exit 2 ⇒ this file has at least one offender. Thresholds come from
# the repo-root bca.toml (discovered automatically); override ad hoc
# with one or more --threshold flags.
bca check path/to/edited_file.rs --threshold cognitive=10

The agent feedback profile suggests tighter limits than a blocking CI gate. Evaluate the extra findings before adopting them: unnecessary refactoring can expand the task or introduce bugs. Choosing thresholds explains the defaults and other profiles.

With thresholds configured in bca.toml (see Local threshold gates), bca check <file> reads the committed limits, baseline, and exclusions. Use the same configuration and tier as CI when you want the same threshold policy.

Put the hook’s exclusions under [check]

One thing does not carry over from the CI invocation, and skipping it produces false positives. A hook names one file per run, and an explicitly named path overrides every walker exclude, following the rg convention that a path you named is a direct request. So a file your project keeps out of scope with -X, --exclude-from, a .bcaignore, or a manifest exclude list is still analyzed the moment the hook passes it by name, and reported as an offender under an exit 2 that frames it as a problem to address.

Walker excludes shape what gets analyzed. Check excludes shape what gets gated. A per-file hook invocation only respects the second:

# bca.toml: survives an explicit path, so the hook sees what CI sees.
[check]
exclude = ["./utils/**", "./benches/**"]

bca warns on stderr whenever an explicitly named path overrides a walker exclude, naming the glob, so the miswiring is visible:

warning: utils/gate.py matches an exclude pattern (./utils/**) but was named explicitly; analyzing anyway

Treat that line as a to-do: move the entry it names to [check] exclude. This repository moved its own dev-tooling globs there for this reason.

The hook uses the nearest bca.toml above the session’s working directory, stopping at the checkout boundary. The reference hooks also recognize targets inside another nested checkout and apply that checkout’s policy.

Claude Code

Use a Claude Code PostToolUse hook to send current threshold findings to the model after a successful file edit. The reference hook returns hookSpecificOutput.additionalContext on stdout and exits 0. The tool’s original result remains intact.

Prerequisites:

  • Claude Code 2.1.139 or later; that release added the hook args field used below (see the official changelog).
  • Node.js LTS and bca.
  • A nonempty [thresholds] table in bca.toml, from bca init or written by hand. Run bca check path/to/a/source-file directly to confirm the configuration loads.

Then copy these files into the same paths in your project:

Merge this configuration into .claude/settings.json, preserving existing settings:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "node",
            "args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/bca-check.mjs"],
            "timeout": 15
          }
        ]
      }
    ]
  }
}

The exec form passes the script path as one argument, including spaces and shell metacharacters. The hook requires Node on PATH; its JSON handling uses no npm packages or jq. Keep only one registration to avoid duplicate checks.

How the hook finds its configuration and analyzer:

  • The event’s cwd selects the nearest manifest within the current checkout. For a target in a different nested checkout, the hook discovers configuration from that target’s directory.
  • CLAUDE_PROJECT_DIR locates the script, but stays at the original project when Claude enters a worktree. Using it to select the analyzer configuration would check the wrong tree.
  • Select $BCA first, unprobed. Otherwise run --version on the checkout’s target/debug/bca, target/release/bca (bca.exe on Windows) and bca on PATH, and take the first whose build inputs: digest equals the one utils/self_scan_digest.py computes over the checkout — the same matching criterion make self-scan applies, so a build that satisfies one satisfies the other (they can still pick different matching files, since the self-scan prefers a matching PATH binary). When none matches, take the newest local build by modification time, then bca on PATH. A copy of the hook without utils/self_scan_digest.py beside it skips the digest step and degrades to that order. An explicit but unavailable $BCA stays unavailable rather than falling back to another binary.
  • Within one checkout, the session’s manifest also applies when it edits a sibling package. Change the session directory to select that package’s own manifest.
  • Nested checkouts, including Git submodules, are separate configuration boundaries. If a nested checkout has no thresholds, the check is unavailable; it does not inherit the parent’s policy.

The hook handles analyzer results as follows; the hook itself exits 0:

  • Exit 0: no model context for a clean or excluded file.
  • Exits 2–5: stdout findings become advisory context, capped at 4,000 characters with a truncation notice.
  • Analyzer failure: no model context for a missing binary, exit 1, abnormal exit, timeout, or output overflow. Each subprocess has a three-second deadline and a 64 KiB output limit per stream. Timeout, cancellation, and output overflow terminate the owned process group on POSIX or process tree on Windows.
  • Concurrent file change: findings are discarded if the file’s identity, size, or timestamps change during analysis.

The adapter keeps no persistent deduplication state, so an unchanged offender can reappear on a later edit. It checks the whole file: [new] and [regr] compare against the configured baseline, not the preceding edit. Use the standing guidance to keep any response within the task’s scope. This matcher covers Edit and Write; shell commands and external editors need separate integration.

Verify model delivery after installation. Using Claude’s Write tool, create a disposable supported source file that breaches a configured limit, and ask Claude to quote the finding it receives. Then simplify the file through Edit and confirm the clean result adds no advisory. In debug output, [bca-check] JSON records distinguish findings, clean/excluded files, out-of-scope paths, superseded results, and unavailable checks. These records go to stderr with exit 0, so Claude does not receive them as additional context. They record the checkout, the analyzer and why it was chosen (analyzerReason), the outcome, and up to 1,000 characters of analyzer stderr for troubleshooting. Offender rows remain in the advisory; CI annotations and summary-file side effects are disabled. The reference suite runs with make plugin-test, including the shared OpenCode helper regressions.

OpenCode V2

Use the reference OpenCode plugin to append current threshold findings to the result of a successful edit, write or patch tool call. The edit stays successful; the advisory is extra text in the tool result, not an error. This is the integration the repository runs on itself, verified with OpenCode 2.1.3.

Prerequisites:

Copy these files into the same paths in your project:

OpenCode loads direct .js files under .opencode/plugins/ automatically (Plugins). Do not add the file to plugins in opencode.json(c): that key takes published packages and local plugin directories, and a single-file path there is skipped with a configured plugin path must be a directory warning in the server log.

How it behaves:

  • It reacts to completed edit, write and patch calls, and to nothing else; shell commands and external editors need separate integration.
  • It finds the configuration and analyzer the same way the Claude Code hook does, including the build inputs: match against a local build. The session’s directory, not the plugin’s, selects the checkout, so a session moved into a Git worktree checks that worktree. A copy without utils/self_scan_digest.py beside it degrades to the newest local build, then bca on PATH.
  • A patch touching several files checks at most 8 of them and says how many it skipped. One advisory is at most 4,000 characters, guidance included; longer reports are truncated with a notice.
  • Findings already delivered for the same file in the same session are not repeated, so a second edit that leaves the offenders unchanged is silent. A clean check, deleting the file, or reloading the plugin resets that; truncated reports are not recorded and can reappear.
  • Analyzer failure, exit 1, timeout and output overflow stay out of the tool result and are logged as [bca-check] warnings.

Verify model delivery after installation. Open a session in the project and use the write tool to create a disposable source file that breaches a configured limit; the tool result should end with a bca advisory block that the model can quote back. Simplify the file with edit and confirm the clean result adds nothing. The server log records [bca-check] analyzer=… reason=… on the first check per checkout, and the plugin keeps a per-location health record under its storage (health/<directory>) with the last outcome and the analyzer chosen. Configuration changes under .opencode/ reload without a restart; if a freshly copied plugin does not load, opencode service restart is the fallback.

Agent guidance (ship this verbatim)

Bare metric feedback can encourage changes that lower scores without improving readability, such as extracting helpers with no useful responsibility. Tell the agent what to do with a finding. Paste this block into your agent rules file (CLAUDE.md or AGENTS.md) as standing policy. Use the short bca-guidance.txt reminder in runtime feedback; the reference hooks bound that text separately from the findings.

The policy block below remains in English in translated editions; copy it as written.

**Responding to `bca` metric feedback.** A threshold violation is a
reason to review readability, not proof of a defect. Improve the code
where that helps the current task; lowering a score is not itself a goal.

- **Do not game the metric.** Do not extract a helper that exists only
  to move complexity off one function, split a cohesive function at an
  arbitrary line, collapse readable branches into a dense expression,
  or inline/obfuscate logic to dodge the count. These lower the
  per-function score without necessarily improving readability.
  New helpers also add names and interfaces that readers must understand.
- **Refactor only when it truly clarifies.** A good split has a name
  that means something and a boundary a reader would have drawn anyway.
  If you cannot name the extracted piece without inventing a
  `foo_part2`, reconsider the split.
- **When the complexity is essential, suppress with a reason.** Some
  functions are clearest left whole, such as a
  dispatch `match`, a hand-rolled parser table, an exhaustive state
  machine. For these, do not contort the code: add a suppression marker
  with the rationale on the same line:
  `// bca: suppress(cognitive) -- exhaustive opcode dispatch`.
  A clear function with an honest marker is better than a
  "compliant" tangle.
- **Keep the fix within the task.** Review findings relevant to the
  current change. Findings may concern other functions or the whole
  file; mention unrelated findings without expanding the edit.

Honest suppression (exact syntax)

Suppression requires the exact syntax described in Suppression markers:

  • Per function: place the marker in a comment inside the function body, naming the metric(s): // bca: suppress(cyclomatic, abc) -- hand-rolled parser table. A bare // bca: suppress (no list) silences every suppressible metric for that function; tokens remains enforced.
  • Whole file: // bca: suppress-file(halstead, nargs, nexits) anywhere in the file; the bare // bca: suppress-file form silences every suppressible metric file-wide. It does not suppress tokens.
  • Put the rationale on the marker line, after a metric list. The text after the list is free-form and needs no separator. Without a metric list, trailing text is not accepted because it cannot be distinguished from prose about the marker. Include a reason so a reviewer can assess the exemption. bca exemptions lists the markers for that review.
  • Use canonical metric names. The accepted identifiers are abc, cognitive, cyclomatic, halstead, loc, mi, nargs, nexits, nom, npa, npm, wmc. It is nexits, not exit (the legacy exit alias was retired). An unknown identifier warns and is skipped; the recognized names beside it still suppress, so suppress(cognitive, exit) silences cognitive and complains about exit. tokens is deliberately not suppressible; treat it as a hard resource cap.

Gate at the task boundary, not per edit

Per-edit hooks provide early advice. Run the project’s validation gate after a coherent change, including its tests and linters. The make self-scan / pre-commit pattern uses the CI limits for its hard gate. For higher-is-worse metrics, its default soft threshold is 95% of the hard limit, giving an early warning near that limit.

Intermediate edits can produce temporary findings that disappear when the refactoring is complete. Let the agent finish a coherent edit before responding to those findings. A metric is an indirect indicator of readability; it does not establish correctness.

Avoid requiring an extra metric check after every edit when the hook already runs one. Keep the final validation requirement explicit and rerun a check when its inputs change or its previous result is unavailable.

Caveats

  • Metric-gaming: optimizing a proxy can undermine the intended goal, a risk described by Goodhart’s law. Asking for a lower number can encourage moving complexity across function boundaries without improving readability. Include the agent guidance and explain justified suppression.
  • Thresholds are indicators, not correctness checks: a threshold crossing calls for a readability review. It does not prove the code is defective. Send findings as advice the agent considers, and verify behavior with tests and other checks appropriate to the change.
  • Scope creep: a finding can invite a larger refactoring than the task requires. Keep fixes within the current task. Findings may concern unrelated functions or the whole file; report unrelated findings without expanding the edit.

AST queries

Recipes that work with the parsed syntax tree directly: searching for node types, counting them, or dumping the tree.

Library-side equivalents. Every recipe below has an in-process Rust counterpart in Walking the AST directly. Use that when shelling out per file is too slow or when you want to compose metrics with custom AST analysis in one parse.

Detect parse errors before committing

Tree-sitter exposes a synthetic ERROR node anywhere it could not parse. Use find to surface them:

bca find \
    --include "*.rs" \
    --paths "$PWD" \
    -t ERROR

One glob per occurrence. --include and --exclude take exactly one value each time they appear; repeat the flag for multiple globs (--include "*.rs" --include "*.py"). A positional path that follows is never swallowed. The = form (--include="*.rs") also works.

A clean run prints nothing. Wire this into a pre-commit hook to fail fast when a syntactically broken file is staged.

Count specific syntactic constructs

count takes one or more node types via the repeatable -t/--type flag and reports the totals. For example, to count if, for, and while constructs across a Rust project:

bca count \
    --include "*.rs" \
    --paths src/ \
    -t if_expression -t for_expression -t while_expression

The exact node-type names come from the underlying tree-sitter grammar. To discover them, dump the AST of a small sample file (see below) and read the node names off the tree.

Find all unsafe blocks in a Rust crate

bca find \
    --include "*.rs" \
    --paths src/ \
    -t unsafe_block

Each match prints the line and column range of the node, under a banner naming its file; see Reading find output for the format.

Dump the AST of a file

Useful for understanding why a metric came out the way it did, or for discovering the tree-sitter node names you need for find / count:

bca dump --paths src/lib.rs

To narrow the dump to a specific function or block, add line bounds with the --line-start and --line-end flags (they must follow the dump subcommand):

bca dump \
    --paths src/lib.rs \
    --line-start 42 --line-end 88

--line-start / --line-end apply to dump and find, so the same range scopes a search to a single function:

bca find \
    --paths src/lib.rs \
    --line-start 42 --line-end 88 -t return_expression

The short --ls / --le spellings remain as deprecated aliases and are slated for removal in the next major.

List every function or method

For a quick human-readable inventory:

bca functions \
    --include "*.rs" \
    --paths src/

The output is a tree per file: an In file … header followed by an indented row per function with name and line span. It is meant for reading, not parsing.

For tooling that needs a structured inventory (coverage mapping, documentation generation, code-owner reports), use the JSON metrics output instead and walk .spaces[] recursively with jq, taking entries whose kind is function:

bca metrics \
    --include "*.rs" \
    --paths src/ \
    -O json \
  | jq -c '
      . as $root
      | def funcs: if .kind == "function" then [.] else [] end
                   + (.spaces // [] | map(funcs) | add // []);
      funcs[] | {file: $root.name, name, start_line, end_line}
    '

This emits one JSON object per function, ready to pipe into downstream tooling.

Exporting metric data

The metrics, ops, and preproc subcommands all support structured output formats meant for machine consumption. This page covers writing those documents, diffing two runs, discovering the metric catalog at runtime, and stripping comments for downstream tools. Pair the output with a JSON processor like jq for ad-hoc analysis, or feed it into a database or dashboard.

Export per-file metrics as JSON

bca metrics \
    --paths src/ \
    -O json \
    --output-dir /tmp/metrics

This writes one JSON file per analyzed source file under /tmp/metrics/. The output filename is the input path with the format extension appended: src/lib.rs becomes src/lib.rs.json, not src/lib.json. Use --pretty if you intend to read the files by hand:

bca metrics -p src/ --pretty -O json --output-dir /tmp/metrics

To collect the whole run into one file instead of a tree, use --output <file>; it writes a single aggregate document (a top-level JSON array of the per-file results):

bca metrics -p src/ -O json --output /tmp/metrics.json

Its elements are ordered by file path regardless of the order the parallel workers finished in, so the document is byte-identical between runs over unchanged sources. bca ops --output <file> orders its aggregate the same way.

CBOR (-O cbor) is the most compact format; it is binary and so requires a destination (--output or --output-dir). JSON, TOML, and YAML stream to stdout when no destination is given, which suits pipelines.

Compare two metric runs with bca diff

bca diff compares two JSON metric runs and reports, per metric, which files changed (old → new), plus any files added or removed between the two sets. Each side is a single per-file JSON document, the aggregate file metrics -O json --output <file> writes, or a whole directory tree of either (what metrics -O json --output-dir <dir> writes), so the common workflow is two bca metrics runs into separate directories:

snap=$(mktemp -d "${TMPDIR:-/tmp}/bca-diff.XXXXXX")

# Capture the "before" state.
bca metrics -p src/ -O json --output-dir "$snap/before"

# ...make a change (for example, bump a tree-sitter grammar)...

# Capture the "after" state and diff.
bca metrics -p src/ -O json --output-dir "$snap/after"
bca diff "$snap/before" "$snap/after"

The output buckets every per-file delta by metric name, using the same names bca list-metrics prints (cyclomatic, cognitive, sloc, …):

2 metric(s) changed, 1 added file(s), 0 removed file(s), 0 added field(s), 0 removed field(s)

## Added files
  src/new_module.rs

## cyclomatic (2 change(s))
  src/lib.rs.max  4 → 6
  src/lib.rs.sum  12 → 14

## halstead (1 change(s))
  src/lib.rs.effort  5820.3 → 7104.9

A leaf that carries a number on one side only (a family the other capture never computed because the two runs used different --metrics sets, or a field a newer bca started emitting) has nothing to diff against, so it is never compared with zero. It is listed under ## Added fields / ## Removed fields as a <file> <metric> <field> row, is counted in the summary line, and counts as a difference for --exit-code. When the whole family is missing on one side — the different---metrics case, where every leaf is one-sided for the same reason — the text and Markdown forms roll its leaves up to one row per file and metric that counts them, src/lib.rs mi (all 3 field(s)); the per-leaf rows are kept where the family exists on both sides and only some of its leaves differ. The roll-up is still per file: a default-metrics capture diffed against a --metrics cyclomatic one renders 17 rows and about 1.2 KiB of Markdown per file (down from 110 rows and about 7 KiB on the same pair), so GitHub’s 65 536-character comment limit is reached at roughly 53 files rather than under ten. A form that is independent of tree size — one row per (metric, field) with a file count — is not implemented. The JSON form is per leaf either way: added_fields / removed_fields are arrays of {file, metric, field} rows, and the summary counts are leaf counts, so the rolled-up row and the summary line’s N added field(s) can disagree in number by design. --metric filters these rows as it filters buckets; --min-change does not apply to them, since there is no delta to measure. “Present” means numeric: a leaf written as null on one side (how a non-finite derived value, such as a vcs ratio with nothing to divide by, is serialized) reads as absent there, so two captures by the same bca version can list a field that became applicable between them.

A document pairs with the other side’s document of the same name, the source path bca recorded, compared in lexical form: . components are dropped, so captures taken with -p ./src and -p src pair, and a .. is kept. A directory side reads every *.json file under it and passes over any other file, such as a README. Input bca diff cannot pair is an error (exit 1) naming it, never a file silently missing from one side: a directory with no *.json file (a capture written with -O yaml, say), JSON with no metrics object, two documents with one name, and a document with no name unless it is a whole file inside a directory side, where its relative path stands in. bca metrics writes no name for a source path that is not valid UTF-8, and no layout can diff such a file. A symlinked *.json file is read like the file it names, and one that dangles is an error. A symlinked directory inside a side is not entered, as the source walk enters none.

Useful flags:

  • --format markdown produces Markdown for a sticky PR comment; --format json produces a stable machine-readable schema for CI consumers.
  • --min-change <N> reports only deltas whose absolute change is at least N (the default 0 reports any change).
  • --metric <name> (repeatable) restricts the diff to specific metrics.

A saved document carries no record of the walker settings that produced it, so bca diff cannot tell two captures made under different settings apart from a real change. Compare captures taken by the same version with the same exclude_tests, cyclomatic_count_try and mi_charges_docs; a different MI documentation mode moves the mi.sei value of every documented space. --since runs both sides itself with one set of flags, so it cannot mix modes.

Diff against a git ref with --since

To answer “what did my uncommitted change do to the metrics?”, --since <ref> skips the two captures: it analyzes the tree at a git ref for the before side and diffs it against the current working tree, which is always the after side:

# Before = the tree at HEAD~1; after = the current working tree.
bca diff --since HEAD~1 -p src/

# The same diff, scoped by the optional positional instead of -p. It
# selects the src/ subtree on both sides; it never names another tree.
bca diff --since HEAD~1 src

--since materializes the ref’s tree into a temporary directory (via git ls-tree + git cat-file), runs the same metric walk against it, then diffs. The temp tree is removed when the command finishes, whether it succeeds or exits with an error. Only a run killed by a signal (Ctrl-C included) leaves it behind in the temporary directory. The same -p/--paths, -I/--include, and -X/--exclude selection applies to both sides so they analyze the same file set. Each side reads an --exclude-from file for itself, a relative one from its own root (the ref’s copy for the before side), so --exclude-from - (stdin, readable only once) is rejected, as is --paths-from. Selection paths are repo-root-relative and must be relative: the working-tree side is anchored at the repository root (matching the whole-tree materialization of the ref), so bca diff --since produces the same result from any subdirectory. An absolute --paths or positional is rejected (exit 1) because it cannot address the extracted ref tree.

Unlike bca check --since (best-effort), bca diff --since is an explicit request: an unresolvable ref, a missing git, or a non-git working directory is a hard error (exit 1). --since takes at most one positional, the relative path scope above; passing two is an error.

bca diff is informational, not a gate, and exits 0 on success. The opt-in --exit-code flag changes that: it exits 2 when the filtered diff is non-empty.

Pull a single metric across an entire tree

Combine streamed JSON output with jq to extract one value per file:

bca metrics -p src/ -O json \
  | jq -c '{file: .name, mi: .metrics.mi.visual_studio}'

The same idea works for any metric: cyclomatic.sum, cognitive.sum, loc.sloc, and so on. Run bca list-metrics descriptions to see the catalog.

Discover the metric catalog at runtime

Tooling that drives the CLI should not hard-code metric names. Ask the binary:

bca list-metrics                # one name per line
bca list-metrics descriptions   # name + summary

This is the right input for code generators, schema definitions, or tab-completion.

Extract operands and operators (Halstead)

ops emits the raw operand and operator lists per file, which is the input to Halstead-style metric calculations beyond what the built-in report shows:

bca ops \
    --include "*.rs" \
    --paths src/ \
    -O json --pretty \
    --output-dir /tmp/ops

One glob per occurrence. --include and --exclude take exactly one value each time they appear; repeat the flag for multiple globs (--include "*.rs" --include "*.py"). A positional path that follows is never swallowed. The = form (--include="*.rs") also works.

Each output file mirrors the input path under /tmp/ops/.

Both lists are sorted, so re-running ops over unchanged sources produces byte-identical output. It is safe to diff between runs, check into a repository, or use as a cache key.

Strip comments from a tree

strip-comments rewrites source so that downstream tools that do not understand comment syntax can still consume the code. Output routing has three modes:

  • stdout (default). With neither flag, the stripped source streams to stdout, which suits a single file in a pipeline.
  • --output / -o <FILE> (single file). Writes the stripped source to <FILE>, leaving the input untouched. Only meaningful for one input file; mutually exclusive with --in-place.
  • --in-place (multi-file). Rewrites each matched input file on disk. Use this for a whole tree; mutually exclusive with --output.
# Stream a single file with comments removed.
bca strip-comments --paths src/lib.rs

# Write a single stripped file to a new path (input untouched).
bca strip-comments --paths src/lib.rs --output src/lib.stripped.rs

# Rewrite every Python file in src/ in place.
bca strip-comments --include "*.py" --paths src/ \
    --in-place

--in-place is destructive: commit or back up the tree first. Passing both --in-place and --output is a usage error.

Driving the REST API

bca-web exposes the same analysis primitives as the bca CLI over HTTP. Use it when the consumer is a long-running service (an editor plugin, CI worker, or web app) that should not pay the cost of spawning the CLI per file. This page is a set of end-to-end curl calls, one per endpoint; the REST API reference documents every field.

Every endpoint is mounted under the /v1 prefix; the old unprefixed paths were removed at the 2.0 release (#637) and now return 404.

Start the server

bca-web --host 127.0.0.1 --port 8080 -j "$(nproc)"

Verify it is up:

curl -sf http://127.0.0.1:8080/v1/ping && echo "ok"
# => ok

/ping returns 200 OK with an empty body. curl -sf exits 0 on success and non-zero on any HTTP error, which is what scripts want.

For building, flag and environment-variable tuning, resource limits, and the security posture of the daemon itself, see Operating bca-web.

Compute metrics for an inline snippet

curl -s http://127.0.0.1:8080/v1/metrics \
    -H 'Content-Type: application/json' \
    -d '{
          "id": "snippet-1",
          "file_name": "demo.rs",
          "code": "fn add(a: i32, b: i32) -> i32 { a + b }",
          "scope": "full"
        }' \
  | jq '.root.metrics'

scope: "file" returns only top-level metrics; "full" (the default) walks every function and class space inside the snippet. The server infers language from file_name, so the extension matters.

Compute metrics for a file from disk

curl --data-binary plus jq packages a real file into the JSON envelope the server expects:

jq -nc \
    --arg id "$(uuidgen)" \
    --arg file_name "src/lib.rs" \
    --rawfile code src/lib.rs \
    '{id: $id, file_name: $file_name, code: $code, scope: "full"}' \
  | curl -s http://127.0.0.1:8080/v1/metrics \
      -H 'Content-Type: application/json' \
      --data-binary @- \
  | jq '.root.metrics.cyclomatic, .root.metrics.cognitive'

This pattern (jq -n --rawfile to build the request, curl --data-binary @- to stream it) avoids quoting problems with multi-line source code.

Strip comments through the API

The endpoint is /comment (singular). It has two variants selected by Content-Type:

  • application/json wraps the request and response in JSON. The response code field is a string holding the stripped source (the request code arrived as a JSON string, so the result is valid UTF-8; #629 replaced the earlier byte-array shape).
  • application/octet-stream accepts the source as the raw request body and returns the stripped source as the raw response body. This is the simpler variant to use from the shell.

Octet-stream form (recommended for one-off shell use):

curl -s "http://127.0.0.1:8080/v1/comment?file_name=demo.py" \
    -H 'Content-Type: application/octet-stream' \
    --data-binary $'# leading comment\nprint("hi")  # trailing'
# => print("hi")

JSON form (use when your client speaks JSON natively):

curl -s http://127.0.0.1:8080/v1/comment \
    -H 'Content-Type: application/json' \
    -d '{
          "id": "strip-1",
          "file_name": "demo.py",
          "code": "# leading comment\nprint(\"hi\")  # trailing"
        }' \
  | jq -r '.code'

The JSON response carries the same id you sent, so a client that multiplexes many requests can correlate them.

Extract function spans for an editor plugin

The endpoint is /function (singular):

curl -s http://127.0.0.1:8080/v1/function \
    -H 'Content-Type: application/json' \
    -d '{
          "id": "spans-1",
          "file_name": "demo.rs",
          "code": "fn a() {}\nfn b() {}\n"
        }' \
  | jq '.spans'

Each entry has name, start_line, and end_line; name is null when the parser could not resolve the function’s name (an anonymous or malformed definition). That is enough for an editor to draw a function navigator without re-parsing the file locally.

Rank a repository by change-history risk

The /vcs endpoint analyses a git working tree that already exists on the server’s filesystem (change history has no in-request representation), and returns the files ranked by composite risk score. See Change-history (VCS) metrics for the signal and formula reference.

Security: unlike the other endpoints, /vcs takes a server-side repo_path and will walk any git repository the server process can read, returning that repo’s file paths and change signals. Do not expose /vcs to untrusted clients without an authorization layer; the default 127.0.0.1 bind keeps it local.

curl -s http://127.0.0.1:8080/v1/vcs \
    -H 'Content-Type: application/json' \
    -d '{
          "id": "risk-1",
          "repo_path": "/srv/checkouts/my-project",
          "top": 20
        }' \
  | jq '.files[] | {path, risk_score: .vcs.risk_score, churn_recent: .vcs.churn_recent}'

The body accepts the same knobs as bca vcs (long_window, recent_window, top, ref, risk_formula, file_types, full_history, include_merges, follow_renames, exclude_bots, bot_pattern, as_of, emit_author_details, author_hash_key, include_deleted, bus_factor_threshold, no_cache, cache_dir) as optional fields; top defaults to 50 like the CLI, and 0 returns every file. file_types scopes the ranking (metrics, the default, or all, or a rs,py-style extension list), mirroring the CLI --file-types. A repo_path that does not exist or is not a git working tree, or a malformed window / timestamp / formula / scope, returns 400 with the uniform JSON error body. Unknown fields are rejected with 400 too.

Trend over time

POST /vcs/trend samples the same metrics at several points in time and returns a per-file time series (see Historical trend). The body takes the /vcs fields except the cache controls (no_cache / cache_dir are rejected with 400, since trend does not cache) plus points (>= 2, default 12), span (default 12mo), and top_deltas (default 10; 0 returns all).

curl -s http://127.0.0.1:8080/v1/vcs/trend \
    -H 'Content-Type: application/json' \
    -d '{
          "id": "trend-1",
          "repo_path": "/srv/checkouts/my-project",
          "points": 12,
          "span": "24mo",
          "top": 20
        }' \
  | jq '.deltas.regressed[] | {path, delta}'

A point count below 2 (or above the supported maximum) returns 400 with the uniform JSON error body, like the other bad-request cases.

Just-in-time commit / diff scoring

POST /vcs/jit scores a single commit (see Just-in-time scoring). The body takes repo_path, commit (default HEAD), and the long_window / recent_window / full_history / include_merges / follow_renames / as_of knobs; it returns the commit JitReport JSON with the echoed id.

curl -s http://127.0.0.1:8080/v1/vcs/jit \
    -H 'Content-Type: application/json' \
    -d '{ "id": "jit-1", "repo_path": "/srv/checkouts/my-project",
          "commit": "HEAD" }' \
  | jq '{risk_score, purpose: .commit.purpose}'

To score an arbitrary diff instead, send a diff field carrying the unified diff (no repo_path needed). The response is then the partial report: source: "diff", a partial_risk_score, and no history / experience / purpose groups, which are absent rather than zero. The partial score is not comparable to a commit score.

git diff | jq -Rs '{id: "jit-diff", diff: .}' \
  | curl -s http://127.0.0.1:8080/v1/vcs/jit \
      -H 'Content-Type: application/json' -d @- \
  | jq '{source, partial_risk_score}'

A malformed diff (or an unresolvable commit, or a repo_path that is not a git working tree) returns 400 with the uniform JSON error body.

Calling the API from CI

For a short-lived CI job, start the server as a background process and stop it when the job finishes:

bca-web --port 8080 &
SERVER_PID=$!
trap 'kill "$SERVER_PID"' EXIT

# Wait for it to come up.
until curl -sf http://127.0.0.1:8080/v1/ping >/dev/null; do sleep 0.1; done

# … run your analysis calls here …

For longer-lived workers, run the server as a systemd unit (or a container) and point your jobs at its host and port.

Using as a Library

big-code-analysis is published on crates.io as a Rust library. The CLI (bca) and REST server (bca-web) are both thin wrappers around the same public API, so anything they can do you can do directly from your own crate.

This section is task-oriented. For full type signatures and field docs, follow the rustdoc on docs.rs.

When to embed the library

Use the library instead of shelling out to bca when you want any of the following:

  • In-process analysis. Avoid spawning a subprocess per file when scoring thousands of files in a custom tool, IDE plugin, or static-analysis pipeline.
  • In-memory source. Score generated, pre-processed, or streamed source without writing it to disk first. See Analyzing in-memory source.
  • Selective walking. Drive a custom traversal over the FuncSpace tree to extract per-function metrics on your own schedule. See Walking FuncSpace results.
  • Custom output. Skip the bca serializers (JSON / YAML / TOML / CBOR in the CLI, CSV / SARIF / Checkstyle in the library’s output module) and emit your own format, such as a database row.

If you only want a Markdown quality report or a CI threshold gate, the bca CLI is faster to wire up.

Pages in this section

API stability

The library is on the 2.x line and ships under a written stability contract: the shape of the public API is held stable across patch and minor bumps, and breaking changes are reserved for the next major bump. Every example in this section compiles against the current published crate and is expected to keep compiling across 2.x without edits.

Metric values may still drift across minor bumps when a grammar pin moves or a metric definition is fixed; see STABILITY.md § What is stable in value for the carve-out. Each drift is called out in the changelog entry that introduces it.

Quick start

This page shows the minimum code needed to compute metrics from a string of source code. It assumes a working Rust toolchain and a crate to add the dependency to.

1. Add the crate

# Cargo.toml
[dependencies]
big-code-analysis = "2.6.0"

The crate uses Rust edition 2024 and pins rust-version = "1.94". Older toolchains will not build it; see the MSRV section of STABILITY.md for the policy.

2. Compute metrics from a string

The recommended entry point is analyze: pass a Source carrying the language, source bytes, and an optional display name, plus a MetricsOptions for any per-traversal flags. No filesystem path is needed.

use big_code_analysis::{analyze, MetricsOptions, Source, LANG};

fn main() {
    let source = "fn add(a: i32, b: i32) -> i32 { a + b }";

    let space = analyze(
        Source::new(LANG::Rust, source.as_bytes())
            .with_name(Some("snippet.rs".to_owned())),
        MetricsOptions::default(),
    )
    .expect("Rust source should parse");

    println!(
        "cognitive complexity (file-level): {}",
        space.metrics.cognitive.cognitive_sum(),
    );
}

Source::name becomes the top-level FuncSpace::name; passing None leaves the top-level name unset. The return type is Result<FuncSpace, MetricsError>. In practice the Err variant means the requested language’s Cargo feature is disabled in this build. A parse failure does not produce Err: tree-sitter recovers with ERROR nodes. See Error handling for the variant set and matching patterns. MetricsError is #[non_exhaustive], so always include a _ arm when matching.

Tip: use big_code_analysis::prelude::*; brings the recommended entry points (analyze, Ast, Source, MetricsOptions, MetricsError, FromPathError, SkipReason, LANG, FuncSpace, CodeMetrics, SpaceKind, Metric) into scope in one line. Anything outside the prelude is still reachable by name, for example use big_code_analysis::guess_language;.

Need more than metrics from one parse (operators and operands, an AST dump, a function-span list)? Parse once with Ast::parse and call the per-pass methods on the handle. See Parse once, run metrics many times. If you already drive your own tree_sitter::Parser, adopt the resulting tree with Ast::from_tree_sitter (see Reusing an existing tree-sitter Tree).

3. Inspect the result

FuncSpace is a tree of spaces. The top-level node represents the whole file; its spaces field holds nested function, class and impl spaces. Every node carries the same CodeMetrics struct, so you can read any metric at any level.

use big_code_analysis::{analyze, MetricsOptions, Source, SpaceKind, LANG};

fn main() {
    let source = "\
fn outer() {
    fn inner() {}
}
";
    let space = analyze(
        Source::new(LANG::Rust, source.as_bytes())
            .with_name(Some("snippet.rs".to_owned())),
        MetricsOptions::default(),
    )
    .expect("Rust source should parse");

    assert_eq!(space.kind, SpaceKind::Unit);
    assert_eq!(space.spaces.len(), 1); // `outer`
    assert_eq!(space.spaces[0].spaces.len(), 1); // `inner`
}

For a deeper walk over FuncSpace, see Walking FuncSpace results.

Picking a language

If you do not know the language up front, use guess_language. It consults the path extension, an Emacs mode declaration in the buffer, and the shebang, in that order:

use std::path::PathBuf;

use big_code_analysis::{analyze, guess_language, MetricsOptions, Source};

fn main() {
    let source = b"print('hi')\n";
    let path = PathBuf::from("hello.py");

    let (Some(lang), _name) = guess_language(source, &path) else {
        eprintln!("unrecognised language");
        return;
    };

    let _space = analyze(
        Source::new(lang, source).with_name(Some("hello.py".to_owned())),
        MetricsOptions::default(),
    );
}

guess_language returns (None, _) only when the extension, the mode declaration, and the shebang all fail to name a language; treat that as “skip this file” rather than as a parse error.

Analyzing in-memory source

big-code-analysis never requires source to live on disk. The recommended entry point analyze takes a Source carrying the language, source bytes, and an optional caller-supplied display name. No filesystem path is involved unless the C/C++ preprocessor lookup needs one (Source::preproc_path).

This is useful for:

  • Scoring generated code before it is written out.
  • Scoring pre-processed or bundled source, such as the output of a template expansion.
  • Driving the analyzer from a language server or editor plugin that already holds the buffer in memory.
  • Running stdin pipelines and unit tests that should not touch the filesystem.

Reading from a buffer

#![allow(unused)]
fn main() {
use big_code_analysis::{analyze, MetricsOptions, Source, LANG};

fn analyze_buffer(source: &[u8]) -> Option<u64> {
    // `Source::name` is the display identifier baked into the
    // top-level `FuncSpace`. Pick whatever is meaningful for
    // downstream consumers (logs, JSON output); pass `None` if
    // you have nothing useful to attach.
    let space = analyze(
        Source::new(LANG::Python, source).with_name(Some("<stdin>".to_owned())),
        MetricsOptions::default(),
    )
    .ok()?;

    Some(space.metrics.cognitive.cognitive_sum())
}
}

Source::new borrows the source bytes; the caller keeps ownership. If your downstream pipeline needs to highlight findings on the same bytes, you can keep using the original buffer after analyze returns.

Reading from stdin

use std::io::{self, Read};

use big_code_analysis::{analyze, MetricsOptions, Source, LANG};

fn main() -> io::Result<()> {
    let mut source = Vec::new();
    io::stdin().read_to_end(&mut source)?;

    let space = match analyze(
        Source::new(LANG::Javascript, &source)
            .with_name(Some("<stdin>".to_owned())),
        MetricsOptions::default(),
    ) {
        Ok(space) => space,
        Err(err) => {
            eprintln!("parse failed: {err}");
            std::process::exit(1);
        }
    };

    println!("{}", space.metrics.cyclomatic.cyclomatic_sum());
    Ok(())
}

Picking the language from content

If you do not know the language up front, combine guess_language with analyze. guess_language checks the path extension, an Emacs mode declaration, and the shebang, in that order:

#![allow(unused)]
fn main() {
use std::path::PathBuf;

use big_code_analysis::{analyze, guess_language, MetricsOptions, Source};

fn analyze_unknown(path: PathBuf, source: Vec<u8>) -> Option<()> {
    let (lang, _name) = guess_language(&source, &path);
    let lang = lang?;
    // `.ok()?` collapses `MetricsError` into `None` so this helper's
    // `Option` return shape is preserved. See `error-handling.md` for
    // a richer mapping that preserves the variant.
    let _space = analyze(
        Source::new(lang, &source)
            .with_name(path.to_str().map(str::to_owned)),
        MetricsOptions::default(),
    )
    .ok()?;
    Some(())
}
}

guess_language returns (None, _) when the extension, the mode line, and the shebang all fail to name a language. Treat that as “skip” rather than as a hard error.

Watch out for these

  • Name identity matters. Top-level FuncSpace::name is whatever string you put in Source::name. Two analyses sharing the same name look identical to a downstream consumer that keys on it. Use distinct labels for distinct buffers.
  • Source::name is Option<String>. Passing None leaves the top-level FuncSpace::name as None, which suits ad-hoc snippets with no meaningful identity. Downstream consumers that require a stable identifier should check for None explicitly.
  • No filesystem fallback. Unlike the CLI, the library does not read sibling files, follow #includes, or interpret a .gitignore. Feed it exactly the bytes you want analyzed.

Reusing one parse for several passes

analyze is the one-shot entry point: bytes in, FuncSpace out. When you need more than metrics from a single parse (operators and operands, an AST dump, a function-span list), parse once with Ast::parse and call the per-pass methods on the returned handle (metrics, ops, dump, functions, and so on). See Parse once, run metrics many times.

Reusing an existing tree-sitter Tree

Callers who already drive tree-sitter for syntax highlighting, code folding, or queries otherwise parse every file twice: once for their own tree, once inside the metric walker. The parse seam lets you hand big-code-analysis an already-parsed tree_sitter::Tree and get the same FuncSpace back without re-parsing.

Use Ast::from_tree_sitter. It adopts a caller-built tree_sitter::Tree and lets you run the metric walker more than once against the same parse: different MetricsOptions::with_only selections, custom tree-sitter walks interleaved with metrics, or Ast::ops for operator and operand extraction. See Parse once, run metrics many times. It carries an explicit name: Option<String> rather than deriving the top-level FuncSpace::name from a path via lossy UTF-8 conversion.

When to use this

Use the parse seam if you:

  • Already keep a tree_sitter::Tree per open buffer (editor, language server, custom static-analysis pipeline) and want to reuse that parse for metrics rather than paying the byte-based cost again.
  • Want to run several passes (metrics, AST dump, custom analysis) against one parse result.
  • Intend to pin tree-sitter on your side without taking a separate dependency from this library. The re-exported big_code_analysis::tree_sitter module is the same crate we link against, so the types agree by definition.

Use the byte-based entry point analyze (with a Source) if you do not already have a tree. It constructs the parser internally and owns the parse end to end.

Working example

use big_code_analysis::{analyze, tree_sitter, Ast, LANG, MetricsOptions, Source};

let source_code = "fn main() { if true { 1 } else { 2 }; }";
let source = source_code.as_bytes().to_vec();

// Step 1: build a tree with the *re-exported* tree-sitter crate.
// Using `big_code_analysis::tree_sitter` (rather than a direct
// `tree-sitter` dependency on your side) guarantees the version
// matches the one the metric walker was compiled against.
let mut parser = tree_sitter::Parser::new();
parser
    .set_language(
        &LANG::Rust.tree_sitter_language().expect("rust feature enabled"),
    )
    .expect("rust grammar pinned to a compatible version");
let tree = parser
    .parse(&source, None)
    .expect("parser has a language set");

// Step 2: adopt the tree with an explicit display name.
let from_tree = Ast::from_tree_sitter(
    LANG::Rust,
    tree,
    source.clone(),
    Some("foo.rs".to_owned()),
)
.expect("rust feature enabled")
.metrics(MetricsOptions::default())
.expect("non-empty input");

// Step 3 (optional): confirm the values match the byte-based path.
let from_bytes = analyze(
    Source::new(LANG::Rust, &source).with_name(Some("foo.rs".to_owned())),
    MetricsOptions::default(),
)
.expect("non-empty input");

assert_eq!(
    from_tree.metrics.cyclomatic.cyclomatic_sum(),
    from_bytes.metrics.cyclomatic.cyclomatic_sum(),
);

The same shape works for any LANG variant: pass the matching grammar to tree_sitter::Parser::set_language (via LANG::tree_sitter_language) and the metric walker produces the same FuncSpace it would have produced from bytes.

The single tree-adoption seam

Ast::from_tree_sitter is the entry point for tree reuse. It dispatches on a LANG at runtime and hides the parser plumbing entirely. The former lower-level path (the generic Parser<T> / ParserTrait and the per-language *Parser / *Code tag types) is now crate-private (pub(crate)) and no longer part of the public surface; see STABILITY.md. Ast::from_tree_sitter exposes no per-language tag types or trait bounds.

Out of scope

  • Incremental re-computation. Applying a tree_sitter::InputEdit and re-querying only the changed spans is not supported yet; the metric walker still walks the entire tree on every call. The parse seam is the first step; making the walker itself incremental is a follow-up.
  • Promising every Node method. Node exposes its inner tree_sitter::Node through Node::as_tree_sitter() for ad-hoc traversal, and only the accessor list in STABILITY.md § Escape hatches is shape-stable; the helpers that take a Search or Ancestors are not covered.

Parse once, run metrics many times

The one-shot entry point analyze re-parses its Source on every call. For pipelines that score a file several times (different metric subsets, an interleaved custom tree-sitter walk, or a metric re-run after a configuration change) that re-parse is wasted work.

The Ast type, added in 1.1.0, exposes the seam: parse the source once, then call Ast::metrics as many times as you need against the held parse. Ast::from_path is the file-backed counterpart: it reads, language-detects and parses one file, returning a FromPathError naming the reason when it cannot.

When to use this

Reach for Ast when any of the following applies:

  • Selective metric runs. You compute one set of metrics for a report, then another for a CI threshold gate, against the same file.
  • Custom tree-sitter walks. You already drive a tree_sitter::Tree for queries, highlighting or symbol extraction and want to fold the metric walker into the same parse.
  • Cached analysis. A language-server-like service that holds parsed files in memory re-runs metrics on demand when configuration changes, without going back to bytes.

If you only ever compute every metric once per file, stick with analyze. It delegates to Ast internally, so the shapes line up, but the one-shot API stays simpler.

Selective metrics across calls

#![allow(unused)]
fn main() {
use big_code_analysis::{Ast, LANG, Metric, MetricsOptions, Source};

let source = b"fn f(x: i32) -> i32 { if x > 0 { 1 } else { -1 } }";

// One parse, two metric subsets.
let ast = Ast::parse(Source::new(LANG::Rust, source))
    .expect("rust feature enabled");

let loc = ast
    .metrics(MetricsOptions::default().with_only(&[Metric::Loc]))
    .expect("walker succeeds");
let cyclomatic = ast
    .metrics(MetricsOptions::default().with_only(&[Metric::Cyclomatic]))
    .expect("walker succeeds");

println!("ploc = {}", loc.metrics.loc.ploc());
println!("ccn  = {}", cyclomatic.metrics.cyclomatic.cyclomatic_sum());
}

Each metrics call walks the tree once. The savings versus calling analyze twice come from skipping the parse, which dominates runtime for everything except the very largest source files.

Custom tree-sitter walk + metrics on the same parse

Ast::as_tree_sitter borrows the underlying tree_sitter::Tree. The returned reference is valid for the lifetime of the Ast; nodes obtained from it resolve against Ast::source (see the note on the C++ preprocessor below for what source returns under macro expansion).

For realistic AST work (counting node kinds, finding constructs by name, detecting parse errors, building a symbol table) see Walking the AST directly. The example below is a minimal smoke test; the dedicated chapter shows the full pattern (reusable depth-first walker, field-name lookup, error detection).

#![allow(unused)]
fn main() {
use big_code_analysis::{Ast, LANG, MetricsOptions, Source};

let ast = Ast::parse(Source::new(LANG::Rust, b"fn f() {}"))
    .expect("rust feature enabled");

// Walk the tree for your own purposes…
let root = ast.as_tree_sitter().root_node();
assert_eq!(root.kind(), "source_file");

// …and run the metric walker over the same parse.
let space = ast
    .metrics(MetricsOptions::default())
    .expect("walker succeeds");
println!("name = {:?}", space.name);
}

Operators and operands on the same parse

Ast::ops returns the per-space operator and operand Ops tree, the data behind the Halstead metrics. The top-level Ops::name is the Source::name you supplied, carried explicitly (including the None case) rather than a lossy-path string, so Ops::name_was_lossy is never set on this path.

#![allow(unused)]
fn main() {
use big_code_analysis::{Ast, LANG, Source};

let ops = Ast::parse(
    Source::new(LANG::Rust, b"fn f() { let x = 1 + 2; }")
        .with_name(Some("snippet.rs".to_owned())),
)
.expect("rust feature enabled")
.ops()
.expect("walker succeeds");
assert_eq!(ops.name.as_deref(), Some("snippet.rs"));
assert!(ops.operators.iter().any(|op| op == "+"));
}

Adopting a caller-built tree

If you already build the tree_sitter::Tree yourself (for example, because your editor or language server has its own parser pool), Ast::from_tree_sitter is the tree-adoption seam. It carries an explicit name: Option<String> end to end instead of deriving one from a path via lossy UTF-8 conversion.

#![allow(unused)]
fn main() {
use big_code_analysis::{Ast, LANG, MetricsOptions, tree_sitter};

let source = b"fn f() {}".to_vec();
let mut parser = tree_sitter::Parser::new();
parser
    .set_language(
        &LANG::Rust
            .tree_sitter_language()
            .expect("rust feature enabled"),
    )
    .expect("rust grammar compatible");
let tree = parser
    .parse(&source, None)
    .expect("parser has a language set");

let ast = Ast::from_tree_sitter(LANG::Rust, tree, source, None)
    .expect("rust feature enabled");
let _ = ast.metrics(MetricsOptions::default()).expect("walker succeeds");
}

The tree must have been produced from code with the grammar returned by LANG::tree_sitter_language for lang. A mismatch is not unsafe, but it is a precondition violation. The walk slices code by node byte ranges, so a tree built from longer source than code indexes out of bounds and panics. A tree from a different grammar that stays in bounds yields nonsensical results instead, because the metric walker matches on tree-sitter kind_id values from the language’s enum.

C++ preprocessor

When Ast::parse is called on a Source carrying preprocessor inputs (Source::with_preproc_path plus Source::with_preproc) and the language is a C-family one (LANG::C, LANG::Cpp or LANG::Mozcpp), the macro pre-pass runs before tree-sitter does. Ast::source then returns the expanded bytes the parser saw, not the original input.

Ast::from_tree_sitter is unaffected: it adopts whatever tree the caller built. Ast::source reflects whatever expansion, if any, the caller applied before building the tree.

Concurrency

Ast is Send + Sync. Running Ast::metrics from several threads against the same &Ast is safe, because the walker only reads from the held tree_sitter::Tree. Benchmarking parallel metric runs is a separate follow-up.

Out of scope

  • Incremental reparse via tree_sitter::InputEdit. Caching a stable Ast across an analysis pipeline is in scope; editing the held tree is not.
  • Parallel-by-default APIs. Ast::metrics does not internally parallelize across the metric set. Callers that want one thread per subset are free to do so.

Walking the AST directly

Ast::parse gives you a parsed tree_sitter::Tree together with the source bytes it was parsed from; Ast::as_tree_sitter hands that tree out as a borrowed reference. This chapter shows how to use it to drive your own syntax-tree analysis (counting node kinds, finding constructs by name, detecting parse errors, or pulling out a symbol table) without paying for a second parse. It assumes you know the tree-sitter node model: a tree of nodes, each with a kind name, a byte range and optional named fields.

When to use this

Reach for direct AST traversal when:

  • You want to count or find syntactic constructs in-process. The CLI equivalents (bca count -t <kind>, bca find -t <kind>, recipe) mean launching a separate process and parsing its output; the library path is one parse and one Rust loop.
  • You want to detect parse errors programmatically. Tree-sitter emits a synthetic ERROR node anywhere the grammar could not match. Node::has_error is O(1) because tree-sitter caches the error bit on every node, so the check is free even on a multi-MB source file.
  • You want to mix metrics with custom analysis in one parse: for example, capture metric values and a list of function names for a coverage mapping, an IDE outline, or a code-owner report.

If you only need standard metrics, stay with analyze or Ast::metrics, which walk the tree for you. The direct path is for things the metric walker does not already compute.

Use the re-exported tree_sitter

Import tree_sitter from big_code_analysis::tree_sitter rather than adding a sibling tree-sitter dependency. The re-export is pinned to the exact version the metric walker was built against, so the Tree types agree by definition. See Reusing an existing tree-sitter Tree and Stability and versioning for the value-not-stable posture this re-export carries.

A reusable DFS walker

Most of the examples below need a depth-first traversal of every descendant. Tree-sitter ships a TreeCursor that does this in O(1) per step (no allocations beyond the cursor itself). The canonical walk is short enough to inline:

#![allow(unused)]
fn main() {
use big_code_analysis::tree_sitter;

/// Visit every node in `tree` in pre-order, root first, passing each
/// node to `visit`. Allocation-free apart from the cursor itself.
fn walk_preorder<F: FnMut(tree_sitter::Node<'_>)>(
    tree: &tree_sitter::Tree,
    mut visit: F,
) {
    let mut cursor = tree.walk();
    'walk: loop {
        visit(cursor.node());
        if cursor.goto_first_child() {
            continue;
        }
        loop {
            if cursor.goto_next_sibling() {
                continue 'walk;
            }
            if !cursor.goto_parent() {
                return;
            }
        }
    }
}
}

The pattern is: visit, descend, climb back up while there is no next sibling, repeat. Every example in this chapter is a thin wrapper around this walker. The code fences below are marked ignore because they assume walk_preorder is already in scope; the matching tests in tests/api/book_ast_traversal_examples.rs keep them honest, so a refactor that broke an example would fail cargo test.

Count nodes by kind

Library equivalent of bca count -t if_expression -t for_expression -t while_expression from the AST-queries recipe:

use big_code_analysis::{Ast, LANG, Source};
use std::collections::HashMap;

let ast = Ast::parse(Source::new(
    LANG::Rust,
    b"fn a() { if true { 1 } else { 2 } } fn b() { for _ in 0..10 {} }",
))
.expect("rust feature enabled");

let mut counts: HashMap<&str, usize> = HashMap::new();
walk_preorder(ast.as_tree_sitter(), |node| {
    *counts.entry(node.kind()).or_default() += 1;
});

assert_eq!(counts.get("if_expression").copied().unwrap_or(0), 1);
assert_eq!(counts.get("for_expression").copied().unwrap_or(0), 1);

The string keys ("if_expression", "for_expression", …) are the tree-sitter grammar’s node-type names. To discover a language’s node-type names, run bca dump --paths sample.rs, which prints the full AST.

Anonymous tokens. The walker visits every node tree-sitter emits, including anonymous tokens like "{", ";", and keyword literals. The targeted counts.get("if_expression") lookups above are unaffected, since anonymous tokens have different kind names, but counts.values().sum() would be much larger than the count of named grammar productions. Filter with tree_sitter::Node::is_named() inside the visitor if you only want named nodes.

Find nodes by kind

Library equivalent of bca find -t unsafe_block:

use big_code_analysis::{Ast, LANG, Source};

let ast = Ast::parse(Source::new(
    LANG::Rust,
    b"fn safe() {} fn risky() { unsafe { } }",
))
.expect("rust feature enabled");

let source = ast.source();
// Captured slices borrow from `source`: no per-hit `String` allocation.
let mut hits: Vec<((usize, usize), &str)> = Vec::new();
walk_preorder(ast.as_tree_sitter(), |node| {
    if node.kind() == "unsafe_block" {
        let span = (node.start_position().row, node.end_position().row);
        let text = node
            .utf8_text(source)
            .expect("source is valid utf-8");
        hits.push((span, text));
    }
});

assert_eq!(hits.len(), 1);

Node::utf8_text(&source[..]) slices the source bytes by the node’s byte range. Pair it with Ast::source: for C++ with preprocessor inputs supplied to Ast::parse, source is the expanded buffer the parser saw, not the original input (see the C++ preprocessor note).

Detect parse errors

Tree-sitter is lossless: even on malformed input it returns a tree, with the nodes it could not match tagged as errors. Check the root for a cached error flag:

#![allow(unused)]
fn main() {
use big_code_analysis::{Ast, LANG, Source};

let ast = Ast::parse(Source::new(LANG::Rust, b"fn broken("))
    .expect("rust feature enabled");

// Walks far enough to confirm something went wrong, but does not
// enumerate every error site.
assert!(ast.as_tree_sitter().root_node().has_error());
}

To list the offending nodes, walk the tree and check each:

use big_code_analysis::{Ast, LANG, Source};

let ast = Ast::parse(Source::new(LANG::Rust, b"fn broken("))
    .expect("rust feature enabled");

let mut error_lines = Vec::new();
walk_preorder(ast.as_tree_sitter(), |node| {
    if node.is_error() || node.is_missing() {
        error_lines.push(node.start_position().row);
    }
});

assert!(!error_lines.is_empty());

Node::is_error() flags the synthetic ERROR node tree-sitter inserts where it could not match the grammar; Node::is_missing() flags phantom nodes the parser invented to recover from a missing token. The CLI’s bca find -t ERROR recipe uses the same nodes.

Combine metrics with a custom walk

The whole point of Ast is parse-once / compute-many. A realistic pipeline computes metrics and extracts a symbol table from the same parse:

use big_code_analysis::{Ast, LANG, MetricsOptions, Source};

let ast = Ast::parse(Source::new(
    LANG::Rust,
    b"fn outer() { fn inner() {} } fn alone() {}",
))
.expect("rust feature enabled");

// One parse: metrics walker uses it…
let space = ast
    .metrics(MetricsOptions::default())
    .expect("walker succeeds");

// …and so does the custom walk, against the very same tree. The
// captured names borrow from `source` rather than allocating a fresh
// `String` per function, the same pattern as `find_unsafe_blocks`
// above.
let source = ast.source();
let mut functions: Vec<&str> = Vec::new();
walk_preorder(ast.as_tree_sitter(), |node| {
    if node.kind() == "function_item"
        && let Some(name_node) = node.child_by_field_name("name")
    {
        let name = name_node
            .utf8_text(source)
            .expect("source is valid utf-8");
        functions.push(name);
    }
});

assert_eq!(space.metrics.nom.functions_sum(), 3);
assert_eq!(functions, ["outer", "inner", "alone"]);

Node::child_by_field_name walks the named grammar fields, the same fields that appear in the field_name key of the serialized AST (REST /ast, Ast::dump). Field-based lookup survives grammar changes that positional indexing does not, because it does not depend on which anonymous tokens (commas, parentheses, and so on) the grammar emits as children.

Want a serializable JSON tree?

For pipelines that want a structured AST as data (diffing, queries on the wire, language-agnostic schema work), Ast::dump materializes the tree as a Serialize-able AstResponse of AstNodes. This is the same shape the REST /ast endpoint produces. Call it on the parse handle:

#![allow(unused)]
fn main() {
use big_code_analysis::{Ast, AstCfg, AstPayload, LANG, Source};

let payload = AstPayload {
    id: "snippet".to_owned(),
    file_name: "snippet.rs".to_owned(),
    code: "fn f() {}".to_owned(),
    comment: false,
    span: true,
};
let cfg = AstCfg {
    id: payload.id.clone(),
    language: "rust".to_owned(),
    comment: payload.comment,
    span: payload.span,
};
let response = Ast::parse(
    Source::new(LANG::Rust, payload.code.as_bytes())
        .with_name(Some(payload.file_name.clone())),
)
.expect("rust feature enabled")
.dump(cfg);
let json = serde_json::to_string(&response).expect("AstResponse serializes");
println!("{json}");
}

For one-off in-process work, the as_tree_sitter() walker above is cheaper (no allocation per node). Reach for Ast::dump when you need a serializable owned tree.

Ast::dump walks the tree with one cursor, the same way the walker above does. Two siblings name the strategy explicitly: Ast::dump_cursor is that walk, and Ast::dump_indexed reaches each child through Node::child and its field through Node::field_name_for_child instead. They produce the same tree; the difference is cost. Both lookups scan toward the requested child. Lookup costs O(log n) for lists stored as balanced repeat() structures and O(index) for children stored in a flat array. Error recovery can create such flat arrays from skipped tokens. Enumerating a wide flat node by index is therefore quadratic in its width. Prefer the cursor walk for a whole-tree export, field lookup (child_by_field_name) for a grammatical role, and indexed access for picking one slot. A loop that needs repeated random access to a wide list should collect the children once and index the Vec.

Out of scope

  • Incremental reparse. Tree-sitter supports tree_sitter::InputEdit for incremental updates, but Ast is a snapshot. To reflect a source edit, build a fresh Ast::parse or drive tree_sitter::Parser::parse(&new_source, Some(&old_tree)) directly via the re-exported tree_sitter and feed the result through Ast::from_tree_sitter.
  • Everything on big_code_analysis::Node that is not listed in STABILITY.md. The wrapper is re-exported, and its traversal methods (kind, child_count, children, cursor, preorder, child, child_by_field_name, and so on) are pub, but only the STABILITY.md accessor list is shape-stable; the helpers that take a Search or Ancestors argument are not. For anything else, reach the tree-sitter Node through as_tree_sitter().root_node(), the documented seam.

Selecting metrics

By default, every call to analyze computes the full metric suite: ABC, cognitive, cyclomatic, Halstead, LoC, MI, NArgs, NExits, NOM, NPA, NPM, tokens, and WMC (see Metrics for what each measures). That is the right default for the CLI, where the user has asked for the metrics, but it is heavyweight for callers that only want one number per file.

MetricsOptions::with_only(&[Metric]) restricts the walker to a subset of metrics. Unselected metrics are skipped at the per-node level (no T::Halstead::compute, no T::Cognitive::compute, and so on) and elided from the CodeMetrics serialization output.

A worked example

Compute LoC only, then read the result:

use big_code_analysis::{analyze, LANG, Metric, MetricsOptions, Source};

fn main() {
    let source = b"fn f(x: i32) -> i32 { if x > 0 { 1 } else { 0 } }";

    let opts = MetricsOptions::default().with_only(&[Metric::Loc]);
    let space = analyze(
        Source::new(LANG::Rust, source).with_name(Some("snippet.rs".to_owned())),
        opts,
    )
    .expect("parses");

    // LoC was selected, so it carries real numbers.
    println!("ploc = {}", space.metrics.loc.ploc());

    // Halstead, cognitive, cyclomatic, … were skipped. Their
    // `Stats` fields are at `Default` and elided from JSON output.
    let json = serde_json::to_string_pretty(&space.metrics).unwrap();
    println!("{json}");
}

The JSON output for that call contains only the loc object; every other metric is absent.

Dependencies between metrics

Some metrics are derived: they consume the outputs of other metrics during the finalize step, or divide by a count another metric owns. Metric::dependencies is the source of truth:

MetricDependencies
Metric::MiLoc, Cyclomatic, Halstead
Metric::WmcCyclomatic, Nom
Metric::Cognitive, Metric::Nexits, Metric::NargsNom (Nargs divides its per-channel averages by the counts Nom supplies; Cognitive and Nexits count their own function spaces since #1284 and keep the declaration so their selection’s output shape is unchanged)

with_only automatically adds the selected metrics’ dependencies. Selecting Mi therefore also computes Loc, Cyclomatic and Halstead, rather than calculating MI from default-zero inputs:

#![allow(unused)]
fn main() {
use big_code_analysis::{Metric, MetricsOptions};
let opts = MetricsOptions::default().with_only(&[Metric::Mi]);
// The stored set now contains Mi + Loc + Cyclomatic + Halstead;
// `MetricsOptions::with_metric_set` closes a `MetricSet` the same way.
}

You can introspect the final set from the resulting FuncSpace via space.metrics.selected():

#![allow(unused)]
fn main() {
use big_code_analysis::{analyze, LANG, Metric, MetricsOptions, Source};
let space = analyze(
    Source::new(LANG::Rust, b"fn f() {}"),
    MetricsOptions::default().with_only(&[Metric::Mi]),
).unwrap();
let sel = space.metrics.selected();
assert!(sel.contains(Metric::Mi));
assert!(sel.contains(Metric::Loc)); // auto-added dependency
}

Default behaviour is unchanged

MetricsOptions::default() selects every metric. Calling analyze (or Ast::metrics) without with_only produces byte-for-byte the same JSON it always did.

What about “everything except X”?

There is no built-in complement API: with_only takes a positive selection, not an exclusion list. The asymmetry is deliberate and keeps the dependency closure unambiguous. A positive list always grows through Metric::dependencies, whereas an exclusion list would need to decide what to do when the caller excludes a dependency of a metric they kept.

If you want “all except Halstead”, build the list explicitly. Because Metric is #[non_exhaustive], downstream crates can construct the variants but cannot exhaustively match on them. The conventional pattern is to enumerate the variants you want and accept that a future Metric variant will not silently opt you in:

#![allow(unused)]
fn main() {
use big_code_analysis::{Metric, MetricsOptions};

let opts = MetricsOptions::default().with_only(&[
    Metric::Cognitive,
    Metric::Cyclomatic,
    Metric::Loc,
    Metric::Nom,
    Metric::Tokens,
    Metric::Nargs,
    Metric::Nexits,
    Metric::Abc,
    Metric::Npm,
    Metric::Npa,
    Metric::Wmc,
    // Metric::Mi intentionally omitted: it would pull Halstead
    // back in via the dependency closure.
]);
}

Note the trap: keeping Metric::Mi re-adds Metric::Halstead through Metric::dependencies. To drop Halstead you must also drop Mi.

When to reach for with_only

  • Hot paths that need only one or two metrics per file. Halstead in particular owns its own per-space HalsteadMaps allocation and is the headline saving for an LoC-only run.
  • CI integrations that display one number (a cognitive-complexity gate, say) and want the rest of CodeMetrics to drop out of the cached JSON payload.
  • Library callers wiring big-code-analysis into their own reports who would otherwise see fields for every metric in their own UI.

Per-metric Cargo features (compile-time stripping) are not covered by this knob.

Per-language Cargo features

Every tree-sitter grammar this library bundles is gated behind its own Cargo feature. The default feature set is all-languages, so the default

[dependencies]
big-code-analysis = "2.6.0"

pulls every grammar in, matching the library’s historical behaviour and what the bca / bca-web binaries ship with. The cost is concrete: every grammar crate compiles when the library compiles, and every grammar’s parsing tables stay live in the final binary.

Library consumers that need only a subset of languages can opt out of the defaults and re-enable just the grammars they use.

A worked example

A downstream service that only analyses Rust and TypeScript:

[dependencies]
big-code-analysis = { version = "2.6.0", default-features = false, features = ["rust", "typescript"] }

The library still compiles, the LANG enum still has every variant, and analyze / Ast / the rest of the dispatch surface still work for the enabled languages.

Supported features

The following per-language features are available. Each feature pulls in the matching grammar crate (and any helper grammars the per-language pipeline depends on).

FeatureGrammar crates pulled in
bashtree-sitter-bash
ctree-sitter-c (+ c-family-helpers); dedicated C variant owning .c, added in #721
c-family-helpersbca-tree-sitter-ccomment, bca-tree-sitter-preproc; internal, enabled automatically by c / cpp / mozcpp; gates the Ccomment / Preproc helper variants. Not meant to be selected directly
cpptree-sitter-cpp (+ c-family-helpers); the Cpp variant, upstream grammar since #720. Also enables the Ccomment / Preproc helper variants
csharptree-sitter-c-sharp
elixirtree-sitter-elixir
gotree-sitter-go
groovydekobon-tree-sitter-groovy
irulestree-sitter-irules (F5 iRules, a Tcl dialect)
javatree-sitter-java
javascripttree-sitter-javascript
kotlintree-sitter-kotlin-ng
luatree-sitter-lua
mozcppbca-tree-sitter-mozcpp (+ c-family-helpers); opt-in Mozilla/Gecko C++ dialect, Mozcpp variant; owns no extensions, selected by name
mozjsbca-tree-sitter-mozjs
objctree-sitter-objc; the Objc variant (Objective-C). Owns .m; .mm Objective-C++ stays on Cpp
perltree-sitter-perl
phptree-sitter-php
pythontree-sitter-python
rubytree-sitter-ruby
rusttree-sitter-rust
tclbca-tree-sitter-tcl
typescripttree-sitter-typescript (used by both the Typescript and Tsx variants)

Since #1376 the grammar crates are dependencies of big-code-analysis-ast, the parse and classification layer this crate is built on, so every feature above forwards to the same-named feature there and the grammars appear one level down in cargo tree. Nothing changes for a consumer: the feature names, the LANG enum and the whole analysis surface are unaffected, and enabling rust still links exactly one grammar.

The umbrella all-languages feature enables every entry in this table. The bca-tree-sitter-* crates are in-tree forks of the upstream Mozilla / community grammars; the Rust import path remains tree_sitter_<lang> regardless. See RELEASING.md for the rename rationale and the workspace package = ... alias trick that keeps consumer call sites unchanged.

What happens when a feature is off

The LANG enum keeps every variant defined regardless of the active feature set. Disabling a feature does not change the enum surface or any of the file-extension and Emacs mode declaration detection helpers. Selecting a LANG whose feature is off only affects the dispatch path.

Every dispatch entry point that returns a Result surfaces the disabled state as Err(MetricsError::LanguageDisabled(LANG)):

  • analyze
  • Ast::parse / Ast::from_tree_sitter (and the metrics / ops methods on the returned Ast)
  • LANG::tree_sitter_language, which returns Result<tree_sitter::Language, MetricsError> rather than the bare Language the upstream project returned

Callers can query the compiled-in set without going through a dispatcher:

#![allow(unused)]
fn main() {
use big_code_analysis::LANG;

for lang in LANG::into_enum_iter() {
    if lang.is_enabled() {
        println!("{:?} is compiled in", lang);
    }
}
}

This pairs well with the get_language_for_file / guess_language helpers, which still hand back any LANG variant for a recognised extension. Callers walking a directory may want to skip files whose language is not enabled in the current build.

Stability

Per-language features are themselves part of the contract. Adding a new language feature is an additive minor-bump change; removing one is a major-bump (3.0) break. The all-languages default is permanent within 2.x, so the variants a default build covers do not shrink before 3.0. Any such change will be flagged in the changelog under (breaking).

Walking FuncSpace results

FuncSpace is the tree the library hands back from analyze. The top-level node represents the whole file; its spaces field holds nested function, class, impl, trait and namespace spaces. Each node carries the same CodeMetrics payload, so any metric is available at any level.

Anatomy of a FuncSpace

The fields you reach for most often are:

FieldTypeWhat it is
nameOption<String>Caller-supplied identifier (top-level) or symbol name (nested)
kindSpaceKindUnit, Function, Class, Impl, …
start_lineusizeFirst line (1-based)
end_lineusizeLast line (1-based)
spacesVec<FuncSpace>Nested spaces
metricsCodeMetricsAll per-space metric values
suppressedSuppressionScopeIn-source suppression markers

SpaceKind is an enum; match on it to filter what you care about (Function only, or “anything that owns methods”).

start_line..=end_line is inclusive and never runs past the end of the file, and a nested space’s range always lies inside its parent’s, so it is safe to slice source by it.

Recursive walk

Recursion mirrors the tree shape. Here we collect every function space whose cognitive complexity exceeds a threshold:

use big_code_analysis::{
    analyze, FuncSpace, MetricsOptions, SpaceKind, Source, LANG,
};

fn hotspots(space: &FuncSpace, threshold: u64, out: &mut Vec<String>) {
    if space.kind == SpaceKind::Function
        && space.metrics.cognitive.cognitive_sum() > threshold
        && let Some(name) = &space.name
    {
        out.push(format!(
            "{name} (lines {}–{})",
            space.start_line, space.end_line,
        ));
    }
    for child in &space.spaces {
        hotspots(child, threshold, out);
    }
}

fn main() {
    let source = b"\
fn easy() { let _ = 1; }
fn hard(x: i32) -> i32 {
    if x > 0 { if x > 10 { 1 } else { 2 } } else { 3 }
}
";
    let space = analyze(
        Source::new(LANG::Rust, source).with_name(Some("snippet.rs".to_owned())),
        MetricsOptions::default(),
    )
    .expect("parses");

    let mut hits = Vec::new();
    hotspots(&space, 2, &mut hits);
    for hit in hits {
        println!("{hit}");
    }
}

Iterative walk

For deep trees, prefer an explicit stack. Rust does not guarantee tail-call optimisation, and pathological generated code can be arbitrarily nested:

#![allow(unused)]
fn main() {
use big_code_analysis::FuncSpace;

fn total_functions(root: &FuncSpace) -> usize {
    let mut stack = vec![root];
    let mut count = 0;
    while let Some(space) = stack.pop() {
        if space.kind == big_code_analysis::SpaceKind::Function {
            count += 1;
        }
        stack.extend(space.spaces.iter());
    }
    count
}
}

Reading per-metric numbers

CodeMetrics exposes each metric as its own Stats struct. Each struct offers integer-valued summary accessors plus per-space derived ones. A few patterns:

#![allow(unused)]
fn main() {
use big_code_analysis::FuncSpace;

fn summary(space: &FuncSpace) {
    let m = &space.metrics;

    println!("cognitive (this space):     {}", m.cognitive.cognitive());
    println!("cyclomatic (this space):    {}", m.cyclomatic.cyclomatic());
    println!("# functions in this space:  {}", m.nom.functions_sum());
    println!("source lines (sloc):        {}", m.loc.sloc());
    println!("physical lines (ploc):      {}", m.loc.ploc());
    println!("ABC branches:               {}", m.abc.branches());
}
}

The *_sum accessors aggregate across child spaces; bare accessors like m.loc.sloc() are the value attributable to this node. The full list of fields and methods lives in the per-metric rustdoc.

Do not rely on traversal order

The library walks the AST in source order, but the contract is only that every space appears once in the tree. If you need a stable order across versions, sort by start_line after the walk:

#![allow(unused)]
fn main() {
use big_code_analysis::FuncSpace;

fn flatten(space: &FuncSpace, out: &mut Vec<(usize, String)>) {
    if let Some(name) = &space.name {
        out.push((space.start_line, name.clone()));
    }
    for child in &space.spaces {
        flatten(child, out);
    }
}

fn sorted(space: &FuncSpace) -> Vec<(usize, String)> {
    let mut v = Vec::new();
    flatten(space, &mut v);
    v.sort_by_key(|&(line, _)| line);
    v
}
}

Error handling

The entry point analyze returns Result<FuncSpace, MetricsError>. This page documents what each variant means and how to act on it.

MetricsError is #[non_exhaustive], so always include a _ arm when matching to stay forward-compatible with future variants.

Pattern-matching the error variants

use big_code_analysis::{analyze, LANG, MetricsError, MetricsOptions, Source};

fn main() {
    let result = analyze(
        Source::new(LANG::Rust, b"this is not rust")
            .with_name(Some("snippet.rs".to_owned())),
        MetricsOptions::default(),
    );

    match result {
        Ok(space) => println!("ok: {} lines", space.metrics.loc.sloc()),
        Err(MetricsError::EmptyRoot) => {
            eprintln!("walker produced no top-level FuncSpace");
        }
        Err(MetricsError::LanguageDisabled(lang)) => {
            eprintln!("language {:?} is not enabled in this build", lang);
        }
        // `MetricsError` is `#[non_exhaustive]`; new variants may be added.
        Err(_) => eprintln!("unexpected MetricsError variant"),
    }
}

What each variant means

  • EmptyRoot: reserved; not produced today. The metric walker always pushes a synthetic top-level Unit FuncSpace before walking the AST, so every parse (including empty, whitespace-only, and comment-only input) returns Ok(FuncSpace { kind: Unit, .. }). The variant is kept for a future walker change that could let the state stack legitimately drain to empty.
  • LanguageDisabled(LANG): the requested LANG is not enabled in this build. Every dispatch entry point produces it when the caller selects a LANG whose per-language Cargo feature is off. The default feature set (all-languages) compiles every grammar in, so you only see this variant after opting into a narrower set (--no-default-features --features rust,…).

MetricsError has no ParseHasErrors or NonUtf8Path variant. Since it stays #[non_exhaustive], a future strict-parsing or strict-identifier mode can introduce them without a breaking change. Non-UTF-8 paths are already handled up front: the recommended analyze entry point takes a caller-supplied Source::name (Option<String>), so a lossy path is never round-tripped in the first place. The file-backed Ast::from_path is the one entry point that reads a path itself, and it returns its own #[non_exhaustive] FromPathError (Io, NonUtf8Path, Skipped(SkipReason), UnknownLanguage, Parse(MetricsError)) rather than widening MetricsError. Skipped carries the same SkipReason the bca walker prints when it skips an empty, tiny, UTF-16 or non-UTF-8 file, so a caller can say which gate declined the file; it supersedes the deprecated Unreadable.

Tree-sitter does not always say “no”

Most parse errors do not surface as Err(_). Tree-sitter is an error-recovering parser: it produces a tree even for syntactically broken input, marking the bad regions with ERROR nodes. The metric walk computes numbers over the recovered tree. That means:

  • Garbage in, numbers out. Feeding C++ source to LANG::Python generally produces an Ok(FuncSpace) whose metrics are nonsense. Select the right language (via guess_language, for example) before trusting the result.
  • Partial files score. A truncated file with an unterminated brace still returns Ok(FuncSpace). The metrics reflect the recovered tree, not the intended source.

If you need to know whether the input parsed cleanly, count ERROR nodes by walking the tree-sitter AST yourself (see the Node escape hatch in STABILITY.md) or use bca find -t ERROR on the CLI side (see the Nodes page).

Bubbling MetricsError through ?

Because MetricsError implements [std::error::Error], you can bubble it through any Result<_, Box<dyn Error>> chain without boilerplate:

#![allow(unused)]
fn main() {
use std::error::Error;

use big_code_analysis::{analyze, FuncSpace, LANG, MetricsOptions, Source};

pub fn run(
    lang: LANG,
    source: &[u8],
    name: Option<String>,
) -> Result<FuncSpace, Box<dyn Error>> {
    Ok(analyze(
        Source::new(lang, source).with_name(name),
        MetricsOptions::default(),
    )?)
}
}

If you want a project-specific error type, an explicit From impl keeps call sites clean while letting you attach extra context (file path, language guess, and so on).

Warnings are not errors

The library writes warnings to stderr for non-fatal issues (malformed bca: suppression markers, and a ConcurrentRunner input path that is not a regular file). They do not abort the walk and they do not flip Ok to Err. If you run embedded inside a server or library and need to capture those warnings, redirect stderr at the process level; the library does not expose a programmatic warning sink.

Stability and versioning

big-code-analysis is on the 2.x line (currently 2.6.0). The full stability contract lives in STABILITY.md at the root of the repository. That file is the source of truth and is updated alongside the changelog at every release; this page is the summary for library consumers.

The headlines:

  • Shape stability across patch and minor bumps. Every public type and function signature listed in STABILITY.md § “What is stable in shape” is held across the 2.x line. Additive changes (new items, new LANG variants, new MetricsError variants, new language features) are allowed in minor bumps. Breaking shape changes are reserved for the next major bump and will appear in the changelog under (breaking) in the 3.0.0 section.
  • No value stability guarantee within 2.x. A grammar pin bump or a bug fix in a metric definition can shift any metric value on any file in any direction, even across a patch bump. Each such drift is flagged in the changelog. Pin to an exact version (big-code-analysis = "= 2.6.0") if you need bit-for-bit reproducibility across runs.
  • The minimum supported Rust version (MSRV) is 1.94. Bumping the MSRV is treated as a minor-bump event and is flagged in the changelog under (breaking); see STABILITY.md § MSRV policy.
  • Escape hatches. The Node wrapper exposes its tree_sitter::Node through Node::as_tree_sitter(), and the tree_sitter crate is re-exported as big_code_analysis::tree_sitter. Node also carries the accessors the metric walk itself uses (kind, kind_id, child, children, child_by_field_name, utf8_text, the position accessors). Their signatures are shape-stable, but the values they return (node kinds and kind_id numbers) belong to the pinned grammar and move when it does. Anything reached through those seams follows the pinned tree-sitter version rather than our own SemVer. See STABILITY.md § Escape hatches before depending on them.
  • big-code-analysis-ast is not a stability surface. Since #1376 the parse and classification layer lives in that separate published crate, which this one pins exactly and releases in lockstep. It appears in your cargo tree. Nothing in it carries a promise of its own: depend on big-code-analysis and use what it re-exports.

On the 3.0 horizon

The breaking changes once staged for 2.0 shipped in 2.0.0: the #[non_exhaustive] markers on the open public enums and the per-metric Stats structs, the serialized-key normalization, the integer-metric u64 shift, the language-dispatch and grammar defaults, the Python and REST surface changes, and a consolidated metric-value re-baseline folding in the drift accumulated since 1.0. The path-positional callback dispatch (action / the Callback trait), the free metrics / metrics_with_options / get_function_spaces / metrics_from_tree / get_ops functions, and the generic Parser<T> / ParserTrait plumbing were removed at the same time; analyze and Ast are now the single analysis seam. Since #1376 Parser, ParserTrait and the per-language parser and tag types live in big-code-analysis-ast, which this crate does not re-export.

No 3.0 is scheduled. The #[non_exhaustive] markers added at 2.0 keep most future additions (new enum variants, new fields) non-breaking, so 2.x is the surface you should depend on. See STABILITY.md § On the 3.0 horizon.

Python Bindings

big-code-analysis ships first-party Python bindings, built with PyO3 and maturin, that expose the same metric pipeline as the Rust library and the bca CLI: same JSON shape, same numeric formatting, same language coverage. This chapter is for a Python developer deciding whether to use the bindings and then finding the page for each workflow.

import big_code_analysis as bca

result = bca.analyze("src/main.rs")
if result is not None:
    print(result["metrics"]["cyclomatic"]["sum"])

The bindings cover the analysis workflows: file and in-memory analysis (big_code_analysis::analyze), the FuncSpace result as a typed dict, metric selection, AST traversal, SARIF output, and the VCS metrics. The metrics are computed by the same Rust code, so the numbers are identical. Python also exposes the language-detection helpers. Some lower-level Rust facilities, including the C preprocessor pass and the output writers, have no Python counterpart. Pick whichever language fits your pipeline.

When to reach for Python

  • You work in a data-pipeline stack (pandas, Jupyter, Airflow, dbt, Polars) and want metric records as dict or DataFrame rows without shelling out to the CLI.
  • You integrate with a Python-native security tool that consumes SARIF; see SARIF output.
  • You build a code-quality dashboard on a Python web framework (FastAPI, Django).

If you only need a one-shot quality report from the command line, the bca CLI is the simpler tool; see Commands → Metrics.

If you embed the analysis in a long-running Rust program, the Rust library has lower overhead.

Chapter contents

The headline example on each page is embedded verbatim from an importable file under big-code-analysis-py/examples/ and run end-to-end by big-code-analysis-py/tests/test_book_examples.py, so a renamed kwarg or a removed function on the primary path fails CI before it can rot the docs. The shorter snippets around each embedded example (logging recipes, regex parsing of the errno suffix, the asyncio anti-pattern, the pandas one-liner) are inline and deliberately not test-pinned. Treat the embedded blocks as the canonical reference when the two disagree.

Installation

The bindings are distributed as a prebuilt binary wheel. Install with pip, or with your lockfile manager of choice (uv, poetry, pdm):

pip install big-code-analysis

Python >=3.12 is required. The compiled extension uses CPython’s stable abi3 surface (abi3-py312), so one wheel covers 3.12, 3.13, 3.14, and every future minor release without a per-version wheel build.

Wheel matrix

CI publishes wheels for the following targets today. If your platform is not listed, build from source.

PlatformArchitectures
Linux (manylinux_2_28)x86_64, aarch64

The wheel matrix is defined in .github/workflows/python-wheels.yml. manylinux_2_28 requires glibc >= 2.28 (RHEL 8 / Debian 10 / Ubuntu 18.10 and newer); older distributions (RHEL 7 / CentOS 7, glibc 2.17) need to build from source. macOS and Windows wheels are not yet shipped, so pip install on those platforms falls back to a source build today.

Verifying the install

python -c "import big_code_analysis as bca; print(bca.__version__)"

The printed version equals [workspace.package].version in the Rust workspace’s Cargo.toml. The bindings and Rust library share a version.

Building from source

If no wheel matches your platform, or you want to bind against an unreleased Rust commit, build with maturin:

git clone https://github.com/dekobon/big-code-analysis.git
cd big-code-analysis/big-code-analysis-py
python -m venv .venv && source .venv/bin/activate
pip install --upgrade pip
pip install "maturin>=1.7,<2.0"
maturin develop --release   # editable install of big_code_analysis
python -c "import big_code_analysis as bca; print(bca.__version__)"

maturin develop builds the Rust extension in place and installs it into the active venv so import big_code_analysis resolves locally; no separate pip install -e . step is required. The --release flag turns on the optimiser; omit it during development for faster rebuilds.

You also need:

  • A stable Rust toolchain (MSRV: 1.94). Install via rustup.
  • A C compiler (used by the tree-sitter grammar crates).
  • CPython development headers (python3-dev on Debian / Ubuntu).

Next

Walk through the quick-start to compute your first metric, or skip ahead to batch processing if you are wiring this into a pipeline over many files.

Quick start

This page shows the least code needed to compute metrics for one source file: install the package, call analyze, read a value. It assumes Python 3.12 or newer and pip.

1. Install the package

pip install big-code-analysis

See Installation for the wheel matrix and build-from-source instructions.

2. Analyse a file

bca.analyze(path) returns a dict matching the JSON bca metrics --format json emits for the same file: same field order, same numeric formatting, same shape.

"""Quick-start: analyse one file and print the headline cyclomatic count.

Mirrors the worked example shown on the book's
``python/quick-start.md`` page. The book embeds this file verbatim,
so the snippet is the test fixture — if the API drifts, the
``test_book_examples.py`` test fails and the docs are forced back
into sync.
"""

from __future__ import annotations

from pathlib import Path

import big_code_analysis as bca
from big_code_analysis import FuncSpaceDict


def run(path: Path) -> FuncSpaceDict:
    """Analyse ``path`` and return its metric dict."""
    result = bca.analyze(path)
    if result is None:
        msg = f"{path} was skipped (empty, binary, or generated)"
        raise SystemExit(msg)

    cyclomatic = result["metrics"]["cyclomatic"]
    print(f"{result['name']}: cyclomatic sum = {cyclomatic['sum']:.0f}")
    return result


if __name__ == "__main__":
    import sys

    if len(sys.argv) != 2:
        sys.exit("usage: python quick_start.py <path>")
    run(Path(sys.argv[1]))

Three details matter here:

  • analyze returns None for any file the CLI walker would skip: one that is three bytes or fewer (treated as empty), one whose leading window is not valid UTF-8 (treated as binary), or, with the default skip_generated=True, one matching the walker’s is_generated predicate (a leading @generated, DO NOT EDIT, or GENERATED CODE marker). Handle the None before reaching into result["metrics"].
  • The returned object is a plain dict at runtime, so you can serialise it with json.dumps, ship it to a downstream service, or feed it to flatten_spaces for tabular consumers. Type checkers see it as the FuncSpaceDict TypedDict (generated from the Rust wire shapes), so nested metric access checks statically under mypy and pyright without casts.
  • Language detection mirrors the CLI: path extension first, then an Emacs or Vim mode line, then the shebang. Pass bca.analyze_source(code, language) when the source is already in memory.

3. Analyse an in-memory snippet

import big_code_analysis as bca

metrics = bca.analyze_source("fn main() {}\n", "rust")
print(metrics["metrics"]["loc"]["sloc"])

analyze_source accepts str, bytes, or bytearray. The returned dict has the same shape as analyze’s output, with name set to None (no path is associated with an in-memory buffer).

Where to go next

Batch processing

bca.analyze_batch(paths) runs the same analysis as bca.analyze over every path in an iterable and never raises on per-file errors: each result element is an analysis dict, a bca.AnalysisFailure describing the failure, or None. Results preserve input order, so zip(inputs, results) lines up by index when no path is skipped. This page is for a caller analysing many files who wants one list back instead of a try/except per file.

analyze_batch shares analyze’s keyword-only options (exclude_tests, allow_lossy_path, skip_generated with default True, metrics, vcs, vcs_per_function, and mi_charges_docs), so both entry points treat a file the same way. The list shape matches the [bca.analyze(p) for p in paths] comprehension only under skip_generated=False; with the default, the comprehension keeps a None per skipped file while the batch drops the slot.

def run(paths: Iterable[Path]) -> dict[str, int]:
    """Analyse ``paths`` as a batch and bucket successes vs failures.

    Returns a small summary dict (`ok`, `errors`, `skipped`, `total`) so
    the accompanying test can assert on it without re-parsing.
    """
    materialised = list(paths)
    # `skip_generated=False` guarantees one result element per input
    # (generated files are analysed, not dropped), so the `strict=True`
    # zip against `materialised` cannot raise `ValueError`. Under the
    # 2.0 default (`skip_generated=True`) a generated input yields no
    # slot, the lengths diverge, and the strict zip blows up — the same
    # bug #660 fixed in `pipeline_db.py`.
    results = bca.analyze_batch(materialised, skip_generated=False)

    ok = 0
    errors = 0
    skipped = 0
    for path, result in zip(materialised, results, strict=True):
        if isinstance(result, bca.AnalysisFailure):
            errors += 1
            print(f"  skip {path}: ({result.error_kind}) {result.error}")
        elif result is None:
            # The read gate declined this file; the slot is held open
            # so the strict zip above stays aligned (#1238).
            skipped += 1
            print(f"  skip {path}: nothing to parse (empty or binary)")
        else:
            ok += 1
            sloc = result["metrics"]["loc"]["sloc"]
            print(f"  ok   {path}: sloc = {sloc:.0f}")

    return {
        "ok": ok,
        "errors": errors,
        "skipped": skipped,
        "total": len(materialised),
    }

The contracts to rely on:

  • AnalysisFailure is returned, not raised. It is not an Exception subclass; isinstance(slot, bca.AnalysisFailure) is the discriminator.
  • paths is consumed lazily, so generators work. To keep the input around for zip, materialise it into a list first.
  • With the default skip_generated=True, a generated file is skipped and produces no element, so the result list can be shorter than the input. This matches single-file analyze, which returns None for a generated file. Pass skip_generated=False to guarantee one element per input (the pre-2.0 default). The default flipped at 2.0 so that switching between analyze and analyze_batch no longer silently changes generated-file handling.
  • A file that cannot be parsed at all still holds its slot under skip_generated=False, as None. The read gate shared with the CLI walker declines a file of three bytes or fewer, one carrying a UTF-16 BOM, and one whose leading window is not valid UTF-8. That gate is unconditional, so before #1238 those files shrank the list even with the flag off and the zip above mis-paired every later entry. None is the same value single-file analyze returns for them, and bca.to_sarif skips it, so a batch list can be passed straight through.

Walking a directory: analyze_paths

analyze_batch analyses an explicit list of paths verbatim. To find the source files first (“analyze my repo”), use analyze_paths (#658), which reuses the CLI’s gitignore-aware walker:

import big_code_analysis as bca

results = bca.analyze_paths("path/to/repo", include="*.py")

Each positional seed may be a file or a directory. Directories are walked honouring .gitignore, the include / exclude globs (a single glob string or a sequence; a leading ./ is optional, so dir/** ≡ ./dir/**), and the generated-file filter. A seed naming a file directly is always analysed regardless of exclude, because an explicit request overrides ignore-style rules; include still narrows it by basename. respect_gitignore=False opts into walking ignored files.

The result is the same list[FuncSpaceDict | AnalysisFailure | None] shape and never-raise contract as analyze_batch, and it forwards the same exclude_tests / allow_lossy_path / skip_generated / metrics / vcs / vcs_per_function / mi_charges_docs kwargs. A seed that does not exist leads the result list as an AnalysisFailure (error_kind="IoError") rather than being dropped.

The None slots reach this entry point too, under skip_generated=False, one per discovered file the read gate declined. Results follow the walk, so there is no caller-supplied ordering to zip against; the slots exist so that a file the walk found but could not analyse stays visible in the output. On a tree with many binary assets that is a lot of Nones; filter them with [r for r in results if r is not None] if you only want records.

Attaching change-history metrics

analyze_batch and analyze_paths accept the same vcs=True / vcs_per_function=True kwargs as single-file analyze (#670). The batch builds one history index and blame engine per containing repository and reuses it across that repository’s files, amortising the walk that a comprehension over analyze(p, vcs=True) would repeat per file. A VCS failure on one file leaves its AST metrics intact (it never becomes an AnalysisFailure); a file outside any repository gets no vcs block. To rank a whole repository rather than attach per-file numbers, use the dedicated big_code_analysis.vcs surface instead.

Parallel execution

analyze_batch has no built-in concurrency; it is a sequential sweep. For parallelism, fan the per-file analyze call out across a thread pool:

def run_parallel(paths: Iterable[Path], *, workers: int = 4) -> list[FuncSpaceDict | None]:
    """Fan ``analyze`` out across a thread pool.

    PyO3 releases the GIL across each file's read + parse, so a
    thread pool actually parallelises the heavy work. Use this when
    you need per-file exceptions instead of ``AnalysisFailure`` slots.
    """

    def _analyze(p: Path) -> FuncSpaceDict | None:
        return bca.analyze(p)

    with ThreadPoolExecutor(max_workers=workers) as pool:
        return list(pool.map(_analyze, paths))

PyO3’s Python::detach releases the GIL across each file’s read and tree-sitter parse, so the threads do not serialise on the interpreter lock.

AnalysisFailure taxonomy

error_kind is a closed Literal:

error_kindTriggered by
"UnsupportedLanguage"Unknown extension + no shebang / emacs-mode hit
"ParseError"tree-sitter rejected the source, or a rare internal serialisation failure (internal: serialization error: …)
"IoError"std::fs::read failed or the path was not valid UTF-8

AnalysisFailure is frozen and implements __eq__ / __hash__ / __repr__ over all three fields, so callers can put errors in a set to deduplicate failures across runs. For retry classification, the errno is preserved in the error string via Rust’s default formatting:

import re

match = re.search(r"\(os error (\d+)\)$", slot.error)
errno = int(match.group(1)) if match else None

If you need typed dispatch (FileNotFoundError, PermissionError, and so on), call bca.analyze(path) per file instead of analyze_batch; single-file analyze raises the canonical OSError subclass. See Error handling.

Flat-record iteration

bca.flatten_spaces(result) walks the nested FuncSpace tree in pre-order and yields one flat, scalar-only dict per node, ready for sqlite3.executemany, pandas.DataFrame.from_records, or any other tabular consumer. This page is for a caller loading metrics into a database or a DataFrame.

Metric keys use the same dotted convention as the CLI’s CSV writer (cyclomatic.modified.sum, halstead.volume, loc.lloc_average, and so on). Identity keys (path, name, kind, start_line, end_line, parent_name, depth) are added on every record.

SQLite via executemany

The example below analyses one file and inserts one row per FuncSpace into a sqlite table whose columns are the union of all flattened keys.

"""Flatten a FuncSpace tree into scalar rows for sqlite / pandas.

Demonstrates ``bca.flatten_spaces`` + ``sqlite3.executemany``. The
pandas equivalent is shown in the book as a non-executed snippet so
this example stays dependency-free (sqlite ships with the stdlib).

Tied to the book's ``python/flat-records.md`` page.
"""

from __future__ import annotations

import sqlite3
from contextlib import closing
from pathlib import Path

import big_code_analysis as bca


def run(path: Path, db_path: Path) -> int:
    """Analyse ``path`` and insert one row per FuncSpace into ``db_path``.

    Returns the number of rows inserted so the test can assert on it.
    """
    result = bca.analyze(path)
    if result is None:
        msg = f"{path} was skipped (looks generated)"
        raise SystemExit(msg)

    # The flattened keys are dotted, lowercase names
    # (`halstead.unique_operators`, `halstead.total_operators`, …) that
    # are unique under SQLite's case-insensitive column comparison (the
    # old `N1`/`n1` Halstead collision was removed in #511), so each
    # lands on its own column without renaming.
    records = [dict(r) for r in bca.flatten_spaces(result)]
    if not records:
        return 0

    columns = sorted({k for r in records for k in r})
    cols_sql = ", ".join(f'"{c}"' for c in columns)
    placeholders = ", ".join("?" for _ in columns)
    rows = [tuple(r.get(c) for c in columns) for r in records]

    # `closing(sqlite3.connect(...))` is the documented idiom — the
    # bare ``with sqlite3.connect(...)`` context manager only commits
    # / rolls back the transaction; it does NOT close the connection,
    # so a long-running consumer leaks file descriptors (and on
    # Windows holds an exclusive write lock on the db file).
    with closing(sqlite3.connect(db_path)) as db, db:
        db.execute(f"CREATE TABLE IF NOT EXISTS metrics ({cols_sql})")
        db.executemany(
            f"INSERT INTO metrics ({cols_sql}) VALUES ({placeholders})",
            rows,
        )

    return len(rows)


if __name__ == "__main__":
    import sys

    if len(sys.argv) != 3:
        sys.exit("usage: python flat_records.py <source-file> <out.db>")
    inserted = run(Path(sys.argv[1]), Path(sys.argv[2]))
    print(f"inserted {inserted} rows into {sys.argv[2]}")

The iterator is lazy and single-use: it walks the input once without materialising the whole list. A second iteration of the same iterator yields nothing; call list() once if you need to re-iterate.

Pandas

flatten_spaces is the natural input to pandas.DataFrame.from_records. pandas is not a dependency of the bindings; install it separately if you want the DataFrame view.

import big_code_analysis as bca
import pandas as pd

result = bca.analyze("src/lib.rs")
if result is not None:
    df = pd.DataFrame.from_records(bca.flatten_spaces(result))
    print(df.head())
    # Group by space kind to inspect the average cyclomatic per
    # function vs. per class vs. per file.
    by_kind = df.groupby("kind")["cyclomatic.sum"].mean()

Identity columns vs CLI CSV

The flat-record schema is mostly aligned with the CLI’s CSV writer, with a few intentional deltas:

  • Identity columns use name / kind here; the CSV writer uses space_name / space_kind. Flat records also add parent_name / depth; the CSV writer omits those.
  • The per-space cognitive.value, cyclomatic.value, cyclomatic.modified.value, abc.value and nargs.value leaves (the numbers bca check thresholds against) are present here, as in the JSON shape, but absent from the CSV header.
  • tokens.* flattens to the JSON shape (tokens.tokens, tokens.average, tokens.min, tokens.max). Only the sum leaf differs from CSV, which spells it tokens.sum; the average / min / max leaves now match (#590). Rename the sum leaf in the consumer if you need exact CSV alignment.

Anonymous spaces (Rust closures, JavaScript function expressions and arrows) keep their name == "<anonymous>" marker verbatim; flatten_spaces does not normalise.

Caveats

  • parent_name alone cannot disambiguate same-named siblings nested under different parents (two Inner classes under two different outer classes both surface as parent_name == "Inner" for their own children). Pair it with depth and source-order position, or rebuild the qualified name in your consumer, if you need a fully-qualified path.
  • Do not mutate the input result while iterating: the walker keeps references into it, so mutations to not-yet-yielded subtrees show up in later records.
  • Missing metric subtrees produce no keys (absent, not None), matching the “Halstead disabled” edge case for metric selection.
  • flatten_spaces raises TypeError if the input is not a mapping. Filter out the None returns from bca.analyze (generated files under skip_generated=True) before passing, and likewise the None slots analyze_batch emits under skip_generated=False, which mark a file the read gate declined to parse.

Metric selection

Pass metrics=[…] to compute only a subset of the metric suite. metrics=None (the default) computes everything. Unrequested metrics are absent from the result dict, not present with None placeholders. This page is for a caller who wants fewer metrics, either to save time on a large batch or to narrow the output.

def run(path: Path) -> FuncSpaceDict:
    """Compute only LoC + cyclomatic for ``path`` and return the result.

    ``bca.METRIC_NAMES`` is a ``tuple[MetricName, ...]`` of canonical
    names accepted by ``metrics=``; its ``StrEnum`` members are
    ``str``-comparable, so ``"halstead" in bca.METRIC_NAMES`` works — an
    ABI smoke check the catalog is populated, not a test of the selection.
    """
    if "halstead" not in bca.METRIC_NAMES:
        msg = "halstead is missing from METRIC_NAMES — bindings ABI drift"
        raise RuntimeError(msg)
    selected = bca.analyze(path, metrics=["loc", "cyclomatic"])
    if selected is None:
        msg = f"{path} was skipped (looks generated)"
        raise SystemExit(msg)

    metric_keys = sorted(selected["metrics"])
    print(f"computed only: {metric_keys}")
    return selected


def run_derived(path: Path) -> FuncSpaceDict:
    """Selecting ``mi`` auto-pulls in its three dependencies."""
    selected = bca.analyze(path, metrics=["mi"])
    if selected is None:
        msg = f"{path} was skipped (looks generated)"
        raise SystemExit(msg)

    pulled = sorted(selected["metrics"])
    print(f"mi pulled in: {pulled}")
    return selected

bca.analyze_source and bca.analyze_batch honour the same kwarg; the batch applies the selection uniformly to every file. Validation runs before any file I/O: an empty list or unknown name raises ValueError immediately and never returns an AnalysisFailure slot for what is really a caller bug.

Canonical names

The full set is available as a tuple of MetricName members. Each member is a StrEnum, so it is a str: "halstead" in bca.METRIC_NAMES works, and bca.MetricName.HALSTEAD == "halstead" is True. Pass either a plain string or a member to metrics=:

import big_code_analysis as bca
from big_code_analysis import MetricName

assert "halstead" in bca.METRIC_NAMES
assert bca.MetricName.HALSTEAD == "halstead"

# Either spelling works in `metrics=`:
selection = [MetricName.CYCLOMATIC, "cognitive"]

The members are generated from the same Metric table the CLI and JSON output use, so the values never drift from the slugs you see in bca metrics --format json.

Accepted names

metrics= takes every name bca metrics --metrics takes, so a name copied from bca list-metrics or from a bca check --threshold limit works unchanged:

SpellingExampleSelects
Family name, from METRIC_NAMES"loc", "cyclomatic"that family
bca list-metrics row name"sloc", "dloc"the family that owns the row, loc
Dotted threshold id"cyclomatic.modified", "mi.original"that id’s family

Selection works per family, because the walk computes whole families: metrics=["dloc"] returns the full loc block, sloc and all, exactly as metrics=["loc"] does. METRIC_NAMES lists only the family names, which are also the JSON keys, so name in bca.METRIC_NAMES does not tell you whether metrics= accepts name.

Names are case-sensitive lowercase. An unknown name raises ValueError naming the closest accepted names, then listing every family and row name, as the CLI does:

ValueError: unknown metric: dlocc; did you mean `dloc`?; known metrics: abc, blank, cloc, …

The canonical spelling for the exit-point metric is "nexits" everywhere (the enum Display, METRIC_NAMES, and the JSON output key). The legacy "exit" alias was retired at 2.0 and now raises ValueError like any other unknown name. Duplicates are silently collapsed.

Dependencies

Selecting a metric that reads another metric’s result computes that one too. nargs divides its per-channel averages by the function and closure counts nom supplies. A lambda is one of those closures but opens no space, so its parameters fold into the enclosing function’s own closure_args and closure_args_average can fall below closure_args_min (see NArgs). cognitive and nexits count their own function spaces but still pull nom in, so a selection naming either returns the same blocks it always did.

MetricJSON keyDependencies pulled in
LoClocnone
Cyclomaticcyclomaticnone
Cognitivecognitivenom
Halsteadhalsteadnone
ABCabcnone
nargsnargsnom
nomnomnone
npanpanone
npmnpmnone
nexitsnexitsnom
tokenstokensnone
Maintainability Indexmiloc, cyclomatic, halstead
Weighted Methods per Classwmccyclomatic, nom

Performance trade-off

Computing the full suite is the default because it is what the CLI does. Selecting fewer metric families skips their computations but still runs the tree-sitter parse and walks the AST. Benchmark a representative batch to measure the saving for your workload.

Unrequested metrics are absent from the result. Code that unconditionally indexes into result["metrics"]["mi"] raises KeyError if you opted out of mi; guard with if "mi" in result["metrics"] or use .get("mi").

See also

  • Batch processing: metrics= applies uniformly to every file in a batch; validation runs once, before the input is iterated.
  • SARIF output: threshold names are independent of the metrics= selection. You can request metrics=["loc"] and still gate on cyclomatic thresholds, but the SARIF has no findings for the dropped metrics.
  • Flat-record iteration: flatten_spaces emits no keys for metrics absent from the source dict, so a metrics= selection narrows the flattened columns.

AST traversal

bca.analyze(...) gives you metrics. When you need the syntax tree itself (to find every function definition, pull a docstring, or port a py-tree-sitter matcher), parse once into an Ast and walk it with lazy Node handles. This page assumes you know what a tree-sitter parse tree is; the Node API mirrors py-tree-sitter’s.

The Ast handle

bca.Ast.parse(code, language) (or bca.Ast.from_path(path)) parses the source once and hands back a handle you can draw both metrics and the tree from, instead of parsing twice (once in py-tree-sitter, once in analyze()):

import big_code_analysis as bca

ast = bca.Ast.parse("fn main() { let x = 1 + 2; }", "rust")
ast.metrics()      # same dict as analyze_source(...)
ast.root_node      # the syntax tree, walked lazily (below)

The handle is immutable and thread-safe, so it composes with ThreadPoolExecutor fan-out exactly like analyze.

The Node handle

ast.root_node is the tree’s root as a lazy Node. Unlike ast.dump(), which materialises one dict per node, a Node is a cursor into the retained tree: it costs nothing until you read from it, and a selective extractor pays only for the nodes it visits.

root = ast.root_node
root.kind                       # "source_file"
root.type                       # "source_file" (py-tree-sitter alias for kind)
root.children                   # list[Node], direct children
root.child_by_field_name("…")   # a field child, or None
node.text                       # the node's source bytes

Traversal mirrors py-tree-sitter: children / named_children, parent, next_sibling / prev_sibling (and the *_named_* variants), child(i) / named_child(i), child_by_field_name(name) / children_by_field_name(name), and the field_name the parent reaches a node through.

Walking the whole subtree

walk() is a lazy pre-order iterator over a node and its descendants; descendants_by_kind(kinds) collects the matches in one pass; and ast.find(filters) searches the whole tree, accepting the same vocabulary as bca count (all, function, call, comment, string, error, an exact kind, or a numeric kind_id):

# Every function name in the file, the lazy way.
for fn in ast.find(["function_item"]):
    name = fn.child_by_field_name("name")
    print(name.text.decode())

# Or filter a subtree by raw grammar kind.
idents = root.descendants_by_kind(["identifier"])

These have Rust counterparts, Node::preorder and Node::descendants_by_kind, so library callers get the same helpers.

Coordinates

A node reports its one location in every vocabulary, so nothing has to be converted by hand:

AccessorMeaning
start_byte / end_bytebyte offsets into ast.source
start_point / end_point0-based (row, col) (py-tree-sitter parity)
start_line / end_line1-based lines
spanthe {start_line, start_col, …, start_byte, end_byte} dict dump() emits: lines and columns are 1-based; byte offsets are 0-based and half-open

So node.start_line == node.start_point[0] + 1, and ast.source[node.start_byte:node.end_byte] == node.text.

node.type is a py-tree-sitter-compatible alias for node.kind, so a matcher written against py-tree-sitter’s node.type ports over unchanged; kind stays the canonical bca spelling.

Lazy nodes vs. dump()

ast.dump() returns the tree as nested dicts; ast.root_node returns lazy handles. They differ in two ways that matter:

  1. Memory. dump() builds one dict (with span, value, children) per node: fine for small files, costly for large ones. A Node walk allocates only the handles you touch.

  2. Taxonomy. A Node’s kind is the raw grammar kind. dump() kinds pass through bca’s Alterator and are curated; for example, string-literal nodes are renamed to "string" and flattened (their grammar children removed). So the two surfaces intentionally disagree on altered nodes:

    ast = bca.Ast.parse('fn f() { let s = "hi"; }', "rust")
    # Raw tree: the string keeps its quote/content children.
    raw = next(n for n in ast.root_node.walk() if "string" in n.kind)
    assert raw.children
    

    Use lazy nodes when you want exactly what the grammar produced (the right choice for porting a py-tree-sitter matcher); use dump() when you want bca’s curated, JSON-serialisable view.

Lifetime and threading

A Node keeps its Ast alive: it stays valid even after you drop every other reference to the parse, so returning a node (or a list of nodes) from a function that builds the Ast locally is safe. Nodes are also safe to share across threads.

C/C++ preprocessor. The Python Ast takes no preprocessor inputs: Ast.parse accepts only code, language and name, and Ast.from_path does not run the C/C++ preprocessor pass. So ast.source, and every node’s byte offsets, always index into the bytes the parser was given: the code you passed to parse, or the file as from_path read it (through the same EOL-normalising reader analyze uses). The expanded-source behaviour described in the Rust chapter applies only to a Rust Source built with preprocessor results.

Where to go next

  • Metric selection: compute only the metrics you need from the same parse.
  • The CLI’s dump and count commands: the shell-level equivalents of dump() and find().

SARIF output

bca.to_sarif(result, *, thresholds=None) renders an analysis result (or an iterable of them) into a SARIF 2.1.0 JSON document, ready for upload to GitHub Code Scanning or any other SARIF consumer. The output is produced by the same Rust writer that backs bca check --report-format sarif, so the schema URL, tool driver name / version, and rule descriptions match the CLI byte-for-byte.

Findings match in order as well as in content. Both surfaces sort their findings by path, then start line, then metric name (the order bca check applies after its walk), and findings tying on all three keep the depth-first source order of the space tree. For the same files and thresholds the two results arrays therefore line up entry for entry against bca check --no-suppress. to_sarif compares raw metric values, so it applies none of the in-source suppression markers bca check honours by default (each marked space keeps its suppressed key, for a caller that wants to filter), no baseline, and no [check] exclude globs.

“The same files” means a unique file set. The CLI folds repeated path seeds together, so bca check -p a.py -p a.py analyses a.py once and emits one finding per breach. analyze_batch instead returns one result per input, and to_sarif renders every result it is handed, so to_sarif(analyze_batch([a, a]), ...) emits each finding twice. Deduplicating in the binding would be wrong, because two distinct results may legitimately share a name (analyze_source takes the caller’s). Hand to_sarif a unique file set when comparing the two documents positionally.

Examples on this page import the package as bca (import big_code_analysis as bca). A bare bca in a shell command is the CLI binary.

def run(
    paths: Iterable[Path],
    sarif_path: Path,
    thresholds: Mapping[str, float],
) -> str:
    """Analyse ``paths`` and write a SARIF document to ``sarif_path``.

    Returns the rendered SARIF JSON so the caller (or the test) can
    inspect it without re-reading the file.
    """
    batch = bca.analyze_batch(paths)
    sarif = bca.to_sarif(batch, thresholds=dict(thresholds))

    sarif_path.parent.mkdir(parents=True, exist_ok=True)
    sarif_path.write_text(sarif, encoding="utf-8")
    print(f"wrote {sarif_path} ({len(sarif.encode('utf-8'))} bytes)")
    return sarif

to_sarif accepts:

  • A single dict returned by bca.analyze or bca.analyze_source.
  • Any iterable yielding such dicts, bca.AnalysisFailure instances, or None (the natural shape of bca.analyze_batch’s return value). AnalysisFailure and None entries are skipped silently; they represent files for which no record was emitted, not findings.
  • A scalar None, the documented return of bca.analyze for a skipped file; it yields an empty SARIF run.

Thresholds

Accepted threshold names are the library’s metric_catalog::METRICS table in src/metric_catalog.rs, the same table the CLI’s --threshold reads:

  • cognitive, cyclomatic, cyclomatic.modified
  • halstead.volume, halstead.difficulty, halstead.effort, halstead.time, halstead.bugs
  • loc.sloc, loc.ploc, loc.lloc, loc.cloc, loc.blank, loc.dloc
  • nom, tokens, nexits, nargs
  • mi.original, mi.sei, mi.visual_studio
  • abc, wmc, npm, npa

Names resolve by the rule bca check --threshold applies, through the library’s metric_catalog::resolve_threshold_metric, so a name copied from a CLI invocation works unchanged. A bare loc sub-metric name (sloc, ploc, lloc, cloc, blank, dloc, the spelling bca diff --metric buckets by) stands for its dotted id, so {"sloc": 1200} and {"loc.sloc": 1200} render the same document. A family head with no scalar of its own (mi, halstead, loc) raises ValueError naming the ids to choose from:

ambiguous metric "mi": this family has no single threshold scalar; did you mean one of: mi.original, mi.sei, mi.visual_studio?

One id under both spellings ({"sloc": 1200, "loc.sloc": 1500}) is also a ValueError, as the same pair is in a bca.toml table: it is one metric, so set it once. Any other name raises ValueError listing the accepted set, with a “did you mean” hint when an id is close, so a typo fails fast instead of silently producing an empty SARIF run.

Limits are validated by the same rules bca check applies, so a dict that the binding accepts is one the CLI accepts too. Most metrics are counts: the limit is a ceiling, a value strictly above it is a finding, and the limit must be a finite, non-negative number. The mi.* Maintainability Index family is the other way round. Its limit is a floor, and a value strictly below it is the finding. mi.original and mi.sei have no true zero and go negative on large functions, so {"mi.original": -50} is a legal floor. A limit that parses but can never do what you meant raises a UserWarning instead of a ValueError. mi.visual_studio is clamped to [0, 100], so {"mi.visual_studio": 0} warns limit mi.visual_studio=0 cannot fire: 0 is the metric's floor; gate mi.original instead. A NaN or infinite limit raises ValueError, and a bool limit raises TypeError rather than reading False as 0. The CLI side of these rules is described under bca check.

thresholds=None (the default) and thresholds={} both produce a well-formed SARIF document with empty results and rules arrays. This matches bca check, which applies no implicit limits: every run supplies its own, from --threshold or a bca.toml (which bca init scaffolds with a starting table).

Upload to GitHub Code Scanning

# .github/workflows/code-scanning.yml (excerpt)
- name: Compute metric SARIF
  run: |
    python - <<'PY'
    import big_code_analysis as bca
    with open("paths.txt", encoding="utf-8") as paths_fh:
        results = bca.analyze_batch(paths_fh.read().splitlines())
    with open("metrics.sarif", "w", encoding="utf-8") as fh:
        fh.write(bca.to_sarif(results, thresholds={"cyclomatic": 15}))
    PY
- name: Upload to Code Scanning
  uses: github/codeql-action/upload-sarif@v3
  with:
    sarif_file: metrics.sarif

The upload action is documented under github/codeql-action/upload-sarif. The bindings produce one SARIF run per call; the action handles the upload to the repository’s Code Scanning alerts.

Which spaces produce findings

to_sarif emits a finding when a space’s own value breaches a limit and the metric applies to that space, exactly matching bca check --report-format sarif. LoC limits apply to file units; nom, wmc, npm, and npa apply to containers; the remaining limits apply to functions. For most metrics the JSON headline at a space already is that space’s own value. The five subtree-aggregate metrics (cyclomatic, cyclomatic.modified, cognitive, abc and nargs) also expose a sum / magnitude / total rolled up across child spaces; the binding reads their per-space value field instead, so it reports an interior breach (for example a function whose own complexity breaches even though a nested closure’s does not) without being fooled by the larger aggregate. For nargs that means a function is gated on its own parameter list, exactly as bca check has been since #1196; a closure with its own space produces its own finding rather than inflating the enclosing function’s.

Unit findings carry logicalLocations: [{"fullyQualifiedName": "<file>"}]. Every other space carries its qualified symbol. Within that symbol, a closure/lambda (the <anonymous> name every grammar emits) and the None-name parse-failure case both collapse to <anon@L{start_line}>, matching the CLI’s space_segment.

See also

  • Batch processing: the natural source of input iterables for to_sarif; AnalysisFailure entries are skipped silently.
  • Metric selection: threshold names are a closed set independent of metrics=. Metrics omitted by metrics= produce no findings; other selected metrics can still produce findings for their supplied thresholds.
  • Error handling: the typed exceptions to_sarif raises for bad caller input (TypeError / ValueError).

Change-history (VCS) metrics

The big_code_analysis.vcs submodule ranks files and scores commits by change-history risk: signals derived from git history rather than the source AST. It is the Python analogue of the bca vcs CLI command, and the same Rust engine backs both, so the returned dicts match the CLI’s structured output field-for-field. This page is for a caller who already knows what the signals mean and wants the Python call shapes.

from big_code_analysis import vcs

report = vcs.rank("path/to/repo", top=20)
trend = vcs.trend("path/to/repo", points=6)
commit = vcs.commit("path/to/repo", commit="HEAD")
diff = vcs.score_diff(unified_diff_text)

The four entry points mirror the bca vcs subcommands: vcs.rank ranks files (bca vcs), vcs.trend samples that ranking over time (bca vcs trend), vcs.commit scores one commit (bca vcs commit), and vcs.score_diff scores a bare unified diff (bca vcs commit --diff). For background on the signals, the composite risk score, and the underlying defect-prediction literature, read the CLI chapter; this page covers the Python surface.

This is distinct from analyze(..., vcs=True), which attaches a vcs block to a single file’s metrics. The vcs submodule walks history once for a whole repository, so prefer it for ranking; reach for the analyze kwarg only when you want change-history numbers alongside a file’s AST metrics. See Batch processing for the analyze_batch(..., vcs=True) path that amortises the walk across a repository’s files.

Ranking files

vcs.rank(repo_path, *, options=None, top=None, no_cache=False, cache_dir=None) ranks every in-scope file by descending risk and returns a VcsReportDict. The keyword-only knobs that vary per call live on rank; the history-walk knobs shared with trend and commit live on a shared Options object (covered below).

from big_code_analysis import vcs

report = vcs.rank("path/to/repo", top=20)

print(f"long window:  {report['long_window_days']} days")
print(f"recent window: {report['recent_window_days']} days")

for ranked in report["files"]:
    block = ranked["vcs"]
    print(f"{block['risk_score']:6.2f}  {ranked['path']}")

top caps how many files the ranking keeps; 0 or None keeps all. The report carries the resolved window lengths and the risk_score_version / vcs_schema_version stamps once at the top level (not on each file’s vcs block). The files list is ordered by descending vcs.risk_score, and a vcs_aggregate key holds the repository bus-factor summary when it was computed:

aggregate = report.get("vcs_aggregate")
if aggregate is not None:
    bus = aggregate["bus_factor"]
    print(f"repo bus factor: {bus['repo']['bus_factor']}")

Scoring a commit

vcs.commit(repo_path, *, commit="HEAD", options=None) scores a single commit for just-in-time (commit-level) risk against its first parent, returning a JitCommitReportDict. The commit argument is any git revision spelling ("HEAD", "HEAD~3", a branch, a tag, a SHA).

from big_code_analysis import vcs

report = vcs.commit("path/to/repo", commit="HEAD")

print(f"risk score: {report['risk_score']}")
print(f"is merge:   {report['commit']['is_merge']}")

size = report["features"]["size"]
print(f"+{size['lines_added']} -{size['lines_deleted']} "
      f"across {size['files_touched']} files")

# Each feature group's signed push on the ordinal score.
for group, value in report["contributions"].items():
    print(f"  {group:<11} {value}")

The score is ordinal: rank commits by it, or compare a commit against the repository’s own distribution, but do not read the magnitude as a probability. The contributions block reports each feature group’s signed contribution so a consumer can see why a commit ranked where it did.

Scoring an arbitrary diff

vcs.score_diff(diff) scores a git-style unified diff that has not been committed yet, the shape a pre-commit hook or code-review bot works with. It returns a JitDiffReportDict.

import subprocess
from big_code_analysis import vcs

staged = subprocess.run(
    ["git", "diff", "--cached"],
    capture_output=True, text=True, check=True,
).stdout

report = vcs.score_diff(staged)
print(f"partial risk: {report['partial_risk_score']}")

A bare diff carries no author, parent, or file history, so source is the literal "diff", only the size and diffusion groups are computable, and partial_risk_score is not comparable to a commit’s risk_score. The history, experience, and purpose groups are absent, not zero.

Sampling a trend

vcs.trend(repo_path, *, options=None, points=12, span=None, top=None, top_deltas=None) samples the file ranking at several points in time and returns a VcsTrendDict. Each point re-anchors at that moment’s mainline tip, so the series is a sequence of true historical snapshots rather than the current ranking re-projected backwards.

from big_code_analysis import vcs

trend = vcs.trend("path/to/repo", points=6, span="6mo", top_deltas=10)

# The sample timestamps, oldest first (Unix seconds).
print("sampled at:", trend["as_of_points"])

# Files that regressed most over the window.
for delta in trend["deltas"]["regressed"]:
    print(f"  +{delta['delta']:.2f}  {delta['path']}")

The trend contains points samples, with a minimum of 2. They cover the interval specified by span (default 12mo) and end at options.as_of. The files map aligns each file’s series 1:1 with as_of_points, with a None element where the file did not yet exist. The deltas summary splits files into improved and regressed lists; top_deltas trims each list and top caps how many files are kept.

Shared options

The three repository-walking entry points (rank, trend, and commit) accept the same vcs.Options object, so one configuration can drive a rank plus a trend pass without restating the common knobs. Every field is keyword-only and optional, and the defaults reproduce the bca vcs CLI defaults, so Options() matches the default ranking.

from datetime import datetime, timezone
from big_code_analysis import vcs

options = vcs.Options(
    long_window="2y",
    recent_window="60d",
    risk_formula="percentile",
    file_types=["rs", "py"],
    as_of=datetime(2026, 1, 1, tzinfo=timezone.utc),
)

report = vcs.rank("path/to/repo", options=options, top=20)

The widened option kwargs (issue #619) each accept more than a bare string:

  • file_types selects which files to rank: "metrics" (the default: only files bca computes metrics for), "all" (every tracked text file), a comma-separated extension allow-list ("rs,py"), or a Sequence[str] of extensions (["rs", "py"]).
  • as_of pins the reference “now” for reproducible snapshots, as either a datetime or a string (RFC 3339, @unix, or a git date). With as_of pinned, the ranking is computed as it stood at that moment, not against the wall clock.
  • cache_dir (on rank, not Options) accepts a str or any os.PathLike; a pathlib.Path passes straight through.

The history-walk toggles mirror the CLI flags: full_history, include_merges, follow_renames (default True), exclude_bots (default True), and bot_pattern to override the bot-author regular expression. bus_factor_threshold sets the coverage fraction for the bus-factor flag (default 0.5), and emit_author_details includes SHA-256-hashed canonical author identities. author_hash_key (requires emit_author_details) hardens those digests into a keyed HMAC-SHA256, the same opt-in described under Author-detail privacy.

Caching

vcs.rank keeps a persistent cache of each history walk, on by default. A cache hit is bit-identical to a fresh walk, and the time windows are recomputed against the current moment on every run. The caching section lists what invalidates an entry, and the one walk input that does not.

from big_code_analysis import vcs

# First call primes the cache; the second replays it.
vcs.rank("path/to/repo")
vcs.rank("path/to/repo")  # reuses the prior walk

vcs.rank("path/to/repo", no_cache=True)            # ignore the cache
vcs.rank("path/to/repo", cache_dir="/tmp/bca")     # override the directory

By default the cache lives under the platform cache directory. Author identities are stored only as their SHA-256 digests, never plaintext. Hashing is pseudonymization, not anonymization: the digests are recoverable against a candidate email set. See Author-detail privacy.

Releasing the GIL

The repository-walking calls (vcs.rank, vcs.trend, and the commit score in vcs.commit) release the GIL across the history walk (issue #620), so a ThreadPoolExecutor can rank several repositories in parallel without serialising on the interpreter lock:

from concurrent.futures import ThreadPoolExecutor
from big_code_analysis import vcs

repos = ["service-a", "service-b", "service-c"]

with ThreadPoolExecutor() as pool:
    reports = list(pool.map(lambda r: vcs.rank(r, top=20), repos))

This is the same pattern the Async patterns page applies to the per-file analyze calls.

Errors

The vcs functions raise a typed exception hierarchy rooted at bca.VcsError (itself a ValueError). See Error handling for the full taxonomy and which call raises which type.

See also

  • Change-history (VCS) metrics: the CLI chapter, with the signal definitions, the composite risk-score formula, and the defect-prediction literature behind them.
  • Batch processing: analyze_batch(..., vcs=True) attaches a per-file vcs block while sharing one history index per repository.
  • Error handling: the VCS exception taxonomy.

Error handling

Single-file analysis raises exceptions for file and analysis failures. Batch analysis returns those failures as AnalysisFailure values and continues. Invalid caller arguments (ValueError for a bad value, TypeError for the wrong type) still raise and abort the batch. This page lists every exception each entry point can raise and the one value type a batch returns instead.

def run(
    fixtures: Path,
    *,
    missing_path: Path,
) -> dict[str, Any]:
    """Trigger each error path and return a small report.

    ``fixtures`` is a directory containing at least ``hello.rs``;
    ``missing_path`` must NOT exist on disk.
    """
    report: dict[str, Any] = {
        "file_not_found": False,
        "unsupported": False,
        "batch_errors": 0,
    }

    # 1. analyze() on a missing path raises a typed OSError subclass.
    try:
        bca.analyze(missing_path)
    except FileNotFoundError as err:
        report["file_not_found"] = True
        print(f"file_not_found: errno={err.errno} filename={err.filename}")

    # 2. analyze() on an unknown extension raises
    #    UnsupportedLanguageError (itself a ValueError subclass).
    #    The write is inside the try/finally so a future second
    #    mutation before the analyse call still gets cleaned up.
    unknown = fixtures / "hello.unknown_extension"
    try:
        unknown.write_text("noop", encoding="utf-8")
        bca.analyze(unknown)
    except bca.UnsupportedLanguageError as err:
        report["unsupported"] = True
        print(f"unsupported_language: {err}")
    finally:
        unknown.unlink(missing_ok=True)

    # 3. analyze_batch() returns AnalysisFailure, never raises per-file.
    paths = [fixtures / "hello.rs", missing_path]
    for slot in bca.analyze_batch(paths):
        if isinstance(slot, bca.AnalysisFailure):
            report["batch_errors"] += 1
            print(f"batch_error: ({slot.error_kind}) {slot.error}")

    return report

Single-file exceptions

bca.analyze and bca.analyze_source raise:

ExceptionSubclass ofTriggered by
bca.UnsupportedLanguageErrorValueErrorUnknown extension + no shebang / emacs-mode hit
bca.ParseErrorValueErrortree-sitter rejected the source
ValueError (raw)(builtin)Non-UTF-8 path with allow_lossy_path=False (the default)
OSError and subclasses(builtin)std::fs::read failed

The OSError raised by analyze dispatches to the canonical subclass based on errno:

import big_code_analysis as bca

path = "src/example.rs"

try:
    bca.analyze(path)
except FileNotFoundError as err:
    print("missing:", err.errno, err.filename)
except PermissionError as err:
    print("denied:", err.errno, err.filename)
except IsADirectoryError as err:
    print("directory:", err.errno, err.filename)

Each branch dispatches on the underlying errno:

ExceptionTypical err.errno (Linux)When it fires
FileNotFoundError2 (ENOENT)Path does not exist.
PermissionError13 (EACCES)Read bit denied for the calling user.
IsADirectoryError21 (EISDIR)Path resolves to a directory.

On Windows the Win32 code is on err.winerror, and err.errno holds the POSIX code CPython maps it to, so the same branches apply: a missing file or a missing parent directory is a FileNotFoundError. Windows reports a directory read as access denied, so a directory raises PermissionError there rather than IsADirectoryError.

Use except OSError if you want to catch the whole family and inspect err.errno / err.filename yourself.

UnsupportedLanguageError and ParseError are both ValueError subclasses, so a single except ValueError catches both. Prefer the typed catches when you want to differentiate.

Batch errors

bca.analyze_batch returns bca.AnalysisFailure values instead of raising, so a single bad file does not break the whole batch.

for slot in bca.analyze_batch(paths):
    if isinstance(slot, bca.AnalysisFailure):
        log.warning("%s (%s): %s", slot.path, slot.error_kind, slot.error)
    else:
        process(slot)

error_kind is a closed Literal:

  • "UnsupportedLanguage": extension and shebang / emacs-mode resolution both came up empty.
  • "ParseError": tree-sitter rejected the input, or (rarely) a Rust-side JSON serialisation of the result failed. The serialisation case is prefixed with internal: serialization error: in the error string; check for the prefix when the distinction matters, since re-reading the file does not recover from a serialisation failure.
  • "IoError": a filesystem read or path-encoding failure. The closed taxonomy folds non-UTF-8 paths in here rather than adding a distinct fourth value.

For "IoError" instances the underlying OS errno is preserved in the error string via Rust’s default formatting ("<msg> (os error <N>)" on Unix). Parse it with a regex if you need it for retry classification:

import re

match = re.search(r"\(os error (\d+)\)$", slot.error)
errno = int(match.group(1)) if match else None

If you need typed OSError subclasses, call bca.analyze per file instead of analyze_batch; single-file analyze raises FileNotFoundError / PermissionError / IsADirectoryError directly.

Programmer errors in batches

analyze_batch does still raise on caller bugs:

  • TypeError if paths is not iterable, or an element is not str / os.PathLike[str]. This aborts the whole call; any results computed before the bad element are discarded.
  • ValueError if metrics= is an explicitly empty sequence or contains an unknown name. Validation runs before the input iterable’s __iter__, so a generator’s side effects (and any partial yields) are preserved on this raise path.

Change-history (VCS) exceptions

The big_code_analysis.vcs functions raise a typed hierarchy rooted at bca.VcsError, itself a ValueError, so an existing except ValueError (or except bca.VcsError) catches every VCS failure (#624). The analyze(..., vcs=True) kwarg shares the same option-parsing errors.

ExceptionSubclass ofTriggered by
bca.NotARepositoryErrorbca.VcsErrorrepo_path is not inside a git working tree
bca.InvalidRevisionErrorbca.VcsErrorA reference / commit could not be resolved
bca.InvalidDiffErrorbca.VcsErrorThe diff passed to vcs.score_diff is malformed
bca.VcsEnvironmentErrorbca.VcsErrorHistory walk, diffing, .mailmap, blame, or cache I/O failed
bca.VcsError (raw)ValueErrorA malformed option value (window / timestamp / formula / file-type scope / bus-factor threshold / bot pattern / trend point count); the message names the offending value

NotARepositoryError is the variant to branch on for “not a repo, skip this directory”. The base VcsError is raised directly for a bad option, where the message names the offending value, while the named subclasses cover the input failures (a missing revision, a malformed diff). VcsEnvironmentError is the environment / backend bucket, mirroring the 500 (not 400) responses the web crate returns for the same failures.

import big_code_analysis as bca
from big_code_analysis import vcs

try:
    report = vcs.rank("path/to/repo", top=20)
except bca.NotARepositoryError:
    print("not a git repository, skipping")
except bca.VcsError as err:
    # Malformed window, formula, file-type scope, and so on.
    print("bad VCS option:", err)

analyze(..., vcs=True) is the exception to the NotARepositoryError rule: a file outside any repository yields no vcs block rather than raising, so only the option-parsing VcsError reaches the caller from that path.

Logging recipe

A small logging helper for batch output keeps successes and failures aligned without bespoke formatting:

import logging
import big_code_analysis as bca

log = logging.getLogger(__name__)

def report(paths: list[str]) -> None:
    # skip_generated=False keeps the result list index-aligned with
    # `paths`; with the default True, a generated file yields no slot
    # and the zip silently misaligns.
    for path, slot in zip(paths, bca.analyze_batch(paths, skip_generated=False)):
        if isinstance(slot, bca.AnalysisFailure):
            log.warning(
                "skip %s (%s): %s", path, slot.error_kind, slot.error
            )
        elif slot is None:
            # Nothing to parse: three bytes or fewer, a UTF-16 BOM, or a
            # binary leading window. The slot is held open so the zip
            # above stays aligned (#1238); it is not a failure.
            log.info("skip %s: empty or binary", path)
        else:
            log.info(
                "ok %s sloc=%s", path,
                slot["metrics"]["loc"]["sloc"],
            )

See also

  • Batch processing: the never-raise contract that routes per-file failures into AnalysisFailure slots.
  • Async patterns: asyncio.gather(..., return_exceptions=True) is the async-side equivalent of the batch contract; per-task exceptions land in the result list instead of cancelling the whole gather.
  • Quick start: the single-file analyze path that raises typed OSError subclasses.

Async patterns

bca.analyze is CPU-bound: the work is a tree-sitter parse plus the metric passes, both of which release the GIL on the Rust side via PyO3’s Python::detach. The canonical pattern for calling it from an asyncio application is therefore asyncio.to_thread:

async def analyze_async(path: Path) -> FuncSpaceDict | None:
    """Run ``bca.analyze(path)`` on the default thread executor."""
    return await asyncio.to_thread(bca.analyze, path)


async def analyze_all(
    paths: Iterable[Path],
) -> list[FuncSpaceDict | BaseException | None]:
    """Fan ``analyze_async`` out across ``paths`` with ``asyncio.gather``.

    ``return_exceptions=True`` matters here: ``bca.analyze`` runs
    inside ``asyncio.to_thread`` and Python threads cannot be
    cancelled. If one call raises and gather re-raises with
    ``return_exceptions=False``, the surviving threads keep running
    in the default executor, producing results that are silently
    discarded. With ``return_exceptions=True`` every thread's
    result (success OR exception) lands in the returned list so
    the caller can dispatch per-file.
    """
    return await asyncio.gather(
        *(analyze_async(p) for p in paths),
        return_exceptions=True,
    )

Why to_thread, not native async

bca.analyze is a synchronous Python function backed by synchronous Rust code; there is no await boundary inside it. Wrapping it in asyncio.to_thread:

  1. Schedules the call on the default thread pool.
  2. Lets other coroutines progress while the parse and metric pass run.
  3. Returns the result to the calling coroutine when done.

Because the Rust side releases the GIL across the heavy work, several to_thread(bca.analyze, ...) calls run in parallel. This is not co-operative I/O multiplexing; it is real multi-core use, capped by the thread pool’s size.

Custom executors

to_thread always uses the event loop’s default executor. For a tighter cap on the worker count, use loop.run_in_executor with a purpose-built pool instead:

import asyncio
from concurrent.futures import ThreadPoolExecutor

import big_code_analysis as bca

async def analyze_many(paths: list[str]) -> list[object]:
    loop = asyncio.get_running_loop()
    with ThreadPoolExecutor(max_workers=8) as pool:
        return await asyncio.gather(
            *(loop.run_in_executor(pool, bca.analyze, p) for p in paths)
        )

Start with a worker count near the CPUs available to the process. Benchmark your workload before increasing it; additional workers compete for CPU time.

Streaming results

asyncio.as_completed lets you start consuming results as soon as the first analysis finishes, which helps when the per-file work varies widely in cost (a 5 KB file against a 500 KB generated bundle):

import asyncio
import big_code_analysis as bca

async def first_failure(paths: list[str]) -> str | None:
    """Return the path of the first file with cyclomatic > 50."""
    tasks = [asyncio.create_task(asyncio.to_thread(bca.analyze, p)) for p in paths]
    try:
        for coro in asyncio.as_completed(tasks):
            result = await coro
            if result is None:
                continue
            if result["metrics"]["cyclomatic"]["sum"] > 50:
                return result["name"]
    finally:
        for t in tasks:
            t.cancel()
    return None

The finally block cancels pending asyncio tasks; as_completed does not cancel them itself when the caller returns early. Analyses already running in worker threads continue until they finish; cancellation does not interrupt the Rust work.

Anti-pattern: calling bca.analyze directly in a coroutine

# Don't do this.
async def bad(path: str) -> dict | None:
    return bca.analyze(path)  # blocks the event loop on every call

async def does not make the body asynchronous. Without to_thread or an explicit executor, every coroutine that calls bca.analyze stalls the event loop for the full duration of the parse: other tasks waiting on I/O, timers, or queues all freeze until the parse returns. The to_thread wrapper is one line and is the difference between a responsive server and a single-threaded one.

When analyze_batch is the better fit

If you are processing a static, finite list of paths and do not need streaming results, bca.analyze_batch is simpler than gather(*to_thread(...)): it runs sequentially on the calling thread but never raises on per-file errors. Wrap the whole analyze_batch call in asyncio.to_thread to keep the event loop responsive:

import asyncio
import big_code_analysis as bca

async def batch(paths: list[str]) -> list[object]:
    return await asyncio.to_thread(bca.analyze_batch, paths)

This trades the per-file parallelism of gather for the simpler error model of analyze_batch. Pick gather when you want both parallelism and typed OSError dispatch; pick to_thread(analyze_batch, paths) when you want one async call and the never-raise contract.

Developers Guide

How to build, test, lint and run big-code-analysis from a checkout. It is for contributors; the validation gate and review conventions live in CONTRIBUTING.md, and the rules shared with coding agents in AGENTS.md, both at the repository root.

You need a current stable Rust toolchain; the minimum supported version is declared in the root Cargo.toml.

Clone Repository

Clone through HTTPS

git clone -j8 https://github.com/dekobon/big-code-analysis.git

or through SSH

git clone -j8 git@github.com:dekobon/big-code-analysis.git

Make is the canonical entry point

The repository ships a Makefile that wraps every common build, test, lint, format, and docs task. Run make help to see the full list of targets. Run make check-tools to list the optional tools and check which are installed.

The two composite targets you will use most:

  • make pre-commit: the recommended local gate before committing. It runs formatting, both clippy configurations, tests, documentation checks, dependency checks, and the lint families. It also runs repository consistency checks, both self-scan tiers, and the Python validation stages, in one parallel pass. The membership comparison (make check-test-lang-gates-compare) requires COMPARE_REF and runs separately in CI. See _pc-all in the Makefile for the complete stage list. The gate ends with a single BCA_GATE: pass or BCA_GATE: fail (…) line; read the verdict from that line and nothing else.
  • make ci: the same checks in the order CI runs them, with no auto-fixing. Use this to reproduce a failing CI run locally.

If GNU Make 4 or any of the optional tools are unavailable, fall back to the raw cargo commands shown below; they are equivalent to the corresponding Make targets.

Building

To build the big-code-analysis library, the CLI, and the web server in one shot:

make build           # cargo build --workspace --all-targets
make build-release   # cargo build --workspace --release

For an individual crate, invoke cargo directly:

cargo build                              # library only
cargo build -p big-code-analysis-ast     # parse + classification layer only
cargo build -p big-code-analysis-cli     # CLI only
cargo build -p big-code-analysis-web     # web server only

make check runs cargo check --workspace --all-targets for fast type-checking during iteration.

Testing

To verify that all tests pass:

make test       # cargo nextest run --workspace --all-features
make test-doc   # cargo test --workspace --all-features --doc

make test prefers cargo-nextest, which is also what CI runs. Plain cargo test finishes each test binary before starting the next, so parallelism exists only within a binary; nextest schedules every binary’s tests into one global pool. Install it with cargo install --locked cargo-nextest.

Without it, make test falls back to cargo test --workspace --all-features --lib --bins --tests, which runs the same set of tests. The two runners are not quite interchangeable, though: nextest runs each test in its own process, so anything sharing state across tests within a binary (a OnceLock cache, an environment variable, the working directory) is isolated under nextest and shared under cargo test. Preferring nextest locally means local runs match CI on that axis.

Set NEXTEST= to force the fallback without uninstalling anything (make test NEXTEST=), or point it at a specific binary (make test NEXTEST=/path/to/cargo-nextest).

Doctests are a separate target because nextest cannot run them. make pre-commit and make ci run both.

If you only want to run the cargo command yourself:

cargo test --workspace --all-features --verbose

Updating insta tests

We use insta; install cargo insta to manage snapshots. The Makefile wraps the two operations you need:

make insta-review   # cargo insta test --review (interactive)
make insta-accept   # cargo insta test --accept (use with care)

make insta-review runs the tests, generates the new snapshot references, and lets you review each diff. Reach for make insta-accept only for bulk metric-value-only refreshes (grammar bumps, Halstead operator reclassification) where you have already verified the diff pattern is uniform.

Code Formatting

Format before opening a pull request. The make fmt target runs every formatter in the project (Rust, Markdown, TOML, Bash) in one shot; make fmt-check verifies formatting without modifying files.

make fmt         # cargo fmt + rumdl check --fix + shfmt -w + taplo fmt
make fmt-check   # the equivalent --check variants

Rustfmt

rustfmt formats Rust code to the project style.

To install:

rustup component add rustfmt

To format the code (handled automatically by make fmt):

cargo fmt

Clippy

Clippy catches common mistakes. The warnings it reports must be fixed before making a pull request.

make clippy runs both clippy invocations the project enforces (default-features and --all-features); make lint additionally runs the markdown, shell, TOML, and Makefile linters.

To install:

rustup component add clippy

To detect errors and warnings:

make clippy
# or, manually:
cargo clippy --workspace --all-targets -- -D warnings
cargo clippy --workspace --all-targets --all-features -- -D warnings

Unused dependencies

make udeps runs cargo +nightly udeps --workspace --all-targets to catch dependencies declared in Cargo.toml but never referenced. Requires the nightly toolchain (rustup toolchain install nightly) and cargo-udeps.

Code Documentation

make doc        # cargo doc --no-deps --workspace --all-features  (warning-tolerant)
make doc-open   # same, then open in a browser
make doc-check  # strict gate: appends -D warnings to RUSTDOCFLAGS, fails on any rustdoc warning

make doc and make doc-open are the interactive viewers: they build whatever they can so you can still inspect rendered output mid-refactor. make doc-check is the strict gate that runs as part of make pre-commit and CI (cargo doc --no-deps --workspace --all-features with RUSTDOCFLAGS extended by -D warnings); it catches broken intra-doc links, links into private items, and other rustdoc regressions.

Remove the --no-deps option from the underlying cargo invocation if you also want to build the documentation of each dependency used by big-code-analysis.

Building this book

The book you are reading is an mdBook under big-code-analysis-book/:

make book        # mdbook build
make book-serve  # mdbook serve with live reload

Run your code

Run bca:

cargo run -p big-code-analysis-cli -- [bca-parameters]

List its parameters:

cargo run -p big-code-analysis-cli -- --help

Run bca-web:

cargo run -p big-code-analysis-web -- [bca-web-parameters]

List its parameters:

cargo run -p big-code-analysis-web -- --help

make install, make install-cli, and make install-web invoke cargo install --path for the respective binary crates.

Practical advice

  • Add at least one unit or integration test with every new feature.
  • Document the public API.
  • Do not add dead code.
  • Comment intricate code so others can follow it.
  • Run make pre-commit before pushing; it is the same gate CI runs.

Supporting a new language

How to add a language to big-code-analysis, for contributors who have a tree-sitter grammar crate for it. The work has three parts:

  1. Generate the kind enum from the grammar.
  2. Wire the grammar into the workspace.
  3. Implement the traits and tests for every supported metric.

Generating the grammar

As a prerequisite, a tree-sitter grammar crate for the language must exist that matches the tree-sitter runtime version used in this project.

The kind enums are generated by a crate in this repository called enums. The following steps register the grammar crate there and generate the enum file this project matches on when it evaluates metrics.

  1. Add the language specific tree-sitter crate to the enums crate, making sure the dependency is pinned with =X.Y.Z to the same version used in the root big-code-analysis Cargo.toml. For example, for the Rust support the following line exists in the /enums/Cargo.toml: tree-sitter-rust = "=0.24.2".
  2. Append the language to the mk_langs! list in /enums/src/languages.rs. Keeping with Rust as the example, the line is Rust,: one variant name per line, which becomes the name of the Rust enum that will be generated. Add a // -> <crate> comment when the backing crate is not the obvious tree_sitter_<name>.
  3. Add a case to the match in the mk_get_language macro rule in /enums/src/macros.rs, which binds each variant to its grammar crate. The current convention uses the LANGUAGE constant exposed by modern grammar crates: for Rust that line is Lang::Rust => tree_sitter_rust::LANGUAGE.into().
  4. Run the /recreate-grammars.sh script, which runs the enums crate to generate the enum for the new language.

This produces a new enum file for the language in /big-code-analysis-ast/src/languages/. See /big-code-analysis-ast/src/languages/language_rust.rs as an example of the generated enum.

Adding the new grammar to big-code-analysis

  1. Add the language specific tree-sitter crate to the big-code-analysis workspace, with the same =X.Y.Z pin as the enums crate uses. Since #1376 this is three edits across two manifests. In the root Cargo.toml [workspace.dependencies], add the exact grammar pin (for Rust, tree-sitter-rust = "=0.24.2"). In /big-code-analysis-ast/Cargo.toml, add the optional workspace dependency (tree-sitter-rust = { workspace = true, optional = true }) and its feature (rust = ["dep:tree-sitter-rust"]), then include that feature in all-languages. In the root [features], add the forwarding feature (rust = ["big-code-analysis-ast/rust"]) and include it in all-languages.
  2. Add the new language module to /big-code-analysis-ast/src/languages/mod.rs:
#![allow(unused)]
fn main() {
pub mod language_rust;
pub use language_rust::*;
}
  1. Add a definition of the language to the arguments of the mk_langs! macro in /big-code-analysis-ast/src/langs.rs.
#![allow(unused)]
fn main() {
// 1) Cargo feature name that enables this variant's grammar
// 2) Name for enum
// 3) Language description
// 4) Display name
// 5) Empty struct name to implement
// 6) Parser name
// 7) tree-sitter function to call to get a Language
// 8) file extensions
// 9) emacs modes
// 10) pinned grammar crate version (mirrors the `=X.Y.Z` pin in the
//     workspace Cargo.toml; a drift test asserts the two agree)
(
    "rust",
    Rust,
    "The `Rust` language",
    "rust",
    RustCode,
    RustParser,
    tree_sitter_rust,
    [rs],
    ["rust"],
    "0.24.2"
)
}

Implementing traits and tests

Wiring the grammar is only the first step. The new <Lang>Code type must also implement the AST plumbing and every metric trait the workspace defines:

  • Checker in a new per-language file under /big-code-analysis-ast/src/checker/ (checker/<lang>.rs, registered with a mod <lang>; line in checker.rs): comment, function, closure, call, string-literal, and else-if predicates over the grammar’s kind_ids, most of which forward to the language’s role table (below).
  • Getter in a new per-language file under /big-code-analysis-ast/src/getter/ (getter/<lang>.rs, registered in getter.rs): get_space_kind_with_code, its byte-less get_space_kind, and the Halstead operator/operand classification table.
  • Alterator in /big-code-analysis-ast/src/alterator.rs: an alterate body that flattens the kinds the language’s StringLit row lists to one verbatim leaf, so the AST dump agrees with find string. Every grammar carries one; only the Preproc and Ccomment helper variants, whose dump is not a user surface, keep the bare default.
  • A role table in /big-code-analysis-ast/src/roles/ (roles/<lang>.rs, one role_table! per language), declaring for every role either its rows or absent "why" (pending Rnn is accepted by the grammar but no table has used it since the series closed). The module doc of roles/mod.rs is the reference: the role set, the row grammar, what a table emits, how a consumer reads a row, and what the gates cannot see. make role-census, make role-coverage and make role-bypass (all in make pre-commit) fail on a language with no table, a kind id that is neither claimed by a row, excluded with a reason, nor baselined, or a claimed kind restated as classification in a dispatch file without a // role-bypass: allow(<Role>) exemption. A new language also needs a control-flow fixture under control_flow/<lang>/ in the integration-snapshot submodule (tests/repositories/big-code-analysis-output) and a make role-coverage-observe run, so .role-coverage-observed.txt records which of its kinds the corpus emits and the derived parity suite (make role-parity-corpus) has nodes to compare.
  • All thirteen metric traits: Abc, Cognitive, Cyclomatic, Exit (the nexits metric), Halstead, Loc, Mi, NArgs, Nom, Npa, Npm, Tokens, Wmc. Register twelve of them via the implement_metric_trait! macro invocation in each module under /src/metrics/ to start with default (no-op) bodies, then replace with real impls for the metrics that have meaningful semantics for the language. Halstead is the exception: every grammar is listed in impl_halstead_forwarding! in src/metrics/halstead.rs, which routes the metric through the language’s Getter, so there is no per-language Halstead body to write.

Audit aliased grammar variants

Tree-sitter grammars frequently emit several distinct kind_ids that map to the same node.kind() string (Identifier / Identifier2 / Identifier3 in Go, InvocationExpression / InvocationExpression2 in C#, QuotedContent through QuotedContent20 in Elixir). Every match node.kind_id() arm that touches an aliasable rule must either list every numbered variant or compare on the string node.kind() instead. Missing an alias silently drops nodes from the metric. See the add-lang skill for the mechanical audit procedure, and docs/development/grammar_dispatch.md plus lessons 2, 19 and 34 in docs/development/lessons_learned.md for the failure modes.

Tests

Add per-language tests under each src/metrics/*.rs test module; aim for parity with the existing Rust coverage across the metric files. Every insta assert_json_snapshot! call MUST be anchored: either with an inline expected block, a positive assert_eq! on the headline integer accessor above it, or an explanatory // expected: comment. make snapshot-anchors (run as part of make pre-commit) enforces this against .snapshot-anchor-baseline.txt. Every test that names the new parser also needs #[cfg(feature = "<lang>")], since the *Parser alias compiles in every build and panics in one without the grammar; make check-test-lang-gates (also in make pre-commit) derives the required gate, and ./utils/check-test-lang-gates.py --fix writes it (the Makefile target forwards no arguments).

End-to-end workflow

For an opinionated, end-to-end recipe (including the alias audit, test layout, snapshot anchoring, and code-quality post-passes) see the project’s add-lang Claude Code skill. It is the canonical workflow used by recent language additions (Elixir, PHP, C#, Bash, Go).

Lines of Code (LoC)

How to implement the LoC metrics for a language, for contributors adding or fixing a Loc impl. Lines of code is a software metric that indicates the size of source code by counting its lines. There are several kinds of LoC; this page first explains each by way of an example, then covers the implementation.

Types of LoC

#![allow(unused)]
fn main() {
/*
Instruction: Implement factorial function
For extra credits, do not use mutable state or a imperative loop like `for` or `while`.
 */

/// Factorial: n! = n*(n-1)*(n-2)*(n-3)...3*2*1
fn factorial(num: u64) -> u64 {

    // use `product` on `Iterator`
    (1..=num).product()
}
}

The example above illustrates each of the LoC metrics described below.

SLOC

A straight count of all lines in the file including code, comments, and blank lines.

METRIC VALUE: 11

PLOC

A count of the instruction lines of code contained in the source code. This includes any brackets or similar syntax on a new line. Comments and blank lines are not counted.

METRIC VALUE: 3

LLOC

The “logical” lines are a count of the statements in the code. What a statement is depends on the language. In the example above there is a single statement, the call of product with the Iterator as its argument.

METRIC VALUE: 1

CLOC

A count of the comment lines in the code. The type of comment does not matter: single line, block, or doc. A Python docstring counts here as well, and is also reported on its own as DLOC below.

METRIC VALUE: 6

BLANK

A count of the blank lines in the code.

METRIC VALUE: 2

DLOC

Documentation Lines Of Code: the rows the language treats as documentation, such as a doc comment (the /// Factorial line above). Documentation includes Python docstrings and Perl POD blocks. Those rows are always counted in CLOC too, so DLOC never exceeds it. Each language follows its documentation tool’s rules (Doxygen, Javadoc, KDoc, JSDoc, PHPDoc, C# XML docs, LDoc, go/doc, YARD); see the DLOC reference. The SEI variant of the Maintainability Index excludes DLOC from the comment lines used in its formula. An Elixir @doc heredoc is an assignment that counts as code.

METRIC VALUE: 1

Whitespace-only files

Source that contains no token at all (a file of nothing but spaces, tabs, and newlines) reports the rows it has: a four-row file of spaces is sloc 4, ploc 0, blank 4, with or without a trailing newline.

This used to be the one input class where a trailing newline changed a LoC value. Most grammars collapse tree-sitter’s root node to a zero-width node at end-of-input for such input rather than spanning the file, and the file-level SLOC span was measured from that node, so those files reported sloc 0 when they ended in a newline and sloc 1 when they did not, while Elixir, Tcl, iRules and the preproc / ccomment helpers kept the root span and reported the rows either way.

The file-level span is now anchored at line 1 rather than measured from the root node’s first token, so where the root node starts is no longer observable in LoC and every grammar answers alike. The sweep that used to pin the split, whitespace_only_input_is_uniform_across_grammars in /src/metrics/loc.rs, now pins the absence of one.

The same anchoring is what makes leading blank lines count. A file opening with three blank rows before its first token reports those rows in sloc and blank, exactly as interior blank rows are reported.

Implementation

Implement the Loc trait, and its compute function, for the language. Each language’s impl Loc lives in its own file under /src/metrics/loc/ (rust.rs, python.rs, …), with the row-billing helpers every arm routes through in shared.rs; the tests stay in /src/metrics/loc.rs. Read a sibling language’s file for the shape.

Take care with the catch-all _ arm that inserts a row into PLOC. If the grammar surfaces its row terminator as a token rather than as extra, as the Tcl family does, that token’s start row is the row it terminates, so the catch-all credits comment-only and blank rows to PLOC. Give such tokens an explicit no-op arm.

If the grammar splices \ + newline away in the lexer (an extras entry such as /\\\r?\n/), a row holding only \ surfaces no node, and no arm can credit it. Declare the language’s comment kinds on Loc::bare_rows instead: the walk then runs the shared gap scan (shared.rs, credit_bare_continuation_rows) ahead of each compute, which credits such a row to the innermost space as the logical line it continues. Choose BareRowPolicy::Gap where a comment never continues past its row (Bash, Ruby, Elixir) or where it does but only as the comment’s own text (Tcl, iRules); choose BareRowPolicy::Indexed where a block comment can leave a \ on a row of comments alone, as in the C family, so that what a bare row is depends on the whole logical line and is settled once per file (continuation.rs).

Update grammars

How to bump a tree-sitter grammar, for contributors maintaining the grammar pins. big-code-analysis parses each language with a tree-sitter grammar. Grammars change over time and carry bugs of their own, so they need updating now and then.

The update scripts are bash, so grammars can be regenerated natively only on Linux and macOS. On Windows, run the scripts under Windows Subsystem for Linux (WSL).

Two kinds of grammar are in use: third-party grammars published on crates.io and maintained by external developers, and internal grammars defined inside the project for the variants of some languages used in Firefox. Each has its own procedure below.

Third-party grammars

Update the grammar version in Cargo.toml and enums/Cargo.toml. For example, for the tree-sitter-java grammar:

tree-sitter-java = "=x.xx.x"

where x represents a digit. The pin must be exact (=X.Y.Z); make check-excluded-manifests rejects a caret range. The same version also appears as the last field of the language’s mk_langs! tuple in big-code-analysis-ast/src/langs.rs, and the grammar_version_matches_cargo_toml_pin test there fails until the two agree.

Run ./recreate-grammars.sh to regenerate every grammar’s enum:

./recreate-grammars.sh

Once the script finishes, fix any failed tests and problems the grammar changes introduced. make enums-codegen-drift (part of make pre-commit) re-runs the codegen into a temporary directory and fails if the checked-in language_*.rs or c_langs_macros/*.rs files differ from it.

A clean build does not prove the bump safe. A release can give a rule a second alias, after which the parser reports only the new, suffixed id: dekobon-tree-sitter-groovy 0.6.0 did this to variable_declarator, so code matching Groovy::VariableDeclarator compiled and never matched again. Diff the regenerated language_<lang>.rs. For every rule that gains a numeric-suffixed variant, run bca dump on a fixture containing it to see which id the parser reports, and move every match arm that names an id the parser no longer reports.

Commit your changes and open a pull request.

Internal grammars

Update the version of tree-sitter-cli in the package.json file of the internal grammar, then refresh its committed package-lock.json with npm install --package-lock-only --ignore-scripts in the same directory and commit both files together. The regen scripts install with npm ci, which fails loudly when the lockfile is missing or out of sync with package.json. This keeps every regen hash-verified and byte-reproducible (OpenSSF Scorecard Pinned-Dependencies).

The five vendored grammars publish under the bca-tree-sitter-* namespace (see RELEASING.md for the rename rationale), but consumer call sites still reference them as tree-sitter-<lang> via Cargo’s package = ... alias. A grammar refresh does not bump the leaf’s version on its own: every crate in this repository shares one workspace-wide version, and bumping the leaves out of step with the parent is not allowed (see the “Lockstep version policy” in RELEASING.md). Regenerate the parser tables, accept the resulting test-snapshot drift, and ship the change under the current version. The next workspace release picks up the new grammars at whatever shared version the next tag declares.

If a regeneration also needs an updated tree-sitter runtime dependency, bump the dev-dependency line inside the leaf’s Cargo.toml:

[dev-dependencies]
tree-sitter = "=x.x.x"

Leave [package] name = "bca-tree-sitter-<lang>", [package] version, and [lib] name = "tree_sitter_<lang>" untouched; the rename trick in [lib] is what keeps Rust import paths stable, and the version line is managed by the lockstep bump at release time.

Then run the script for the grammar, which regenerates every file.

For tree-sitter-ccomment and tree-sitter-preproc run ./generate-grammars/generate-grammar.sh followed by the name of the grammar, for example:

./generate-grammars/generate-grammar.sh tree-sitter-ccomment

tree-sitter-mozcpp and tree-sitter-mozjs have their own scripts.

For tree-sitter-mozcpp, run

./generate-grammars/generate-mozcpp.sh

For tree-sitter-mozjs, run

./generate-grammars/generate-mozjs.sh

Both of those crates carry a notification-only marker dependency in their Cargo.toml (tree-sitter-cpp for mozcpp, tree-sitter-javascript for mozjs) that Cargo never compiles. make grammar-marker-sync compares each marker against .grammar-marker-baseline.toml, so a Dependabot bump of the marker without a regen fails the gate. After a verified regen, refresh the baseline in the same commit with ./utils/check-grammar-marker-sync.py --update.

tree-sitter-tcl, the fifth vendored grammar, has no regeneration script: it vendors pre-generated parser sources only (no grammar.js), so updating it means re-vendoring the generated src/ from its upstream project rather than running tree-sitter generate locally.

Once the script finishes, fix any failed tests and problems the grammar changes introduced.

Commit your changes and open a pull request.