Fix new (nullability) compiler warnings

This commit is contained in:
Tamas Vajk
2021-02-10 10:40:57 +01:00
parent 71f095d6d4
commit 4f383be13b
9 changed files with 74 additions and 22 deletions

View File

@@ -88,6 +88,11 @@ namespace Semmle.BuildAnalyser
var projDir = fileName.Directory;
var root = projFile.DocumentElement;
if (root is null)
{
throw new NotSupportedException("Project file without root is not supported.");
}
// Figure out if it's dotnet core
var netCoreProjectFile = root.GetAttribute("Sdk") == "Microsoft.NET.Sdk";
@@ -96,34 +101,52 @@ namespace Semmle.BuildAnalyser
{
var explicitCsFiles = root
.SelectNodes("/Project/ItemGroup/Compile/@Include", mgr)
.NodeList()
?.NodeList()
.Select(node => node.Value)
.Select(cs => Path.DirectorySeparatorChar == '/' ? cs.Replace("\\", "/") : cs)
.Select(f => Path.GetFullPath(Path.Combine(projDir.FullName, f)));
.Select(cs => GetFullPath(cs, projDir))
.Where(s => s is not null)
?? Enumerable.Empty<string>();
var additionalCsFiles = System.IO.Directory.GetFiles(directoryName, "*.cs", SearchOption.AllDirectories);
#nullable disable warnings
return (explicitCsFiles.Concat(additionalCsFiles).ToArray(), Array.Empty<string>());
#nullable restore warnings
}
var references = root
.SelectNodes("/msbuild:Project/msbuild:ItemGroup/msbuild:Reference/@Include", mgr)
.NodeList()
?.NodeList()
.Select(node => node.Value)
.ToArray();
.Where(s => s is not null)
.ToArray()
?? Array.Empty<string>();
var relativeCsIncludes = root
.SelectNodes("/msbuild:Project/msbuild:ItemGroup/msbuild:Compile/@Include", mgr)
.NodeList()
?.NodeList()
.Select(node => node.Value)
.ToArray();
.ToArray()
?? Array.Empty<string>();
var csFiles = relativeCsIncludes
.Select(cs => Path.DirectorySeparatorChar == '/' ? cs.Replace("\\", "/") : cs)
.Select(f => Path.GetFullPath(Path.Combine(projDir.FullName, f)))
.Select(cs => GetFullPath(cs, projDir))
.Where(s => s is not null)
.ToArray();
#nullable disable warnings
return (csFiles, references);
#nullable restore warnings
}
private static string? GetFullPath(string? file, DirectoryInfo? projDir)
{
if (file is null)
{
return null;
}
return Path.GetFullPath(Path.Combine(projDir?.FullName ?? string.Empty, Path.DirectorySeparatorChar == '/' ? file.Replace("\\", "/") : file));
}
private readonly string[] references;

View File

@@ -113,6 +113,12 @@ namespace Semmle.BuildAnalyser
{
using var p = Process.Start(pi);
if (p is null)
{
pm.FailedNugetCommand(pi.FileName, pi.Arguments, "Couldn't start process.");
return;
}
var output = p.StandardOutput.ReadToEnd();
var error = p.StandardError.ReadToEnd();