Skip to content

Commit d3e047f

Browse files
authored
Merge pull request github#14834 from michaelnebel/csharp/robustassetsfileread
C#: Make assets file reading more robust.
2 parents db180d9 + 7531852 commit d3e047f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Assets.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,31 @@ public bool TryParse(string json, DependencyContainer dependencies)
143143
}
144144
}
145145

146+
private static bool TryReadAllText(string path, ProgressMonitor progressMonitor, out string content)
147+
{
148+
try
149+
{
150+
content = File.ReadAllText(path);
151+
return true;
152+
}
153+
catch (Exception e)
154+
{
155+
progressMonitor.LogInfo($"Failed to read assets file '{path}': {e.Message}");
156+
content = "";
157+
return false;
158+
}
159+
}
160+
146161
public static DependencyContainer GetCompilationDependencies(ProgressMonitor progressMonitor, IEnumerable<string> assets)
147162
{
148163
var parser = new Assets(progressMonitor);
149164
var dependencies = new DependencyContainer();
150165
assets.ForEach(asset =>
151166
{
152-
var json = File.ReadAllText(asset);
153-
parser.TryParse(json, dependencies);
167+
if (TryReadAllText(asset, progressMonitor, out var json))
168+
{
169+
parser.TryParse(json, dependencies);
170+
}
154171
});
155172
return dependencies;
156173
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public bool Exec(string execArgs)
128128
[GeneratedRegex("Restored\\s+(.+\\.csproj)", RegexOptions.Compiled)]
129129
private static partial Regex RestoredProjectRegex();
130130

131-
[GeneratedRegex("[Assets\\sfile\\shas\\snot\\schanged.\\sSkipping\\sassets\\sfile\\swriting.|Writing\\sassets\\sfile\\sto\\sdisk.]\\sPath:\\s(.*)", RegexOptions.Compiled)]
131+
[GeneratedRegex("[Assets\\sfile\\shas\\snot\\schanged.\\sSkipping\\sassets\\sfile\\swriting.|Writing\\sassets\\sfile\\sto\\sdisk.]\\sPath:\\s(.+)", RegexOptions.Compiled)]
132132
private static partial Regex AssetsFileRegex();
133133
}
134134
}

0 commit comments

Comments
 (0)