GitVersion: GitVersionTask does not work correctly with fsharp projects building an exe.

When adding the GitVersionTask to a fsharp fsproj that build an exe one get this error message when running dotnet build

C:\Users\...\repos\reproducegvdn\Program.fs(3,1): error FS0222: Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'. Only the last source file of an application may omit such a declaration. [C:\Users\...\repos\reproducegvdn\reproducegvdn.fsproj]

My best guess is that the generated GitVersionInformation.fs file is placed last in the list of files to compile. The Program.fs containing the <EntryPoint> decorated main function must always be last. See below for details.

Shortest steps to reproduce:

md reproducebug
cd reproducebug
dotnet new console -lang f#
dotnet add package gitversiontask
git init
git add -A
git commit -m "Initial commit"
dotnet build

If one run with dotnet build --verbosity detailed one can find the following input to the fsc compiler (redacted for clarity). Notice the order of the files in the end.

C:\Program Files\dotnet\dotnet.exe "C:\Program Files\dotnet\sdk\2.2.300\FSharp\fsc.exe" -o:obj\Debug\netcoreapp2.2\reproducegvdn.dll
         -g
         --debug:portable
         --noframework
         --define:TRACE
         --define:DEBUG
         --define:NETCOREAPP
         --define:NETCOREAPP2_2
         --optimize-
         --tailcalls-
         ...
         --target:exe
         --warn:3
         --warnaserror:3239,76
         --fullpaths
         --flaterrors
         --highentropyva-
         --targetprofile:netcore
         --nocopyfsharpcore
         --simpleresolution
         C:\Users\...\AppData\Local\Temp\.NETCoreApp,Version=v2.2.AssemblyAttributes.fs
         obj\Debug\netcoreapp2.2\reproducegvdn.AssemblyInfo.fs
         Program.fs
         obj\Debug\netcoreapp2.2\AssemblyInfo_reproducegvdn_da4sk0i1.nf4.g.fs
         obj\Debug\netcoreapp2.2\GitVersionInformation_reproducegvdn_woodujve.ebf.g.fs

Same error with both SDK 2.2.300 and 3.0.100

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (15 by maintainers)

Most upvoted comments

Perhaps the presence of AssemblyVersionAttribute and friends is something we can add a test for by producing an assembly with GitVersion, then loading that assembly and reading its version number?

Ok, some searching tells me this is a F# thing. https://github.com/dotnet/fsharp/blob/master/src/fsharp/FSharp.Build/Microsoft.FSharp.Targets I will try to create a PR with a conditional on the inclusion. Have no clue on how to write a test for it tho.

Keep in mind that this dialog doesn’t show the actual attribute values. .NET assemblies carry some Windows specific resources, which is what the “Properties” dialog shows.

You can check the assembly with the IL Disassembler. If it has the AssemblyVersionAttribute and friends set to the correct values, we’re good.

Yeah, I’ve got some ideas. I’ll get back with them. Hopefully in a PR.

Den tors 31 okt. 2019 22:51Asbjørn Ulsberg notifications@github.com skrev:

That’s great, @da9l https://github.com/da9l! Is there something that can be adjusted in GitVersion to make this experience better out of the box? If you have ideas, we would love a PR.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/GitTools/GitVersion/issues/1831?email_source=notifications&email_token=AABD23JQM2CK2TUSB36EBC3QRNHOVA5CNFSM4I3FI562YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECZLSUI#issuecomment-548583761, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABD23PU2KNBCGXRWM57P2LQRNHOVANCNFSM4I3FI56Q .

Yay, I found the cause of the problem and a workaround! in GitVersionTask.targets UpdateAssemblyInfo and GenerateGitVersionInformation do this:

<ItemGroup>
     <Compile Include="$(AssemblyInfoTempFilePath)" />
</ItemGroup>

<!-- and -->
<ItemGroup>
    <Compile Include="$(GitVersionInformationFilePath)" />
    <FileWrites Include="$(GitVersionInformationFilePath)" />
</ItemGroup>

This appends these two files to the list of files to be compiled. Since F# requires that the [<EntryPoint>] marked expression (in this case in Program.fs) is the last one that is compiled this build will break.

Workaround We found a workaround: Put the Include Program.fs into a task that is guaranteed to be executed after GitVersionInformationFilePath and UpdateAssemblyInfo. Remove the existing Program.fs inclusion and include it again. This makes Program.fs to end up as the last file to be compiled. This seems to be working ok in Visual Studio 2019 but perhaps not in Visual Studio Code (it doesn’t show Program.fs in the right place)

  <ItemGroup>
    <Compile Include="SomeFunctions.fs" />
    <Compile Include="Program.fs" />
  </ItemGroup>

  <Target Name="GitVersionInfoBeforeEntryPointFileWorkaround"
          AfterTargets="GenerateGitVersionInformation;UpdateAssemblyInfo"
          DependsOnTargets="GenerateGitVersionInformation;UpdateAssemblyInfo">
    <ItemGroup>
      <Compile Remove="Program.fs" />
      <Compile Include="Program.fs" />
    </ItemGroup>
  </Target>