-
Notifications
You must be signed in to change notification settings - Fork 25
dotnet test fails at runtime when module values are used in tests (xunit/mstest) #79
Comments
@neoeinstein just to be sure. if i add a If is like that, i just need to update the dotnet new templates i think |
@enricosada That seems to be correct. To be safe, nothing should be in the entry module other than main. To get rid of the "empty main" warning, I add a no-warn directive. My complete module Program = let [<EntryPoint>] main _ = 0
#nowarn "988" |
good @neoeinstein i'll update these asap. i hope we are in time for rtm (dunno about that). |
So I was thinking about updating the built-in testing templates shipped with dotnet/templating by adding a Program.fs file with the entry point, but in doing so I began to have doubts about the way Compile items are handled in the .fsproj file. Currently a wildcard is used to include all .fs files:
I suppose the assumption was that all .fs files would be independent of each other such that compilation order would not matter. With the introduction of a Program.fs file this assumption begins to break down if any other file is alphabetically below the Program.fs file. While this particular issue can be mitigated like so:
I wonder if it might not be best to just use explicit file ordering for all files and not make assumptions about file independence:
Does anyone have any objections to this? |
@lambdakris I think the explicit (no wildcards) is better. |
OK, pr with changes (incl both mstest & xunit, both 1.x & 2.0) has submitted and been merged |
Awesome, thx @lambdakris ! Closing, fixed with dotnet/templating@934a6e8 |
@lambdakris if you want another nice and easy thing to fix, there is #93 (comment) :D |
Removing Program.fs yields the following warning when you compile against netcoreapp (all versions)
Including Program.fs but changing the TargetFramework to net452 yields the following error
What I had to do to support targeting both new & old and not generate warnings or errors was conditionally include Program.fs <ItemGroup Condition="'$(TargetFramework)' != 'net452'">
<Compile Include="Program.fs" />
</ItemGroup> Hopefully this will save someone out there the hours of time I lost trying to get the dotnet cli to work on nuget packages that are made available to both full .NET Framework as well as .NET Standard. @enricosada Perhaps a similar conditional can be added to the template to better support FSharp testing from the dotnet cli for multiplatform projects. I'm sure a better conditional could be included this was just all I needed for my particular use case. |
I just came across this warning and it led me to this particular issue. I would like to say that I appreciate the template being adjusted, but honestly the first thing that I did after Is there way to get around this warning without adding a placeholder EntryPoint? All I want to do is test some stuff anyways. |
AFAIK with .NET Core SDK 2.1.400 you no longer need the entrypoint but you do with the earlier versions. |
@buvinghausen I'm on .NET Core SDK 2.1.403 and I'm still seeing the same warning.
|
@samuela hmm I'm not sure I know when the 2.1.400 SDK dropped I was able to remove it from my F# Nuget repository. You can see on this commit I was able to remove the Program.fs as well as the conditional include from the fsproj file. buvinghausen/Swashbuckle.NodaTime.AspNetCore@6061d45 Do you have Microsoft.NET.Test.Sdk 15.8.0 referenced in your test fsproj? |
@buvinghausen That did the trick! Thank you so much! I was on Microsoft.NET.Test.Sdk 15.7.0 and upgrading to 15.8.0 fixed it. |
This is the SDK match to dotnet/fsharp#1371.
The issue reported here has been around for a long time, but has only become an issue with .NET Core moving to produce
Exe
output and targetnetcoreapp1.x
. Under the new scheme, if your entire test consists of one file containing the following:This test will fail at runtime with a
NullReferenceException
, asmyValue
will never be set.fsc
creates an implicitmain
function which would do the initialization, but when running tests,main
is never called. As a workaround, I have been adding a dummyProgram.fs
which movesmyValue
's initialization to a static constructor:This workaround is non-intuitive, and will catch those creating tests for .NET Core F# projects off guard.
A related
.fsproj
for your enjoyment:Attempting to set the
OutputType
toLibrary
results in the Test SDK bits not being placed in the correct place, giving an error "Could not find testhost.dll". Downgrading theTargetFramework
tonetcoreapp1.0
has no effect.The text was updated successfully, but these errors were encountered: