Microsoft.Unity.Analyzers icon indicating copy to clipboard operation
Microsoft.Unity.Analyzers copied to clipboard

Analyzers don't seem to work during Unity compilation

Open tallior opened this issue 3 years ago • 9 comments

Bug description

I am trying this package with Unity 2020.3.29, but it doesn't seem to work during Unity compilation.

  • Version of analyzers assembly: 1.14.0
  • Analyzer rule: ALL OF THEM
  • Error (exception message, type, and callstack where applicable):

To Reproduce

Steps or code to reproduce the behavior:

  1. Open Unity 2020.3.29
  2. Copy Microsoft.Unity.Analyzers.dll (downloaded from NuGet earlier) into Assets/ folder
  3. Select the .dll in Unity and disable "Any Platform", deselect Editor and Standalone (as mentioned here).
  4. Let the scripts in the project re-compile
  5. No warnings / errors are shown during compilation.
  6. Add a new script file that should trigger one of the rules (e.g: empty Unity message) - a class with an empty Update() method.
  7. No errors / Warnings in the Unity console

Expected behavior

Warnings / errors should be shown in the Unity console

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.

tallior avatar Sep 14 '22 18:09 tallior

This is outside of our scope (our target is to use those analyzers in Visual Studio), but I think you are missing the asset label thing:

Go to Asset Labels and open the Asset Labels sub-menu.

Create and assign a new label named `RoslynAnalyzer`. To do this, enter `RoslynAnalyzer` into the text input window in the Asset Labels sub-menu. 

This label must match exactly and is case sensitive. After you created the label for the first analyzer, The label appears in the Asset Labels sub-menu. 

You can click on the name of the label in the menu to assign it to other analyzers.

If this is not working, our Unity friend @gohmiaoyu should be able to help.

sailro avatar Sep 15 '22 10:09 sailro

I actually forgot to write it, but i did add that label (followed the docs in the link i attached). Another analyzer does work (StyleCop), but the Unity analyzers - don't.

Also, in Rider (which supports Roslyn) it does work. so i believe it's set up correctly, but there's something else at play here. I also tried with Unity 2021 LTS - still no go.

tallior avatar Sep 15 '22 10:09 tallior

Indeed, I repro with 2021 LTS. I tried the manual way, and also by using UnityNuGet (with a new scoped registry) and using our ported package.

In both cases, I'm sure our dll is properly recognized by Unity, given <Analyzer/> nodes are added to generated csproj files (when you use our VS Editor Package, we automatically add our own analyzer dll bundled with Visual Studio, then we query the Unity build pipeline).

But nothing in the console. Are we missing something?

sailro avatar Sep 15 '22 14:09 sailro

Ah when I create a Default.ruleset file in Assets, it is working, but only when I use Error or Warning levels. Info (which is the default for our rules) will display nothing in the console.

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Default Rule Set" Description=" " ToolsVersion="10.0">
    <Rules AnalyzerId="Microsoft.Unity.Analyzers" RuleNamespace="Microsoft.Unity.Analyzers">
        <!-- Full list of rule description available here https://github.com/microsoft/Microsoft.Unity.Analyzers/blob/main/doc/index.md -->
        <Rule Id="UNT0001" Action="Warning" />
    </Rules>
</RuleSet>

sailro avatar Sep 15 '22 14:09 sailro

Thats a good enough workaround for me! Thanks :)

On Thu, Sep 15, 2022, 17:21 Sebastien Lebreton @.***> wrote:

Ah when I create a Default.ruleset file in Assets, it is working, but only when I use Error or Warning levels. Info (which is the default for our rules) will display nothing in the console.

— Reply to this email directly, view it on GitHub https://github.com/microsoft/Microsoft.Unity.Analyzers/issues/242#issuecomment-1248172117, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB3RFPX42XCGTUIC6LZILQDV6MWGHANCNFSM6AAAAAAQMWFU3Y . You are receiving this because you authored the thread.Message ID: @.***>

tallior avatar Sep 15 '22 14:09 tallior

I just tried this - it works in Unity 2021, but doesn't seem to have any effect in 2020 LTS :(

On Thu, Sep 15, 2022 at 5:21 PM Sebastien Lebreton @.***> wrote:

Ah when I create a Default.ruleset file in Assets, it is working, but only when I use Error or Warning levels. Info (which is the default for our rules) will display nothing in the console.

— Reply to this email directly, view it on GitHub https://github.com/microsoft/Microsoft.Unity.Analyzers/issues/242#issuecomment-1248172117, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB3RFPX42XCGTUIC6LZILQDV6MWGHANCNFSM6AAAAAAQMWFU3Y . You are receiving this because you authored the thread.Message ID: @.***>

tallior avatar Sep 15 '22 19:09 tallior

I'm going to discuss this with our Unity friends

sailro avatar Sep 19 '22 15:09 sailro

@sailro what did your friends say ?

tallior avatar Sep 22 '22 12:09 tallior

We have a meeting planned next week, I added this on our list of topics to discuss.

sailro avatar Sep 22 '22 12:09 sailro

@sailro any updates?

tallior avatar Oct 11 '22 19:10 tallior

Yes, so we discussed with Unity folks, and from our investigations the issue is definitively not on our side. It's possible that an update to their internal build pipeline (perhaps related to their new bee compiler) changed the way informational messages are handled.

We recommend opening a ticket on the Unity side for tracking this going forward.

sailro avatar Oct 12 '22 07:10 sailro

@sailro Do you know anything more about it? I mean, even if the problem is not on your side, do you know if Unity finally fixed it in some version?

I'm trying to set most of the rules in this repository to "Error" but no matter how much I change them, they still remain in "Info".

Or is what I mention a completely different problem?

bdovaz avatar Nov 29 '22 07:11 bdovaz

@bdovaz it is on their backlog, but not high priority if I'm not wrong. But if you set correctly the Error level for analyzers like in https://github.com/microsoft/Microsoft.Unity.Analyzers/issues/242#issuecomment-1248172117 you should definitively see them. The issue is for Info messages.

sailro avatar Nov 29 '22 07:11 sailro

@bdovaz and @sailro i don't think they have any plans to support this in 2020 LTS version (which is ending its support in a few months). Here's the reply i got to a ticket i opened for Unity folks:

I've heard back from our dev teams on this issue - the source of this problem seems to be that Unity 2020 LTS uses an earlier version of Roslyn than 2021 which is incompatible with Microsoft's analyzer. Since updating the Roslyn version in 2020 LTS would be a major change, which has the potential to cause more issues, we are unable to backport it to 2020 LTS.

You may have some success using an older version of the analyzers before Microsoft updated their Roslyn version. However, we don't recommend or guarantee this, it is unlikely that Microsoft would support it, and you'll be lacking newer features in the Analyzer.

tallior avatar Nov 29 '22 08:11 tallior

@bdovaz it is on their backlog, but not high priority if I'm not wrong. But if you set correctly the Error level for analyzers like in #242 (comment) you should definitively see them. The issue is for Info messages.

In my case I am doing the same but putting them as error.

What happens to me is that it is as if I had not done it because if I expand the analyzer in the project (VS 2022), they are still with the blue info icon and not the red error icon. I add by hand to the project the ruleset file (only to test that it is seen well in the visual ruleset editor to see if I have some typo in the ruleset file) and I see that everything is perfect.

So I understand that it is something weird that is happening between Unity Visual Studio Tools for Unity or similar.

I am in 2021LTS.

bdovaz avatar Nov 29 '22 08:11 bdovaz