jamescourtney / FlatSharp

Fast, idiomatic C# implementation of Flatbuffers
Apache License 2.0
497 stars 50 forks source link

[Bug] Flatsharp can't compile schemas on seperate drives #390

Closed OptoCloud closed 1 year ago

OptoCloud commented 1 year ago

When trying to compile a schema, flatsharp attempts to get a relative path to that schema from the flatsharp compiler. If the flatsharp compiler exists in the nuget cache and the repository exists on a drive used for development, flatsharp will crash with cryptic error-messages.

This is an error message where these are the conditions:

Flatsharp compiler path: C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\tools\net6.0\FlatSharp.Compiler.exe

Schema path: H:\source\c#\asp.net\backend\schemas\Hello.fbs

Exception message:

FlatSharp.Internal.FlatSharpInternalException: FlatSharp Internal Error! Message = 'expect path to exist: C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\tools\net6.0\..\..\..\..\..\..\..\..\source\c#\asp.net\backend\schemas\Hello.fbs'.File = 'PathHelpers.cs', Member = 'NormalizePathName:27'
    at FlatSharp.Internal.FlatSharpInternal.ThrowAssertFailed(String message, String memberName, String fileName, Int32 lineNumber) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Runtime\FlatSharpInternal.cs:line 44    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at FlatSharp.Compiler.PathHelpers.NormalizePathName(String path) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Compiler\PathHelpers.cs:line 27    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at AbsolutePathSchemaMutator.NormalizePath(String fbsPath) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Compiler\SchemaMutators\AbsolutePathSchemaMutator.cs:line 75    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at AbsolutePathSchemaMutator.Mutate(Schema schema, CompilerOptions options, List`1 postProcessors) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Compiler\SchemaMutators\AbsolutePathSchemaMutator.cs:line 41    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at FlatSharp.Compiler.FlatSharpCompiler.ParseSchema(ISerializer`1 serializer, Byte[] bfbs, CompilerOptions options, List`1 postProcessTransforms, ISchemaMutator[] mutators) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Compiler\FlatSharpCompiler.cs:line 643    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at FlatSharp.Compiler.FlatSharpCompiler.CreateCSharp(List`1 bfbs, String inputHash, CompilerOptions options) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Compiler\FlatSharpCompiler.cs:line 565    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at FlatSharp.Compiler.FlatSharpCompiler.RunCompiler(CompilerOptions options) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Compiler\FlatSharpCompiler.cs:line 126    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at FlatSharp.Compiler.FlatSharpCompiler.<>c__DisplayClass8_0.<Main>b__1(CompilerOptions x) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Compiler\FlatSharpCompiler.cs:line 59    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136    
    at FlatSharp.Compiler.FlatSharpCompiler.Main(String[] args) in D:\a\FlatSharp\FlatSharp\src\FlatSharp.Compiler\FlatSharpCompiler.cs:line 55    Common    C:\Users\optocloud\.nuget\packages\flatsharp.compiler\7.2.0\build\FlatSharp.Compiler.targets    136

Flatsharp needs to detect that the schema files are on a different drive and use alternative methods to get a path to those files.

jamescourtney commented 1 year ago

This is a known issue. Unfortunately, there is little I can do about it.

FlatSharp uses the paths generated by flatc internally. These paths are all relative, and on Windows they do not include the drive letter. An example of a real path that flatc produces is this:

//../../../:/git/FlatSharp/test.fbs

Unfortunately, the drive letter is not present, so FlatSharp can only make guesses about the location of the file. It looks like you've contributed to google/flatbuffers before. One thing that would allow me to effectively mitigate this would be an option to add absolute paths to bfbs output instead of relative.

OptoCloud commented 1 year ago

Ah I see... But that would probably break backwards compatibility, so I'm guessing they won't implement that. I can see if I find some way to have the nuget packages cached on the same drive as the source code. Thanks for the quick answer! 😄

OptoCloud commented 1 year ago

Would be nice to have some kind of detection for seperate drives and give a better exception message tho 🤔

jamescourtney commented 1 year ago

Agree completely. You've actually inspired me to see if I can get flatc to add a drive letter to the relative path.

jamescourtney commented 1 year ago

Fixed in 7.2.1, which uses a custom build of flatc :)

jamescourtney commented 1 year ago

@OptoCloud -- is this working for you now?

OptoCloud commented 1 year ago

Yeah, looks to be working fine 😄 Thanks! 😁