diff --git a/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs b/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs index c7dbd03b04bff33f229eeac3cba35b6c3cde8feb..155950099ab752a67f0b60f38ed65a863d921674 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs +++ b/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs @@ -7045,6 +7045,15 @@ internal class CSharpResources { } } + /// + /// Looks up a localized string similar to An error occurred while writing the output file: {0}. + /// + internal static string ERR_PeWritingFailure { + get { + return ResourceManager.GetString("ERR_PeWritingFailure", resourceCulture); + } + } + /// /// Looks up a localized string similar to Neither 'is' nor 'as' is valid on pointer types. /// diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index 74de8bc06e2db0d935b5baf6b7f7a43de7cf71ed..11658c6f3e5ad2794c01eac2df36e85d205c0457 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -4676,6 +4676,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals. - An error occurred while writing the output file. + An error occurred while writing the output file: {0} diff --git a/src/Compilers/CSharp/Test/Emit/Attributes/EmitTestStrongNameProvider.cs b/src/Compilers/CSharp/Test/Emit/Attributes/EmitTestStrongNameProvider.cs index 24e6fa4fb790dc4527670e921a56bdbdc00ed41a..64c2df914c614f57eea597d3293cd8723a0df119 100644 --- a/src/Compilers/CSharp/Test/Emit/Attributes/EmitTestStrongNameProvider.cs +++ b/src/Compilers/CSharp/Test/Emit/Attributes/EmitTestStrongNameProvider.cs @@ -16,6 +16,9 @@ public partial class InternalsVisibleToAndStrongNameTests : CSharpTestBase private class StrongNameProviderWithBadInputStream : StrongNameProvider { private StrongNameProvider _underlyingProvider; + + public Exception ThrownException; + public StrongNameProviderWithBadInputStream(StrongNameProvider underlyingProvider) { _underlyingProvider = underlyingProvider; @@ -27,7 +30,8 @@ public StrongNameProviderWithBadInputStream(StrongNameProvider underlyingProvide internal override Stream CreateInputStream() { - throw new IOException("This is a test IOException"); + ThrownException = new IOException("This is a test IOException"); + throw ThrownException; } internal override StrongNameKeys CreateKeys(string keyFilePath, string keyContainerName, CommonMessageProvider messageProvider) => @@ -53,9 +57,9 @@ class C var comp = CreateCompilationWithMscorlib(src, options: options); - comp.VerifyEmitDiagnostics( + comp.Emit(new MemoryStream()).Diagnostics.Verify( // error CS8104: An error occurred while writing the Portable Executable file. - Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments("This is a test IOException").WithLocation(1, 1)); + Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments(testProvider.ThrownException.ToString()).WithLocation(1, 1)); } } } diff --git a/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs b/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs index 2f17b18265ec230ba6aa12cf8221746a2906eff8..4b6d6ca83bde5a419b56219309488bf9d63bf058 100644 --- a/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs +++ b/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs @@ -2701,13 +2701,13 @@ public void BrokenOutStream() var result = compilation.Emit(output); result.Diagnostics.Verify( // error CS8104: An error occurred while writing the Portable Executable file. - Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments("I/O error occurred.").WithLocation(1, 1)); + Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments(output.ThrownException.ToString()).WithLocation(1, 1)); - output.BreakHow = 1; + output.BreakHow = BrokenStream.BreakHowType.ThrowOnSetPosition; result = compilation.Emit(output); result.Diagnostics.Verify( // error CS8104: An error occurred while writing the Portable Executable file. - Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments("Specified method is not supported.").WithLocation(1, 1)); + Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments(output.ThrownException.ToString()).WithLocation(1, 1)); // disposed stream is not writable var outReal = new MemoryStream(); @@ -2723,7 +2723,7 @@ public void BrokenPDBStream() var output = new MemoryStream(); var pdb = new BrokenStream(); - pdb.BreakHow = 2; + pdb.BreakHow = BrokenStream.BreakHowType.ThrowOnSetLength; var result = compilation.Emit(output, pdb); // error CS0041: Unexpected error writing debug information -- 'Exception from HRESULT: 0x806D0004' @@ -2969,12 +2969,12 @@ public static void Main() }"; var compilation = CreateCompilationWithMscorlib(source); var broken = new BrokenStream(); - broken.BreakHow = 0; + broken.BreakHow = BrokenStream.BreakHowType.ThrowOnWrite; var result = compilation.Emit(broken); Assert.False(result.Success); result.Diagnostics.Verify( // error CS8104: An error occurred while writing the Portable Executable file. - Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments("I/O error occurred.").WithLocation(1, 1)); + Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments(broken.ThrownException.ToString()).WithLocation(1, 1)); } } } diff --git a/src/Compilers/Core/Portable/Compilation/Compilation.cs b/src/Compilers/Core/Portable/Compilation/Compilation.cs index 18796e9b2512886da31ed50922b521ee0f9faf19..9fdbdef715c6ee9ef4d542fd3c15f575eeef816d 100644 --- a/src/Compilers/Core/Portable/Compilation/Compilation.cs +++ b/src/Compilers/Core/Portable/Compilation/Compilation.cs @@ -1908,7 +1908,7 @@ private static EmitResult ToEmitResultAndFree(DiagnosticBag diagnostics, bool su } catch (Cci.PeWritingException e) { - diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_PeWritingFailure, Location.None, e.Message)); + diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_PeWritingFailure, Location.None, e.InnerException.ToString())); return false; } catch (ResourceException e) diff --git a/src/Compilers/VisualBasic/Portable/VBResources.Designer.vb b/src/Compilers/VisualBasic/Portable/VBResources.Designer.vb index 05d122330a6875262b8f2c72159443a3a794d43d..c28b867952157ead4b5aeacb7af7e8324d3eb1ad 100644 --- a/src/Compilers/VisualBasic/Portable/VBResources.Designer.vb +++ b/src/Compilers/VisualBasic/Portable/VBResources.Designer.vb @@ -9009,6 +9009,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End Get End Property + ''' + ''' Looks up a localized string similar to An error occurred while writing the output file: {0}. + ''' + Friend ReadOnly Property ERR_PeWritingFailure() As String + Get + Return ResourceManager.GetString("ERR_PeWritingFailure", resourceCulture) + End Get + End Property + ''' ''' Looks up a localized string similar to Cannot embed interop types from assembly '{0}' because it is missing the '{1}' attribute.. ''' diff --git a/src/Compilers/VisualBasic/Portable/VBResources.resx b/src/Compilers/VisualBasic/Portable/VBResources.resx index 7d49b24dd86caea27bddcfa06fa1d9b8c2b85bee..61fc5be02d7eb54ab7b46453e542412cfc6c25bf 100644 --- a/src/Compilers/VisualBasic/Portable/VBResources.resx +++ b/src/Compilers/VisualBasic/Portable/VBResources.resx @@ -5348,6 +5348,6 @@ Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string or XML literals. - An error occurred while writing the output file. + An error occurred while writing the output file: {0} diff --git a/src/Compilers/VisualBasic/Test/Emit/Attributes/EmitTestStrongNameProvider.vb b/src/Compilers/VisualBasic/Test/Emit/Attributes/EmitTestStrongNameProvider.vb index f79884e8ffb1a0bf1205e8889e9f792d9be5be95..3aa0d8c1ed08655a778d712f1467cbf919ded994 100644 --- a/src/Compilers/VisualBasic/Test/Emit/Attributes/EmitTestStrongNameProvider.vb +++ b/src/Compilers/VisualBasic/Test/Emit/Attributes/EmitTestStrongNameProvider.vb @@ -11,6 +11,8 @@ Partial Public Class InternalsVisibleToAndStrongNameTests Private Class StrongNameProviderWithBadInputStream Inherits StrongNameProvider Private _underlyingProvider As StrongNameProvider + Public Property ThrownException As Exception + Public Sub New(underlyingProvider As StrongNameProvider) _underlyingProvider = underlyingProvider End Sub @@ -25,7 +27,8 @@ Partial Public Class InternalsVisibleToAndStrongNameTests End Function Friend Overrides Function CreateInputStream() As Stream - Throw New IOException("This is a test IOException") + ThrownException = New IOException("This is a test IOException") + Throw ThrownException End Function Friend Overrides Function CreateKeys(keyFilePath As String, keyContainerName As String, messageProvider As CommonMessageProvider) As StrongNameKeys @@ -53,8 +56,8 @@ End Class , options:=options) - comp.VerifyEmitDiagnostics( - Diagnostic(ERRID.ERR_PeWritingFailure).WithArguments("This is a test IOException").WithLocation(1, 1)) + comp.Emit(New MemoryStream()).Diagnostics.Verify( + Diagnostic(ERRID.ERR_PeWritingFailure).WithArguments(testProvider.ThrownException.ToString()).WithLocation(1, 1)) End Sub diff --git a/src/Compilers/VisualBasic/Test/Emit/Emit/CompilationEmitTests.vb b/src/Compilers/VisualBasic/Test/Emit/Emit/CompilationEmitTests.vb index f743f944c13656ea8a9314a7ba617986c2abf4db..cf68dca8fda0701d980450c85ab43fc8499845ef 100644 --- a/src/Compilers/VisualBasic/Test/Emit/Emit/CompilationEmitTests.vb +++ b/src/Compilers/VisualBasic/Test/Emit/Emit/CompilationEmitTests.vb @@ -2939,14 +2939,14 @@ End Module Dim emitResult As EmitResult Using output = New BrokenStream() - output.BreakHow = 0 + output.BreakHow = BrokenStream.BreakHowType.ThrowOnWrite emitResult = compilation.Emit(output, Nothing, Nothing, Nothing) - End Using - CompilationUtils.AssertTheseDiagnostics(emitResult.Diagnostics, + CompilationUtils.AssertTheseDiagnostics(emitResult.Diagnostics, -BC37256: An error occurred while writing the output file. +BC37256: An error occurred while writing the output file: <%= output.ThrownException.ToString() %> ) + End Using End Sub End Class End Namespace diff --git a/src/Test/Utilities/Shared/Pe/BrokenStream.cs b/src/Test/Utilities/Shared/Pe/BrokenStream.cs index 24c000eff7fcc62e5abac943b340f8ca8da74edd..7eb5c77c12097355a50e1cb5df101b78049214ae 100644 --- a/src/Test/Utilities/Shared/Pe/BrokenStream.cs +++ b/src/Test/Utilities/Shared/Pe/BrokenStream.cs @@ -7,7 +7,15 @@ namespace Roslyn.Test.Utilities { internal class BrokenStream : Stream { - public int BreakHow; + public enum BreakHowType + { + ThrowOnSetPosition, + ThrowOnWrite, + ThrowOnSetLength + } + + public BreakHowType BreakHow; + public Exception ThrownException { get; private set; } public override bool CanRead { @@ -44,8 +52,11 @@ public override long Position } set { - if (BreakHow == 1) - throw new NotSupportedException(); + if (BreakHow == BreakHowType.ThrowOnSetPosition) + { + ThrownException = new NotSupportedException(); + throw ThrownException; + } } } @@ -61,14 +72,20 @@ public override long Seek(long offset, SeekOrigin origin) public override void SetLength(long value) { - if (BreakHow == 2) - throw new IOException(); + if (BreakHow == BreakHowType.ThrowOnSetLength) + { + ThrownException = new IOException(); + throw ThrownException; + } } public override void Write(byte[] buffer, int offset, int count) { - if (BreakHow == 0) - throw new IOException(); + if (BreakHow == BreakHowType.ThrowOnWrite) + { + ThrownException = new IOException(); + throw ThrownException; + } } } }