Azure / Azure-Functions

1.12k stars 199 forks source link

Unable to run Static Code Analysis due to trailing /bin directory changes #539

Open amazinmoe opened 7 years ago

amazinmoe commented 7 years ago

I tried to enable static code analysis for an Azure Function project but its failing due to the additional bin folder in the output path. Static Code Analysis is looking for the assemblies one directory up since I believe its using TargetPath directory which isn't updated to include the additional bin folder. Do you know if there's a workaround for this?

I enabled Static Code Analysis by editing the project file like so:

Azure Functions Project File `

net461 true ExtendedDesignGuidelineRules.ruleset ` Visual Studio 2017 v15.3.4 Error: Running Code Analysis... 1> * Could not load file: 'bin\Debug\net461\xxxxx.dll'. 1> * Analysis was not performed; at least one valid rules assembly and one valid 1> target file must be specified. 1> * 1 total analysis engine exceptions. 1> MSBUILD : error : CA0055 : * Could not load file: 'bin\Debug\net461\xxxxx.dll'. 1> MSBUILD : error : CA0052 : No targets were selected.
amazinmoe commented 7 years ago

I've experimented with running a PostBuild event which runs Code Analysis using FxCopCmd.exe It works locally & outputs the log.xml file but on the VSTS build it doesn't output the issues as Warnings so I don't see them in the Issues tab once the build completes.

Here's the PostBuild event if EXIST "c:\program files (x86)\microsoft visual studio\2017\enterprise\team tools\static analysis tools\FxCop\FxCopCmd.exe" ( echo "Found FxCopCmd.exe in c:\program files (x86)\microsoft visual studio\2017\enterprise\team tools\static analysis tools\FxCop\" "c:\program files (x86)\microsoft visual studio\2017\enterprise\team tools\static analysis tools\FxCop\FxCopCmd.exe"  /outputCulture:1033 /out:"$(OutDir)bin\$(TargetFileName).CodeAnalysisLog.xml" /file:"$(OutDir)\bin\$(TargetFileName)" /searchgac /ignoreinvalidtargets /forceoutput /successfile /ignoregeneratedcode /saveMessagesToReport:Active /timeout:120 /reportMissingIndirectAssemblies )   if EXIST "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop\FxCopCmd.exe" ( echo "Found FxCopCmd.exe in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop\" "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop\FxCopCmd.exe"  /outputCulture:1033 /out:"$(OutDir)bin\$(TargetFileName).CodeAnalysisLog.xml" /file:"$(OutDir)\bin\$(TargetFileName)" /searchgac /ignoreinvalidtargets /forceoutput /successfile /ignoregeneratedcode /saveMessagesToReport:Active /timeout:120 /reportMissingIndirectAssemblies ) Looking through the build framework there's a step to copy the .pdb to the bin folder PostBuild, if the API can be copied PostBuild as well I believe it will resolve this issue.

I made the following changes to Microsoft.NET.Sdk.Functions.build.targets which resolved the issue for me locally but I'd like to see if they can be added to the official Nuget package so it doesn't fail on the VSTS build server.

Added another Move task for the target assembly. `<Target Name="_GenerateFunctionsPostBuild" AfterTargets="Build">

<!-- TODO: CopyFilesToOutputDirectory does not look at the outdir to copy the pdbs. hence copying it manually. -->
<!-- Copy the application pdb to the bin folder-->
<Move SourceFiles="$(TargetDir)$(TargetName).pdb"
  DestinationFiles="$(TargetDir)bin\$(TargetName).pdb"
  OverwriteReadOnlyFiles="true" 
  Condition="Exists('$(TargetDir)$(TargetName).pdb')" />

<Move SourceFiles="$(TargetDir)$(TargetName).dll"
  DestinationFiles="$(TargetDir)bin\$(TargetName).dll"
  OverwriteReadOnlyFiles="true" 
  Condition="Exists('$(TargetDir)$(TargetName).dll')" />

<BuildFunctions
  TargetPath="$(TargetPath)"
  OutputPath="$(TargetDir)"/>

`

Removed \bin from OutDir property in _FunctionsBeforeCopyFilesToOutputDirectory section `<Target Name="_FunctionsBeforeCopyFilesToOutputDirectory" BeforeTargets="CopyFilesToOutputDirectory">

$(OutDir) $(OutDir)

`

ColbyTresness commented 5 years ago

@soninaren

kpvnreddy commented 5 years ago

I am also facing exact issue. Do we have any solution for this? Please suggest

ColbyTresness commented 5 years ago

@soninaren ping