22using System . Collections . Generic ;
33using System . IO ;
44using System . Linq ;
5- using System . Runtime . InteropServices ;
6- using Semmle . Util ;
75using Semmle . Extraction . CSharp . Standalone ;
86using System . Threading . Tasks ;
9- using System . Collections . Concurrent ;
107
118namespace Semmle . BuildAnalyser
129{
@@ -47,18 +44,18 @@ interface IBuildAnalysis
4744 /// </summary>
4845 class BuildAnalysis : IBuildAnalysis
4946 {
50- readonly AssemblyCache assemblyCache ;
51- readonly NugetPackages nuget ;
52- readonly IProgressMonitor progressMonitor ;
53- HashSet < string > usedReferences = new HashSet < string > ( ) ;
54- readonly HashSet < string > usedSources = new HashSet < string > ( ) ;
55- readonly HashSet < string > missingSources = new HashSet < string > ( ) ;
56- readonly Dictionary < string , string > unresolvedReferences = new Dictionary < string , string > ( ) ;
57- readonly DirectoryInfo sourceDir ;
58- int failedProjects , succeededProjects ;
59- readonly string [ ] allSources ;
60- int conflictedReferences = 0 ;
61- object mutex = new object ( ) ;
47+ private readonly AssemblyCache assemblyCache ;
48+ private readonly NugetPackages nuget ;
49+ private readonly IProgressMonitor progressMonitor ;
50+ private HashSet < string > usedReferences = new HashSet < string > ( ) ;
51+ private readonly HashSet < string > usedSources = new HashSet < string > ( ) ;
52+ private readonly HashSet < string > missingSources = new HashSet < string > ( ) ;
53+ private readonly Dictionary < string , string > unresolvedReferences = new Dictionary < string , string > ( ) ;
54+ private readonly DirectoryInfo sourceDir ;
55+ private int failedProjects , succeededProjects ;
56+ private readonly string [ ] allSources ;
57+ private int conflictedReferences = 0 ;
58+ private readonly object mutex = new object ( ) ;
6259
6360 /// <summary>
6461 /// Performs a C# build analysis.
@@ -80,7 +77,7 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
8077 ToArray ( ) ;
8178
8279 var dllDirNames = options . DllDirs . Select ( Path . GetFullPath ) . ToList ( ) ;
83- PackageDirectory = TemporaryDirectory . CreateTempDirectory ( sourceDir . FullName , progressMonitor ) ;
80+ PackageDirectory = TemporaryDirectory . CreateTempDirectory ( sourceDir . FullName ) ;
8481
8582 if ( options . UseNuGet )
8683 {
@@ -102,6 +99,7 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
10299 }
103100
104101 {
102+ // These files can sometimes prevent `dotnet restore` from working correctly.
105103 using var renamer1 = new FileRenamer ( sourceDir . GetFiles ( "global.json" , SearchOption . AllDirectories ) ) ;
106104 using var renamer2 = new FileRenamer ( sourceDir . GetFiles ( "Directory.Build.props" , SearchOption . AllDirectories ) ) ;
107105
@@ -118,12 +116,6 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
118116 usedReferences = new HashSet < string > ( assemblyCache . AllAssemblies . Select ( a => a . Filename ) ) ;
119117 }
120118
121- if ( ! options . AnalyseCsProjFiles )
122- {
123- usedReferences = new HashSet < string > ( assemblyCache . AllAssemblies . Select ( a => a . Filename ) ) ;
124- }
125-
126-
127119 ResolveConflicts ( ) ;
128120
129121 if ( options . UseMscorlib )
@@ -150,9 +142,8 @@ public BuildAnalysis(Options options, IProgressMonitor progress)
150142 UnresolvedReferences . Count ( ) ,
151143 conflictedReferences ,
152144 succeededProjects + failedProjects ,
153- failedProjects ) ;
154-
155- Console . WriteLine ( $ "Build analysis completed in { DateTime . Now - startTime } ") ;
145+ failedProjects ,
146+ DateTime . Now - startTime ) ;
156147 }
157148
158149 /// <summary>
@@ -285,7 +276,7 @@ void AnalyseProject(FileInfo project)
285276
286277 try
287278 {
288- var csProj = new CsProjFile ( project ) ;
279+ IProjectFile csProj = new CsProjFile ( project ) ;
289280
290281 foreach ( var @ref in csProj . References )
291282 {
@@ -329,8 +320,16 @@ public void Cleanup()
329320 void Restore ( string projectOrSolution )
330321 {
331322 int exit = DotNet . RestoreToDirectory ( projectOrSolution , PackageDirectory . DirInfo . FullName ) ;
332- if ( exit != 0 )
333- progressMonitor . CommandFailed ( "dotnet" , $ "restore \" { projectOrSolution } \" ", exit ) ;
323+ switch ( exit )
324+ {
325+ case 0 :
326+ case 1 :
327+ // No errors
328+ break ;
329+ default :
330+ progressMonitor . CommandFailed ( "dotnet" , $ "restore \" { projectOrSolution } \" ", exit ) ;
331+ break ;
332+ }
334333 }
335334
336335 public void RestoreSolutions ( IEnumerable < string > solutions )
0 commit comments