workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

pg-cloudflare does not work with vite-plugin-cloudflare

Open TrySound opened this issue 6 months ago • 2 comments

What versions & operating system are you using?

wrangler v22.16.0, vite-plugin @1.7.2

Please provide a link to a minimal reproduction

No response

Describe the Bug

pg-cloudflare is using "cloudflare" export condition

https://github.com/brianc/node-postgres/blob/master/packages/pg-cloudflare/package.json#L14-L17

However plugin has hardcoded "workerd" condition and does not allow change it

https://github.com/cloudflare/workers-sdk/blob/main/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts#L128

See also https://github.com/brianc/node-postgres/issues/3493

Please provide any relevant error logs

No response

TrySound avatar Jun 19 '25 06:06 TrySound

This was an issue on my end - not related to vite, I'm just running wrangler.

jokull avatar Jun 19 '25 14:06 jokull

This was an issue on my end - not related to vite, I'm just running wrangler.

I just tried wrangler and it worked fine for me. Maybe was broken with earlier version.

TrySound avatar Jun 19 '25 15:06 TrySound

It is definitely broken on the latest wrangler 4.20.5 (in my case when using beta remote bindings) and is solved by patching pg-cloudflare

diff --git a/package.json b/package.json
index f0d277340fb57ea7caab3c9dd6408d92ca03361f..59839662842c57d944885de56bb05104c922b51a 100644
--- a/package.json
+++ b/package.json
@@ -15,6 +15,10 @@
         "import": "./esm/index.mjs",
         "require": "./dist/index.js"
       },
+      "workerd": {
+        "import": "./esm/index.mjs",
+        "require": "./dist/index.js"
+      },
       "default": "./dist/empty.js"
     }
   },

hexcowboy avatar Jun 23 '25 16:06 hexcowboy

Broken with wrangler sdk, both dev and remote modes

vladshcherbin avatar Jun 25 '25 13:06 vladshcherbin

please fix, huge blocker, don't care to patch the application

PabloG6 avatar Jun 26 '25 03:06 PabloG6

I'll open a PR to fix this today.

jamesopstad avatar Jun 26 '25 09:06 jamesopstad

~~I will add cloudflare as a default condition so that users don't need to add it but~~ user provided conditions already get merged with the defaults if you set them at the environment level.

export default defineConfig({
  environments: {
    [your_worker_environment_name]: {
      resolve: {
        conditions: ["cloudflare"]
      }
    }
  },
  plugins: [cloudflare()]
});

See https://developers.cloudflare.com/workers/vite-plugin/reference/vite-environments/ for more info about configuring Vite Environments. You can use this as a workaround until the fix is released.

jamesopstad avatar Jun 26 '25 11:06 jamesopstad

Having discussed this internally, adding the cloudflare condition on our side isn't the correct fix here. workerd is the standard export condition for Cloudflare Workers, as specified by WinterCG - https://runtime-keys.proposal.wintercg.org/#workerd.

Please use the workaround above for now and we'll open a PR to pg-cloudflare to use workerd instead.

jamesopstad avatar Jun 26 '25 11:06 jamesopstad

This happens outside vite too, I don't use vite (pure wrangler dev) and see this error

vladshcherbin avatar Jun 26 '25 12:06 vladshcherbin

Yes, the label vite-plugin assigned to this issue seems incorrect

kimminss0 avatar Jun 26 '25 12:06 kimminss0

The workaround in Wrangler until pg-cloudflare is updated is to run the command with WRANGLER_BUILD_CONDITIONS=cloudflare,workerd,worker,browser set.

jamesopstad avatar Jun 26 '25 12:06 jamesopstad

This has been fixed in [email protected] ([email protected]).

jamesopstad avatar Jun 27 '25 18:06 jamesopstad