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 is unable to use the bun:sqlite package. #8476

Closed
1 task
sz55net opened this issue Sep 9, 2023 · 6 comments
Closed
1 task

Astro is unable to use the bun:sqlite package. #8476

sz55net opened this issue Sep 9, 2023 · 6 comments
Labels
needs triage Issue needs to be triaged

Comments

@sz55net
Copy link

sz55net commented Sep 9, 2023

Astro Info

Astro                    v3.0.12
Node                     v18.17.0
System                   macOS (x64)
Package Manager          bun
Output                   static
Adapter                  none
Integrations             none

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

No response

Describe the Bug

Astro is unable to use the bun:sqlite package.
Error:

george@Georges-MacBook-Pro words % bun run dev
$ astro dev
  🚀  astro  v3.0.12 started in 97ms
  
  ┃ Local    http://localhost:4321/
  ┃ Network  use --host to expose
  
Error: The following dependencies are imported but could not be resolved:

  bun:sqlite (imported by /Users/george/Desktop/code/cabbage/words/src/util/db.ts)

Are they installed?
    at file:///Users/george/Desktop/code/cabbage/words/node_modules/vite/dist/node/chunks/dep-df561101.js:45705:23
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///Users/george/Desktop/code/cabbage/words/node_modules/vite/dist/node/chunks/dep-df561101.js:45114:38
^C

What's the expected result?

The bun:sqlite can be imported and used.

Link to Minimal Reproducible Example

https:/homelyseven250/astro-bun-bug

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 Sep 9, 2023
@Princesseuh
Copy link
Member

Princesseuh commented Sep 9, 2023

Astro is a Node framework. If something specific to another runtime doesn't work, not only I don't know what we could do about it, but also, it's unrelated to us. I'd recommend raising the issue over to the people at Bun.

@Jarred-Sumner
Copy link

Jarred-Sumner commented Sep 10, 2023

I hate saying this but

This appears to be a bug in Astro.

Astro's module resolution prevents bun builtin modules from being used. Astro resolves modules before Bun's module resolution runs. Bun cannot fix this without overriding Astro's module resolution altogether, which would break Astro on Bun in other ways and is generally not something you want.

You can fix this by marking bun:* as external, probably similarly to what happens with node:*

@Princesseuh
Copy link
Member

I hate saying this but

This appears to be a bug in Astro.

Astro's module resolution prevents bun builtin modules from being used. Astro resolves modules before Bun's module resolution runs. Bun cannot fix this without overriding Astro's module resolution altogether, which would break Astro on Bun in other ways and is generally not something you want.

You can fix this by marking bun:* as external, probably similarly to what happens with node:*

I would assume that this affect every Vite SSR app then, doesn't it?

@andrioid
Copy link

andrioid commented Sep 10, 2023

I just tried @Jarred-Sumner's suggestion, without luck. And yes @Princesseuh, I would assume this affects any Vite SSR app.

I couldn't even get the dev-server running without adding "bun:sqlite" to the vite.optimizeDeps.exclude configuration option. Nothing seems to help with accessing the bun:sqlite in SSR though. I've tried "external", disabling "optimizeDeps", etc.

I kept getting this:

 error   Cannot find module 'bun:sqlite' imported from '/home/andri/git/event-puffin-astro/src/lib/db/index.ts'
Error: Cannot find module 'bun:sqlite' imported from '/home/andri/git/event-puffin-astro/src/lib/db/index.ts'
    at nodeImport (file:///home/andri/git/event-puffin-astro/node_modules/vite/dist/node/chunks/dep-df561101.js:56010:25)
    at ssrImport (file:///home/andri/git/event-puffin-astro/node_modules/vite/dist/node/chunks/dep-df561101.js:55912:30)
    at eval (/home/andri/git/event-puffin-astro/src/lib/db/index.ts:3:50)
    at instantiateModule (file:///home/andri/git/event-puffin-astro/node_modules/vite/dist/node/chunks/dep-df561101.js:55974:15)

In Vite's ssrModuleLoader.ts there's an escape hatch for node: and data:. I'd assume we'd need Bun added there, for this to work.

async function nodeImport(
  id: string,
  importer: string,
  resolveOptions: InternalResolveOptionsWithOverrideConditions,
) {
  let url: string
/* WE NEED TO ADD BUN HERE */
  if (id.startsWith('node:') || id.startsWith('data:') || isBuiltin(id)) {
    url = id
  } else {
    // Internal Vite resolution stuff. Uses node-resolve Rollup plugin.
}

In other words, this needs to be fixed in Vite, not Astro. I'd love to raise the issue, but I'm not familiar with the SSR API in Vite in order to reproduce it there.

@fabriziogiordano
Copy link

It appears the Vite use case is handled by Bun.

By adding --bun: The --bun flag tells Bun to run Vite's CLI using bun instead of node.

bun --bun run astro build

Here the info from the doc: https://bun.sh/guides/ecosystem/vite

@bluwy
Copy link
Member

bluwy commented Sep 11, 2023

Feel free to open an issue in Vite regarding this. I think that line should indeed fix it, but Vite probably needs a more standardized flow internally for handling different runtime builtin protocols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

6 participants