How to fix the CA0053 error in Code Analysis in Visual Studio 2012

Background

You are opening a solution made in Visual Studio 2010 with VS 2012.  When you run Code Analysis you get a series of CA0053 errors, saying it is unable to load the rule sets from the Visual Studio 2010 directory!

In the Error window you get an error message saying “Code Analysis detected errors.”

image

And in the Code Analysis window you will get the “CA0053 Error running code analysis” with its “Unable to load rule assembly”.

clip_image001

This means the project is bound to Visual Studio 2010, where it should have been Visual Studio version independent. The absolute path to the Visual Studio 10 binaries is what causes the problem.

What has happened was that this certain VS2010 specific information was “inadvertently” introduced in SP1, and it has not been fixed in any later update.

It does NOT happen if you run in Debug|x86 or Release|x86, but any other configuration you have added will give you this error.

Fixing the error

The error must be fixed by changing the project files.  If you have only a few projects in your solution, you can fix them manually.  If you have a lot of projects in your solution, I have uploaded a small tool to help in the process.  After the fix, the projects will work both in VS 2010 and in VS 2012 with no side effects.

The error seems to be introduced only when a new configuration is created.  Subsequent changes to an existing configuration doesn’t seem to reintroduce the error.  So the fixes below will stay fixed.  Be aware that if anyone adds a new configuration using VS 2010, the error will be introduced again for that new configuration.  Then run the fix again.

Update:  It has been reported that also other changes you do (not identified which yet) may revert the change, see for example this blogpost.

The bug is fixed in VS2012 however, so if you use VS 2012 to add new configuration, all will be well, and those configurations will be backward compatible with VS 2010.

Manual fix

Open the project file in an editor.

You do this by right click the project in the solution editor and either choose “Edit Project File” or “Unload Project” followed by “Edit” afterwards.

Now find all occurrences of the string “Microsoft Visual Studio 10.0

They should all be in two fields, named <CodeAnalysisRuleDirectories> and <CodeAnalysisRuleSetDirectories>. 

Alternative 1:  The very safe way

Replace the content to be as follows:

<CodeAnalysisRuleDirectories>$(DevEnvDir)….Team ToolsStatic Analysis ToolsFxCopRules</CodeAnalysisRuleDirectories>

<CodeAnalysisRuleSetDirectories>$(DevEnvDir)….Team ToolsStatic Analysis ToolsRule Sets</CodeAnalysisRuleSetDirectories>

Alternative 2: Which seems to work

Simply delete the fields completely. 

It seems to work in the conditions I have tested, but if you should get into a condition where it doesn’t work, go back to alternative 1 for that case.  And please, send me a note, I would like to know.

(I noted this blogpost by Alun Jones , where he deleted only the content of the field, that should also work)

PS:  Why not use Find/Replace:  You can try, but the full content of the fields will differ dependent upon what operating system you run, x86 or x64, and if both have been used, for example because your team’s developers have both, all of these paths will be written into these fields.  That is at least 4 permutations.  A tool is better!

 

Automatic fix using the FixCA0053 tool

The tool can be downloaded from Visual Studio Gallery here.   Place it somewhere easy to find.

Open a command line window, and go down to the top directory of your project. 

Then simply call the program from there.

The program will locate all the csproj files from that location and in all subdirectories of that location, and change the fields as in Alternative 1 above.

The tool will output how many files it fixed, and how many was skipped because the error was not there. 

image

TIP

If it tells you it could not write to some files, you have probably forgot to check them out.  To check out ONLY the csproj files (In Solution Explorer it will check out all files, you may not want that), use the Source Control Explorer, go to the location where you want to start, right click and choose Find in Source control/Wildcard.  Enter the Wildcard “*.csproj” and check Recursive.  In the next window coming up, you can select all the files, right click and choose “Check out”.

Source code

The source code can be found at https://github.com/OsirisTerje/FixCA0053 

About terje

See http://about.me/terjes