Skip to content

Bun runtime fails if there's an .env file in the working directory #130

Closed
@Burgestrand

Description

@Burgestrand

Hi!

When there's an .env in the working directory of Bun, the Bun runtime crashes with a JSON parse error.

I believe the reason for the crash is because Bun outputs [0.32ms] ".env" to STDERR when it loads the .env-file, and the bun runner (or external runtime?) combines STDERR with STDOUT into a single output value, which is then parsed with JSON.parse.

It seems the external runtime merges STDERR on purpose:

io = IO.popen(binary.split(' ') << filename, **(@popen_options.merge({err: [:child, :out]})))

Reproduction steps

This is reproducible on master.

require "bundler/setup"
require "execjs"

ExecJS.runtime = ExecJS::Runtimes::Bun

Dir.mktmpdir do |dir|
  Dir.chdir(dir)

  # Works fine.
  $stdout.puts ExecJS.eval("1+1")

  # Crashes with JSON parse error.
  File.write(".env", "FOO=BAR")
  $stdout.puts ExecJS.eval("1+1")
end

Result:

2
/Users/kim/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/json/common.rb:216:in `parse': 451: unexpected token at 'ms] ".env" (JSON::ParserError)
["ok"]'
        from /Users/kim/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/json/common.rb:216:in `parse'
        from /Users/kim/Projects/execjs/lib/execjs/external_runtime.rb:69:in `extract_result'
        from /Users/kim/Projects/execjs/lib/execjs/external_runtime.rb:40:in `exec'
        from /Users/kim/Projects/execjs/lib/execjs/external_runtime.rb:15:in `initialize'
        from /Users/kim/Projects/execjs/lib/execjs/runtime.rb:68:in `new'
        from /Users/kim/Projects/execjs/lib/execjs/runtime.rb:68:in `compile'
        from /Users/kim/Projects/execjs/lib/execjs/runtime.rb:55:in `eval'
        from /Users/kim/Projects/execjs/lib/execjs/module.rb:23:in `eval'
        from example.rb:14:in `block in <main>'
        from /Users/kim/.asdf/installs/ruby/3.1.2/lib/ruby/3.1.0/tmpdir.rb:96:in `mktmpdir'
        from example.rb:6:in `<main>'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions