Skip to content

Commit 1b5ed12

Browse files
committed
Log and emit diagnostic if incorrectly named files are found
1 parent 5ba3b67 commit 1b5ed12

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)
158158

159159
bool IBuildActions.IsMacOs() => IsMacOs;
160160

161+
public bool IsLinux { get; set; }
162+
163+
bool IBuildActions.IsLinux() => IsLinux;
164+
161165
public bool IsRunningOnAppleSilicon { get; set; }
162166

163167
bool IBuildActions.IsRunningOnAppleSilicon() => IsRunningOnAppleSilicon;

csharp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)
146146

147147
bool IBuildActions.IsMacOs() => IsMacOs;
148148

149+
public bool IsLinux { get; set; }
150+
151+
bool IBuildActions.IsLinux() => IsLinux;
152+
149153
public bool IsRunningOnAppleSilicon { get; set; }
150154

151155
bool IBuildActions.IsRunningOnAppleSilicon() => IsRunningOnAppleSilicon;

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,43 @@ private IEnumerable<string> GetFeeds(Func<IList<string>> getNugetFeeds)
814814
private (HashSet<string> explicitFeeds, HashSet<string> allFeeds) GetAllFeeds()
815815
{
816816
var nugetConfigs = fileProvider.NugetConfigs;
817+
818+
// On systems with case-sensitive file systems (for simplicity, we assume that is Linux), the
819+
// filenames of NuGet configuration files must be named correctly. For compatibility with projects
820+
// that are typically built on Windows or macOS where this doesn't matter, we accept all variants
821+
// of `nuget.config` ourselves. However, `dotnet` does not. If we detect that incorrectly-named
822+
// files are present, we emit a diagnostic to warn the user.
823+
if (SystemBuildActions.Instance.IsLinux())
824+
{
825+
string[] acceptedNugetConfigNames = ["nuget.config", "NuGet.config", "NuGet.Config"];
826+
var invalidNugetConfigs = nugetConfigs
827+
.Where(path => acceptedNugetConfigNames.Contains(Path.GetFileName(path)));
828+
829+
if (invalidNugetConfigs.Count() > 0)
830+
{
831+
this.logger.LogWarning(string.Format(
832+
"Found incorrectly named NuGet configuration files: {0}",
833+
string.Join(", ", invalidNugetConfigs)
834+
));
835+
this.diagnosticsWriter.AddEntry(new DiagnosticMessage(
836+
Language.CSharp,
837+
"buildless/case-sensitive-nuget-config",
838+
"Found NuGet configuration files which are not correctly named",
839+
visibility: new DiagnosticMessage.TspVisibility(statusPage: true, cliSummaryTable: true, telemetry: true),
840+
markdownMessage: string.Format(
841+
"On platforms with case-sensitive file systems, NuGet only accepts files with one of the following names: {0}.\n\n" +
842+
"CodeQL found the following files while performing an analysis on a platform with a case-sensitive file system:\n\n" +
843+
"{1}\n\n" +
844+
"To avoid unexpected results, rename these files to match the casing of one of the accepted filenames.",
845+
string.Join(", ", acceptedNugetConfigNames),
846+
string.Join("\n", invalidNugetConfigs.Select(path => string.Format("- `{0}`", path)))
847+
),
848+
severity: DiagnosticMessage.TspSeverity.Warning
849+
));
850+
}
851+
}
852+
853+
// Find feeds that are explicitly configured in the NuGet configuration files that we found.
817854
var explicitFeeds = nugetConfigs
818855
.SelectMany(config => GetFeeds(() => dotnet.GetNugetFeeds(config)))
819856
.ToHashSet();

csharp/extractor/Semmle.Util/BuildActions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ public interface IBuildActions
119119
/// <returns>True if we are running on macOS.</returns>
120120
bool IsMacOs();
121121

122+
/// <summary>
123+
/// Gets a value indicating whether we are running on Linux.
124+
/// </summary>
125+
/// <returns>True if we are running on Linux.</returns>
126+
bool IsLinux();
127+
122128
/// <summary>
123129
/// Gets a value indicating whether we are running on Apple Silicon.
124130
/// </summary>
@@ -246,6 +252,8 @@ int IBuildActions.RunProcess(string cmd, string args, string? workingDirectory,
246252

247253
bool IBuildActions.IsMacOs() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
248254

255+
bool IBuildActions.IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
256+
249257
bool IBuildActions.IsRunningOnAppleSilicon()
250258
{
251259
var thisBuildActions = (IBuildActions)this;

0 commit comments

Comments
 (0)