Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit f3864e9

Browse files
committed
Merge pull request #30 from nslottow/skip-unformattable-files
Skip non-existent and read-only files
2 parents ffe20fe + 2fbd3f9 commit f3864e9

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel.Composition;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
910
using System.Text;
@@ -39,20 +40,40 @@ public async Task<bool> RunAsync(Workspace workspace, CancellationToken cancella
3940
foreach (var id in documentIds)
4041
{
4142
var document = solution.GetDocument(id);
43+
44+
var fileInfo = new FileInfo(document.FilePath);
45+
if (!fileInfo.Exists || fileInfo.IsReadOnly)
46+
{
47+
Console.WriteLine("warning: skipping document '{0}' because it {1}.",
48+
document.FilePath,
49+
fileInfo.IsReadOnly ? "is read-only" : "does not exist");
50+
continue;
51+
}
52+
4253
var shouldBeProcessed = await ShouldBeProcessedAsync(document);
4354
if (!shouldBeProcessed)
4455
continue;
4556

4657
Console.WriteLine("Processing document: " + document.Name);
4758
var newDocument = await RewriteDocumentAsync(document, cancellationToken);
48-
hasChanges |= newDocument != document;
4959

50-
solution = newDocument.Project.Solution;
51-
}
60+
if (newDocument != document)
61+
{
62+
Debug.Assert(newDocument.FilePath == document.FilePath, "Formatting rules should not change a document's file path");
5263

53-
if (workspace.TryApplyChanges(solution))
54-
{
55-
Console.WriteLine("Solution changes committed");
64+
hasChanges = true;
65+
var sourceText = await newDocument.GetTextAsync(cancellationToken);
66+
67+
using (var file = File.Open(newDocument.FilePath, FileMode.Truncate, FileAccess.Write))
68+
{
69+
using (var writer = new StreamWriter(file, sourceText.Encoding))
70+
{
71+
sourceText.Write(writer, cancellationToken);
72+
}
73+
}
74+
}
75+
76+
solution = newDocument.Project.Solution;
5677
}
5778

5879
return hasChanges;

0 commit comments

Comments
 (0)