bun icon indicating copy to clipboard operation
bun copied to clipboard

BullMQ fails to build with Bun

Open knajjars opened this issue 1 year ago • 6 comments

What version of Bun is running?

v1.0.22

What platform is your computer?

Darwin 22.6.0 arm64 arm

What steps can reproduce the bug?

  1. install BullMQ
  2. Add a bull queue and dummy worker that console.logs in the index.ts
  3. build the index.ts using bun build --target bun --outdir build
  4. run bun build/index.ts

error

function getPkgJsonDir() {
      for (const modPath of module.paths || []) {
                              ^
ReferenceError: Can't find variable: module

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

knajjars avatar Jan 10 '24 17:01 knajjars

Using --compile I get:

ReferenceError: Can't find variable: module_script_loader
      at getPkgJsonDir (/$bunfs/root/cli:46442:25)
      at new ScriptLoader (/$bunfs/root/cli:46509:21)
      at /$bunfs/root/cli:46722:20

MaikuMori avatar Jan 22 '24 14:01 MaikuMori

I'm getting the same error from a bundled BullMQ server script. Running the TS file directly works fine, but building (with target bun) and then running it throws ReferenceError: Can't find variable: module_script_loader.

Seems to be caused by this function, which gets bundled to this:

var getPkgJsonDir = function() {
  for (const modPath of module_script_loader.paths || []) {
    try {
      const prospectivePkgJsonDir = path3.dirname(modPath);
      fs2.accessSync(modPath, fs2.constants.F_OK);
      return prospectivePkgJsonDir;
    } catch (e) {
    }
  }
  return "";
};

Curetix avatar Jan 28 '24 16:01 Curetix

Same error

mrsum avatar Feb 29 '24 08:02 mrsum

Any news on this ?

shm0x avatar Mar 05 '24 10:03 shm0x

I ran into this error too, the BullMQ method that @Curetix mentions seems to hold a clue; the method's comment references a discussion on stack overflow which states;

Node publishes all the module search paths to module.paths. We can traverse these and pick the first one that resolves.

It would appear that Bun follows this behavior only when running. When building/compiling the 'module' global seems to be replaced somehow, which suggests to me it may not be recognized or set as a global.

A simple test file confirms this;

// index.ts
console.log(module.paths);

When launched with bun run this will produce an array output. When built via bun build --target bun --outfile out.js index.ts and subsequently run via bun run out.js this produces undefined.

@knajjars I suggest you change the title of your bug report so to something like module.paths not set when using bun build - surely there will be modules other than BullMQ suffering from this bug.

EDIT: There seems to be a bit more to it; apparently this is caused by BullMQ using the module.paths to dynamically load Lua files onto the Redis instance, which would hide these files from the Bun bundler. These would need to be turned into assets by the bundler somehow.

rvanderfeer avatar Mar 19 '24 17:03 rvanderfeer

Hey! Same problem here. Did anyone found a solution?

Daniel-Manea avatar May 21 '24 12:05 Daniel-Manea

Hello everyone! I encountered the same problem, and I'm still not sure if this is the best solution. However, this is what worked for me using Bun with SandboxedJob, after reviewing the source code.

// worker.ts

import { ParentCommand, type SandboxedJob } from "bullmq";

export default async function(job: SandboxedJob) {
    console.log(`Job ${job.name}:${job.id} started with data`, job.data);
    
   // return value
    exit({
        sent: true,
    });
}

function exit<T extends any>(data: T, success: boolean = true) {
    process.send?.({
        cmd: success ? ParentCommand.Completed : ParentCommand.Failed,
        data: data
    });

    process.exit(0);
}

This way, the jobs are no longer marked as failed. But again, I'm not sure if it's the best solution.

zenozaga avatar Aug 04 '24 16:08 zenozaga