Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: don't overwrite environment from .env file #49424

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ void Dotenv::SetEnvironment(node::Environment* env) {
for (const auto& entry : store_) {
auto key = entry.first;
auto value = entry.second;
env->env_vars()->Set(
isolate,
v8::String::NewFromUtf8(
isolate, key.data(), NewStringType::kNormal, key.size())
.ToLocalChecked(),
v8::String::NewFromUtf8(
isolate, value.data(), NewStringType::kNormal, value.size())
.ToLocalChecked());

auto existing = env->env_vars()->Get(key.data());

if (existing.IsNothing()) {
philnash marked this conversation as resolved.
Show resolved Hide resolved
env->env_vars()->Set(
isolate,
v8::String::NewFromUtf8(
isolate, key.data(), NewStringType::kNormal, key.size())
.ToLocalChecked(),
v8::String::NewFromUtf8(
isolate, value.data(), NewStringType::kNormal, value.size())
.ToLocalChecked());
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/dotenv/simple.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A=1
B=2
26 changes: 26 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { describe, it } = require('node:test');

const validEnvFilePath = '../fixtures/dotenv/valid.env';
const relativePath = '../fixtures/dotenv/node-options.env';
const simpleEnvFilePath = '../fixtures/dotenv/simple.env';

describe('.env supports edge cases', () => {

Expand Down Expand Up @@ -35,4 +36,29 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.code, 0);
});

it('should not override existing environment variables', async () => {
const code = `
require('assert').strictEqual(process.env.BASIC, 'existing');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${validEnvFilePath}`, '--eval', code ],
{ cwd: __dirname, env: { BASIC: 'existing' } },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved

it('should not override existing environment variables but introduce new vars', async () => {
const code = `
require('assert').strictEqual(process.env.A + "," + process.env.B, '3,2');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${simpleEnvFilePath}`, '--eval', code ],
{ cwd: __dirname, env: { A: '3' } },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 tests can be merged

);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});