Let Unity show StyleCop errors - c#

I have a project which is developed with Unity3D. Now I want to add StyleCop to this project.
I have added this awesome package which shows the warnings already in VisualStudio (this is a great benefit over StyleCop Classic). But now I want to see the warnings in Unity and let the build fail. To let the build fail if there are any warnings, I have already added a csc.rsp file with -warnaserror+, but unfortunately, I don't have any warnings in Unity.
Is it possible to get the StyleCop-warnings to the Unity-Editor?
Unity-Version 2019.3.0b7 if that matters.
Edit: Another view of the same issue would be:
How to install an analyzer to roslyn?
Unfortunately I the options to install a roslyn analyzer seem to be to install it as nuget package or as Visual Studio extension. But since both ways integrate the errors into Visual Studio, I need another way to add an analyzer to roslyn, which lets the compiler issue errors which then get displayed in Unity3D.
Edit2: To be absolutely clear: I already can do UnityEngine.Debug.LogError and print every StyleCop error to the UnityConsole. But I want StyleCop to be integrated in the compilation pipeline of unity. So that I cannot press play anymore.

Here is a solution if you don't mind to write some little pieces of code.
Unity has an editor log file, it records every warning during your build. Like this:
Assets/SomeScript.cs(134,33): warning CS0618: UnityEngine.Random.RandomRange(float, float) is obsolete: Use Random.Range instead.
Unity has a callback IPreprocessBuildWithReport.OnPreprocessBuild , which has the following description:
Implement this interface to receive a callback before the
build is started.
You can start to monitor the log changes from then on.
Unity also has a callback when a build is end, by adding a PostProcessBuildAttribute to a specific function. Here is the description:
Add this attribute to a method to get a notification just after building the player.
[Update]
Now you've recorded if there are warnings in last build, you can make a flag in memory or in file system, then you add few lines of code to check this flag, if it's true then stop the playing by using EditorApplication.ExitPlaymode()

Related

Whats the difference between the 'Code Refactoring' and the 'Analyzer with Code Fix' template

I thought I'd delve into writing a custom code analyzer, so that I can enforce a rule and and allow code fixes to be applied.
Upon opening VS after installing the required workload I see the following available templates:
Code Refactoring
Analyzer with Code Fix
Standalone Code analysis Tool
What are the differences? How would i know what to pick?
The "Code Refactoring" template gives you a VSIX extension with a separate project for the refactorings. The extension shows a new entry in the "Quick Actions" menu when the cursor is on matching code. This way, you can provide a quick code fix without having to define an additional analyzer ID and showing the user an entry in the error list.
The "Analyzer with Code Fix" template gives you a VSIX extension and a separate project for the analyzer which can also be deployed via NuGet. The analyzer can be used to show a squiggly line below matching code, showing an entry in the error list, which can be configured to be a message, warning or even an error - providing a code fix is optional.
The "Standalone Code Analysis Tool" template gives you a console project, that will open a solution workspace. This way, you can analyze your projects without having to install any VSIX extensions or NuGet packages to the project or the IDE - it is supposed to be called from a command line, which makes most sense when used on a build server. The template does not include any analyzers or refactorings.

Unity3D Project Configuration: Treat warnings as errors

I'm trying to configure my project from within the Unity Editor to treat all warning messages as errors, but I can't seem to find any documentation pertaining to this. Is there a way that I can configure this for my project? Thank you so much!
Create a text file in ProjectFolder/Assets/smcs.rsp
Everything in that file will end up as compiler command-line parameters
The parameter to force warnings as errors is -warnaserror+, so add this to the smcs.rsp
If you are interested in other available parameters, run UnityInstallFolder/editor/data/bin/smcs /?
More on https://answers.unity.com/questions/216838/how-to-force-warnings-as-errors.html

Lots of squiggly lines after updating to .net core 2.1 - 'may need to supply run time policy'

Don't know why I did, but I updated to .Net Core 2.1 yesterday. Ever since I updated, I am seeing tons of squiggly lines throughout my solution in Visual Studio on mac (so annoying). Here's the warning message -
I think understand the source of the warning. Basically, I am referencing to a lot of 3rd party Nuget packages (Autofac, Serilog, etc.) and they are built with an older version of .Net Core. This is what the warning is trying to tell me I think. But my code works just fine, don't have any issues running it.
After researching a little bit, I think the error is CS1701. It is also added [by default] to be ignored in the Compiler settings for all projects in my solution. This is why I am not seeing this warning reported when I build my project/solution. However, the squiggly's are still shown in the text editor in Visual Studio.
Visual Studio is kind enough to offer a solution to suppress these squiggly lines, by adding a pragma statement at the top of .cs file - #pragma warning disable CS1701 // Assuming assembly reference matches identity. But I am afraid I need to add this line to all .cs file in my solution (there are a lot).
Does anyone know any other better way to suppress these squiggly lines in the text editor, please?
Goto Preferences->Text Editor -> Source Code Analysis -> Code Rules (tab) and change the line
Rule Id="CS1701" Action="Warning" to
Rule Id="CS1701" Action="Hidden"
and press OK.
Wait a for a short while and the lines will disappear.

Roslyn features/patterns branch (C# 7) - How to enable the experimental language features

I want to experiment with the potential C# 7 future language features.
I have a virtual machine into which I have downloaded the Roslyn codebase (features/patterns branch) and built as described on Roslyn's github here: Building and Testing on Windows.
I can successfully fire up a new experimental instance of Visual Studio (set VisualStudioSetup as the startup project and run).
When I try out the new language features (pattern matching and local functions) I get an error in the editor as a squiggly and also error when I build:
Error CS8058 Feature 'local functions' is experimental and
unsupported; use '/features:localFunctions' to enable.
Error CS8058 Feature 'pattern matching' is experimental and
unsupported; use '/features:patterns' to enable.
Where do I put these options?
I tried adding it to the command line arguments, but I get an error.
This is bug 7812 that I haven't fixed yet. Sorry.
Internally, the compiler team mostly just tests things through unit tests or the command line compiler; if we really need to test the IDE we just delete the check that's passing in the feature flag.
Sorry, I haven't done R&D with the said code you have downloaded.
But in Visual Studio '15' preview (announced on 30 March 2016) I have fixed this very easily. It might help other people.
I was getting the error
Error CS8058 Feature 'local functions' is experimental and unsupported; use '/features:localFunctions' to enable.
To fix this select your project name and right click >> Properties Window >> Build and then
add the below two options in "Conditional Compilation symbols" text box __DEMO__,__DEMO_EXPERIMENTAL__
click on save button to Save it. Now build your application and your build will be succeeded.
Even though at my end if I add any one of the __DEMO__ & __DEMO_EXPERIMENTAL__ then it is working fine.
I found way to enable the new language features.
By tracing the code I found where the feature check is made (function IsFeatureEnabled) and changed it to always return true.
Then I rebuilt from the command line as before. And after that the new language features are enabled when I fire up the experimental Visual Studio instance.
BEFORE:
AFTER:
internal bool IsFeatureEnabled(MessageID feature)
{
// Force all demo features:
return true;
// in "demo" mode enable all language features.
if (PreprocessorSymbols.Contains("__DEMO__"))
{
return true;
}

Post build visual studio step not being called at all

The long of it is I built an installer in visual studio that gave me this cheery error when I tried to use the program:
Retrieving the COM class factory for
component with CLSID
{EC10E7E8-797E-4495-A86D-3E9EADA6D5BB}
failed due to the following error:
80040154.
From that it seems I need to embed the manifest in the executable and to do that I should add as a post build event the following:
"$(DevEnvDir)....\VC\bin\mt.exe" -manifest "$(ProjectDir)$(TargetName).exe.manifest"
–outputresource:"$(TargetDir)$(TargetFileName)";#1
Well, when I do that and build the solution the event is not being called at all. In fact, I can put any old random text in the pre and post events and no error is ever given nor do I see anything being called.
Is there something that I should be doing differently to make this work?
Additional information:
I'm building from the IDE. And when I toggle the build types to debug and release I still the correct command in the post build events.
Is there a way to see a log of what it's doing?
Tool + Options, Project and Solutions, Build and Run, set "MSBuild project build output verbosity" to Detailed. You'll get a ton of diagnostics in the Output window.
FWIW, the error message you get is a simple "class not registered" error. Fix with Regsvr32.exe
two guesses:
build is not succeeding
your post/pre build events are defined in a project config that is not being built, like you define your events in the "debug" build but are building the "release" build.
are you building the solution from the command line or anything like that?
Here's what worked. You apparently don't need to bake the manifest into the program. It was enough to include the manifest into the msi package and also include the DLL that the dependency checker missed.
Like most things so simple once you know :-)
Why the post build steps aren't being called I don't know, but the real problem is solved.

Categories

Resources