oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.26k stars 2.69k forks source link

Bun.build does not seem to include all imports #8993

Open itsyoboieltr opened 7 months ago

itsyoboieltr commented 7 months ago

What version of Bun is running?

1.0.27+c34bbb2e3

What platform is your computer?

Darwin 23.2.0 arm64 arm

What steps can reproduce the bug?

I was trying to make a small lambda with tfjs, so I wanted to bundle it to make the size small.

index.ts

import { type Serve } from 'bun';
import { tensor } from '@tensorflow/tfjs-core';
import '@tensorflow/tfjs-backend-cpu';

export default {
  async fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === '/inference') {
      return new Response(tensor([Math.random()]).toString());
    }
    return new Response(null, { status: 404 });
  },
} satisfies Serve;

run build command:

bun build index.ts --outdir dist --target bun --minify

run bundled lambda:

bun dist/index.js

Error:

error: No backend found in registry.
      at getSortedBackends (dist/index.js:2:82420)
      at initializeBackendsAndReturnBest (dist/index.js:2:82894)
      at backend (dist/index.js:2:76900)
      at makeTensor (dist/index.js:2:91761)
      at dist/index.js:2:202590
      at fetch (dist/index.js:2:202658)

I can reproduce this error message by removing import '@tensorflow/tfjs-backend-cpu'; from index.ts. It seems like this import line does not get resolved correctly or is somehow omitted from the bundled js.

What is the expected behavior?

A random tensor returned from server after running bundled js:

Tensor
    [0.7482892]

What do you see instead?

error: No backend found in registry.
      at getSortedBackends (dist/index.js:2:82420)
      at initializeBackendsAndReturnBest (dist/index.js:2:82894)
      at backend (dist/index.js:2:76900)
      at makeTensor (dist/index.js:2:91761)
      at dist/index.js:2:202590
      at fetch (dist/index.js:2:202658)

Additional information

Build also gives this warning:

  ./index.js  101.43 KB
60 |     "./dist/platforms/*.js",
         ^
warn: wildcard sideEffects are not supported yet, which means this package will be deoptimized
   at node_modules/@tensorflow/tfjs-core/package.json:60:5

[43ms] bundle 569 modules

Maybe somehow the deoptimization breaks it?

paperdave commented 7 months ago

When i run this file

import { tensor } from '@tensorflow/tfjs-core';

tensor([Math.random()]).toString();

it throws this same error in both node.js and bun run. It doesn't appear intentional that tfjs-core, without one of the backend packages, will "just work"

just checked. they say this on their npm page for this package.

Note: If you are only importing the Core API, you also need to import a backend (e.g., tfjs-backend-cpu, tfjs-backend-webgl, tfjs-backend-wasm).

you likely want to use https://www.npmjs.com/package/@tensorflow/tfjs-node instead

not a bug in bun.

paperdave commented 7 months ago

i completely misread, it is a bundler issue.

paperdave commented 7 months ago

doesn't happen here so it does feel likely to the todo comment

image