Skip to content

Bugfix: spacetime init --template react-ts creates a broken .env.local - #5941

Open
krisajenkins wants to merge 1 commit into
clockworklabs:masterfrom
krisajenkins:init-env-local-database-name
Open

krisajenkins wants to merge 1 commit into
clockworklabs:masterfrom
krisajenkins:init-env-local-database-name

Conversation

@krisajenkins

Copy link
Copy Markdown
Contributor

Description of Changes

Run spacetime init --template react-ts myproject and accept the defaults. You'll get a project path of myproject/ and a database name like myproject-30ctp. The "Next steps" instructions that follow, and the contents of the generated .env.local, use the wrong database name: they use the project name instead of the database you just confirmed. So out of the box, the client points at a database that doesn't exist.

This affects every built-in template with a TypeScript client, since they all share the same .env.local writer, and the spacetime publish line in "Next steps" was wrong for the Rust and C# templates too. It often goes unnoticed because spacetime dev overwrites .env.local on every build, but running the client directly shows it.

The database name prompt's answer only ever reached spacetime.local.json. It never made it into the template setup, so init_builtin wrote the env file from the project name:

write_typescript_client_env_file(project_path, &config.project_name, config.use_local)?;

The chosen database name is now passed from exec_init through init_from_template to init_builtin, and into print_next_steps. It's passed as a parameter rather than stored on TemplateConfig, because that struct is built in six places before the database name is known.

  • Rename write_typescript_client_env_file's module_name parameter to db_name.
  • Add a test that initialises the react-ts template offline with distinct project and database names, and asserts every *_SPACETIMEDB_DB_NAME in .env.local is the database name.

API and ABI breaking changes

None. Only private functions inside the CLI's init subcommand change signature.

Rollback safety impact

n/a

Expected complexity level and risk

1

Testing

  • New test test_init_builtin_typescript_client_env_uses_database_name passes, and fails when the old &config.project_name argument is restored.
  • cargo test -p spacetimedb-cli --lib subcommands::init::tests, cargo clippy -p spacetimedb-cli --tests and cargo fmt -p spacetimedb-cli -- --check are clean.
  • Run spacetime init --template react-ts myproject, accept the defaults, and check that .env.local and the "Next steps" output both use the suffixed database name.

Run `spacetime init --template react-ts myproject`, accept the defaults. You'll
get a project path of `myproject/` and a database name like `myproject-30ctp`.
The instructions that follow, and the contents of the `.env.local` file will
use the wrong database name. They'll use the project name instead of the actual
database.

This will affect plenty of other templates, but `react-ts` is the one I found
it on. In a lot of cases, it won't be noticed because `spacetime dev` overwrites
the `.env.local` file, but out of the box it's broken.

The database name prompt's answer only ever reached `spacetime.local.json`. It
never made it into the template setup, so `init_builtin` wrote the env file
from the project name instead:

```rust
write_typescript_client_env_file(project_path, &config.project_name, config.use_local)?;
```

The chosen database name is now passed from `exec_init` through
`init_from_template` to `init_builtin`, and into `print_next_steps`. It's
passed as a parameter rather than stored on `TemplateConfig`, because that
struct is built in six places before the database name is known.

- Rename `write_typescript_client_env_file`'s `module_name` parameter to `db_name`.
- Add a test that initialises the `react-ts` template offline with distinct
  project and database names and asserts every `*_SPACETIMEDB_DB_NAME` in
  `.env.local` is the database name. It fails against the old code.
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.

1 participant