Skip to content

Commit aa0a62f

Browse files
Copilotbcollamore
andcommitted
feat: Add comprehensive debugging diagnostics for project.assets.json file discovery in LicenseAnalyzer
Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>
1 parent 8b8000c commit aa0a62f

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

Philips.CodeAnalysis.SecurityAnalyzers/LicenseAnalyzer.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private static bool IsTestProject(Compilation compilation)
126126
private void AnalyzePackagesFromAssetsFile(CompilationAnalysisContext context, HashSet<string> allowedLicenses)
127127
{
128128
// Get project.assets.json path from analyzer config options
129-
var assetsFilePath = TryFindAssetsFileFromSourcePaths();
129+
var assetsFilePath = TryFindAssetsFileFromSourcePaths(context);
130130
if (string.IsNullOrEmpty(assetsFilePath) || !File.Exists(assetsFilePath))
131131
{
132132
// Report diagnostic that project.assets.json was not found
@@ -187,12 +187,16 @@ private void AnalyzePackagesFromAssetsFile(CompilationAnalysisContext context, H
187187
SaveLicenseCache(licenseCache, Path.GetDirectoryName(assetsFilePath));
188188
}
189189

190-
private static string TryFindAssetsFileFromSourcePaths()
190+
private static string TryFindAssetsFileFromSourcePaths(CompilationAnalysisContext context)
191191
{
192192
// For .NET Standard 2.0 compatibility, use alternative approaches to find project.assets.json
193193
// Try common locations relative to the current working directory
194194
var currentDir = Directory.GetCurrentDirectory();
195195

196+
// Report debug information about search starting point
197+
var diagnostic = Diagnostic.Create(InfoDiagnostic, Location.None, $"Searching for {ProjectAssetsFileName} starting from directory: {currentDir}");
198+
context.ReportDiagnostic(diagnostic);
199+
196200
// Look for project.assets.json in common locations
197201
var possiblePaths = new[]
198202
{
@@ -201,38 +205,64 @@ private static string TryFindAssetsFileFromSourcePaths()
201205
Path.Combine(currentDir, "..", "..", "obj", ProjectAssetsFileName)
202206
};
203207

208+
// Report each path being checked
209+
foreach (var path in possiblePaths)
210+
{
211+
var exists = File.Exists(path);
212+
var diagnostic2 = Diagnostic.Create(InfoDiagnostic, Location.None, $"Checking path: {path} - Exists: {exists}");
213+
context.ReportDiagnostic(diagnostic2);
214+
}
215+
204216
var foundPath = possiblePaths.Where(File.Exists).FirstOrDefault();
205217
if (foundPath != null)
206218
{
219+
var diagnostic3 = Diagnostic.Create(InfoDiagnostic, Location.None, $"Found {ProjectAssetsFileName} at: {foundPath}");
220+
context.ReportDiagnostic(diagnostic3);
207221
return foundPath;
208222
}
209223

210224
// If not found in common locations, search in current directory tree
211225
try
212226
{
227+
var diagnostic4 = Diagnostic.Create(InfoDiagnostic, Location.None, "Common paths failed, searching directory tree...");
228+
context.ReportDiagnostic(diagnostic4);
229+
213230
var searchDir = currentDir;
214231
for (var i = 0; i < 5; i++) // Limit search depth
215232
{
216233
var assetsPath = Path.Combine(searchDir, "obj", ProjectAssetsFileName);
217-
if (File.Exists(assetsPath))
234+
var exists = File.Exists(assetsPath);
235+
236+
var diagnostic5 = Diagnostic.Create(InfoDiagnostic, Location.None, $"Tree search depth {i}: checking {assetsPath} - Exists: {exists}");
237+
context.ReportDiagnostic(diagnostic5);
238+
239+
if (exists)
218240
{
241+
var diagnostic6 = Diagnostic.Create(InfoDiagnostic, Location.None, $"Found {ProjectAssetsFileName} via tree search at: {assetsPath}");
242+
context.ReportDiagnostic(diagnostic6);
219243
return assetsPath;
220244
}
221245

222246
DirectoryInfo parentDir = Directory.GetParent(searchDir);
223247
if (parentDir == null)
224248
{
249+
var diagnostic7 = Diagnostic.Create(InfoDiagnostic, Location.None, $"Reached root directory at depth {i}, stopping search");
250+
context.ReportDiagnostic(diagnostic7);
225251
break;
226252
}
227253
searchDir = parentDir.FullName;
228254
}
229255
}
230-
catch (Exception)
256+
catch (Exception ex)
231257
{
232-
// If directory traversal fails, return null
258+
// Report diagnostic about directory traversal failure
259+
var diagnostic8 = Diagnostic.Create(InfoDiagnostic, Location.None, $"Directory traversal failed: {ex.Message}");
260+
context.ReportDiagnostic(diagnostic8);
233261
return null;
234262
}
235263

264+
var diagnostic9 = Diagnostic.Create(InfoDiagnostic, Location.None, $"{ProjectAssetsFileName} not found after exhaustive search");
265+
context.ReportDiagnostic(diagnostic9);
236266
return null;
237267
}
238268

0 commit comments

Comments
 (0)