Skip to content

Commit a7f8358

Browse files
Copilotbcollamore
andcommitted
feat: enhance LicenseAnalyzer diagnostics with license output and conditional search logging
- Add license information output for each package reference found - Show detailed search diagnostics only when project.assets.json cannot be found - When file is found, only show location without verbose search details - Reduces diagnostic clutter while maintaining troubleshooting capabilities Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>
1 parent f48bc7b commit a7f8358

1 file changed

Lines changed: 49 additions & 17 deletions

File tree

Philips.CodeAnalysis.SecurityAnalyzers/LicenseAnalyzer.cs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ private void AnalyzePackagesFromAssetsFile(CompilationAnalysisContext context, H
167167
return;
168168
}
169169

170+
// Report where we found the file (but not the detailed search diagnostics when successful)
171+
ReportDebugDiagnostic(context, $"Found {ProjectAssetsFileName} at: {assetsFilePath}");
172+
170173
// Load and parse project.assets.json manually to avoid external dependencies
171174
List<PackageInfo> packages;
172175
try
@@ -200,6 +203,10 @@ private void AnalyzePackagesFromAssetsFile(CompilationAnalysisContext context, H
200203
// Get license information for this package
201204
var licenseInfo = GetPackageLicenseInfo(package, licenseCache);
202205

206+
// Report the license that was found for each reference (for diagnostic purposes)
207+
var displayLicense = string.IsNullOrEmpty(licenseInfo) ? "unknown" : licenseInfo;
208+
ReportDebugDiagnostic(context, $"Package {package.Name} {package.Version ?? "unknown"}: License = {displayLicense}");
209+
203210
// Check if license is acceptable
204211
if (!string.IsNullOrEmpty(licenseInfo) && !IsLicenseAcceptable(licenseInfo, allowedLicenses))
205212
{
@@ -230,37 +237,44 @@ private static string TryFindAssetsFileFromSourcePaths(CompilationAnalysisContex
230237
.Distinct(StringComparer.OrdinalIgnoreCase)
231238
.ToList();
232239

233-
ReportDebugDiagnostic(context, $"Found {sourceDirectories.Count} unique source directories from compilation");
234-
235240
// Search in each source directory and its parent directories
236241
foreach (var sourceDir in sourceDirectories)
237242
{
238-
ReportDebugDiagnostic(context, $"Searching from source directory: {sourceDir}");
239-
240-
var foundPath = SearchForAssetsFile(context, sourceDir);
243+
var foundPath = SearchForAssetsFile(context, sourceDir, enableDetailedLogging: false);
241244
if (foundPath != null)
242245
{
243-
ReportDebugDiagnostic(context, $"Found {ProjectAssetsFileName} at: {foundPath}");
244246
return foundPath;
245247
}
246248
}
247249

248250
// Fallback to current directory search if source paths didn't work
249251
var currentDir = Directory.GetCurrentDirectory();
250-
ReportDebugDiagnostic(context, $"Source directory search failed, falling back to current directory: {currentDir}");
251-
252-
var fallbackPath = SearchForAssetsFile(context, currentDir);
252+
var fallbackPath = SearchForAssetsFile(context, currentDir, enableDetailedLogging: false);
253253
if (fallbackPath != null)
254254
{
255-
ReportDebugDiagnostic(context, $"Found {ProjectAssetsFileName} via fallback at: {fallbackPath}");
256255
return fallbackPath;
257256
}
258257

258+
// File not found - now show detailed diagnostics to help troubleshoot
259+
ReportDebugDiagnostic(context, $"Could not find {ProjectAssetsFileName}. Starting detailed search diagnostics...");
260+
ReportDebugDiagnostic(context, $"Found {sourceDirectories.Count} unique source directories from compilation");
261+
262+
// Re-search with detailed logging enabled
263+
foreach (var sourceDir in sourceDirectories)
264+
{
265+
ReportDebugDiagnostic(context, $"Searching from source directory: {sourceDir}");
266+
_ = SearchForAssetsFile(context, sourceDir, enableDetailedLogging: true);
267+
}
268+
269+
// Fallback search with detailed logging
270+
ReportDebugDiagnostic(context, $"Source directory search failed, falling back to current directory: {currentDir}");
271+
_ = SearchForAssetsFile(context, currentDir, enableDetailedLogging: true);
272+
259273
ReportDebugDiagnostic(context, $"Exhaustive search completed, {ProjectAssetsFileName} not found");
260274
return null;
261275
}
262276

263-
private static string SearchForAssetsFile(CompilationAnalysisContext context, string startDirectory)
277+
private static string SearchForAssetsFile(CompilationAnalysisContext context, string startDirectory, bool enableDetailedLogging)
264278
{
265279
// Look for project.assets.json in common locations relative to the start directory
266280
var possiblePaths = new[]
@@ -276,7 +290,10 @@ private static string SearchForAssetsFile(CompilationAnalysisContext context, st
276290
try
277291
{
278292
var exists = File.Exists(path);
279-
ReportDebugDiagnostic(context, $"Checking path: {path} - Exists: {exists}");
293+
if (enableDetailedLogging)
294+
{
295+
ReportDebugDiagnostic(context, $"Checking path: {path} - Exists: {exists}");
296+
}
280297

281298
if (exists)
282299
{
@@ -285,7 +302,10 @@ private static string SearchForAssetsFile(CompilationAnalysisContext context, st
285302
}
286303
catch (Exception ex)
287304
{
288-
ReportDebugDiagnostic(context, $"Exception checking path {path}: {ex.Message}");
305+
if (enableDetailedLogging)
306+
{
307+
ReportDebugDiagnostic(context, $"Exception checking path {path}: {ex.Message}");
308+
}
289309
}
290310
}
291311

@@ -300,7 +320,10 @@ private static string SearchForAssetsFile(CompilationAnalysisContext context, st
300320
try
301321
{
302322
var exists = File.Exists(assetsPath);
303-
ReportDebugDiagnostic(context, $"Tree search depth {i}: checking {assetsPath} - Exists: {exists}");
323+
if (enableDetailedLogging)
324+
{
325+
ReportDebugDiagnostic(context, $"Tree search depth {i}: checking {assetsPath} - Exists: {exists}");
326+
}
304327

305328
if (exists)
306329
{
@@ -309,13 +332,19 @@ private static string SearchForAssetsFile(CompilationAnalysisContext context, st
309332
}
310333
catch (Exception ex)
311334
{
312-
ReportDebugDiagnostic(context, $"Exception during tree search at {assetsPath}: {ex.Message}");
335+
if (enableDetailedLogging)
336+
{
337+
ReportDebugDiagnostic(context, $"Exception during tree search at {assetsPath}: {ex.Message}");
338+
}
313339
}
314340

315341
DirectoryInfo parentDir = Directory.GetParent(searchDir);
316342
if (parentDir == null)
317343
{
318-
ReportDebugDiagnostic(context, $"Reached root directory at depth {i}, stopping search");
344+
if (enableDetailedLogging)
345+
{
346+
ReportDebugDiagnostic(context, $"Reached root directory at depth {i}, stopping search");
347+
}
319348
break;
320349
}
321350

@@ -324,7 +353,10 @@ private static string SearchForAssetsFile(CompilationAnalysisContext context, st
324353
}
325354
catch (Exception ex)
326355
{
327-
ReportDebugDiagnostic(context, $"Exception during directory tree search: {ex.Message}");
356+
if (enableDetailedLogging)
357+
{
358+
ReportDebugDiagnostic(context, $"Exception during directory tree search: {ex.Message}");
359+
}
328360
}
329361

330362
return null;

0 commit comments

Comments
 (0)