-
-
Notifications
You must be signed in to change notification settings - Fork 106
Flakiness in macro success #1279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @hughsimpson, It appears you are encountering a known issue #2. Could you please try the suggested workarounds to see if they stabilize your CI builds? The most effective solution, which isn't listed there, is migrating to Scala 3. This version does not use the powerful but sometimes unstable If migrating to Scala 3 isn't feasible at the moment, I can add an additional macro with pre-configured derivation options, similar to these examples, and cut a release for you. |
@plokhotnyuk Thank you for this. If you don't mind, I'll take a little time to delve into this a little -- a work colleague has managed to stumble across a reliable reproduction of the issue, and I want to check a few things. The serdes are constructed by the tapir codegen in a single object that hosts only jsoniter serde declarations (the snippet above is the whole of the object contents), and I don't know how practical it would really be to pull these out into a separate object for each, especially given that mutually-recursive schemas would then need even more care and attention than they currently have. The two configurations most used by the tapir codegen are found here. EDIT: , |
I have something that reliably triggers the bug every time which I'll upload today, and it seems that the issue is specifically triggered by multi-module builds, so there seems to be something at compile time that isn't thread-safe. Will keep poking at this and check if predefining those options sets helps. More to follow... |
OK I have tested locally with introducing 2 new custom macros for use by the tapir codegen: def makeOpenapiLike[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
.withRequireCollectionFields(true).withAllowRecursiveTypes(true))
def makeOpenapiEnumLike[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
.withRequireCollectionFields(true).withAllowRecursiveTypes(true).withDiscriminatorFieldName(scala.None)) -- this seems to have entirely fixed the problem, and covers almost all cases where the codegen would otherwise use the I don't know if it would still be helpful to have a replication of the bug, but I can push it up if you think it's worth it, just need to clean up a bit first. EDIT AGAIN: Ah. Whilst this helps for defns that don't include ADTs, with ADTs it still goes wrong... Have a reproduction up here. It really does seem to all come down to multi-module builds for some reason. Adding two more macros: def makeOpenapiADTLikeDefaultMapping[A: c.WeakTypeTag](c: blackbox.Context)(discriminator: c.Expr[String]): c.Expr[JsonValueCodec[A]] =
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
.withRequireCollectionFields(true).withAllowRecursiveTypes(true)
.withRequireDiscriminatorFirst(false).withDiscriminatorFieldName(Some(c.eval(c.Expr[String](c.untypecheck(discriminator.tree.duplicate))))))
def makeOpenapiADTLike[A: c.WeakTypeTag](c: blackbox.Context)(discriminator: c.Expr[String], mapping: c.Expr[PartialFunction[String, String]]): c.Expr[JsonValueCodec[A]] =
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
.withRequireCollectionFields(true).withAllowRecursiveTypes(true)
.withRequireDiscriminatorFirst(false).withDiscriminatorFieldName(Some(c.eval(c.Expr[String](c.untypecheck(discriminator.tree.duplicate))))).withAdtLeafClassNameMapper(x => c.eval(c.Expr[PartialFunction[String, String]](c.untypecheck(mapping.tree.duplicate))).apply(com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker.simpleClassName(x)))) seems to fix it even for ADT types and I have absolutely no idea why that would be, since they both still use |
I strongly suspect that the pr you just merged will fix this, at least for code generated by tapir, so I'm closing this for now. Thanks again for the help and guidance 🙏 |
@hughsimpson I've renamed added macros and their parameter to be more consistent with names of already existing ones in this commit Please check if all is fine and I will cut a release for you. |
@plokhotnyuk absolutely no issues with the renaming, all looks good to me. Thanks for bearing with me on all this, excited about the release |
Also run the local test case I have (sorry about the delay, was eating dinner) and that's still fine and passing, so think this is grand 👍 |
@hughsimpson Please peek the latest v2.36.0 release with your contribution. |
@plokhotnyuk Thanks! Running tests now.
All looks good! I'm just about to run on $privateCodebaseThatTriggeredAllOfThis, but I'm feeling positive! Final edit (?): Yep, even passes on the private codebase. Awesome. |
Using:
scala 2.13.16
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "2.35.3"
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.35.3" % Provided
(and previous versions of jsoniter-scala going back quite some time -- I don't think this is in anyway a recently-introduced issue)
When compiling the code generated by the tapir openapi generator (example output), I will occasionally hit an error looking as follows:
I have seen this happen on a relatively minimal file:
The relevant models seem to be
although I haven't been able to produce a minimal version that definitely triggers this yet, I'm afraid - in part because it happens so rarely. I can't even get the failure locally on the exact file that's caused it before. It seems to happen more often when building in CI docker for some mysterious reason 😅
Anyway I'm not sure if this is really a jsoniter-scala bug or a scala bug or what, but thought I'd raise it here in case @plokhotnyuk had any ideas.
Perhaps there might be some workaround we could do in the tapir codegen to ameliorate this flakiness?
The text was updated successfully, but these errors were encountered: