Fix stale files and .env.local handling in the basic-ts template - #6036
Open
JulienLavocat wants to merge 1 commit into
Open
JulienLavocat wants to merge 1 commit into
JulienLavocat wants to merge 1 commit into
Conversation
- Load `.env.local` in `src/main.ts`. `spacetime init` writes the database name and host there, but the Node client never read it, so `npm run dev` connected to `ws://localhost:3000` / `basic-ts` regardless. Variables already set in the environment (as `spacetime dev` does) still win. - Regenerate `src/module_bindings`, which were generated by CLI 2.0.0 and lacked the `procedures` export. - Add the `generate` script the other templates have, so the bindings are included in `pnpm generate`. - Remove `spacetimedb/pnpm-lock.yaml`, a leftover pinning `spacetimedb` to 1.9.0 that was copied into every new project.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Changes
Running
spacetime init --template basic-tsand then following the printed "Next steps" gives a project with several problems. This fixes the ones that live in the template itself.The client ignores
.env.local.spacetime initwritesSPACETIMEDB_HOSTandSPACETIMEDB_DB_NAMEto.env.local, butsrc/main.tsonly readsprocess.envand nothing loads the file.npm run devtherefore always connects tows://localhost:3000/basic-ts, even for a project initialised against Maincloud. It works underspacetime devonly because the CLI injects both variables into the client process.src/main.tsnow callsprocess.loadEnvFile?.('.env.local')inside atry. Variables already set in the environment take precedence, sospacetime devbehaves as before.process.loadEnvFileexists from Node 20.12 and 21.7. On older versions the call is skipped and the client still uses the localhost defaults, as it does today, so this does not fix the problem there but does not raise the documented Node 18+ requirement either. A missing.env.localis ignored.The checked-in bindings are stale.
src/module_bindingswas generated by CLI 2.0.0 and lacks theproceduresexport. Regenerated withpnpm generate; the diff is the version header and that export.basic-tsis skipped bypnpm generate. It had nogeneratescript, unlikereact-tsand most other templates. Added the same script they use.A stale lockfile ships with every new project.
spacetimedb/pnpm-lock.yamlpinsspacetimedbto1.*(1.9.0), whilepackage.jsondepends on the current SDK. Template files are embedded from the git-tracked file list, so it was copied into each generated project. Removed.Not addressed here:
spacetime init --template react-tscreates a broken.env.local#5941 fixes the database name mismatch between.env.localandspacetime.local.json, which is ininit.rs. With both PRs, a freshly initialisedbasic-tsclient should connect to the database thatspacetime publishcreates; I have not tested the combination.templates/nodejs-tsreads the same two variables the same way and has the same.env.localproblem.templates/nuxt-ts/spacetimedb/pnpm-lock.yamlis the same kind of leftover.angular-ts,browser-ts,bun-ts,deno-ts,nextjs-ts,nodejs-ts,nuxt-ts,react-ts,remix-ts,solid-ts,svelte-ts,tanstack-ts,vue-ts). CI has no check that the TypeScript template bindings are up to date.API and ABI breaking changes
None.
Rollback safety impact
n/a
Expected complexity level and risk
Testing
pnpm -F spacetimedb build, thenpnpm generateintemplates/basic-ts; the resulting diff is only the version header and theproceduresexport. Without the SDK build first, generate fails withCould not resolve 'spacetimedb/server'.pnpm -F ./templates/basic-ts build,tsc --noEmitandeslint templates/basic-ts/src/main.tsin the workspace.spacetimedb-clifrom this branch and ranspacetime init --template basic-ts --non-interactiveinto an empty directory: nospacetimedb/pnpm-lock.yaml, bindings header reads 2.11.0.spacetime build,tsc --noEmitin both packages, andnpm run buildpass.spacetimedb@2.11.*is not on npm yet, so the generated project was pointed at this branch'scrates/bindings-typescriptbuild for the install.npm startin that project withWebSocketstubbed to print the URL it dials:.env.localpresent:wss://maincloud.spacetimedb.com/v1/database/e2e-app/subscribe(onmaster:ws://localhost:3000/v1/database/basic-ts/subscribe)SPACETIMEDB_HOST/SPACETIMEDB_DB_NAMEset in the environment: those values win over the file.env.local:ws://localhost:3000/v1/database/basic-ts/subscribe, no errorprocess.loadEnvFileset toundefined(simulating an older Node): same defaults, no errorspacetime devin a freshbasic-tsproject against a local server. I did not run this, nor thetest_template_basic_tssmoketest, nor a real Node 18 or 20 runtime (only Node 24 was available).