Search code examples
visual-studio-2017teamcityspecflowmsbuild-task

SpecFlow/MSBuild Codebehind Gen - Generate All task


On a project I am working on, I am maintaining some Feature Tests written in SpecFlow. Our team started using Visual Studio 2017 about a year ago, and we finally got around to doing some upkeep on our tests!

Our tests for the project I'm working on were originally written in SpecFlow 2.3.2, and were last updated in Visual Studio 2015.

The SpecFlowSingleFileGenerator is known to not work on VS 2017, so I spent the better part of yesterday changing our suite to use the MSBuildSingleFileGenerator instead as detailed in this article in SpecFlow's official documentation

Problem:
Locally, I can build my solution, including the Feature Test project just fine. However, I keep getting the following error when I try to build the project on our build server:

[exec]  C:\CheckoutDirectory\My Awesome Project\packages\SpecFlow.Tools.MsBuild.Generation.2.3.2\build\SpecFlow.Tools.MsBuild.Generation.targets(45,5): 
error MSB4036: The "GenerateAll" task was not found. 
Check the following: 
  1.) The name of the task in the project file is the same as the name of the task class. 
  2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 
  3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin" directory. [C:\CheckoutDirectory\My Awesome Project\AwesomeProject.FeatureTest\AwesomeProject.FeatureTest.csproj]

I should point out that our team has no experience with writing MS Build tasks, as hitherto we haven't needed to; we use NAnt build scripts on TeamCity to manage our build work. It's clear that error message would be helpful...if we knew literally anything about it.

Now, normally the correct answer would be: Google it. I did that, and this specific error has no pertinent results.

Additionally, this is blocking my team, since we need our build to work. I don't have the time to do the research and education necessary to properly understand how MS Build technology works. That wil have to come later.

Question:
Bearing in mind that SpecFlow has broken our process, and our team's lack of knowledge about the MS Build system: I need to know how to get around the "GenerateAll" task was not found error. What do I do to get around it?

Secondary Question:
I'm also open to lateral thinking. Is there some way to hack either VS 2017 or SpecFlow to make the SpecFlowSingleFileGenerator "compatible" with each other? The objective here is NOT to avoid making changes, but to control the changes. I need a path towards transitioning from the old file generator to the MS build generation system.

Additional Information: So, I did some digging, and I found a place where "GenerateAll" is being called in the SpecFlow.Tools.MsBuild.Generation.targets file:

<Target Name="UpdateFeatureFilesInProject"  
        DependsOnTargets="BeforeUpdateFeatureFilesInProject"  
        Inputs="@(SpecFlowFeatureFiles)" Outputs="@(SpecFlowFeatureFiles->'%(RelativeDir)\%(Filename).feature.cs')">
  <GenerateAll
    ShowTrace="$(ShowTrace)"

    BuildServerMode="$(BuildServerMode)"
    OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"

    ProjectPath="$(MSBuildProjectFullPath)"
    ForceGeneration="$(ForceGeneration)"
    VerboseOutput="$(VerboseOutput)"
    DebugTask="$(SpecFlow_DebugMSBuildTask)" 
    >
    <Output TaskParameter="GeneratedFiles" ItemName="SpecFlowGeneratedFiles" />
  </GenerateAll>
</Target>

Because I've confirmed that this is being copied out to the build server, the situation is yet more mysterious. It appears that the NuGet package is being pulled down faithfully. Therefore, I can't figure out why my local copy is behaving differently than the copy on the build server.


Solution

  • I am not sure where you found this statement:

    The SpecFlowSingleFileGenerator is known to not work on VS 2017

    The SpecFlowSingleFileGenerator is working in VS2015, VS2017 and VS2019. We see it as a legacy feature, but it's still there. Since some weeks it is disabled by default, but you can enable it in the options.

    enter image description here

    It works for SpecFlow >= 2.3.2 and 2.4. For SpecFlow 3 you have to use the MSBuild integration. There are some problems with older versions of SpecFlow, but with them it can also work. It depends on your setup.


    About your MSBuild error:

    The MSBuild Task for SpecFlow < 3.0 is in the specflow.exe. Is it on your build server? It is part of the SpecFlow NuGet packages. Normally you get this kind of error if MSBuild can't find the assembly where the task is.

    For "debugging" problems with MSBuild, I can highly recommend to use the MSBuild Structured Log Viewer (http://msbuildlog.com/). With it, it makes it easy so see what is happening in your build.

    We have an example for MSBuild Code- Behind- Generation with SpecFlow 2.3.2 here: https://github.com/techtalk/SpecFlow-Examples/tree/master/MSBuild/OldCSProj_SpecFlow232

    You could compare your project with this example.


    Full disclosure: I am one of the maintainers of SpecFlow.