Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables not available when using Wrangler 2 #14924

Closed
drzamich opened this issue Aug 21, 2022 · 3 comments · Fixed by #14925
Closed

Environment variables not available when using Wrangler 2 #14924

drzamich opened this issue Aug 21, 2022 · 3 comments · Fixed by #14925
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: data proxy
Milestone

Comments

@drzamich
Copy link
Contributor

drzamich commented Aug 21, 2022

Bug description

When using the Data Proxy feature on the Cloudflare Workers runtime I'm getting an error caused by what I think is inability of Prisma edge client to access env variables set by Wrangler.
What's important to note is that I'm using Wrangler 2 (what you get by default if you run npm i wrangler). It's different than what's used in the ecosystem-tests where the legacy Wrangler 1 is used.

Your worker has access to the following bindings:
- Vars:
  - CF_DATA_PROXY_URL: "..."

...

'Uncaught Error: InvalidDatasourceError: Datasource "db" references an environment
  variable "CF_DATA_PROXY_URL" that is not set\n'

How to reproduce

  1. git clone git@github.com:prisma/ecosystem-tests.git
  2. cd ecosystem-tests/dataproxy/cloudflare-workers
  3. npm i
  4. npm i -D wrangler
  5. npm uninstall @cloudflare/wrangler
  6. source ./prepare.sh
  7. Remove usage_model = '' line from wrangler.toml
  8. npx prisma generate --data-proxy
  9. npx wrangler dev src/index.ts

Expected behavior

There's no error.

Prisma information

As in the example.

Environment & setup

  • OS: macOS Monterey 12.1
  • Database: PostgreSQL
  • Node.js version: 16.13.2

Prisma Version

Experienced it in my project on 4.2.1. Reproduced also on 4.3.0-dev.48.

@drzamich drzamich added the kind/bug A reported bug. label Aug 21, 2022
@drzamich drzamich changed the title Environment variables not accessible when using Wrangler 2 Environment variables not available when using Wrangler 2 Aug 21, 2022
@drzamich
Copy link
Contributor Author

drzamich commented Aug 21, 2022

I did a little investigation on the matter and have some findings

Root cause

Root cause is the different global object between Wrangler 1 (a.k.a @cloudflare/wrangler) and Wranger 2 (a.k.a. wrangler).

When trying to access env vars, Prisma does the following:

packages/client/src/generation/utils/buildInjectableEdgeEnv.ts#74

const cfwEnv = `typeof global !== 'undefined' && global['${envVarName}']`

This works for Wrangler 1. For Wrangler 2 however, global is undefined. I couldn't find any documentation on the matter but it seems that Wrangler 2's runtime is an actual service worker, because the global object referenced with self.

Proposed fix

The fix couldn't be simpler - replacing global with globalThis on the above mentioned line does the job. globalThis is runtime-agnostic and will always return the global object.

Workaround

Turn on the experimental Node compatibiltiy flag in by adding the following to wrangler.toml

node_compat=true

@do4gr do4gr added team/client Issue for team Client. topic: data proxy bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Aug 22, 2022
@aqrln aqrln added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Aug 24, 2022
@janpio janpio added this to the 4.3.0 milestone Aug 24, 2022
@yamiteru
Copy link

Still doesn't work..

ReferenceError: DATABASE_URL is not defined.
Attempted to access binding using global in modules.
You must use the 2nd `env` parameter passed to exported handlers/Durable Object constructors, or `context.env` with Pages Functions.

I generate with prisma generate and run dev with wrangler dev --local.

@janpio
Copy link
Member

janpio commented Sep 19, 2022

Please open a new issue with a full description of your problem. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: data proxy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants