Skip to content

[NS 1] Add sub-module mounts support + codegen#5167

Open
aasoni wants to merge 1 commit into
alessandro/namespace-base-branchfrom
alessandro/sub-module-mounts
Open

[NS 1] Add sub-module mounts support + codegen#5167
aasoni wants to merge 1 commit into
alessandro/namespace-base-branchfrom
alessandro/sub-module-mounts

Conversation

@aasoni
Copy link
Copy Markdown
Contributor

@aasoni aasoni commented Jun 2, 2026

Description of Changes

Add a new recursive "mounts" field to add submodules to module def. Handle code generation for each language.
Handle identifiers with "/" or "." in the name to handle namespaced reducers (e.g. lib/reducer) and namespaced tables (lib.table).

Handle code generation for the recursive type. This needed some special handling in code generation for typescript and c++.
Typescript codegen in particular is quite complex as it tries to handle circular dependency generically. C++ on the other hand is a lot simpler because it hard-codes a special handling of the V10 definition but doesn't solve circular dependencies in general.
I would advice against solving circular dependencies in a generic way for C++ however we could consider modifying the typescript code gen to just have special handling for the V10 recursive definition which would simplify the code quite a lot. I went down the rabbit hole of handling this generically and came out on the other side, but if there is strong opinion to keep the codegen code simple, I am happy to revisit and align to the C++ way.

API and ABI breaking changes

The change is purely additive and newer host versions will accept older module defs. However older host versions will not accept new module defs.

Expected complexity level and risk

5 - While this specific PR is maybe a 4, the overall namespace change is definitely 5. This is a pretty significant change. It's a large diff which touches the module def and changes code that hasn't been touched in a long time (e.g. Identifier).

Testing

Beyond the rust tests defined in this PR, the following tests were done on the full PR sequence once the entire namespace feature was implemented for typescript:
Feature Test Checklist

Module:

  • root module can import another module and mount it with a namespace under its schema
  • root module and submodule can have the same function and table names without conflicting
  • ctx.db.lib.lib_table is readable/writable inside root module reducer
  • ctx.db.lib.lib_table is readable/writable inside root module procedure withTx block
  • library_reducer(ctx.as.lib) is callable inside root module reducer

Client

  • Client can subscribe to lib.library_table
  • Client can subscribe to lib.library_view
  • Client can call lib/library_reducer
  • Client can call lib/library_procedure
  • Client can subscribe to lib.sublib.sublib_table
  • Client can subscribe to lib.sublib.sublib_view
  • Client can call lib/sublib/sublib_reducer
  • Client can call lib/sublib/sublib_procedure

CLI

  • CLI can subscribe to lib.library_table
  • CLI can subscribe to lib.library_view
  • CLI can call lib/library_reducer
  • CLI can call lib/library_procedure
  • CLI can subscribe to lib.sublib.sublib_table
  • CLI can subscribe to lib.sublib.sublib_view
  • CLI can call lib/sublib/sublib_reducer
  • CLI can call lib/sublib/sublib_procedure

Migration

  • Module migrates without issue from having a submodule to not having a submodule
  • Module migrates without issue from not having a submodule to having a submodule
  • Module migrates without issue when having a submodule and root module change occurs (change reducer signature, add table, add column with default, change reducer function body, change index)
  • Module migrates without issue when having a submodule and a submodule change occurs (change reducer signature, add table, add column with default, change reducer function body, change index)

Commit Log

  • Module loads fine from commit log
  • Module snapshot is created without issue
  • Module loads fine from snapshot

@aasoni aasoni changed the base branch from master to alessandro/namespace-base-branch June 2, 2026 08:51
@aasoni aasoni changed the title Add sub-module mounts support + codegen [NS 1] Add sub-module mounts support + codegen Jun 2, 2026
@aasoni aasoni requested review from coolreader18 and gefjon June 2, 2026 08:51
@aasoni aasoni requested a review from JasonAtClockwork June 2, 2026 11:35
@aasoni aasoni force-pushed the alessandro/sub-module-mounts branch from a5dfaf5 to c8cac1d Compare June 3, 2026 08:00
Comment thread crates/codegen/src/cpp.rs
Comment on lines +269 to +273
bool operator==(const RawModuleMountV10& o) const noexcept {
if (namespace_ != o.namespace_) return false;
if (module && o.module) return *module == *o.module;
return !module && !o.module;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails compilation due to RawModuleDefV10 incomplete at this stage and dereferencing the pointer needs the complete type.

To get through this today we can do a light touch and just check the pointer, it's not as solid as the Rust/TypeScript that does a deeper check but this will compile and we'll have to do the C++ implementation in a future PR anyhow:

Suggested change
bool operator==(const RawModuleMountV10& o) const noexcept {
if (namespace_ != o.namespace_) return false;
if (module && o.module) return *module == *o.module;
return !module && !o.module;
}
bool operator==(const RawModuleMountV10& o) const noexcept {
return namespace_ == o.namespace_ && module == o.module;
}

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.

2 participants