但它是:'EntryPointAttribute' 属性必须是最后一个文件中的最后一个声明

BUT IT IS: 'EntryPointAttribute' attribute must be the last declaration in the last file

我真的很讨厌问这样一个愚蠢的问题,但我的挫败感已经沸腾了。我有 3 个文件,顺序如下:

Work.fs
WorkTests.fs
Main.fs

这是全部 Main.fs

module Main 
open Work

    [<EntryPoint>]
    let main args =
        let p = work "some data"
        printfn "p : %A" p
        0

Work 和 WorkTests (xunit) 首先出现,通过后我添加了 Main.fs

我仍然遇到最后一个 declaration/file 错误。

我尝试了很多方法,但都无济于事。

例如,如果我省略“module Main”,我会得到“只有最后一个源文件......可能会省略这样的声明”

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
        <OutputType>Exe</OutputType>
        <IsPackable>false</IsPackable>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <Tailcalls>true</Tailcalls>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
        <PackageReference Include="xunit" Version="2.4.1" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
        <PackageReference Include="FsUnit.xUnit" Version="4.0.4" />
    </ItemGroup>

    <ItemGroup>
        <Compile Include="Work.fs" />
        <Compile Include="WorkTests.fs" />
        <Compile Include="Main.fs" />
    </ItemGroup>

</Project>

我能够重现该问题。唯一修复它的方法是将 Main.fs 的名称更改为 Program.fsProgram.fs 以外的任何名称都有同样的问题。但我发现如果你删除 Microsoft.NET.Test.Sdk 包构建工作,但只有在 VS 中构建两次之后。离奇。我还不知道为什么会这样。


更新:

当您将名称更改为 Program.fs 时,会出现另一个构建警告:A 'Program.fs' file can be automatically generated for F# .NET Core test projects. To fix this warning, either delete the file from the project, or set the <GenerateProgramFile> property to 'false'.

<GenerateProgramFile>false</GenerateProgramFile> 添加到 FSPROJ 可修复此警告并允许您使用任何文件名。