From 59496c4f723d71e7816c807b976b6599f0f9997f Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Fri, 27 Nov 2026 10:45:47 +0100 Subject: [PATCH 1/4] Make autobuild script selection deterministic Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../BuildScripts.cs | 64 +++++++++++++++++++ .../BuildCommandAutoRule.cs | 11 +++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs b/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs index d0b06753734f..b00768f3be10 100644 --- a/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs +++ b/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs @@ -648,6 +648,70 @@ public void TestLinuxBuildSh() TestAutobuilderScript(autobuilder, 0, 2); } + [Theory] + [InlineData("cibuild.sh\nmsbuild.sh\nbuild.sh")] + [InlineData("build.sh\nmsbuild.sh\ncibuild.sh")] + public void TestLinuxBuildScriptSelectionIndependentOfFileOrder(string files) + { + actions.EnumerateFiles[@"C:\Project"] = files; + actions.EnumerateDirectories[@"C:\Project"] = ""; + actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SCRATCH_DIR"] = "scratch"; + actions.RunProcess[@"/bin/chmod u+x C:\Project/build.sh"] = 0; + actions.RunProcess[@"C:\Project/build.sh"] = 0; + actions.RunProcessWorkingDirectory[@"C:\Project/build.sh"] = @"C:\Project"; + actions.FileExists["csharp.log"] = true; + + var autobuilder = CreateAutoBuilder(false); + TestAutobuilderScript(autobuilder, 0, 2); + } + + [Theory] + [InlineData("z\na")] + [InlineData("a\nz")] + public void TestBuildScriptSelectionUsesDepthThenPath(string directories) + { + actions.EnumerateFiles[@"C:\Project"] = "foo.cs"; + actions.EnumerateDirectories[@"C:\Project"] = directories; + actions.EnumerateFiles[@"C:\Project/a"] = "zbuild.sh"; + actions.EnumerateDirectories[@"C:\Project/a"] = "a"; + actions.EnumerateFiles[@"C:\Project/a/a"] = "build.sh"; + actions.EnumerateDirectories[@"C:\Project/a/a"] = ""; + actions.EnumerateFiles[@"C:\Project/z"] = "abuild.sh"; + actions.EnumerateDirectories[@"C:\Project/z"] = ""; + + var autobuilder = CreateAutoBuilder(false); + var rule = new BuildCommandAutoRule((_, build) => build(null)); + rule.Analyse(autobuilder, true); + + Assert.Equal(@"C:\Project/a/zbuild.sh", rule.ScriptPath); + Assert.Equal(new[] + { + @"C:\Project/a/zbuild.sh", + @"C:\Project/z/abuild.sh", + @"C:\Project/a/a/build.sh" + }, rule.CandidatePaths); + } + + [Theory] + [InlineData("build.bat\nBUILD.cmd\nbuild.sh")] + [InlineData("BUILD.cmd\nbuild.bat\nbuild.sh")] + public void TestWindowsBuildScriptSelectionUsesOrdinalPathOrder(string files) + { + actions.EnumerateFiles[@"C:\Project"] = files; + actions.EnumerateDirectories[@"C:\Project"] = ""; + + var autobuilder = CreateAutoBuilder(true); + var rule = new BuildCommandAutoRule((_, build) => build(null)); + rule.Analyse(autobuilder, true); + + Assert.Equal(@"C:\Project\BUILD.cmd", rule.ScriptPath); + Assert.Equal(new[] + { + @"C:\Project\BUILD.cmd", + @"C:\Project\build.bat" + }, rule.CandidatePaths); + } + [Fact] public void TestLinuxBuildShCSharpLogMissing() { diff --git a/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs b/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs index e12652767d15..7c990283d591 100644 --- a/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs +++ b/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using Semmle.Util; @@ -63,8 +64,12 @@ public BuildScript Analyse(IAutobuilder builder, bool au // e.g. for Linux: build, build.sh var scripts = buildScripts.SelectMany(s => extensions.Select(e => s + e)); // search through the files in the project directory for paths which end in one of - // the names given by `scripts`, then order them by their distance from the root - this.CandidatePaths = builder.Paths.Where(p => scripts.Any(p.Item1.ToLower().EndsWith)).OrderBy(p => p.Item2).Select(p => p.Item1); + // the names given by `scripts`, then order them by depth and path + this.CandidatePaths = builder.Paths + .Where(p => scripts.Any(p.Item1.ToLower().EndsWith)) + .OrderBy(p => p.Item2) + .ThenBy(p => p.Item1, StringComparer.Ordinal) + .Select(p => p.Item1); // pick the first matching path, if there is one this.ScriptPath = this.CandidatePaths.FirstOrDefault(); From 551d4c5c6fb10c6fb186c6b225fbf9d7a68da8d1 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Fri, 27 Nov 2026 11:08:55 +0100 Subject: [PATCH 2/4] Select build script with a linear minimum Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../2026-11-27-deterministic-build-script.md | 4 ++++ .../BuildScripts.cs | 19 ++++++++++++++++-- .../BuildCommandAutoRule.cs | 20 +++++++++++-------- .../2026-11-27-deterministic-build-script.md | 4 ++++ 4 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 cpp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md create mode 100644 csharp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md diff --git a/cpp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md b/cpp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md new file mode 100644 index 000000000000..b56d049b15d4 --- /dev/null +++ b/cpp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md @@ -0,0 +1,4 @@ +--- +category: fix +--- +* The C/C++ autobuilder now consistently selects the same build script when multiple candidates are at the same depth. diff --git a/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs b/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs index b00768f3be10..193eea2e1175 100644 --- a/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs +++ b/csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs @@ -689,7 +689,7 @@ public void TestBuildScriptSelectionUsesDepthThenPath(string directories) @"C:\Project/a/zbuild.sh", @"C:\Project/z/abuild.sh", @"C:\Project/a/a/build.sh" - }, rule.CandidatePaths); + }.OrderBy(p => p, StringComparer.Ordinal), rule.CandidatePaths.OrderBy(p => p, StringComparer.Ordinal)); } [Theory] @@ -709,7 +709,22 @@ public void TestWindowsBuildScriptSelectionUsesOrdinalPathOrder(string files) { @"C:\Project\BUILD.cmd", @"C:\Project\build.bat" - }, rule.CandidatePaths); + }.OrderBy(p => p, StringComparer.Ordinal), rule.CandidatePaths.OrderBy(p => p, StringComparer.Ordinal)); + } + + [Fact] + public void TestBuildScriptSelectionWithNoCandidates() + { + actions.EnumerateFiles[@"C:\Project"] = "foo.cs"; + actions.EnumerateDirectories[@"C:\Project"] = ""; + + var autobuilder = CreateAutoBuilder(false); + var rule = new BuildCommandAutoRule((_, build) => build(null)); + var script = rule.Analyse(autobuilder, true); + + Assert.Null(rule.ScriptPath); + Assert.Empty(rule.CandidatePaths); + Assert.NotEqual(0, script.Run(actions, StartCallback, EndCallback)); } [Fact] diff --git a/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs b/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs index 7c990283d591..8c3d06f0a57a 100644 --- a/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs +++ b/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs @@ -15,7 +15,7 @@ public class BuildCommandAutoRule : IBuildRule private readonly WithDotNet withDotNet; /// - /// A list of paths to files in the project directory which we classified as scripts. + /// Paths to files in the project directory which we classified as scripts, in no guaranteed order. /// public IEnumerable CandidatePaths { get; private set; } @@ -64,14 +64,18 @@ public BuildScript Analyse(IAutobuilder builder, bool au // e.g. for Linux: build, build.sh var scripts = buildScripts.SelectMany(s => extensions.Select(e => s + e)); // search through the files in the project directory for paths which end in one of - // the names given by `scripts`, then order them by depth and path - this.CandidatePaths = builder.Paths + // the names given by `scripts` + var candidates = builder.Paths .Where(p => scripts.Any(p.Item1.ToLower().EndsWith)) - .OrderBy(p => p.Item2) - .ThenBy(p => p.Item1, StringComparer.Ordinal) - .Select(p => p.Item1); - // pick the first matching path, if there is one - this.ScriptPath = this.CandidatePaths.FirstOrDefault(); + .ToArray(); + this.CandidatePaths = candidates.Select(p => p.Item1); + + var comparer = Comparer<(string path, int depth)>.Create((left, right) => + { + var depthComparison = left.depth.CompareTo(right.depth); + return depthComparison != 0 ? depthComparison : StringComparer.Ordinal.Compare(left.path, right.path); + }); + this.ScriptPath = candidates.Length == 0 ? null : candidates.MinBy(p => p, comparer).Item1; if (this.ScriptPath is null) return BuildScript.Failure; diff --git a/csharp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md b/csharp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md new file mode 100644 index 000000000000..c0affe50aec1 --- /dev/null +++ b/csharp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md @@ -0,0 +1,4 @@ +--- +category: fix +--- +* The C# autobuilder now consistently selects the same build script when multiple candidates are at the same depth. From 56a5df0a27a68042f4567cb05b95ad8dfb4afeec Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Fri, 27 Nov 2026 11:17:07 +0100 Subject: [PATCH 3/4] Shared: Mention C# in autobuilder change note Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../lib/change-notes/2026-11-27-deterministic-build-script.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md b/cpp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md index b56d049b15d4..25ab7ca31619 100644 --- a/cpp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md +++ b/cpp/ql/lib/change-notes/2026-11-27-deterministic-build-script.md @@ -1,4 +1,4 @@ --- category: fix --- -* The C/C++ autobuilder now consistently selects the same build script when multiple candidates are at the same depth. +* The C# and C/C++ autobuilders now consistently select the same build script when multiple candidates are at the same depth. From 5f16783f73dd714209747a538cff208b85215817 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Fri, 27 Nov 2026 11:27:02 +0100 Subject: [PATCH 4/4] Shared: Simplify autobuild script selection Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../BuildCommandAutoRule.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs b/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs index 8c3d06f0a57a..3f75a1a520ae 100644 --- a/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs +++ b/csharp/autobuilder/Semmle.Autobuild.Shared/BuildCommandAutoRule.cs @@ -13,11 +13,12 @@ namespace Semmle.Autobuild.Shared public class BuildCommandAutoRule : IBuildRule { private readonly WithDotNet withDotNet; + private (string path, int depth)[] candidates = Array.Empty<(string, int)>(); /// /// Paths to files in the project directory which we classified as scripts, in no guaranteed order. /// - public IEnumerable CandidatePaths { get; private set; } + public IEnumerable CandidatePaths => this.candidates.Select(p => p.path); /// /// The path of the script we decided to run, if any. @@ -27,7 +28,6 @@ public class BuildCommandAutoRule : IBuildRule public BuildCommandAutoRule(WithDotNet withDotNet) { this.withDotNet = withDotNet; - this.CandidatePaths = new List(); } /// @@ -65,17 +65,16 @@ public BuildScript Analyse(IAutobuilder builder, bool au var scripts = buildScripts.SelectMany(s => extensions.Select(e => s + e)); // search through the files in the project directory for paths which end in one of // the names given by `scripts` - var candidates = builder.Paths + this.candidates = builder.Paths .Where(p => scripts.Any(p.Item1.ToLower().EndsWith)) .ToArray(); - this.CandidatePaths = candidates.Select(p => p.Item1); - var comparer = Comparer<(string path, int depth)>.Create((left, right) => - { - var depthComparison = left.depth.CompareTo(right.depth); - return depthComparison != 0 ? depthComparison : StringComparer.Ordinal.Compare(left.path, right.path); - }); - this.ScriptPath = candidates.Length == 0 ? null : candidates.MinBy(p => p, comparer).Item1; + // .NET optimizes FirstOrDefault on an ordered sequence to a linear-time minimum scan without sorting. + this.ScriptPath = this.candidates + .OrderBy(p => p.depth) + .ThenBy(p => p.path, StringComparer.Ordinal) + .Select(p => p.path) + .FirstOrDefault(); if (this.ScriptPath is null) return BuildScript.Failure;