Skip to content

Bundler doesn't include env/define vars if they are already defined #24348

@Melchizedek6809

Description

@Melchizedek6809

What version of Bun is running?

1.3.1+89fa0f343

What platform is your computer?

Linux 6.17.5-arch1-1 x86_64 unknown

What steps can reproduce the bug?

Create a HTML template that references a JS file/module with the following content:

// main.js
const works = () => console.log("Works: " + process.env.ACME_PUBLIC_ENV);

const doesnt_work = () => {
    const process = {
        env: {
            PUBLIC_ENV: "123"
        }
    }

    console.log("Works: " + process.env.ACME_PUBLIC_ENV);
};

works();
doesnt_work();

Bundle with the following command:

ACME_PUBLIC_ENV="development" bun build main.html --outdir ./dist --env ACME_PUBLIC_*

What is the expected behavior?

I'd expect both references to process.env.ACME_PUBLIC_ENV to be replaced with "development"

What do you see instead?

It only replaces the reference to process.env.ACME_PUBLIC_ENV if I didn't define it beforehand, this took a while for me to notice because while giving this feature a try I wanted to keep it safe so though I'd define a default value beforehand so that things wouldn't break if I forgot to specify the env var. Not sure whether this is a bug, can understand the behaviour, but it should probably be noted in the docs or maybe a warning should be emitted. Only later did I discover that me trying to be careful ended up breaking this feature.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions