Skip to content
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

FormatRegistry throws error TypeSystemDuplicateFormat in Hermes engine #1049

Closed
BleedingDev opened this issue Oct 28, 2024 · 4 comments
Closed

Comments

@BleedingDev
Copy link

I use React Native and Hermes engine and this particular line throws an error:

if (FormatRegistry.Has(format)) throw new TypeSystemDuplicateFormat(format)

I don't use anything special and when I delete this line, it fixes the problem.

Otherwise I get this error:

Uncaught TypeSystemDuplicateFormat: Duplicate string format 'date' detected
    at Object.Format (http://localhost:19001/..\..\node_modules\@sinclair\typebox\build\cjs\system\system.bundle//&platform=web&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable&dev=true&minify=false&modulesOnly=true&runModule=false&shallow=true:42:53)
    at http://localhost:19001/index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable:157827:64
    at loadModuleImplementation (http://localhost:19001/index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable:256:7)
    at guardedLoadModule (http://localhost:19001/index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable:156:23)
    at metroRequire (http://localhost:19001/index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable:83:75)
    at runUpdatedModule (http://localhost:19001/index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable:498:7)
    at metroHotUpdateModule (http://localhost:19001/index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable:388:26)
    at define (http://localhost:19001/index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable:39:18)
    at eval (http://localhost:19001/..\..\node_modules\@sinclair\typebox\build\cjs\system\system.bundle//&platform=web&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable&dev=true&minify=false&modulesOnly=true&runModule=false&shallow=true:1:1)
    at inject (http://localhost:19001/index.bundle?platform=web&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.routerRoot=src%2Fapp&unstable_transformProfile=hermes-stable:60932:7)

I found that it might be problem with Hermes as well, so if you suggest it should go there, I'll report it there.

@sinclairzx81
Copy link
Owner

@BleedingDev Hi,

Duplicate format exceptions may result from modules being unintentionally loaded multiple times, or there is logic explicitly trying to set the format. Originally, the intention for TypeSystem was to try and enforce a single point of registration, however in practice, there are cases where repeated initialization may be a consequence of module initialization issues (tooling related) or a preference for using idempotent design patterns with respect to registration (this of course is perfectly valid)

Outside of repeated module loading issues (that would need to be reported elsewhere), you can actually replace TypeSystem with FormatRegistry and TypeRegistry and set registrations directly. This is actually the recommended approach these days (the TypeSystem may be deprecated in subsequent revisions)

import { TypeRegistry, FormatRegistry } from '@sinclair/typebox'

FormatRegistry.Set('date', value => { /** code to check date */ })

By calling these directly, you avoid any issues with duplicate registration.

Hope this helps
S

@sinclairzx81
Copy link
Owner

@BleedingDev Hiya,

Will close off this issue for now as things should be working ok if you assign formats directly on the FormatRegistry. If you run into any problems, happy to re-open the issue, but this should resolve things for you.

Cheers
S

@BleedingDev
Copy link
Author

I don't even use Typebox directly, nor register anything. What I was trying to do is import ElysiaJS into React Native, so that I could use Eden to make type-safe calls. :)

@sinclairzx81
Copy link
Owner

@BleedingDev Hi,

Ah. It might be worth pinging the downstream projects to investigate registering formats via the FormatRegistry instead of the TypeSystem.Format function. Unfortunately, I can't trivially change the duplicate format check on the TypeSystem.Format function without that change being breaking, but the issue can be resolved downstream by registering via the FormatRegistry instead (as it applies no check for duplicate registrations)

There's been a couple of cases where I've seen module systems are violating singleton module instancing rules. I don't know much about the Hermes engine, but it may be doing this which is why you see the error. The TypeSystem.Format function expects systems to respect singleton instancing, but it is possible this assumption may not be applicable everywhere.

Hope this helps
S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants