Skip to content

Commit

Permalink
Improved Diagnostics for error scenarios with authoring projects (#1863)
Browse files Browse the repository at this point in the history
* first commit

* first commit

* return instead of throw

* dont hardcode version num

* delete unused line

---------

Co-authored-by: Sophia Chen <sophia.six.chen@gmail.com>
Co-authored-by: Manodasan Wignarajah <mawign@microsoft.com>
  • Loading branch information
3 people authored Dec 7, 2024
1 parent 2fbe1af commit b1733e9
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ CsWinRT1029 | Usage | Warning | Class implements WinRT interfaces generated usin
### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
CsWinRT1030 | Usage | Warning | Project needs to be updated with `<AllowUnsafeBlocks>true</AllowUnsafeBlocks>` to allow generic interface code generation.
CsWinRT1030 | Usage | Warning | Project needs to be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>' to allow generic interface code generation.

## Release 2.2.1

### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
CsWinRT1031 | Usage | Error | Source generator failed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 69 additions & 4 deletions src/Authoring/WinRT.SourceGenerator/CsWinRTDiagnosticStrings.resx
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
Expand Down Expand Up @@ -120,7 +179,7 @@
</data>
<data name="BindableCustomPropertyClassNotMarkedPartial_Text" xml:space="preserve">
<value>Class '{0}' has attribute GeneratedBindableCustomProperty but it or a parent type isn't marked partial. Type and any parent types should be marked partial to allow source generation for trimming and AOT compatibility.</value>
</data>
</data>
<data name="DisjointNamespaceRule_Brief" xml:space="preserve">
<value>Namespace is disjoint from main (winmd) namespace</value>
</data>
Expand Down Expand Up @@ -231,9 +290,6 @@
<data name="RefParameterFound_Brief" xml:space="preserve">
<value>Parameter passed by reference</value>
</data>
<data name="RefParameterFound_Text" xml:space="preserve">
<value>Method '{0}' has parameter '{1}' marked `ref`; reference parameters are not allowed in Windows Runtime</value>
</data>
<data name="StructHasConstFieldRule_Brief" xml:space="preserve">
<value>Const field in struct</value>
</data>
Expand Down Expand Up @@ -292,4 +348,13 @@
<value>Consider changing the type '{1} in the member signature to one of the following types from System.Collections.Generic: {2}.</value>
<comment>{1} and {2} will be keywords (types) from DotNet</comment>
</data>
<data name="RefParameterFound_Text" xml:space="preserve">
<value>Method '{0}' has parameter '{1}' marked `ref`; reference parameters are not allowed in Windows Runtime</value>
</data>
<data name="SourceGeneratorFailed_Brief" xml:space="preserve">
<value>Source generator failed</value>
</data>
<data name="SourceGeneratorFailed_Text" xml:space="preserve">
<value>CsWinRT component authoring source generator 'WinRT.SourceGenerator' failed to generate WinMD and projection because of '{0}'</value>
</data>
</root>
10 changes: 7 additions & 3 deletions src/Authoring/WinRT.SourceGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading;
using WinRT.SourceGenerator;

namespace Generator
{
Expand Down Expand Up @@ -91,12 +92,15 @@ private void GenerateSources()
{
using var cswinrtProcess = Process.Start(processInfo);
Logger.Log(cswinrtProcess.StandardOutput.ReadToEnd());
Logger.Log(cswinrtProcess.StandardError.ReadToEnd());
var error = cswinrtProcess.StandardError.ReadToEnd();
Logger.Log(error);
cswinrtProcess.WaitForExit();

if (cswinrtProcess.ExitCode != 0)
{
throw new Win32Exception(cswinrtProcess.ExitCode);
var diagnosticDescriptor = WinRTRules.SourceGeneratorFailed;
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, null, error));
throw new Win32Exception(cswinrtProcess.ExitCode, error);
}

foreach (var file in Directory.GetFiles(outputDir, "*.cs", SearchOption.TopDirectoryOnly))
Expand Down Expand Up @@ -190,7 +194,7 @@ public void Generate()
}
Logger.Close();
Environment.ExitCode = -2;
throw;
return;
}

Logger.Log("Done");
Expand Down
5 changes: 5 additions & 0 deletions src/Authoring/WinRT.SourceGenerator/WinRTRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,10 @@ private static DiagnosticDescriptor MakeRule(string id, string title, string mes
CsWinRTDiagnosticStrings.ClassImplementsOldProjection_Brief,
CsWinRTDiagnosticStrings.ClassOldProjectionMultipleInstances_Text,
false);

public static DiagnosticDescriptor SourceGeneratorFailed = MakeRule(
"CsWinRT1031",
CsWinRTDiagnosticStrings.SourceGeneratorFailed_Brief,
CsWinRTDiagnosticStrings.SourceGeneratorFailed_Text);
}
}
4 changes: 3 additions & 1 deletion src/cswinrt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Where <spec> is one or more of:
{
writer console;
console.write("error: '%' when processing %%%\n", e.what(), ns, currentType.empty() ? "" : ".", currentType);
console.flush_to_console();
console.flush_to_console_error();
throw;
}
});
Expand Down Expand Up @@ -541,6 +541,8 @@ ComWrappersSupport.RegisterAuthoringMetadataTypeLookup(new Func<Type, Type>(GetM
{
w.write(" error: %\n", e.what());
result = 1;
w.flush_to_console_error();
return result;
}

w.flush_to_console();
Expand Down
8 changes: 8 additions & 0 deletions src/cswinrt/text_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ namespace cswinrt
m_second.clear();
}

void flush_to_console_error() noexcept
{
fprintf(stderr, "%.*s", static_cast<int>(m_first.size()), m_first.data());
fprintf(stderr, "%.*s", static_cast<int>(m_second.size()), m_second.data());
m_first.clear();
m_second.clear();
}

void flush_to_file(std::string const& filename)
{
if (!file_equal(filename))
Expand Down

0 comments on commit b1733e9

Please sign in to comment.