bun icon indicating copy to clipboard operation
bun copied to clipboard

Error when use Bun build and @google-cloud libraries

Open ftognetto opened this issue 1 year ago • 18 comments

What version of Bun is running?

1.0.30+1424a196f

What platform is your computer?

Darwin 23.2.0 arm64 arm

What steps can reproduce the bug?

In my project I'm currently using @google-cloud/pubsub and @google-cloud/logging When I run the project with bun run index.ts I have no problem. When I build the project (bun build index.ts --target node (or bun, is the same) and run the index.js output I get

Error: undefined undefined: undefined
    at callErrorFromStatus (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:73779:19)
    at Object.onReceiveStatus (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:74458:68)
    at Object.onReceiveStatus (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:74292:139)
    at Object.onReceiveStatus (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:74259:174)
    at file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:77548:67
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
for call at
    at ServiceClientImpl.makeUnaryRequest (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:74429:30)
    at ServiceClientImpl.<anonymous> (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:74721:17)
    at file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:116309:23
    at file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:106773:14
    at repeat (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:106816:21)
    at Task._apiCall (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:106849:9)
    at Task.run (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:105558:33)
    at BundleExecutor._runNow (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:105750:12)
    at Timeout._onTimeout (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:105715:16)
    at listOnTimeout (node:internal/timers:573:17)
Emitted 'error' event on DerivedLogger instance at:
    at DerivedLogger.transportEvent (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:155313:14)
    at LoggingWinston.emit (node:events:526:35)
    at errorOrDestroy (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:26865:14)
    at onwriteError (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:28421:7)
    at onwrite (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:28439:7)
    at WritableState.onwrite (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:28306:7)
    at newCallback (file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:143776:13)
    at file:///Users/fabriziotognetto/quantosprojects/bigartwall/bigartwall-backend/bun/services/album-service/dist/index.js:64731:19 {
  code: undefined,
  details: undefined,
  metadata: Metadata { internalRepr: Map(0) {}, options: {} },
  note: 'Exception occurred in retry method that was not classified as transient'
}

Node.js v20.10.0

The same error when I try to send a message with @google-cloud/pubsub

What is the expected behavior?

Something I don't understand is happening during the transpiling done by Bun.build. I expect to make @google-cloud libraries work after Bun.build

What do you see instead?

No response

Additional information

I'm currently using bun workspace.

The tsconfig I'm using is (taken from bun best practices)

{
	"compilerOptions": {
		// Enable latest features
		"lib": ["ESNext"],
		"target": "ESNext",
		"module": "ESNext",
		"moduleDetection": "force",
		"jsx": "preserve", //"jsx": "react-jsx",
		"allowJs": true,

		// Bundler mode
		"moduleResolution": "Bundler",
		"allowImportingTsExtensions": true,
		"verbatimModuleSyntax": true,
		"noEmit": true,
		"esModuleInterop": true,

		"allowSyntheticDefaultImports": true,

		// Best practices
		"strict": true,
		"skipLibCheck": true,
		"noFallthroughCasesInSwitch": true,

		// Some stricter flags
		"noUnusedLocals": true,
		"noUnusedParameters": false,
		"noPropertyAccessFromIndexSignature": false,

		"types": [
			"bun-types" // add Bun global
		]
	}
}

ftognetto avatar Mar 11 '24 20:03 ftognetto

Is it possible to share a minimal reproduction in code?

Electroid avatar Mar 11 '24 21:03 Electroid

HI @Electroid yes ofc.

I create a repo here https://github.com/ftognetto/-oven-sh-bun-issues-9367

To use this repo you should have a gcp project with pubsub enabled. There are two env variables to setup

  • env "GOOGLE_APPLICATION_CREDENTIALS" pointing to a Google service account json (https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key)
  • TOPIC a pub sub topic where to publish.

I created some scripts in package.json that already load those env so you have everything in one place

  • start - start the application with bun, everything work fine here - you must change the env passing to bun here
  • build - build the app
  • run-build - run the build with node, here we have the problem - you must change the env passing to bun here

Notes:

  • I tried with node 16, 18 and 20
  • I tried changing various tsconfig options

ftognetto avatar Mar 12 '24 06:03 ftognetto

Hi @Electroid forgive the insistence are there any update on this? We are currently waiting for migrating an old lerna monorepo to bun workspaces and we have scheduled work on it. If you need I can send you in private a service account so you can test the repo I sent without additional operation from your side. Let me know, thank you

ftognetto avatar Mar 14 '24 09:03 ftognetto

This issue is stale and may be closed due to inactivity. If you're still running into this, please leave a comment.

github-actions[bot] avatar Aug 08 '24 20:08 github-actions[bot]

I can confirm this issue is still present in the latest version of bun. When running the program directly bun run main.ts it works fine, but after bun build ... --target bun, the error above is still there.

coupez avatar Dec 04 '24 19:12 coupez

Confirm this issue still persists and hope it will be fixed soon :D

We are migrating project from Node to Bun, it is almost completed and just find out the @google-cloud libraries will be broken with bun build (previously we PoC with bun run and it works fine) 😢 🤕

sukinoverse avatar Apr 25 '25 06:04 sukinoverse

Confirm this issue is still existing using Bun 1.2.15, no matter bundling to bun or node.

For my current workaround is to run my backend application directly without using bun build (bundling it), which is common for most backend projects.

Or another workaround I think of is to use @vercel/ncc to bundle it.

But anyway I hope this issue can get fixed so we can just use bun for everything...

darekaze avatar Jun 03 '25 02:06 darekaze

can confirm this exact issue still persists (im using @google-cloud/pubsub). Is there any updates on this issue?

bombnp avatar Jun 30 '25 16:06 bombnp

I can confirm that I have reproduced this issue using Bun v1.2.17 and the current canary build.

I was able to workaround this issue by compiling my bundle using --format cjs.

@cirospaciari Based on past contributions, it looks like you're fairly well versed in this area. Do you potentially have any insight into what could be going wrong here?

charles-salmon avatar Jun 30 '25 23:06 charles-salmon

The issue persists with pubsub.

Everything works perfectly in development, but once you build you have

Error: undefined undefined: undefined

I was able to make it work with pubsub by adding fallback: true which makes it use REST rather than gRPC

const pubsub = new PubSub({ fallback: true } as any);

dylantientcheu avatar Jul 04 '25 09:07 dylantientcheu

I have exactly the same error (Error: undefined undefined: undefined) in my CLI app after I build it with Bun.build. When I run the app in a dev mode (from the source), there are no errors.

Bun 1.2.18
"@google-cloud/storage": "^7.16.0"
"@google-cloud/translate": "^9.1.0"

phenomen avatar Jul 15 '25 22:07 phenomen

We are also getting the same error

fragkakis avatar Jul 18 '25 07:07 fragkakis

Can confirm this is still present in Bun 1.2.19. In my case with "@google-cloud/secret-manager": "^6.1.0".

I can confirm that I have reproduced this issue using Bun v1.2.17 and the current canary build.

I was able to workaround this issue by compiling my bundle using --format cjs.

And this build flag workaround also worked for me.

ashtonland avatar Jul 20 '25 23:07 ashtonland

Also affected by this issue. In our case we went with the fallback, which also supports rest explicitly, shame that it's not in the types.

new PubSub({ fallback: 'rest' } as any)

maxmil avatar Aug 21 '25 10:08 maxmil

Tried it with Bun 1.3.0 now and the issue still persist fyi. Would this perhaps work better when Bun gets HTTP2 support? At least for me this only happens in Google Cloud Run.

mackattack3k avatar Oct 13 '25 17:10 mackattack3k

Still happening. For anyone searching, just importing @google-cloud/monitoring also causes the issue. I don't see an error logged in this case, requests just hang, and setting { fallback: 'rest' } doesn't have any effect. The issue happens even before initializing it.

macecchi avatar Nov 28 '25 23:11 macecchi

YES! I've found the issue on this weird error. Also have it (I use Pub/Sub — @google-cloud/pubsub).

Without Single-file executable builds everything works fine. But when I build it, it fails with Error: undefined undefined: undefined during runtime when endpoint is called (the one which uses Pub/Sub). So weird!

I tried different params etc, didn't find solution so far (except not building a Single-file executable, just run JS code). I guess somebody from the Bun team should have a look. CC @Jarred-Sumner.

dmythro avatar Dec 05 '25 16:12 dmythro

This should be raised. I have the same issue

denysmohylin avatar Dec 08 '25 11:12 denysmohylin