Skip to content

bun.build --define should not substitution a variable that is assigned to with a substitution that is not assignable #21210

Open

Description

@mizulu

What version of Bun is running?

1.2.19

What platform is your computer?

x64

What steps can reproduce the bug?

index.js

VAR1 = "hello"

run build

bun build index.js  --define="VAR1=2"

output ( Invalid Javascript )

2 = "hello"

What is the expected behavior?

  1. a variable being "defined" with --define should not be substituted in an assignment expression
    when it is on the left side ( leftSide = rightSide ), and the value is not assignable like a number, string ,etc
  • a warning should be raised.
  • "read" of the VAR1 will be substituted.
bun build ... --define  "VAR1=3" 
VAR1 = 3             // VAR1 is being assigned to 
console.log(VAR1)    // VAR1 is being read

will output

VAR1 = 3           // excluded from substitution 
console.log(3)     // substituted 

We should also still be able to define an assignable expression to the left hand sign
if the value is assignable such as var name, member expression , etc .

bun build ... --define  "VAR1=can.assign" 

will output

can.assign = 3           // excluded from substitution 
console.log(can.assign)     // substituted 

esbuild detects this case and shows this warning, and skips that substitution for the assignment ( as expected )

Image

related issue

#21205 []
The feature makes substitution explicit for reads only.
solving this particular case, and the case
that VAR1 may be replaced with another global. (causing the other global to get overridden)

What do you see instead?

2 = "hello"

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbundlerSomething to do with the bundler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions