Skip to content

Commit b673e61

Browse files
Copilotrchiodo
andcommitted
Improve escape sequence handling in environment parser
Co-authored-by: rchiodo <19672699+rchiodo@users.noreply.github.com>
1 parent 45a8ecc commit b673e61

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/extension/common/variables/environment.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export function parseEnvFile(lines: string | Buffer, baseVars?: EnvironmentVaria
104104

105105
for (let i = 0; i < content.length; i++) {
106106
const char = content[i];
107-
const prevChar = i > 0 ? content[i - 1] : '';
108107

109108
// Track if we've seen an '=' sign (indicating we're in the value part)
110109
if (char === '=' && !inQuotes) {
@@ -113,16 +112,29 @@ export function parseEnvFile(lines: string | Buffer, baseVars?: EnvironmentVaria
113112
continue;
114113
}
115114

116-
// Handle quote characters
117-
if ((char === '"' || char === "'") && afterEquals && prevChar !== '\\') {
118-
if (!inQuotes) {
119-
// Starting a quoted section
120-
inQuotes = true;
121-
quoteChar = char;
122-
} else if (char === quoteChar) {
123-
// Ending a quoted section
124-
inQuotes = false;
125-
quoteChar = '';
115+
// Handle quote characters - need to check for proper escaping
116+
if ((char === '"' || char === "'") && afterEquals) {
117+
// Count consecutive backslashes before this quote
118+
let numBackslashes = 0;
119+
let j = i - 1;
120+
while (j >= 0 && content[j] === '\\') {
121+
numBackslashes++;
122+
j--;
123+
}
124+
125+
// Quote is escaped if there's an odd number of backslashes before it
126+
const isEscaped = numBackslashes % 2 === 1;
127+
128+
if (!isEscaped) {
129+
if (!inQuotes) {
130+
// Starting a quoted section
131+
inQuotes = true;
132+
quoteChar = char;
133+
} else if (char === quoteChar) {
134+
// Ending a quoted section
135+
inQuotes = false;
136+
quoteChar = '';
137+
}
126138
}
127139
currentLine += char;
128140
continue;

0 commit comments

Comments
 (0)