locked
How to run code analysis in a Build RRS feed

  • Question

  • Hello!

    We want to run code analysis in our nightly build. If there are warnings, we want the build not to fail, but to partially succeed.

    I've been spending 2 full days now on this problem, but I'm not getting anywhere, so I hope someone can help me in the right direction!

    We are using VS2008 en TFS2008. This build script compiles six solutions, each with some projects under it. We want almost all standard rules to be applied, only some of them we want to be excluded.

    The most easy option would be to do code analysis at compile time during the build. Just set the RunCodeAnalysis property to Always in the build script, and the build will do the rest, and even take care of the rules that we've excluded on a per-project basis. BUT: the build would still succeed, even if there are warnings, so that's not a solution for our problem. And if I 'treat warnings as errors', the build will completely fail. And we want it to partially succeed! So I think we can't use this easy option and have to set RunCodeAnalysis to Never :-(

    To accomplish a partially succeeded build in case there are code analysis warnings, I think we have to run code analysis in a custom target, after compilation. Am I right? In my opinion there are multiple options to do that, and I've tried 3 of them: (1) run FxCop from the SDC Tasks library, (2) run FxCop from the MSBuild Extension Pack or (3) execute the FxCop exe itself. I've given all three possibilities a try, but I can't get them to work, each with different problems.

    It might be a little too much to describe each of the problems I encountered, but the problems include: I can't pass the exact rules we've set for each project to the target; I can't get the library tasks to take all the compiled dll's as input; I have no fxcop file that I can use as an input for the library tasks; I can't pass the (custom) FxCop executable path to the SDC task; I can't get FxCop to run for each solution in the build script etc etc.

    Does someone have a solution for our problem? All help is appreciated very much!

    Thank you very much in advance!

    Kind greetings,

    Jeroen

    Thursday, April 8, 2010 3:56 PM

Answers

  • Couldnt you simply just run the code analysis in the build and then, before publishing the buildresult, check for contents of the codeanalysis logfiles and set the buildstatus to partially succeded with the SetBuildPropertiestask like this

    < Target Name = "AfterDropBuild "> < SetBuildProperties TeamFoundationServerUrl = "$(TeamFoundationServerUrl) " BuildUri = "$(BuildUri) " Status = "Part iallySucceded" /> </ Target >

    You can read more about the SetBuildPropertiesTask at http://msdn.microsoft.com/en-us/library/bb399143.aspx

     

     

    Saturday, April 10, 2010 10:10 PM

All replies

  • Couldnt you simply just run the code analysis in the build and then, before publishing the buildresult, check for contents of the codeanalysis logfiles and set the buildstatus to partially succeded with the SetBuildPropertiestask like this

    < Target Name = "AfterDropBuild "> < SetBuildProperties TeamFoundationServerUrl = "$(TeamFoundationServerUrl) " BuildUri = "$(BuildUri) " Status = "Part iallySucceded" /> </ Target >

    You can read more about the SetBuildPropertiesTask at http://msdn.microsoft.com/en-us/library/bb399143.aspx

     

     

    Saturday, April 10, 2010 10:10 PM
  • Hi Mattias,

    Thank you very much! Your answer could very well be the solution to my problem! I will try to implement it within the next few days. Right now, I have two questions about your solution:

    1. What do you mean by 'check for the contents of the codeanalysis logfiles', what do I have to look for and how would I do that?
    2. How do I distinguish code analysis compile errors from normal compile errors? If there are also normal compile errors, I want the build to fail. If there are ONLY code analysis errors, I want it to partially succeed. So I don't want to set the build result simply ALWAYS to partially succeeded.

    I hope someone finds the time to explain some more. I'm just beginning to understand the world of build scripts, I hope I'm not asking too novice questions...

    Kind greetings,

    Jeroen

    Monday, April 12, 2010 12:07 PM
  • If we start with question #2 How to distinguish static code analysis errors from normal compile errors.

    First all SCA  errors and warnings have uniqu a ruleidentification, second SCA is generated much later in the build process, by running fxcop on the assemblies. During this the result of the SCA run can be stored into xml logfiles.

    Question #1, I dont have the correct anwer without looking at the buiold and doing some digging. But I would look for files in the build or output folder. I think their is a property specifiing the path for XMl reports and logfiles used by FxCop in TeamBuilds default SCA handling.

    Monday, April 12, 2010 10:57 PM
  • The only thing I could come up is to use the MSBuild extensions File class, with which you can select files based on their contents:

    <MSBuild.ExtensionPack.FileSystem.File TaskAction="FilterByContent" RegexPattern="warning : CA" Files="$(BuildDirectory)\BuildType\BuildLog.txt">

    I could use the task's output parameter IncludedFileCount: if that =1, there are warnings; if that =0, there are no code analysis warnings. Nice idea, I thought myself.

    BUT: I get the following error in the build: E:\...\x.proj(145,3): error : IOException: The process cannot access the file 'E:\...\BuildType\ErrorsWarningsLog.txt' because it is being used by another process.

    Now, I'm completely out of ideas! What can I do?! All help is still appreciated!

    Tuesday, April 13, 2010 3:58 PM
  • I dont get it, you search in the BuildLog.txt and you get an error in ErrorsWarningsLog.txt ?
    The buildlog is by the way not located in the Buildtype directory, you should find it in the drop location depending on witch target your overrides.

    Tuesday, April 13, 2010 6:45 PM
  • Yeah, I mixed up a few copy-pastes in my reply, sorry for the confusion. But that's because I've already tried a lot of combinations:

    • From the BuildType directory I tried to read the ErrorsWarningsLog.txt as well as the BuildLog.txt (that file IS also located there, in our case!). In these cases I get the 'in use by another process' error.
    • From the DropLocation I tried to read the BuildLog.txt, ErrorsWarningsLog.txt and Release.txt. But in those cases, I get a 'could not find file' error, even in the AfterDropBuild target!
    • I've tried to do the searching of the file in the AfterCompile, AfterDropBuild and even BeforeOnBuildBreak targets (as late as possible?), but it seems that the files are locked till the end of the build and I can't get access to it from the build script.

    I'm out of ideas of how to find out the results of the code analysis! :-(

    Wednesday, April 14, 2010 7:54 AM
  • Were you able to get a solution for your problem, coz even I am trying to extract the result of the code analysis from the build output.

     


    Simi
    Wednesday, July 14, 2010 11:19 AM