Skip to content

project.assets.json should indicate "analyzer" assets like "compile" and "runtime" #6279

Closed

Description

@nguerrera

project.assets.json doesn't actually indicate which analyzer references a project should use.

Unlike "compile" and "runtime", the SDK is just pattern matching on all files in the package to find analyzers: https://github.com/dotnet/sdk/blob/634680ab0ae045ba45977b6293b46f95a0385aac/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs#L251

"analyzer" assets should work like "compile" and "runtime" and the SDK should resolve them the same way. The current mechanism blocks respecting PrivateAssets and ExcludeAssets=analyzers

cc @emgarten

Activity

  1. added this to the Backlog milestone on Dec 11, 2017
  2. added
    Priority:1
    on Dec 11, 2017
  3. emgarten commented on Dec 11, 2017

    @emgarten
    Member

    Analyzers were added without work being done on the NuGet side originally. We should add it to project.assets.json so that the SDK can switch over to using that instead of reading packages directly to find the analyzers folder.

  4. nguerrera commented on Dec 23, 2017

    @nguerrera
    Author

    Related to this, is there a spec for how analyzer assets should be selected based on project language?

    Current implementations are here:
    https://github.com/NuGet/NuGet.BuildTasks/blob/1d8af3499f94a32c1d128a42faceade39c1f4337/src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs#L444-L470
    https://github.com/dotnet/sdk/blob/634680ab0ae045ba45977b6293b46f95a0385aac/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs#L251-L272

    I believe they are semantically equivalent, but both have some strange behavior if I'm reading correctly.

    • Leading "analyzers" is matched without separator
      • So "analyzersBanana/myanalyzer.dll" would match
    • Leading "analyzers" is case-sensitive.
      • Is that how the others like "lib" and "ref" are matched too?
    • C# projects matches ("*/cs/*" or not "*/vb/*") and VB matches ("*/vb/*" or not "*/cs/*")?
      • This seems to ignore the possibility of other languages gaining analyzers. e.g. If there were F# analyzers, they would match VB and C# projects.
    • Looking for language in any path component seems like a hammer vs how other nuget assets are matched.

    cc @jinujoseph @natidea @jasonmalinowski

  5. jasonmalinowski commented on Dec 23, 2017

    @jasonmalinowski
    Member

    Adding @jmarolf too.

    This seems to ignores the possibility of other languages gaining analyzers. e.g. If there were F# analyzers, they would match VB and C# projects.

    I think that was just the code trying to be tricky and deal with the "no language either" thing at the same time.

  6. nguerrera commented on Jan 23, 2018

    @nguerrera
    Author
  7. davidfowl commented on Jan 23, 2018

    @davidfowl
    Member

    This seems pretty broken. It means that analyzers don't respect the PrivateAssets or flags specified on the PacakgeReference (I see a few people suffering from this already today).

  8. nguerrera commented on Jan 23, 2018

    @nguerrera
    Author

    Yes, it is pretty broken. See two linked sdk bugs that are blocked on this.

  9. roryprimrose commented on Feb 7, 2018

    @roryprimrose

    I have to run this powershell on my builds as a workaround using the parameters -path $(Build.SourcesDirectory) -packageName $(Build.DefinitionName)

    Param(
       [string]$path,
       [string]$packageName
    )
    
    Add-Type -Assembly System.IO.Compression.FileSystem
    
    Function Remove-RoslynAnalyzers([string] $filePath, [string] $projectName)
    {
        if ((Test-Path -Path $filePath) -eq $false)
        {
            Write-Host "$filePath was not found"
    
            return
        }
    
        Write-Host "Scanning $filePath"
    
        $ZipStream = [System.IO.Compression.ZipFile]::Open("$filePath", 'Update')
        $ZipItem = $ZipStream.GetEntry("$projectName.nuspec")
    
        if ($ZipItem -eq $null)
        {
            Write-Host "$projectName.nuspec was not found in the zip"
    
            return
        }
    
        [xml]$nuspec = $null
        $reader = $null
        
        $reader = New-Object System.IO.StreamReader($ZipItem.Open())
    
        try
        {
            [xml]$nuspec = $reader.ReadToEnd()
        }
        finally
        {
            if ($null -ne $reader) 
            { 
                $reader.Close() 
                $reader.Dispose() 
            }
        }
    
        Clean-NuSpec $nuspec
        
        # Re-open the file this time with streamwriter
        $writer = [System.IO.StreamWriter]($ZipItem).Open()
        
        try
        {
            # If needed, zero out the file -- in case the new file is shorter than the old one
            $writer.BaseStream.SetLength(0)
    
            # Insert the $text to the file and close
            $writer.Write($nuspec.OuterXml)
        }
        finally
        {
            $writer.Flush()
            $writer.Close()
        }
    
        # Write the changes and close the zip file
        $ZipStream.Dispose()
    }
    
    Function Clean-NuSpec([xml]$xml)
    {
        $namespace = "http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"
            
        [System.Xml.XmlNamespaceManager] $nsMgr = New-Object System.Xml.XmlNamespaceManager($nuspec.NameTable)
        $nsMgr.AddNamespace("ns", "http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd");
        
        Remove-Package $nuspec "CodeCracker.CSharp" $nsMgr
            
        [string]$analyzersPath = "//ns:package/ns:metadata/ns:dependencies/ns:group/ns:dependency[contains(@id,'.Analyzers')]"
            
        $nodes = $nuspec.SelectNodes($analyzersPath, $nsMgr)
            
        foreach ($node in $nodes)
        {
            [string]$id = $node.Attributes["id"].Value
    
            Write-Host "Removing $id"
    
            $node.ParentNode.RemoveChild($node)
        }
    }
    
    Function Remove-Package([xml]$xml, [string] $name, [System.Xml.XmlNamespaceManager] $nsMgr)
    {
        [string]$path = "//ns:package/ns:metadata/ns:dependencies/ns:group/ns:dependency[@id='$name']"
    
        $nodes = $xml.SelectNodes($path, $nsMgr)
        
        foreach ($node in $nodes)
        {
            [string]$id = $node.Attributes["id"].Value
    
            Write-Host "Removing $id"
    
            $node.ParentNode.RemoveChild($node)
        }
    }
    
    $items = Get-ChildItem $path -Recurse -Filter "$packageName.*.nupkg"
    
    foreach ($item in $items)
    {
         Remove-RoslynAnalyzers $item.FullName $packageName | out-null
    }
    
  10. davidfowl commented on Feb 14, 2018

    @davidfowl
    Member

    @rrelyea I see this is both a pri 0 and on the backlog. Any chance we can re-triage this?

  11. added
    Priority:2
    and removed
    Priority:1
    on Jun 29, 2018
  12. nguerrera commented on Aug 3, 2018

    @nguerrera
    Author

    See also dotnet/sdk#968 (comment) that notes that PrivateAssets="all" can prevent analyzers from flowing, if you can afford to prevent all other package assets from flowing.

  13. 59 remaining items

  14. MeikelLP commented on Jul 25, 2024

    @MeikelLP

    Still unable to exclude analyzers after 7 years...

  15. Drmorph commented on Aug 8, 2024

    @Drmorph

    if someone search a hack to be able to do something like ExcludeAssets=analyzers (in my case StyleCop.Analyzers) , i found an old tweet https://twitter.com/Nick_Craver/status/1173996405276467202?s=09 who help me a lot to do it , added this on my .csproj

      <Target Name="DisableStaticAnalysis" BeforeTargets="CoreCompile" Condition="$(RunStaticAnalysis) == '' OR $(RunStaticAnalysis) == 'false'">
        <ItemGroup>
          <Analyzer Remove="@(Analyzer)" Condition="%(Filename) == 'StyleCop.Analyzers'" />
        </ItemGroup>
      </Target>
  16. dominikjeske commented on Oct 4, 2024

    @dominikjeske

    Any updates on this?

  17. davidebbo commented on Oct 31, 2024

    @davidebbo

    That seems like a really bad issue, because those analyzers end up being viral. In my scenario:

    • I have a Project Foo, which uses package UFX.Relay, which uses Package Nerdbank.Streams
    • Nerdbank.Streams depends on several Microsoft.VisualStudio.* analyzer packages
    • There are dozens of other projects in my solution that depend on Project Foo.

    And the end result is that every single project in my solution gets those unwanted analyzers.

    Is there a way to disable them globally in the solution? Doing it at project level is quite painful...

  18. AArnott commented on Oct 31, 2024

    @AArnott
    Contributor

    @davidebbo Yes, just add an .editorconfig file in the root of your solution and set the severity for each analyzer to None that you don't want to see.

  19. davidebbo commented on Nov 1, 2024

    @davidebbo

    @AArnott ah yes, that's a much better solution, thanks!

    e.g. (but you don't need to manually edit it if you do it through the designer):

    dotnet_diagnostic.VSTHRD003.severity = none
    dotnet_diagnostic.VSTHRD002.severity = none
    dotnet_diagnostic.VSTHRD103.severity = none
    dotnet_diagnostic.VSTHRD110.severity = none
    dotnet_diagnostic.VSTHRD011.severity = none
    
  20. dominikjeske commented on Nov 2, 2024

    @dominikjeske

    yes but this is not fixing the problem but rather covering up the problem - when someone is developing nuget package there should be readme with info - just add .editorconfig to hide warnings from analyzers you are not expecting :)

  21. davidebbo commented on Nov 2, 2024

    @davidebbo

    yes but this is not fixing the problem but rather covering up the problem - when someone is developing nuget package there should be readme with info - just add .editorconfig to hide warnings from analyzers you are not expecting :)

    Of course, it's just a workaround. Fixing the underlying issue is still very much needed. This is just to unblock...

  22. self-assigned this
    on Jul 1, 2025
  23. added this to the 7.12 milestone on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area:AnalyzersCategory:SeasonOfGivingFunctionality:RestorePartner:DotNetPriority:2Style:PackageReferenceType:Feature

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions