diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 7d84578c..ae7c6ce4 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;NU1608;NU1109 - 28.0.1 + 28.0.2 preview 1.0.0 Markdown, Snippets, mdsnippets, documentation, MarkdownSnippets @@ -16,4 +16,4 @@ - \ No newline at end of file + diff --git a/src/MarkdownSnippets/Reading/FileSnippetExtractor.cs b/src/MarkdownSnippets/Reading/FileSnippetExtractor.cs index b80c8c7c..f9e0b9c7 100644 --- a/src/MarkdownSnippets/Reading/FileSnippetExtractor.cs +++ b/src/MarkdownSnippets/Reading/FileSnippetExtractor.cs @@ -101,7 +101,8 @@ public static IEnumerable Read(string path, int maxWidth = int.MaxValue return []; } - using var reader = File.OpenText(path); + using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var reader = new StreamReader(stream); return Read(reader, path, maxWidth, newLine).ToList(); } diff --git a/src/Tests/SnippetExtractor/SnippetExtractorTests.CanReadFileWhileLockedByAnotherProcess.verified.txt b/src/Tests/SnippetExtractor/SnippetExtractorTests.CanReadFileWhileLockedByAnotherProcess.verified.txt new file mode 100644 index 00000000..105a9c3e --- /dev/null +++ b/src/Tests/SnippetExtractor/SnippetExtractorTests.CanReadFileWhileLockedByAnotherProcess.verified.txt @@ -0,0 +1,10 @@ +[ + { + Key: CodeKey, + Language: cs, + Value: The Code, + Error: , + FileLocation: LockedFile.cs(1-3), + IsInError: false + } +] \ No newline at end of file diff --git a/src/Tests/SnippetExtractor/SnippetExtractorTests.cs b/src/Tests/SnippetExtractor/SnippetExtractorTests.cs index eb414cf4..0e21e21d 100644 --- a/src/Tests/SnippetExtractor/SnippetExtractorTests.cs +++ b/src/Tests/SnippetExtractor/SnippetExtractorTests.cs @@ -39,6 +39,29 @@ await Verify(snippets) } } + [Fact] + public Task CanReadFileWhileLockedByAnotherProcess() + { + var temp = Path.Combine(Path.GetTempPath(), "LockedSnippetFile.cs"); + try + { + File.WriteAllText(temp, + """ + #region CodeKey + The Code + #endregion + """); + using var lockingStream = new FileStream(temp, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); + var snippets = FileSnippetExtractor.Read(temp); + return Verify(snippets) + .AddScrubber(_ => _.Replace(temp, "LockedFile.cs")); + } + finally + { + File.Delete(temp); + } + } + [Fact] public Task CanExtractWithInnerWhiteSpace() {