Skip to content

C# and C/C++: Make autobuild script selection deterministic. - #23276

Merged
michaelnebel merged 7 commits into
github:mainfrom
michaelnebel:michaelnebel-deterministic-csharp-build-script
Dec 2, 2026
Merged

michaelnebel merged 7 commits into
github:mainfrom
michaelnebel:michaelnebel-deterministic-csharp-build-script

Conversation

@michaelnebel

@michaelnebel michaelnebel commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

As a part of looking into the QA results for C#, we found an example, where different scripts were picked for attempting to (auto)build the code in aspnet/AspNetKatana.
It appears that we randomly pick a script matching a certain naming convention, if multiple scripts are available at the same level (non-deterministic selection).

In this PR we make the choice deterministic, by using the "min" element in terms of sorting the scrips by name.

Below are some links to DCA runs, but they are only internally visible for GH employees.

  • DCA run for nightly/nightly which also auto-builds some projects appears uneventfull. https://github.com/github/codeql-dca-main/issues/41158
  • DCA run for aspnet/AspNetKatana, which shows that we now "pick" build.sh instead of cibuild.sh (which is randomly picked for the first variant). Unfortunately, for this specific project the build now fails due to selecting build.sh instead of cibuild.sh, but the choice of build script is now deterministic (note, that in other cases, we might be lucky that the selected build-script will now built the code). https://github.com/github/codeql-dca-main/issues/41225. The interesting bits of the output are

Note that the latter DCA execution had some tweaks to the CODEQL_VERBOSITY to get the console output from the autobuilder. I expect to make some follow up internal PRs to make this easier to reproduce.

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 783b3c1a777e36f605347747d735753161c7790f

Dimension Assessment Rationale
Change confidence 9/10 ↑ Rebased patch matches the previous net changes; C# and C++ autobuilder suites pass on .NET 11.
↓ No real-world build or CI result has been inspected.
Diagnosis confidence 9.5/10 ↑ The original code preserved filesystem enumeration order for equal-depth candidates; regression cases fail before the fix.
Evidence fit 4/10 ↑ The reported differing script choices are consistent with the identified ordering defect.
↓ The linked job logs are SAML-protected and could not be inspected.

The non-determinism showed itself during a QA run (It picked different scripts in the different runs) when the autobuilder attempted to build the code from https://github.com/aspnet/AspNetKatana.

Summary

  • Select the shallowest build script with OrderBy, ThenBy, and FirstOrDefault, breaking ties by ordinal path order while retaining existing candidate matching. .NET optimizes selection to a linear-time minimum scan without sorting.
  • Store candidates in a backing array and expose candidate paths through a get-only projection, in no guaranteed order.
  • Cover different file and directory enumeration orders, depth priority, Windows script candidates, and an empty candidate set. The shared rule is used by both C# and C++ autobuilders.
  • Add fix change notes under both languages; the C/C++ note also calls out the shared C# impact.

Validation

  • dotnet test csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/Semmle.Autobuild.CSharp.Tests.csproj (.NET 11): 41 passed.
  • dotnet test csharp/autobuilder/Semmle.Autobuild.Cpp.Tests/Semmle.Autobuild.Cpp.Tests.csproj (.NET 11): 2 passed.
  • The Bazel test attempt could not analyze the target because the local @semmle_code repository lacks git_info.

The reported example job logs could not be inspected because access requires SAML authorization: successful run and failed run.

@michaelnebel michaelnebel changed the title Make autobuild script selection deterministic C# and C/C++: Make autobuild script selection deterministic. Nov 27, 2026
@michaelnebel
michaelnebel requested an adaptive review from Copilot November 27, 2026 13:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the deterministic build-script selection change.

No open issues 🎉

What changed in this PR

This PR makes build-script selection deterministic in the shared C# and C/C++ autobuilder, preventing filesystem enumeration order from changing which script runs.

Changes:

  • Preserve shallowest-script priority and break ties using ordinal path order.
  • Store candidates in a backing array and expose their paths through a get-only projection.
  • Add regression tests for enumeration order, depth, Windows candidates, and no candidates, plus change notes for both languages.
File Description
csharp/​ql/​lib/​change-notes/​2026-11-27-deterministic-build-script.md Documents deterministic C# script selection.
csharp/​autobuilder/​Semmle.Autobuild.Shared/​BuildCommandAutoRule.cs Adds deterministic tie-breaking and array-backed candidate storage.
csharp/​autobuilder/​Semmle.Autobuild.CSharp.Tests/​BuildScripts.cs Tests ordering, depth priority, Windows candidates, and empty results.
cpp/​ql/​lib/​change-notes/​2026-11-27-deterministic-build-script.md Documents the shared C# and C/C++ autobuilder fix.

Review effort: Balanced (adaptive)

michaelnebel and others added 4 commits December 2, 2026 08:27
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@michaelnebel
michaelnebel force-pushed the michaelnebel-deterministic-csharp-build-script branch from 783b3c1 to 0aad9ca Compare December 2, 2026 07:29
@michaelnebel
michaelnebel marked this pull request as ready for review December 2, 2026 12:31
@michaelnebel
michaelnebel requested review from a team as code owners December 2, 2026 12:31
@michaelnebel
michaelnebel requested a review from jketema December 2, 2026 12:38
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@michaelnebel
michaelnebel requested a review from jketema December 2, 2026 13:17
jketema
jketema previously approved these changes Dec 2, 2026

@jketema jketema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure what the reason is for mentioning C# in the C++ change note, but LGTM.

@michaelnebel

Copy link
Copy Markdown
Contributor Author

I'm not exactly sure what the reason is for mentioning C# in the C++ change note, but LGTM.

Ah, you are right. Will fix.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@michaelnebel
michaelnebel merged commit 0bcd916 into github:main Dec 2, 2026
26 checks passed
@michaelnebel
michaelnebel deleted the michaelnebel-deterministic-csharp-build-script branch December 2, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants