`prisma dev` shares one env.json between every `--name` run from a directory, so a stack can start with another stack's addresses

Open
#330 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript

Research direction

Start with the dev entry points that compute devDir and DEV_DIR in @prisma/composer-cli, then inspect envStore, LocalEnvironmentVariableProvider, LocalDeploymentProvider, runDevPreflight, and runDevTeardown in @prisma/composer-prisma-cloud. Trace Alchemy's noop and state behavior in Plan.ts and Apply.ts. Add a local-target test for the A, B, A sequence and verify per-name environments, safe --fresh behavior, and upgrade recovery.

Written by the indexing model from the issue text.

Description

What is wrong

prisma dev keeps the environment rows of a local stack in <project>/.prisma-composer/dev/env.json. The path depends on the directory, not on the stack name given with --name. When two names are started from one directory, the second start overwrites the first stack's rows with its own.

When the first name is started again, Alchemy plans every unchanged EnvironmentVariable resource as noop, so nothing writes the first stack's rows back. Any of its services whose deployment is re-applied on that start (because its code changed) gets its environment from the file, which now holds the other stack's values. That service then talks to the other stack's services.

Who it hurts: anyone who runs more than one --name from one checkout, for example a developer who keeps a seeded stack and a scratch stack, or several agents that each run a stack from one worktree. Nothing reports the mix-up. In the case that led to this issue, sign-in to one stack failed with "Invalid origin", because its web service sent sign-in to the other stack's auth service. I saw it happen to the web service only. From the source, the same code path also hands over database URLs and service keys: in my run, env.json held the other stack's COMPOSER_APP_DATABASE_URL, so an API whose deployment was re-applied would have read and written the other stack's database.

Versions

  • @prisma/composer 0.21.0
  • @prisma/composer-prisma-cloud 0.21.0 (the compute and Postgres emulator daemons report 0.21.0)
  • @prisma/composer-cli 0.25.0, through prisma 8.0.0-rc.19
  • alchemy 2.0.0-beta.74
  • Node 24.13.0, macOS

How to reproduce

I reproduced it with the Asks application (prisma/asks at commit c70a404). Its module.ts provisions six services; the web service depends on app (the API) and on the auth module's auth.service. In Asks, pnpm dev is a small wrapper (scripts/dev.mjs) that sets two env params, ASKS_ENV (the stack name) and ASKS_WEB_URL (the web's own port, read from the compute emulator), and then runs prisma dev module.ts in deploy/. The wrapper plays no part in the fault.

pnpm install
cp deploy/.env.example deploy/.env      # platform credentials as placeholders

pnpm dev --name envjson-a --no-interactive --yes   # wait for "ready", then Ctrl-C
pnpm dev --name envjson-b --no-interactive --yes   # wait for "ready", then Ctrl-C

# change any line of the web service, for example the <title> in apps/web/app/root.tsx

pnpm dev --name envjson-a --no-interactive --yes   # wait for "ready" and leave it running
pnpm dev --name envjson-b --no-interactive --yes   # in a second terminal, the same directory

# a sign-in with a made-up account: 401 means the origin was accepted, 403 INVALID_ORIGIN means it was refused
curl -X POST http://localhost:<envjson-a web port>/api/auth/sign-in/email \
  -H 'content-type: application/json' -H 'origin: http://localhost:<envjson-a web port>' \
  -d '{"email":"[email protected]","password":"not-a-password"}'

To see what a service was started with, read ~/.prisma-composer/emulators/compute/apps.json under <app>.services.<service>.env.

I did not build a minimal module. From the source below, any module in which one service depends on another should show the same thing: start prisma dev module.ts --name a, stop it, start and stop --name b from the same directory, change the dependent service's code, and start --name a again. The dependent service's COMPOSER_<SERVICE>_<DEP>_URL should then name b's port. This is untested.

What I saw

The emulator gave each stack its own ports. envjson-a: web 3031, auth.service 3034, app 3036. envjson-b: web 3037, auth.service 3039, app 3042.

  1. First start of envjson-a: env.json held COMPOSER_WEB_SESSION_URL=http://localhost:3034, COMPOSER_WEB_API_URL=http://localhost:3036, COMPOSER_WEB_INPUT={"environment":"envjson-a"} and COMPOSER_AUTH_SERVICE_INPUT with baseUrl http://localhost:3031. The test sign-in answered 401 (origin accepted).
  2. envjson-b from the same directory: env.json was rewritten with envjson-b's values (COMPOSER_WEB_SESSION_URL=http://localhost:3039, COMPOSER_WEB_API_URL=http://localhost:3042, environment envjson-b, baseUrl http://localhost:3037). Its sign-in answered 401.
  3. envjson-a again, nothing changed: all 66 resources planned noop and Alchemy printed Done: 0 succeeded. env.json stayed byte for byte the file envjson-b wrote. The services were restarted from the env the compute emulator had kept from step 1, so sign-in still answered 401. The file was wrong, but nothing read it on this start.
  4. envjson-a again, after one changed line in the web's source: the plan was [web-deploy] update and 65 noop, Done: 2 succeeded. The web service of envjson-a was then running with envjson-b's values; only its port was its own:
envjson-a/web: port 3031 status running
  COMPOSER_WEB_API_URL = http://localhost:3042
  COMPOSER_WEB_AUTHAPI_URL = http://localhost:3039
  COMPOSER_WEB_INPUT = {"environment":"envjson-b"}
  COMPOSER_WEB_ORIGIN = "http://localhost:3037"
  COMPOSER_WEB_PORT = 3031
  COMPOSER_WEB_SESSION_URL = http://localhost:3039

With envjson-b stopped, envjson-a's sign-in answered 502 auth proxy: upstream unreachable. With envjson-b started beside it, envjson-a's sign-in answered 403 {"message":"Invalid origin","code":"INVALID_ORIGIN"}, and envjson-b's auth service logged ERROR [Better Auth]: Invalid origin: http://localhost:3031. envjson-b's own sign-in still answered 401.

Cause

The dev directory has no stack name in it. In @prisma/composer-cli 0.25.0, dist/execute-dev-BLCK5wF7-CuzMSK7T.mjs line 1765 is const devDir = path.join(cwd, DEV_DIR);, with DEV_DIR = ".prisma-composer/dev" (dist/local-target-BOGjATc2.mjs line 5). The generated alchemy.run.ts does the same at line 1622: const devDir = path.join(process.cwd(), DEV_DIR);.

The Alchemy state does have the stack name in it. The generated stack is rendered with name: <the --name value> and deployed with stage: "dev" (line 1845), and Alchemy's localState() keeps rows under .alchemy/state/<stack>/<stage>/ (alchemy/src/State/LocalState.ts lines 105 to 106). So each name has its own record of which rows it already wrote, but all names share the file those rows are written to.

In @prisma/composer-prisma-cloud 0.21.0, dist/local-target.mjs:

  • Lines 5701 to 5703: envStore(devDir) is path.join(devDir, "env.json"), documented as "every EnvironmentVariable row, key → value. Last write wins."
  • Lines 5862 to 5871: LocalEnvironmentVariableProvider.reconcile writes a row into that file. It runs only on create or update.
  • Lines 5821 to 5829 and 5922: LocalDeploymentProvider.reconcile builds the child's environment with materializeEnv(input.devDir, address, port), which reads every row from the file, and sends it to the compute emulator with putDeployment.
  • Line 6254: runDevPreflight writes secrets.json into the same shared directory, so envSecret values have the same problem.
  • Lines 6288 to 6295: runDevTeardown (used by --fresh) deletes .alchemy/state/${app}/dev, which belongs to one name, but also all of ${cwd}/${DEV_DIR}, which belongs to every name. So --fresh on one name also deletes the other names' env.json, secrets.json and unpacked artifacts.

Why an unchanged stack does not write the file again: Alchemy plans an update only when the props differ from that stack's own state (alchemy/src/Plan.ts lines 1398 to 1407, havePropsChanged(oldProps, news)), and a noop node calls no provider (alchemy/src/Apply.ts line 625). A deployment is still re-applied when its artifact changes or when one of its own rows changes (Deployment.triggers, described in dist/s3-credentials-resource-CLB5qGGZ-D0XhVg6M.mjs lines 355 to 368). That re-applied deployment reads the shared file and gets whatever the last stack wrote.

Expected behaviour

Each stack name started from one directory keeps its own environment. Starting another name, stopping it, or running --fresh on it never changes what an existing name's services are started with.

Recommendation

Put everything that belongs to one stack under a directory named after it, for example .prisma-composer/dev/<app>/env.json and .prisma-composer/dev/<app>/secrets.json, and have runDevTeardown delete only that directory. The unpacked artifacts are named by their content hash, so they can stay shared, but then --fresh must not delete them while another name's emulator entries still point at them.

The move also needs one guard. A row that is noop is never written again, so after an upgrade the new per-name file starts empty, and the first re-applied deployment would get no rows at all. Before the converge, the dev command should check whether the stack's file is missing while the stack's Alchemy state has rows, and in that case force an update of that stack's EnvironmentVariable rows, so the file is filled from the stack's own values.

Until this is fixed, users can avoid it by running each --name from its own checkout or worktree.

Acceptance criteria

  • Start name A, then name B, then A again from one directory, with a change to one service's code before the last start. Every service of A runs with A's URLs, origins, inputs, service keys and database URLs. B is the same when it is started next.
  • A and B can run at the same time from one directory, and each keeps its own environment.
  • prisma dev --name A --fresh does not delete or change B's files.
  • A project that ran with the shared env.json before the fix, and is then upgraded, does not start a re-applied deployment with an empty or another stack's environment.
  • The local target has a test for the A, B, A sequence with a re-applied deployment.

Out of scope

  • The order in which the compute emulator gives out ports.
  • The auth module's baseUrl being a param that the consumer must set by hand (a separate issue).
  • Emulator daemons shared between macOS accounts.

Related: prisma/composer#329 (the emulator runs every connection in one session). Found while building Asks (prisma/asks). Agent: tyr-16

Dominant language
TypeScript
Stars
6
Forks
2
Avg merge
1d 2h
Merged PRs (30d)
33

Getting set up

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.

More from prisma/composer

All issues in prisma/composer

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.