clockworklabs / clockworklabs/SpacetimeDB

Better namespace handling in generated code / ModuleDefs

Open
#1,540 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

abi-break bug
Dominant language
Rust
Stars
25.2k
Forks
1.1k
Avg merge
2d 7h
Merged PRs (30d)
46

Description

Consider this contrived module.

use spacetime::*;

pub mod gems {
    use super::*;

    #[derive(SpacetimeType)]
    pub enum Gem {
        Diamond,
        WorthlessGlass,
    }

    #[derive(SpacetimeType)]
    pub struct Inventory(Vec<Gem>);
}

pub mod fruit {
    use super::*;

    #[derive(SpacetimeType)]
    pub enum Fruit {
        Apple,
        Banana,
    }

    #[derive(SpacetimeType)]
    pub struct Inventory(Vec<Fruit>);
}

#[spacetimedb(table(public))]
pub struct Person {
    #[primarykey]
    #[autoinc]
    id: u32,
    name: String,
    age: u8,
    gems: gems::Inventory,
    fruit: fruit::Inventory,
}

I believe this module will compile and upload fine. However, if you attempt to generate a client for it, the client will have compile errors.

This is because of how type names are registered on ModuleDef. Types implement SpacetimeType::make_type which calls the TypespaceBuilder::add method. They call this with their name, which is a single identifier, not a sequence of identifiers.

So, both Inventory structs above will register themselves with exactly the same name, Inventory. This may result in name collisions when you generate a client.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ModuleDef, SpacetimeType::make_type, and TypespaceBuilder::add to trace how type names are registered. Determine how namespace-qualified names should be represented and propagated into generated clients. Done means modules containing same-named types in different namespaces generate clients without compile-time name collisions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.