bun icon indicating copy to clipboard operation
bun copied to clipboard

Yargs does not work with Bun.

Open eli-rich opened this issue 3 years ago • 1 comments
trafficstars

What version of Bun is running?

0.2.1 (canary)

What platform is your computer?

Darwin 21.6.0 Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:25 PDT 2022; root:xnu-8020.140.41~1/RELEASE_X86_64 x86_64 i386

What steps can reproduce the bug?

  1. Create a new directory
  2. bun init
  3. bun add yargs
  4. bun add -d @types/yargs
  5. Use the code from their example
  6. bun index.ts

How often does it reproduce? Is there a required condition?

Every time.

What is the expected behavior?

The program runs without error.

What do you see instead?

TypeError: Invalid regular expression: invalid flags while parsing module "/Users/elirichardson/buntest/node_modules/yargs/yargs"
      at /Users/elirichardson/buntest/index.ts:1:22

Additional information

NOTE: In the docs for bun there is one example showing you can import argv from bun. However, typescript errors saying Module '"bun"' has no exported member 'argv'. However, after bun index.ts that error is ignored and I get the same error as below.

Here is what my VS Code says:

eli-rich avatar Oct 22 '22 06:10 eli-rich

Confirmed on Ubuntu in virtual machine: Linux 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64

eli-rich avatar Oct 22 '22 06:10 eli-rich

I've gotten it working by

  1. calling .parse(process.argv) instead of .argv at the end of the chain
  2. yargs/build/lib/utils/set-blocking.js
export default function setBlocking(blocking) {
    if (typeof process === 'undefined')
        return;
    [process.stdout, process.stderr].forEach(_stream => {
++        if (_stream) {
            const stream = _stream;
            if (stream._handle &&
                stream.isTTY &&
                typeof stream._handle.setBlocking === 'function') {
                stream._handle.setBlocking(blocking);
            }
++        }
    });
}
  1. yargs/lib/platform-shims/esm.mjs
--    stdColumns: typeof process.stdout.columns !== 'undefined' ? process.stdout.columns : null
++  stdColumns: process && process.stdout && typeof process.stdout.columns !== 'undefined' ? process.stdout.columns : null

Very hacky, so hesitant to PR, but seems to work

boehs avatar Dec 02 '22 20:12 boehs

@robobun

var argv = require('yargs/yargs')(process.argv.slice(2)).argv;

if (argv.ships > 3 && argv.distance < 53.5) {
    console.log('Plunder more riffiwobbles!');
} else {
    console.log('Retreat from the xupptumblers!');
}

Electroid avatar Dec 07 '22 20:12 Electroid

@Electroid here you go!

Retreat from the xupptumblers!
Code
var argv = require('yargs/yargs')(process.argv.slice(2)).argv;

if (argv.ships > 3 && argv.distance < 53.5) {
    console.log('Plunder more riffiwobbles!');
} else {
    console.log('Retreat from the xupptumblers!');
}

Ran using the latest build of Bun, an all-in-one JavaScript runtime.

robobun avatar Dec 07 '22 20:12 robobun

yargs should work in Bun v0.3.0, if you're still having issues please feel free to open another issue.

Electroid avatar Dec 07 '22 20:12 Electroid

@Electroid here you go!

1 | yargs;
   ^
ReferenceError: Can't find variable: yargs
      at /tmp/bun-9zJHg9/index.tsx:1:0
Code
yargs

Ran using the latest build of Bun, an all-in-one JavaScript runtime.

robobun avatar Dec 07 '22 20:12 robobun