-
Notifications
You must be signed in to change notification settings - Fork 86
Fix multiline environment variable parsing in .env files #928
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
base: main
Are you sure you want to change the base?
Changes from all commits
4b2a2d0
45a8ecc
b673e61
4bafda0
e4a6a92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| import { expect } from 'chai'; | ||
| import { parseEnvFile } from '../../../extension/common/variables/environment'; | ||
|
|
||
| suite('Environment File Parsing Tests', () => { | ||
| test('Should parse simple environment variables', () => { | ||
| const content = 'VAR1=value1\nVAR2=value2'; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| expect(result).to.deep.equal({ | ||
| VAR1: 'value1', | ||
|
Check warning on line 16 in src/test/unittest/common/environment.unit.test.ts
|
||
| VAR2: 'value2', | ||
|
Check warning on line 17 in src/test/unittest/common/environment.unit.test.ts
|
||
| }); | ||
| }); | ||
|
|
||
| test('Should parse single-quoted multiline values', () => { | ||
| const content = "EXAMPLE_VAR='very long value\nwith new line , we need to get all the lines'"; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.EXAMPLE_VAR).to.equal('very long value\nwith new line , we need to get all the lines'); | ||
| }); | ||
|
|
||
| test('Should parse double-quoted multiline values', () => { | ||
| const content = 'EXAMPLE_VAR="very long value\nwith new line , we need to get all the lines"'; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.EXAMPLE_VAR).to.equal('very long value\nwith new line , we need to get all the lines'); | ||
| }); | ||
|
|
||
| test('Should parse escaped newlines in single-quoted values', () => { | ||
| const content = "VAR='line1\\nline2'"; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.VAR).to.equal('line1\nline2'); | ||
| }); | ||
|
|
||
| test('Should parse escaped newlines in double-quoted values', () => { | ||
| const content = 'VAR="line1\\nline2"'; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.VAR).to.equal('line1\nline2'); | ||
| }); | ||
|
|
||
| test('Should handle multiple variables with multiline values', () => { | ||
| const content = "VAR1='multiline\nvalue1'\nVAR2='multiline\nvalue2'"; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.VAR1).to.equal('multiline\nvalue1'); | ||
| expect(result.VAR2).to.equal('multiline\nvalue2'); | ||
| }); | ||
|
|
||
| test('Should handle unquoted values', () => { | ||
| const content = 'VAR=value_without_quotes'; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.VAR).to.equal('value_without_quotes'); | ||
| }); | ||
|
|
||
| test('Should handle empty values', () => { | ||
| const content = 'VAR='; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.VAR).to.equal(''); | ||
| }); | ||
|
|
||
| test('Should ignore lines without equals sign', () => { | ||
| const content = 'VAR1=value1\nInvalid line\nVAR2=value2'; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| expect(result).to.deep.equal({ | ||
| VAR1: 'value1', | ||
|
Check warning on line 77 in src/test/unittest/common/environment.unit.test.ts
|
||
| VAR2: 'value2', | ||
|
Check warning on line 78 in src/test/unittest/common/environment.unit.test.ts
|
||
| }); | ||
| }); | ||
|
|
||
| test('Should handle multiline value with multiple newlines', () => { | ||
| const content = "VAR='line1\nline2\nline3\nline4'"; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.VAR).to.equal('line1\nline2\nline3\nline4'); | ||
| }); | ||
|
|
||
| test('Should parse environment file as Buffer', () => { | ||
| const content = Buffer.from("VAR='multiline\nvalue'"); | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.VAR).to.equal('multiline\nvalue'); | ||
| }); | ||
|
|
||
| test('Should handle whitespace around variable names and equals', () => { | ||
| const content = " VAR1 = value1 \n VAR2='multiline\nvalue'"; | ||
| const result = parseEnvFile(content); | ||
|
|
||
| expect(result.VAR1).to.equal('value1'); | ||
| expect(result.VAR2).to.equal('multiline\nvalue'); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot could you use this npm package instead to do the parsing:
https://www.npmjs.com/package/envfile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, the
envfilepackage doesn't support multiline environment variables. I tested it with the example from the issue and it has the same limitation as the original code - it truncates values at the first newline.Test result with
envfile:My implementation:
The issue specifically mentions that
python-dotenvhandles this correctly, and my implementation matches that behavior. Usingenvfilewould revert the fix.