thoth-org / Thoth.Json

Library for working with JSON in a type safe manner, this libs is targeting Fable
https://thoth-org.github.io/Thoth.Json/
MIT License
150 stars 36 forks source link

Thoth.Json.Core methods references in js without import #204

Closed joprice closed 2 months ago

joprice commented 4 months ago

I'm trying out switching to from Thoth.Json to Thoth.Json.Core, and found that the generated js references the method Thoth_Json_Core_Decode_object, without a corresponding import. It seems to me that something odd is going wrong related to inlining the function. I can try to reproduce this, but as I'm not doing anything especially interesting, I wanted to open an issue to see if this is some common misuse or known issue I'm hitting.

MangelMaxime commented 4 months ago

This error does not ring a bell to me sorry.

Note that only the manual API is supported right now, by the new Thoth.Json.Core project. And you need to use one of its corresponding runtime implementation Thoth.Json.JavaScript, Thoth.Json.NewtonSoft to have access to an implemented decoder/encoder.

joprice commented 4 months ago

Yup I wasn't trying to use Auto and had the language specific lib configured. I reverted back to the old api for now but will create a repro branch sometime soon and try it out, as I'd like to share encoders/decoders between python code generating data using an ml model and the node server consuming it.

joprice commented 4 months ago

Here's the repro https://github.com/joprice/fable-repro/blob/cd0af322f66bee966cb034313c0b434e8b018bb8/Program.fs. It has the following compilation error:


./Program.fs(1,1): error EXCEPTION: Cannot find inline member: Thoth.Json.Core.Encode_string
   at Fable.Transforms.State.CompilerImpl.Fable.Compiler.GetInlineExpr(String memberUniqueName) in /workspaces/Fable/src/Fable.Transforms/State.fs:line 372
   at Fable.Transforms.FSharp2Fable.Util.inlineExpr(IFableCompiler com, Context ctx, FSharpOption`1 r, Type t, FSharpOption`1 callee, CallInfo info, String membUniqueName) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.Util.fs:line 2557
   at Fable.Transforms.FSharp2Fable.Util.|Inlined|_|(IFableCompiler com, Context ctx, FSharpOption`1 r, Type t, FSharpOption`1 callee, CallInfo info, FSharpMemberOrFunctionOrValue memb) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.Util.fs:line 2633
   at Fable.Transforms.FSharp2Fable.Util.makeCallWithArgInfo(IFableCompiler com, Context ctx, FSharpOption`1 r, Type typ, FSharpOption`1 callee, FSharpMemberOrFunctionOrValue memb, MemberRef membRef, CallInfo callInfo) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.Util.fs:line 2699
   at Fable.Transforms.FSharp2Fable.Util.makeCallFrom(IFableCompiler com, Context ctx, FSharpOption`1 r, Type typ, FSharpList`1 genArgs, FSharpOption`1 callee, FSharpList`1 args, FSharpMemberOrFunctionOrValue memb) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.Util.fs:line 2782
   at Fable.Transforms.FSharp2Fable.Compiler.transformExpr@1024-33.Invoke(Context ctx) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.fs:line 1024
   at MonadicTrampoline.run[a](Thunk`1 _arg1) in /workspaces/Fable/src/Fable.Transforms/MonadicTrampoline.fs:line 9
   at MonadicTrampoline.Bind@24.Invoke(Unit unitVar0) in /workspaces/Fable/src/Fable.Transforms/MonadicTrampoline.fs:line 24
   at MonadicTrampoline.run[a](Thunk`1 _arg1) in /workspaces/Fable/src/Fable.Transforms/MonadicTrampoline.fs:line 9
   at MonadicTrampoline.Bind@24.Invoke(Unit unitVar0) in /workspaces/Fable/src/Fable.Transforms/MonadicTrampoline.fs:line 24
   at MonadicTrampoline.run[a](Thunk`1 _arg1) in /workspaces/Fable/src/Fable.Transforms/MonadicTrampoline.fs:line 9
   at Fable.Transforms.FSharp2Fable.Compiler.transformMemberFunction(IFableCompiler com, Context ctx, String name, FSharpMemberOrFunctionOrValue memb, FSharpList`1 args, FSharpExpr body) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.fs:line 1731
   at Fable.Transforms.FSharp2Fable.Compiler.transformMemberFunctionOrValue(IFableCompiler com, Context ctx, FSharpMemberOrFunctionOrValue memb, FSharpList`1 args, FSharpExpr body) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.fs:line 1814
   at Fable.Transforms.FSharp2Fable.Compiler.transformMemberDecl(FableCompiler com, Context ctx, FSharpMemberOrFunctionOrValue memb, FSharpList`1 args, FSharpExpr body) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.fs:line 1949
   at Microsoft.FSharp.Primitives.Basics.List.collectToFreshConsTail[T,TResult](FSharpFunc`2 f, FSharpList`1 list, FSharpList`1 cons) in /home/dev/Projects/fsharp/src/FSharp.Core/local.fs:line 434
   at Microsoft.FSharp.Primitives.Basics.List.collect[T,TResult](FSharpFunc`2 f, FSharpList`1 list) in /home/dev/Projects/fsharp/src/FSharp.Core/local.fs:line 442
   at Microsoft.FSharp.Primitives.Basics.List.collectToFreshConsTail[T,TResult](FSharpFunc`2 f, FSharpList`1 list, FSharpList`1 cons) in /home/dev/Projects/fsharp/src/FSharp.Core/local.fs:line 434
   at Microsoft.FSharp.Primitives.Basics.List.collect[T,TResult](FSharpFunc`2 f, FSharpList`1 list) in /home/dev/Projects/fsharp/src/FSharp.Core/local.fs:line 442
   at Fable.Transforms.FSharp2Fable.Compiler.transformFile(Compiler com) in /workspaces/Fable/src/Fable.Transforms/FSharp2Fable.fs:line 2709
   at Fable.Cli.Pipeline.Js.compileFile@178.Invoke(Unit unitVar) in /workspaces/Fable/src/Fable.Cli/Pipeline.fs:line 179
   at Microsoft.FSharp.Control.AsyncPrimitives.CallThenInvoke[T,TResult](AsyncActivation`1 ctxt, TResult result1, FSharpFunc`2 part2) in /home/dev/Projects/fsharp/src/FSharp.Core/async.fs:line 510
   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in /home/dev/Projects/fsharp/src/FSharp.Core/async.fs:line 112```
joprice commented 4 months ago

Downgrading Thoth.Json.Core to 0.2.1 fixes it. Is Thoth.Json.JavaScript 0.2.0 compatible with Thoth.Json.Core? i ask because I published the deps locally and I no longer get the missing imports. I did this by adding to the changelogs of Core and JavaScript a dummy 0.3.1-alpha header, then ran dotnet pack, then added

source ../Thoth.Json/packages/Thoth.Json.Core/bin/Release/
source ../Thoth.Json/packages/Thoth.Json.JavaScript/bin/Release/

to my paket.dependencies file

and pinned the versions

nuget Thoth.Json.Core 0.3.1-alpha
nuget Thoth.Json.Javascript 0.3.1-alpha

However, if I pin Thoth.Json.Javascript to 0.2.0, I get a new error

./Program.fs(36,7): (36,42) error FSHARP: The type referenced through 'Thoth.Json.Core.Decoder`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'Thoth.Json.Core'. (code 74)
./Program.fs(44,16): (44,54) error FSHARP: The type referenced through 'Thoth.Json.Core.IEncodable' is defined in an assembly that is not referenced. You must add a reference to assembly 'Thoth.Json.Core'. (code 74)
MangelMaxime commented 4 months ago

Is Thoth.Json.JavaScript 0.2.0 compatible with Thoth.Json.Core?

I believe they should be because, both got their latest version released the same day. I will have a look at your reproduction repo

MangelMaxime commented 2 months ago

Ok, I believe the problem was that Fable.Package.SDK had a bug in it.

I am currently making a new batch of release, to use the fixed version of Fable.Package.SDK. What is strange is that if you just did dotnet pack this should not have fixed the issue. So I am waiting for NuGet to index the packages to confirm that it was the only issue.

MangelMaxime commented 2 months ago

This issue is now fixed in the new releases versions sorry for the delay...