Skip to content

Commit dcd84f4

Browse files
committed
Improve source archiving to handle non-existent files
1 parent 31ad195 commit dcd84f4

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

csharp/extractor/Semmle.Extraction/TrapWriter.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,6 @@ public void Archive(string originalPath, PathTransformer.ITransformedPath transf
115115
ArchivePath(fullInputPath, transformedPath, inputEncoding);
116116
}
117117

118-
/// <summary>
119-
/// Archive a file given the file contents.
120-
/// </summary>
121-
/// <param name="inputPath">The path of the file.</param>
122-
/// <param name="contents">The contents of the file.</param>
123-
public void Archive(PathTransformer.ITransformedPath inputPath, string contents)
124-
{
125-
if (string.IsNullOrEmpty(archive))
126-
return;
127-
128-
ArchiveContents(inputPath, contents);
129-
}
130-
131118
/// <summary>
132119
/// Try to move a file from sourceFile to destFile.
133120
/// If successful returns true,
@@ -209,23 +196,21 @@ public void Emit(ITrapEmitter emitter)
209196
/// <exception cref="PathTooLongException">If the output path in the source archive would
210197
/// exceed the system path limit of 260 characters.</exception>
211198
private void ArchivePath(string fullInputPath, PathTransformer.ITransformedPath transformedPath, Encoding inputEncoding)
212-
{
213-
var contents = File.ReadAllText(fullInputPath, inputEncoding);
214-
ArchiveContents(transformedPath, contents);
215-
}
216-
217-
private void ArchiveContents(PathTransformer.ITransformedPath transformedPath, string contents)
218199
{
219200
var dest = FileUtils.NestPaths(logger, archive, transformedPath.Value);
220-
var tmpSrcFile = Path.GetTempFileName();
221-
File.WriteAllText(tmpSrcFile, contents, utf8);
222201
try
223202
{
203+
var contents = File.ReadAllText(fullInputPath, inputEncoding);
204+
var tmpSrcFile = Path.GetTempFileName();
205+
File.WriteAllText(tmpSrcFile, contents, utf8);
206+
224207
FileUtils.MoveOrReplace(tmpSrcFile, dest);
225208
}
226209
catch (Exception ex)
227210
{
228-
// If this happened, it was probably because the same file was compiled multiple times.
211+
// If this happened, it was probably because
212+
// - the same file was compiled multiple times, or
213+
// - the file doesn't exist (due to wrong #line directive or because it's an in-memory source generated AST).
229214
// In any case, this is not a fatal error.
230215
logger.LogWarning("Problem archiving " + dest + ": " + ex);
231216
}

0 commit comments

Comments
 (0)