TypeLoadException with .NET Core RC4 and malformed stylecop.json #2290

Closed
fubar-coder opened this Issue Feb 9, 2017 · 17 comments

Comments

Projects
None yet
@fubar-coder

I added StyleCop.Analyzers to my project and managed to manually add a ruleset file to the csproj.

Creating the project

md test
cd test
dotnet new console

The ruleset file

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="WebDAV-Server Ruleset" Description=" Rules for the WebDAV-Server project" ToolsVersion="15.0">
  <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
    <Rule Id="SA0001" Action="None" />
    <Rule Id="SA1101" Action="None" />
    <Rule Id="SA1300" Action="None" />
    <Rule Id="SA1309" Action="None" />
    <Rule Id="SA1400" Action="None" />
    <Rule Id="SA1503" Action="None" />
    <Rule Id="SA1600" Action="None" />
    <Rule Id="SA1633" Action="None" />
  </Rules>
</RuleSet>

The project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <CodeAnalysisRuleSet>test.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

  <ItemGroup>
    <AdditionalFiles Include="stylecop.json" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta001">
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

The stylecop.json file

This file is malformed, because comments aren't allowed in the project.json file.

{
  // ACTION REQUIRED: This file was automatically added to your project, but it
  // will not take effect until additional steps are taken to enable it. See the
  // following page for additional information:
  //
  // https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md

  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "documentationRules": {
      "documentInternalElements": false
    },
    "orderingRules": {
      "usingDirectivesPlacement": "outsideNamespace"
    }
  }
}

Commands

dotnet restore
dotnet build

Output

Microsoft (R) Build Engine version 15.1.545.13942
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(1,1): warning SA1200: Using directive must appear within a namespace declaration [C:\temp\test\test.csproj]
CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.SpecialRules.SA0002InvalidSettingsFile' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'. [C:\temp\test\test.csproj]
  test -> C:\temp\test\bin\Debug\netcoreapp1.0\test.dll

Build succeeded.

Program.cs(1,1): warning SA1200: Using directive must appear within a namespace declaration [C:\temp\test\test.csproj]
CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.SpecialRules.SA0002InvalidSettingsFile' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'. [C:\temp\test\test.csproj]
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.02

Expected output

Either a good error message that the project.json is invalid or accepting the json file and not throwing the TypeLoadException

Additional information

The error doesn't appear when run from inside VS 2017.

@fubar-coder

This comment has been minimized.

Show comment
Hide comment
@fubar-coder

fubar-coder Feb 9, 2017

Sometimes I also see the following error:

CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.DocumentationRules.SA1642ConstructorSummaryDocumentationMustBeginWithStandardText' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'. [D:\Entwicklung\WebDavServer\FubarDev.WebDavServer\FubarDev.WebDavServer.csproj]

Sometimes I also see the following error:

CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.DocumentationRules.SA1642ConstructorSummaryDocumentationMustBeginWithStandardText' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'. [D:\Entwicklung\WebDavServer\FubarDev.WebDavServer\FubarDev.WebDavServer.csproj]
@martincostello

This comment has been minimized.

Show comment
Hide comment
@martincostello

martincostello Feb 10, 2017

Contributor

I am also getting the above error 100% of the time with the RC3 tooling with 1.0.0 of the analyzers.

Contributor

martincostello commented Feb 10, 2017

I am also getting the above error 100% of the time with the RC3 tooling with 1.0.0 of the analyzers.

@martincostello

This comment has been minimized.

Show comment
Hide comment
@martincostello

martincostello Feb 10, 2017

Contributor

Adding <Rule Id="AD0001" Action="None" /> to the ruleset suppresses the warning and the analyzers seem to be otherwise unaffected and work correctly.

Contributor

martincostello commented Feb 10, 2017

Adding <Rule Id="AD0001" Action="None" /> to the ruleset suppresses the warning and the analyzers seem to be otherwise unaffected and work correctly.

@vweijsters vweijsters added the bug label Feb 15, 2017

@vweijsters

This comment has been minimized.

Show comment
Hide comment
@vweijsters

vweijsters Feb 15, 2017

Contributor

This seems to be an issue with Json.NET. I noticed that we are using quite an old version of it as well. Maybe upgrading Json.NET to 9.0.1 will solve this issue.

Contributor

vweijsters commented Feb 15, 2017

This seems to be an issue with Json.NET. I noticed that we are using quite an old version of it as well. Maybe upgrading Json.NET to 9.0.1 will solve this issue.

@sharwell

This comment has been minimized.

Show comment
Hide comment
@sharwell

sharwell Feb 15, 2017

Member

@vweijsters I expect you are correct regarding cause and solution.

Member

sharwell commented Feb 15, 2017

@vweijsters I expect you are correct regarding cause and solution.

@Romanx

This comment has been minimized.

Show comment
Hide comment
@Romanx

Romanx Mar 5, 2017

Contributor

I've run into this too and i'm not using a ruleset file. I'm just using the stylecop.json

Contributor

Romanx commented Mar 5, 2017

I've run into this too and i'm not using a ruleset file. I'm just using the stylecop.json

@kant2002

This comment has been minimized.

Show comment
Hide comment
@kant2002

kant2002 Mar 6, 2017

I have same issue on Linux as well.
After I update to 1.1.0-beta1 issue gone.

kant2002 commented Mar 6, 2017

I have same issue on Linux as well.
After I update to 1.1.0-beta1 issue gone.

@sharwell

This comment has been minimized.

Show comment
Hide comment
@sharwell

sharwell Mar 9, 2017

Member

@fubar-coder 📝 Comments aren't allowed in strict JSON, but the parser we use allows them.

Member

sharwell commented Mar 9, 2017

@fubar-coder 📝 Comments aren't allowed in strict JSON, but the parser we use allows them.

@ChristophB125

This comment has been minimized.

Show comment
Hide comment
@ChristophB125

ChristophB125 Mar 10, 2017

I get TypeLoadException as well when trying to use stylecop.json. I am using the latest version of the .Net SDK 1.0 that was released with VS2017

I get TypeLoadException as well when trying to use stylecop.json. I am using the latest version of the .Net SDK 1.0 that was released with VS2017

@Dresel

This comment has been minimized.

Show comment
Hide comment
@Dresel

Dresel Mar 10, 2017

Same here, failing when using stylecop.json (without comments) and the SDK from VS2017.

Dresel commented Mar 10, 2017

Same here, failing when using stylecop.json (without comments) and the SDK from VS2017.

@tpluscode

This comment has been minimized.

Show comment
Hide comment
@tpluscode

tpluscode Mar 16, 2017

Similar here, getting a buttload of warnings after migrating to new csproj:

CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.DocumentationRules.FileHeaderAnalyzers' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'.

Similar here, getting a buttload of warnings after migrating to new csproj:

CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.DocumentationRules.FileHeaderAnalyzers' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'.

@AArnott

This comment has been minimized.

Show comment
Hide comment
@AArnott

AArnott Mar 16, 2017

Contributor

It would be great if a 1.0.1 release of StyleCop could include the fix for this.

Contributor

AArnott commented Mar 16, 2017

It would be great if a 1.0.1 release of StyleCop could include the fix for this.

@cdroulers

This comment has been minimized.

Show comment
Hide comment
@cdroulers

cdroulers Mar 16, 2017

Getting the same error with a well formed JSON file.

CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.SpecialRules.SA0002InvalidSettingsFile' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'.

Getting the same error with a well formed JSON file.

CSC : warning AD0001: Analyzer 'StyleCop.Analyzers.SpecialRules.SA0002InvalidSettingsFile' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'System.Runtime.Serialization.Formatters.FormatterAssemblyStyle' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.'.

@vweijsters

This comment has been minimized.

Show comment
Hide comment
@vweijsters

vweijsters Mar 16, 2017

Contributor

Prepared a PR to fix this issue in 1.0.0.
@sharwell can you check if this is enough for a 1.0.1 release?

Contributor

vweijsters commented Mar 16, 2017

Prepared a PR to fix this issue in 1.0.0.
@sharwell can you check if this is enough for a 1.0.1 release?

@vweijsters vweijsters modified the milestones: 1.0.1, 1.1.0 Beta 2 Mar 16, 2017

@cdroulers

This comment has been minimized.

Show comment
Hide comment
@cdroulers

cdroulers Mar 17, 2017

@vweijsters There doesn't seem to be a due date for 1.0.1 or 1.1.0-beta2. When can we expect it?

@vweijsters There doesn't seem to be a due date for 1.0.1 or 1.1.0-beta2. When can we expect it?

@carlos-sarmiento

This comment has been minimized.

Show comment
Hide comment
@carlos-sarmiento

carlos-sarmiento Mar 22, 2017

@sharwell Is there anything we can do to help get this fix out the door? We have a linux build server that cannot enforce our rules because of this error

@sharwell Is there anything we can do to help get this fix out the door? We have a linux build server that cannot enforce our rules because of this error

@sharwell

This comment has been minimized.

Show comment
Hide comment
@sharwell

sharwell May 29, 2017

Member

This should be fixed with 1.0.2.

Member

sharwell commented May 29, 2017

This should be fixed with 1.0.2.

@sharwell sharwell closed this May 29, 2017

@sharwell sharwell added this to the 1.0.2 milestone May 29, 2017

@sharwell sharwell removed this from the 1.1.0 Beta 2 milestone May 29, 2017

@sharwell sharwell added the fixed label May 29, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment