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

Astro DB build fails with Cloudflare adapter #10668

Closed
1 task
casungo opened this issue Apr 3, 2024 · 41 comments · Fixed by #10677
Closed
1 task

Astro DB build fails with Cloudflare adapter #10668

casungo opened this issue Apr 3, 2024 · 41 comments · Fixed by #10677
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) pkg: db

Comments

@casungo
Copy link

casungo commented Apr 3, 2024

Astro Info

Astro                    v4.5.15
Node                     v20.7.0
System                   Windows (x64)
Package Manager          pnpm
Output                   hybrid
Adapter                  none
Integrations             astro:db
                         @astrojs/db/file-url

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I am logged into Astro Studio, linked, and able to work with production data using the --remote flag in dev mode.
When attempting to build with the --remote flag, I get the following error:

[ERROR] [vite] x Build failed in 1.30s
[commonjs--resolver] [plugin vite:resolve] Cannot bundle Node.js built-in "url" imported from "node_modules.pnpm@[email protected]\node_modules@astrojs\db\dist\runtime\index.js". Consider disabling ssr.noExternal or remove the built-in dependency.

What's the expected result?

astro build --remote should build using the remote data.

(forked the minimal reproducible example from #10617 since the issue is in the db integration)

(for the minimal reproducible example to work you would need to download the project, login, link and then execute a build with the remote flag)

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-6icpvd-oyrvsw

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Apr 3, 2024
@matthewp matthewp self-assigned this Apr 3, 2024
@matthewp matthewp added pkg: db - P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) and removed needs triage Issue needs to be triaged labels Apr 3, 2024
@aklevecz
Copy link

aklevecz commented Apr 3, 2024

Running into this same issue. Appreciate the urgency!

@matthewp
Copy link
Contributor

matthewp commented Apr 3, 2024

I'm not seeing the issue here. Will try on my Windows computer to make sure it's not specific to Windows.

Could you try force reinstalling? Maybe something didn't get installed correctly.

@aklevecz
Copy link

aklevecz commented Apr 3, 2024

For myself I was able to successfully build and deploy once I drank more coffee and realized that I'm using the Cloudflare adapter, and followed the docs on Node compatability to enable the Node url library.

My config looks like this now:

import { defineConfig } from "astro/config";
import cloudflare from "@astrojs/cloudflare";

import db from "@astrojs/db";

export default defineConfig({
  output: "server",
  adapter: cloudflare({
    platformProxy: {
      enabled: true
    }
  }),
  integrations: [db()],
  vite: {
    ssr: {
      external: ["url"]
    }
  }
});

@matthewp
Copy link
Contributor

matthewp commented Apr 3, 2024

@aklevecz Yeah the service is unfortunately not yet compatible with Cloudflare out of the box. Hoping to work with @alexanderniebuhr to fix that soon.

@matthewp
Copy link
Contributor

matthewp commented Apr 3, 2024

@casungo are you also using Cloudflare adapter?

@casungo
Copy link
Author

casungo commented Apr 3, 2024

@casungo are you also using Cloudflare adapter?

yep, idk why the astro info didn't pick that up

@matthewp
Copy link
Contributor

matthewp commented Apr 3, 2024

@casungo cool, does the config @aklevecz help in your case?

@casungo
Copy link
Author

casungo commented Apr 3, 2024

@matthewp yes, thank you so much! that works as a workaround
[build] Complete!

@matthewp matthewp added - P3: minor bug An edge case that only affects very specific usage (priority) and removed - P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) labels Apr 3, 2024
@matthewp matthewp changed the title "Build failed" when building with --remote Astro DB build fails with Cloudflare adapter Apr 3, 2024
@serdar
Copy link

serdar commented Apr 4, 2024

Unfortunately I'm getting Cannot create a remote client: missing app token. reintroduced when it was fixed with this ticket (#10617):

Subset of package.json

  "@astrojs/cloudflare": "^10.0.2",
    "@astrojs/db": "^0.10.0",
    "@astrojs/mdx": "^2.2.3",
    "@astrojs/tailwind": "^5.1.0",
    "astro": "^4.5.15"

astro.config.mjs:

export default defineConfig({
  output: "server",
  adapter: cloudflare({
    platformProxy: {
      enabled: false
    },
    functionPerRoute: false,
    routes: {
      exclude: ['/post/*']
    }
  }),
  integrations: [mdx(), db(), tailwind()],
  vite: {
    ssr: {
      external: ["url"]
    }
  }
});

@aklevecz
Copy link

aklevecz commented Apr 4, 2024

@serdar is this happening when you deploy to Cloudflare or developing locally?

I got this error when I first deployed my app to Cloudflare, but it worked once I created a token in Astro Studio and set the ASTRO_STUDIO_APP_TOKEN Environment Variable in the Settings for my Pages site.

https://docs.astro.build/en/recipes/studio/#create-a-studio-app-token

@serdar
Copy link

serdar commented Apr 4, 2024

@aklevecz no issues locally, only whilst running the deploy command (astro build --remote && wrangler pages deploy ./dist --branch=production).

I have the app token generated in Astro Studio and have the value set in the Environments tab in Cloudflare.

@aklevecz
Copy link

aklevecz commented Apr 4, 2024

@serdar oh I see, so you're building the project locally and then pushing it? I personally like connecting my Pages to Github and allowing my pushed commits to automatically trigger the build steps.

But I am able to run astro build --remote on my machine after running:

# Log in to Astro Studio with your GitHub account
npx astro login

# Link to a new project by following the prompts
npx astro link

Does that work for you? Apologies if you had already tried that.

@serdar
Copy link

serdar commented Apr 4, 2024

Hey @aklevecz no need to apologize! I appreciate your contributions :) I'll try the Github route and see if that changes anything.

But even running astro build --remote locally I'm getting the error.

I reauth'ed the CLI and relinked the project (can see a link file within the .astro folder) but still getting the same error. Connection wise it seem to be able to get there:

⠧ Connecting to remote database...16:40:59 [build] Waiting for integration "astro:db", hook "astro:config:setup"...
✔ Connected to remote database.

@alexanderniebuhr
Copy link
Member

alexanderniebuhr commented Apr 4, 2024

@matthewp Ohh.. that is new to me.. It was compatible with Cloudflare before. Did we change anything which introduced the use of url in runtime..? Or is it used during astro build and not used during runtime.. if so it should be fixed with (withastro/adapters#220)?

@alexanderniebuhr
Copy link
Member

@serdar IIRC you need to have the token during build and runtime. So you would need to set it as an environmental variable on the machine/CI you use to build and as a environmental variable in the Cloudflare Dashboard for all your environments.

@serdar
Copy link

serdar commented Apr 4, 2024

@alexanderniebuhr I have it already set in my .env and .dev.vars file

@alexanderniebuhr
Copy link
Member

Just to make sure, you did set them in the Cloudflare dashboard?
I don't think env or dev.var file get's pushed to Cloudflare

@serdar
Copy link

serdar commented Apr 4, 2024

Yep, 100%. BTW calls to Astro DB don't have the same issue when called from .astro files - only when I'm using the Server Endpoints.

@alexanderniebuhr
Copy link
Member

only when I'm using the Server Endpoints

@matthewp very important detail, I think I saw a similar on Discord

@serdar
Copy link

serdar commented Apr 4, 2024

only when I'm using the Server Endpoints

@matthewp very important detail, I think I saw a similar on Discord

That could be me btw :) I'm raven!

@ElianCodes
Copy link
Member

I'm also running into this issue with the Vercel Adapter, so I don't think it's cloudflare specific. It was introduced in astro:db v0.10.0

@matthewp
Copy link
Contributor

matthewp commented Apr 4, 2024

@ElianCodes can you file a separate issue (or ping me on discord to debug)? Would like to keep this one cloudflare specific as it's likely coming from different causes.

@matthewp
Copy link
Contributor

matthewp commented Apr 4, 2024

@alexanderniebuhr Started depending on pathToFileURL here: 157392e. Can maybe do it without. But does process.cwd() work in Cloudflare? Probably this codepath can't work in cloudflare anyways.

@alexanderniebuhr
Copy link
Member

I would separate both issues, since they are not the same. One is about Cloudflare support the other is that it seems there is a difference between Astro files and API Endpoints :)

@matthewp
Copy link
Contributor

matthewp commented Apr 4, 2024

Talking over on discord it seems like removing the usage of pathToFileURL should resolve this issue.

@matthewp
Copy link
Contributor

matthewp commented Apr 4, 2024

Should be fixed with @astrojs/[email protected] which should be out in a few minutes.

@serdar
Copy link

serdar commented Apr 4, 2024

Appreciate all the effort going into this issue but still broken for me after updating to the latest version.

Issuing astro build --remote results in

Cannot create a remote client: missing app token.
  Stack trace:
    at createRemoteDatabaseClient (file:///home....

Astro versions:

    "@astrojs/check": "^0.5.10",
    "@astrojs/cloudflare": "^10.0.2",
    "@astrojs/db": "^0.10.1",
    "@astrojs/mdx": "^2.2.4",
    "@astrojs/tailwind": "^5.1.0",
    "astro": "^4.5.16",

ASTRO_STUDIO_APP_TOKEN set locally in .env and in Cloudflare

@casungo
Copy link
Author

casungo commented Apr 4, 2024

for me the issue is fixed, the site builds fine without the vite external url config workaround, this is my astro info output:

Astro                    v4.5.16
Node                     v20.7.0
System                   Windows (x64)
Package Manager          pnpm
Output                   hybrid
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/sitemap
                         @astrojs/svelte
                         astro:db
                         @astrojs/db/file-url

@serdar
Copy link

serdar commented Apr 4, 2024

Interesting, switching to hybrid works but I need to be using output: server for the API endpoints to remain dynamic and not statically generated.

@aklevecz
Copy link

aklevecz commented Apr 4, 2024

Everything works for me as well without using the vite external, and I use output: server

Astro                    v4.5.15
Node                     v21.5.0
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             astro:db
                         @astrojs/db/file-url

@aklevecz
Copy link

aklevecz commented Apr 4, 2024

@serdar have you tried adding it into your build command?

"build": "astro check && ASTRO_STUDIO_APP_TOKEN=token astro build --remote"

Not the sexiest solution, but I can't think of a more direct way to set an environment variable

@serdar
Copy link

serdar commented Apr 4, 2024

@aklevecz thank you! That worked, agreed not sexy, but gets me running for now :)

I still can't pinpoint the problem, created a new project but with github actions for deployment and that worked without a problem.

@bkyerv
Copy link

bkyerv commented Apr 11, 2024

@serdar have you tried adding it into your build command?

"build": "astro check && ASTRO_STUDIO_APP_TOKEN=token astro build --remote"

Not the sexiest solution, but I can't think of a more direct way to set an environment variable

is it possible to access env vars from script? otherwise token gets exposed when commited to remote repo like github?

@chadananda
Copy link

I'm on a Macbook (M3) and have been unable to build since adding Astro DB.

I've updated to 0.10.1 but still have the issue. I thought it was related to limited ulimit on macs but pushing to Vercel results in the same build failure. It looks like::

ENOENT: no such file or directory, open '/vercel/path0/.vercel/output/_functions/_astro/...DqH3CDp7_2vCbLk.webp'

I have no data in the database other than a few seeded records for testing. They work great in local dev mode with no errors.

@alexanderniebuhr
Copy link
Member

@chadananda thanks for the update. I think the issue you face is different than the original one. The original one is related to the Cloudflare adapter, while the issue you explain look like being related to vercel. I always suggest to open a support thread on Discord first or open a new Issue on GitHub with all the informations for your project and include a minimal reproduction.

@aklevecz
Copy link

@chadananda looks like Vercel can't find a webp file. Is there an image file you aren't committing to GitHub?

@diogoribeirodev
Copy link

i still get the error when using cloudflare

@alexanderniebuhr
Copy link
Member

@diogoribeirodev thank you for reporting that, can you please open a new issue with a minimal reproducible example which shows your setup and the error?

@YannisSefsaf
Copy link

YannisSefsaf commented Apr 20, 2024

@serdar have you tried adding it into your build command?

"build": "astro check && ASTRO_STUDIO_APP_TOKEN=token astro build --remote"

Not the sexiest solution, but I can't think of a more direct way to set an environment variable

Tried this but it doesn't work for me either. I keep getting this error: "Cannot create a remote client: missing app token."

I'm using the cloudflare adapter with server mode, and I have the ASTRO_STUDIO_APP_TOKEN set in the Cloudflare dashboard as well as in my .env file.

@alexanderniebuhr
Copy link
Member

@YannisSefsaf Please open a new issue and provide a minimal reproduction, or explanation what steps you did after using create-astro. I just did another project from scratch and can't reproduce any issue on Cloudflare

@chiubaca
Copy link

for me the issue is fixed, the site builds fine without the vite external url config workaround, this is my astro info output:

Astro                    v4.5.16
Node                     v20.7.0
System                   Windows (x64)
Package Manager          pnpm
Output                   hybrid
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/sitemap
                         @astrojs/svelte
                         astro:db
                         @astrojs/db/file-url

setting my output to hybrid was also what fixed this for me. no need for for this - "build": "astro check && ASTRO_STUDIO_APP_TOKEN=token astro build --remote"

I had to add export const prerender = false; to bunch of my API routes and asto files which was a bit annoying, but luckily I dont have a big site

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) pkg: db
Projects
None yet
Development

Successfully merging a pull request may close this issue.