oven-sh / bun

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

Build script using pbjs fails when called with bun, but not npm. Works when build script uses npm, but called with bun run. #9440

Open losh11 opened 6 months ago

losh11 commented 6 months ago

What version of Bun is running?

1.0.31+e25675121

What platform is your computer?

Darwin 23.2.0 x86_64 i386

What steps can reproduce the bug?

My library has a build command which previously worked when doing npm run build. This build command runs another command calls bun run protobuf, this command in return runs another commands which uses pbjs to generate js from a .proto file.

package.json

"build": "rm -rf dist && mkdir dist/ && bun run protobuf && bun tsc -p ./src",
"protobuf": "bun run protobuf:rpc & ...",
"protobuf:rpc": "mkdir -p dist/protos && pbjs -t static-module -w commonjs -o dist/protos/rpc.js src/protos/rpc.proto && pbjs -t static-module src/protos/rpc.proto | pbts -o dist/protos/rpc.d.ts - && cp dist/protos/rpc.* ./src/protos/",

What is the expected behavior?

Changing from npm -> bun in the build scripts fails to run with error below. However previously when build scripts use npm, running bun run build worked as expected.

What do you see instead?

$ bun run build
$ rm -rf dist && mkdir dist/ && bun run protobuf && bun tsc -p ./src
$ bun run protobuf:rpc && bun run protobuf:walletunlocker && bun run protobuf:stateservice && bun run protobuf:wtclient
$ mkdir -p dist/protos && pbjs -t static-module -w commonjs -o dist/protos/rpc.js src/protos/rpc.proto && pbjs -t static-module src/protos/rpc.proto | pbts -o dist/protos/rpc.d.ts - && cp dist/protos/rpc.* ./src/protos/
installing uglify-js@^3.7.7
installing escodegen@^1.13.0
installing estraverse@^5.1.0
installing estraverse@^5.1.0
node:child_process:965
    throw err;
    ^

Error: Command failed: npm --silent install estraverse@^5.1.0
    at checkExecSyncError (node:child_process:890:11)
    at Object.execSync (node:child_process:962:15)
    at modInstall (/Users/loshan/Repos/ltc/react-native-lndltc/node_modules/protobufjs/cli/util.js:129:19)
    at exports.setup (/Users/loshan/Repos/ltc/react-native-lndltc/node_modules/protobufjs/cli/util.js:156:5)
    at Object.<anonymous> (/Users/loshan/Repos/ltc/react-native-lndltc/node_modules/protobufjs/cli/pbts.js:8:6)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12) {
  status: 190,
  signal: null,
  output: [ null, null, null ],
  pid: 32856,
  stdout: null,
  stderr: null
}

Node.js v18.19.1
node:events:495
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at afterWriteDispatched (node:internal/stream_base_commons:160:15)
    at writeGeneric (node:internal/stream_base_commons:151:3)
    at Socket._writeGeneric (node:net:962:11)
    at Socket._write (node:net:974:8)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at targetCallback (/Users/loshan/Repos/ltc/react-native-lndltc/node_modules/protobufjs/cli/pbjs.js:317:36)
    at /Users/loshan/Repos/ltc/react-native-lndltc/node_modules/protobufjs/cli/targets/static-module.js:27:9
    at static_target (/Users/loshan/Repos/ltc/react-native-lndltc/node_modules/protobufjs/cli/targets/static.js:47:16)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

Node.js v18.19.1
error: script "protobuf:rpc" exited with code 1
error: script "protobuf" exited with code 1
error: script "build" exited with code 1

Additional information

Weirdly, running the bun run build script after it first errors, seems to work as expected.

Electroid commented 6 months ago

Based on the output of the command, even though you are running bun run, the script is using node. What if you try bun --bun run build?

ttmx commented 2 months ago

Reproduced independently.

me@minimind:~/src/cmet/discord-bot$ bun run --bun gen
$  pbts -o src/.generated/gtfs.d.ts src/.generated/gtfs.js
 7 |  */
 8 | const _ = require('lodash');
 9 | const fs = require('fs');
10 | const Module = require('module');
11 | 
12 | const originalWrapper = Module.wrapper.slice(0);
                                    ^
TypeError: undefined is not an object (evaluating 'Module.wrapper.slice')
      at //discord-bot/node_modules/requizzle/lib/loader.js:12:32
      at //discord-bot/node_modules/requizzle/lib/requizzle.js:10:7
      at //discord-bot/node_modules/requizzle/index.js:8:7
      at //discord-bot/node_modules/jsdoc/jsdoc.js:17:5
      at //discord-bot/node_modules/jsdoc/jsdoc.js:34:36

Bun v1.1.17 (Linux x64)
121 |             try { cleanup.forEach(fs.unlinkSync); } catch(e) {/**/} cleanup = [];
122 | 
123 |             if (code) {
124 |                 out = out.join("").replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
125 |                 process.stderr.write(out);
126 |                 var err = Error("code " + code);
                                ^
error: code 1
      at //discord-bot/node_modules/protobufjs-cli/pbts.js:126:27
      at emit (node:events:180:48)
      at #maybeClose (node:child_process:809:14)
      at #handleOnExit (node:child_process:591:16)

Bun v1.1.17 (Linux x64)
error: script "gen" exited with code 1

No error if I omit the --bun flag.

Thank you!

ttmx commented 2 months ago

https://github.com/carrismetropolitana/discord-bot Can reproduce here. Had to add a node install to my Dockerfile to actually bundle stuff properly.

Try to run bun run --bun gen.