Skip to content

Commit f5d7765

Browse files
authored
Merge pull request github#13952 from michaelnebel/csharp/runtimewhitespacefix
C#: .NET Runtime path detection (bugfix).
2 parents 75e6fd9 + 9e03a21 commit f5d7765

File tree

5 files changed

+96
-83
lines changed

5 files changed

+96
-83
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
6767
if (options.ScanNetFrameworkDlls)
6868
{
6969
var runtimeLocation = new Runtime(dotnet).GetRuntime(options.UseSelfContainedDotnet);
70-
progressMonitor.Log(Util.Logging.Severity.Debug, $"Runtime location selected: {runtimeLocation}");
70+
progressMonitor.Log(Util.Logging.Severity.Info, $"Runtime location selected: {runtimeLocation}");
7171
dllDirNames.Add(runtimeLocation);
7272
}
7373

csharp/extractor/Semmle.Extraction.CSharp.Standalone/DotNet.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public bool AddPackage(string folder, string package)
7878

7979
public IList<string> GetListedRuntimes()
8080
{
81-
var args = "--list-runtimes";
81+
const string args = "--list-runtimes";
82+
progressMonitor.RunningProcess($"{dotnet} {args}");
8283
var pi = new ProcessStartInfo(dotnet, args)
8384
{
8485
RedirectStandardOutput = true,
@@ -90,6 +91,7 @@ public IList<string> GetListedRuntimes()
9091
progressMonitor.CommandFailed(dotnet, args, exitCode);
9192
return new List<string>();
9293
}
94+
progressMonitor.LogInfo($"Found runtimes: {string.Join("\n", runtimes)}");
9395
return runtimes;
9496
}
9597
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/ProgressMonitor.cs

Lines changed: 60 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -12,121 +12,101 @@ public ProgressMonitor(ILogger logger)
1212
this.logger = logger;
1313
}
1414

15-
public void FindingFiles(string dir)
16-
{
17-
logger.Log(Severity.Info, "Finding files in {0}...", dir);
18-
}
15+
public void Log(Severity severity, string message) =>
16+
logger.Log(severity, message);
17+
18+
public void LogInfo(string message) =>
19+
logger.Log(Severity.Info, message);
20+
21+
private void LogDebug(string message) =>
22+
logger.Log(Severity.Debug, message);
23+
24+
private void LogError(string message) =>
25+
logger.Log(Severity.Error, message);
26+
27+
public void FindingFiles(string dir) =>
28+
LogInfo($"Finding files in {dir}...");
1929

2030
public void IndexingReferences(int count)
2131
{
22-
logger.Log(Severity.Info, "Indexing...");
23-
logger.Log(Severity.Debug, "Indexing {0} DLLs...", count);
32+
LogInfo("Indexing...");
33+
LogDebug($"Indexing {count} DLLs...");
2434
}
2535

2636
public void UnresolvedReference(string id, string project)
2737
{
28-
logger.Log(Severity.Info, "Unresolved reference {0}", id);
29-
logger.Log(Severity.Debug, "Unresolved {0} referenced by {1}", id, project);
38+
LogInfo($"Unresolved reference {id}");
39+
LogDebug($"Unresolved {id} referenced by {project}");
3040
}
3141

32-
public void AnalysingSolution(string filename)
33-
{
34-
logger.Log(Severity.Info, $"Analyzing {filename}...");
35-
}
42+
public void AnalysingSolution(string filename) =>
43+
LogInfo($"Analyzing {filename}...");
3644

37-
public void FailedProjectFile(string filename, string reason)
38-
{
39-
logger.Log(Severity.Info, "Couldn't read project file {0}: {1}", filename, reason);
40-
}
45+
public void FailedProjectFile(string filename, string reason) =>
46+
LogInfo($"Couldn't read project file {filename}: {reason}");
4147

4248
public void FailedNugetCommand(string exe, string args, string message)
4349
{
44-
logger.Log(Severity.Info, "Command failed: {0} {1}", exe, args);
45-
logger.Log(Severity.Info, " {0}", message);
50+
LogInfo($"Command failed: {exe} {args}");
51+
LogInfo($" {message}");
4652
}
4753

48-
public void NugetInstall(string package)
49-
{
50-
logger.Log(Severity.Info, "Restoring {0}...", package);
51-
}
54+
public void NugetInstall(string package) =>
55+
LogInfo($"Restoring {package}...");
5256

53-
public void ResolvedReference(string filename)
54-
{
55-
logger.Log(Severity.Info, "Resolved {0}", filename);
56-
}
57+
public void ResolvedReference(string filename) =>
58+
LogInfo($"Resolved {filename}");
5759

5860
public void Summary(int existingSources, int usedSources, int missingSources,
5961
int references, int unresolvedReferences,
6062
int resolvedConflicts, int totalProjects, int failedProjects,
6163
TimeSpan analysisTime)
6264
{
63-
logger.Log(Severity.Info, "");
64-
logger.Log(Severity.Info, "Build analysis summary:");
65-
logger.Log(Severity.Info, "{0, 6} source files in the filesystem", existingSources);
66-
logger.Log(Severity.Info, "{0, 6} source files in project files", usedSources);
67-
logger.Log(Severity.Info, "{0, 6} sources missing from project files", missingSources);
68-
logger.Log(Severity.Info, "{0, 6} resolved references", references);
69-
logger.Log(Severity.Info, "{0, 6} unresolved references", unresolvedReferences);
70-
logger.Log(Severity.Info, "{0, 6} resolved assembly conflicts", resolvedConflicts);
71-
logger.Log(Severity.Info, "{0, 6} projects", totalProjects);
72-
logger.Log(Severity.Info, "{0, 6} missing/failed projects", failedProjects);
73-
logger.Log(Severity.Info, "Build analysis completed in {0}", analysisTime);
65+
const int align = 6;
66+
LogInfo("");
67+
LogInfo("Build analysis summary:");
68+
LogInfo($"{existingSources,align} source files in the filesystem");
69+
LogInfo($"{usedSources,align} source files in project files");
70+
LogInfo($"{missingSources,align} sources missing from project files");
71+
LogInfo($"{references,align} resolved references");
72+
LogInfo($"{unresolvedReferences,align} unresolved references");
73+
LogInfo($"{resolvedConflicts,align} resolved assembly conflicts");
74+
LogInfo($"{totalProjects,align} projects");
75+
LogInfo($"{failedProjects,align} missing/failed projects");
76+
LogInfo($"Build analysis completed in {analysisTime}");
7477
}
7578

76-
public void Log(Severity severity, string message)
77-
{
78-
logger.Log(severity, message);
79-
}
79+
public void ResolvedConflict(string asm1, string asm2) =>
80+
LogDebug($"Resolved {asm1} as {asm2}");
8081

81-
public void ResolvedConflict(string asm1, string asm2)
82-
{
83-
logger.Log(Severity.Debug, "Resolved {0} as {1}", asm1, asm2);
84-
}
82+
public void MissingProject(string projectFile) =>
83+
LogInfo($"Solution is missing {projectFile}");
8584

86-
public void MissingProject(string projectFile)
87-
{
88-
logger.Log(Severity.Info, "Solution is missing {0}", projectFile);
89-
}
85+
public void CommandFailed(string exe, string arguments, int exitCode) =>
86+
LogError($"Command {exe} {arguments} failed with exit code {exitCode}");
9087

91-
public void CommandFailed(string exe, string arguments, int exitCode)
92-
{
93-
logger.Log(Severity.Error, $"Command {exe} {arguments} failed with exit code {exitCode}");
94-
}
88+
public void MissingNuGet() =>
89+
LogError("Missing nuget.exe");
9590

96-
public void MissingNuGet()
97-
{
98-
logger.Log(Severity.Error, "Missing nuget.exe");
99-
}
100-
101-
public void MissingDotNet()
102-
{
103-
logger.Log(Severity.Error, "Missing dotnet CLI");
104-
}
91+
public void MissingDotNet() =>
92+
LogError("Missing dotnet CLI");
10593

106-
public void RunningProcess(string command)
107-
{
108-
logger.Log(Severity.Info, $"Running {command}");
109-
}
94+
public void RunningProcess(string command) =>
95+
LogInfo($"Running {command}");
11096

111-
public void FailedToRestoreNugetPackage(string package)
112-
{
113-
logger.Log(Severity.Info, $"Failed to restore nuget package {package}");
114-
}
97+
public void FailedToRestoreNugetPackage(string package) =>
98+
LogInfo($"Failed to restore nuget package {package}");
11599

116100
public void FailedToReadFile(string file, Exception ex)
117101
{
118-
logger.Log(Severity.Info, $"Failed to read file {file}");
119-
logger.Log(Severity.Debug, $"Failed to read file {file}, exception: {ex}");
102+
LogInfo($"Failed to read file {file}");
103+
LogDebug($"Failed to read file {file}, exception: {ex}");
120104
}
121105

122-
public void MultipleNugetConfig(string[] nugetConfigs)
123-
{
124-
logger.Log(Severity.Info, $"Found multiple nuget.config files: {string.Join(", ", nugetConfigs)}.");
125-
}
106+
public void MultipleNugetConfig(string[] nugetConfigs) =>
107+
LogInfo($"Found multiple nuget.config files: {string.Join(", ", nugetConfigs)}.");
126108

127-
internal void NoTopLevelNugetConfig()
128-
{
129-
logger.Log(Severity.Info, $"Could not find a top-level nuget.config file.");
130-
}
109+
internal void NoTopLevelNugetConfig() =>
110+
LogInfo("Could not find a top-level nuget.config file.");
131111
}
132112
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Runtime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public int CompareTo(RuntimeVersion? other)
7474
public override string ToString() => FullPath;
7575
}
7676

77-
[GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)(-([a-z]+)\.(\d+\.\d+\.\d+))?\s\[(\S+)\]$")]
77+
[GeneratedRegex(@"^(\S+)\s(\d+\.\d+\.\d+)(-([a-z]+)\.(\d+\.\d+\.\d+))?\s\[(.+)\]$")]
7878
private static partial Regex RuntimeRegex();
7979

8080
/// <summary>

csharp/extractor/Semmle.Extraction.Tests/Runtime.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,36 @@ public void TestRuntime3()
100100
Assert.Equal("/path/dotnet/shared/Microsoft.NETCore.App/8.0.0-rc.4.43280.8", FixExpectedPathOnWindows(netCoreApp.FullPath));
101101
}
102102

103+
[Fact]
104+
public void TestRuntime4()
105+
{
106+
// Setup
107+
var listedRuntimes = new List<string>
108+
{
109+
@"Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]",
110+
@"Microsoft.AspNetCore.App 6.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]",
111+
@"Microsoft.AspNetCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]",
112+
@"Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]",
113+
@"Microsoft.NETCore.App 6.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]",
114+
@"Microsoft.NETCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]",
115+
@"Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]",
116+
@"Microsoft.WindowsDesktop.App 6.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]",
117+
@"Microsoft.WindowsDesktop.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]"
118+
};
119+
var dotnet = new DotNetStub(listedRuntimes);
120+
var runtime = new Runtime(dotnet);
121+
122+
// Execute
123+
var runtimes = runtime.GetNewestRuntimes();
124+
125+
// Verify
126+
Assert.Equal(3, runtimes.Count);
127+
128+
Assert.True(runtimes.TryGetValue("Microsoft.AspNetCore.App", out var aspNetCoreApp));
129+
Assert.Equal(@"C:/Program Files/dotnet/shared/Microsoft.AspNetCore.App/7.0.2", FixExpectedPathOnWindows(aspNetCoreApp.FullPath));
130+
131+
Assert.True(runtimes.TryGetValue("Microsoft.NETCore.App", out var netCoreApp));
132+
Assert.Equal(@"C:/Program Files/dotnet/shared/Microsoft.NETCore.App/7.0.2", FixExpectedPathOnWindows(netCoreApp.FullPath));
133+
}
103134
}
104135
}

0 commit comments

Comments
 (0)