jamescourtney / FlatSharp

Fast, idiomatic C# implementation of Flatbuffers
Apache License 2.0
511 stars 51 forks source link

Failing to build FBS schema into VStudio and by dotnet #143

Closed MaxDZ8 closed 3 years ago

MaxDZ8 commented 3 years ago

Hello, thank you for your efforts. I am sold on this library and I'm using it. I am having difficulty running the build-time FBS schema conversion.

According to wiki, my project would only need to reference FlatSharp.Compiler and FlatSharp.Runtime. My .csproj is kinda like this:

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

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
    <Version>0.0.1</Version>
  </PropertyGroup>

  <ItemGroup>
    <FlatSharpSchema Include="my_schema.fbs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="FlatSharp.Compiler" Version="5.2.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="FlatSharp.Runtime" Version="5.2.0" />
  </ItemGroup>

</Project>

It looks quite close to me but if I build from Visual studio:

Build started...
1>------ Build started: Project: MyProject, Configuration: Debug Any CPU ------
1>net5.0 .NETCoreApp: dotnet C:\Users\Massimo\.nuget\packages\flatsharp.compiler\5.2.0\tools\net5.0\FlatSharp.Compiler.dll --nullable-annotations true --input "C:\snip\MyProject\my_schema.fbs" --output obj\Debug\net5.0\
1>C:\snip\MyProject\Framer.cs(72,83,72,103): error CS0103: The name 'FlatBufferSerializer' does not exist in the current context
1>C:\snip\MyProject\Framer.cs(82,84,82,104): error CS0103: The name 'FlatBufferSerializer' does not exist in the current context
1>C:\snip\MyProject\Framer.cs(88,28,88,48): error CS0103: The name 'FlatBufferSerializer' does not exist in the current context
1>C:\snip\MyProject\Framer.cs(91,24,91,44): error CS0103: The name 'FlatBufferSerializer' does not exist in the current context
1>Done building project "MyProject.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The same happens if I try dotnet build (the messages are localized but that's it).

I originally used Visual Studio 2019 16.8.2 but I figured it might have been a version issue so I updated to 16.9.4. It made no difference.

I tried fiddling with it. It seems adding the FlatSharp package fixes the issue, likely due to the depandancy Microsoft.CodeAnalysis.CSharp.Workspaces but that makes me slightly uncomfortable as the dependancies make it to publish.

I'm trying to convince I can live with the extra files, the amount of storage isn't much of a big deal. Is this as intended?

jamescourtney commented 3 years ago

Hey, thanks for the kind words!

As you've discovered, FlatBufferSerializer doesn't exist in the packages you've referenced. FlatBufferSerializer is the runtime serializer for people who just use attributes and not fbs files (in effect, the FlatSharp.Compiler package invokes FlatBufferSerializer at build time so you don't have to carry all of those dependencies).

Assuming your fbs file has some table with fs_serializer set, you can just do SomeType.Serializer to access the type's serializer. This sample should help. Let me know if you have more questions!

MaxDZ8 commented 3 years ago

I forgot about that detail.

I still have a question however: shall I do a PR updating this info into the wiki? It seems to me this should be mentioned with emphasis.

jamescourtney commented 3 years ago

Sure -- I'm open to all contributions. I should make a pass through the wiki and link to relevant samples from those wiki pages.

jamescourtney commented 3 years ago

Closing this now; feel free to open a PR to address any oversights in the wiki. Thanks again for using FlatSharp!