bun icon indicating copy to clipboard operation
bun copied to clipboard

Assigning process.env.NODE_ENV -> "SyntaxError: Left side of assignment is not a reference"

Open JL102 opened this issue 3 years ago • 8 comments

Version

0.1.2

Platform

Linux JDESK 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux ---- Microsoft Windows NT 10.0.19043.0 x64

What steps will reproduce the bug?

Attempt to directly assign a string to process.env.NODE_ENV in any JS or TS script.

process.env.FOO = 'test1'; // works without errors

process.env.NODE_ENV = 'test2'; // SyntaxError: Left side of assignment is not a reference.

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

Consistently

What is the expected behavior?

Expected behavior: A string is assigned to process.env.NODE_ENV just like any other assignment to process.env.X.

What do you see instead?

SyntaxError: Left side of assignment is not a reference.
      at /path/to/test.js:3:0

Additional information

No response

JL102 avatar Jul 10 '22 23:07 JL102

This happens because process.env.NODE_ENV doesn't exist at runtime. This enables dead code elimination

This:

process.env.NODE_ENV = 'test1';

Becomes:

"development" = 'test1';

To set process.env.NODE_ENV, use --define like so:

bun run --define:process.env.NODE_ENV="'production'"  script.js

Perhaps there should be a better error or a runtime-only version should be set. I'm not exactly sure what to do, but code using this shouldn't break.

Jarred-Sumner avatar Jul 11 '22 10:07 Jarred-Sumner

This happens because process.env.NODE_ENV doesn't exist at runtime. This enables dead code elimination

Is that in a spec somewhere or was it decided by you and/or other Bun developers?

NodeJS does allow NODE_ENV to be overwritten at runtime:

~$ cat test.js
console.log(`process.env.NODE_ENV = ${process.env.NODE_ENV}`);

require('./test1');

console.log(`process.env.NODE_ENV = ${process.env.NODE_ENV}`);
~$ cat test1.js
process.env.NODE_ENV = 'test1';
~$ node test.js
process.env.NODE_ENV = undefined
process.env.NODE_ENV = test1

JL102 avatar Jul 11 '22 19:07 JL102

It breaks jest image

bun --define process.env.NODE_ENV:"test" path/to/jest also does not work

SegmentationFault at 5680500659196

–––– bun meta ––––
Bun v0.1.3 macOS x64 19.4.0
AutoCommand: define
Elapsed: 43ms | User: 28ms | Sys: 13ms
RSS: 20.94MB | Peak: 20.94MB | Commit: 67.11MB | Faults: 0
–––– bun meta ––––

Ask for #help in https://bun.sh/discord or go to https://bun.sh/issues

smelukov avatar Jul 12 '22 08:07 smelukov

Same is true for process.env.PATH. The GitHub Actions toolkit sets this here so currently it isn't possible to use this toolkit to create JS GitHub Actions powered by bun.

GregBrimble avatar Sep 17 '22 10:09 GregBrimble

Edit: This might actually be related to something else, e.g. child-process not yet supported.

Seems to affect ShellJS as well.

Test code:

import * as shell from 'shelljs';
shell.echo('hello');

Error:

20 | // Include the docs for all the default commands
21 | //@commands
22 | 
23 | // Load all default commands
24 | require('./commands').forEach(function (command) {
25 |   require('./src/' + command);
      ^
TypeError: Left side of assignment is not a reference. while parsing module "/tmp/node_modules/shelljs/src/cd.js"
      at /tmp/node_modules/shelljs/shell.js:25:2
      at /tmp/node_modules/shelljs/shell.js:24:8
      at bun:wrap:1:16360

The snippet of the code using process.env from node_modules/shelljs/src/cd.js:

//@ Changes to directory `dir` for the duration of the script. Changes to home
//@ directory if no argument is supplied.
function _cd(options, dir) {
  if (!dir) dir = os.homedir();

  if (dir === '-') {
    if (!process.env.OLDPWD) {
      common.error('could not find previous directory');
    } else {
      dir = process.env.OLDPWD;
    }
  }

ristomatti avatar Oct 19 '22 13:10 ristomatti

Just want to confirm, this is a compatibility issue that we will fix.

Electroid avatar Nov 01 '22 23:11 Electroid

@robobun

process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2'; 

Electroid avatar Dec 07 '22 20:12 Electroid

@Electroid here you go!

SyntaxError: Left side of assignment is not a reference.
      at /tmp/bun-uwqFLw/index.js:3:0
Code
process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2';

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

robobun avatar Dec 07 '22 20:12 robobun

As of bun version 0.7.1 this will no longer throw a SyntaxError. However, modifying process.env.NODE_ENV will not stick, is this intentional behaviour?

process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2';

console.log(process.env.FOO); // logs test1
console.log(process.env.NODE_ENV); // logs development

simylein avatar Jul 29 '23 09:07 simylein

As of bun version 0.7.1 this will no longer throw a SyntaxError. However, modifying process.env.NODE_ENV will not stick, is this intentional behaviour?

process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2';

console.log(process.env.FOO); // logs test1
console.log(process.env.NODE_ENV); // logs development

Looks like process.env.NODE_ENV still gets replaced with the string "development" when it's syntactically valid, i.e. not the left side of an assignment:

$ bun build test.js
// test.js
process.env.FOO = "test1";
process.env.NODE_ENV = "test2";
console.log(process.env.FOO);
console.log("development");

JL102 avatar Jul 30 '23 20:07 JL102

yeah we talked about this here https://github.com/oven-sh/bun/issues/3835

trnxdev avatar Jul 30 '23 22:07 trnxdev

This has been fixed, tested using Bun v1.0.7. If you still run into this error, please feel free to re-open this issue.

Electroid avatar Oct 24 '23 22:10 Electroid

Hi @Electroid, I just upgraded bun and tested the same test.js file as in the previous comment, and unfortunately the behavior is the same.

drak@JDESK:~$ cat test.js
process.env.FOO = 'test1';
process.env.NODE_ENV = 'test2';

console.log(process.env.FOO); // logs test1
console.log(process.env.NODE_ENV); // logs development
drak@JDESK:~$ bun -v
1.0.7
drak@JDESK:~$ bun test.js
test1
development
drak@JDESK:~$ bun build test.js --target=bun
// @bun
// test.js
process.env.FOO = "test1";
process.env.NODE_ENV = "test2";
console.log(process.env.FOO);
console.log("development");
drak@JDESK:~$ 

FYI, as a non contributor I don't think I have permission to re-open issues.

JL102 avatar Oct 24 '23 22:10 JL102

Thanks!

JL102 avatar Oct 24 '23 22:10 JL102

We removed the inlining feature in the runtime, so this bug no longer occurs.

Electroid avatar Feb 05 '24 16:02 Electroid

Do we know how big the performance implications of not inlining anymore are?

simylein avatar Feb 05 '24 18:02 simylein