Skip to content

Conversation

GSGerritsen
Copy link

The TypeScript code generator throws an error when it encounters references to external types that aren't available in the current compilation context. This happens when a Cap'n Proto schema imports types from other schemas that aren't being compiled in the same invocation.

When the compiler encounters an external type reference (struct, enum, or interface) that isn't in the current node context, it throws an error in lookupNode() with message: "missing node".

Expected Behavior

The compiler should gracefully handle external type references by generating placeholder type names, allowing the compilation to complete successfully.

Changes

  • Add a tryLookupNode function that returns undefined instead of throwing when a node isn't found
  • Generate placeholder type names (e.g., Struct_${typeId}, Enum_${typeId}) for external types
  • This allows TypeScript generation to complete while maintaining type safety

Impact

This fix prevents compilation failures in projects with modular schema organization where not all referenced types are available during every compilation run.

Resolves #45

- Add tryLookupNode function to safely check for node existence
- Update getJsType to generate placeholder types for external structs/enums/interfaces
- Update getConcreteListType to handle missing struct nodes
- Update generateNestedImports to skip missing import nodes

This allows TypeScript generation to complete for files that import types from other compilation units, fixing the 'Failed to look up node id' errors.
Add tryLookupNode function to safely handle missing external type references
without throwing errors. Generate placeholder type names for external types
that aren't available in the current compilation context.
@GSGerritsen GSGerritsen requested review from pi0 and vicb as code owners July 31, 2025 23:50
@vicb
Copy link
Contributor

vicb commented Aug 1, 2025

The TypeScript code generator throws an error when it encounters references to external types that aren't available in the current compilation context.

Note that doing that could be dangerous as the compiler is pre 1.0 and the API might break at any time.

That being said I guess it's ok to add this feature.

Could you please add a test to the PR?

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

Successfully merging this pull request may close these issues.

Type generation crashes when encountering external type references

2 participants