Skip to content

Commit c19cb5b

Browse files
Copilotquanru
andauthored
fix(yaml): skip environment variable interpolation in YAML comments (#1361)
* Initial plan * fix(yaml): skip environment variable interpolation in YAML comments * style(yaml): apply biome linting fixes Co-authored-by: quanru <11739753+quanru@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: quanru <11739753+quanru@users.noreply.github.com>
1 parent a871e67 commit c19cb5b

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

packages/core/src/yaml/utils.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,29 @@ import yaml from 'js-yaml';
1111
const debugUtils = getDebug('yaml:utils');
1212

1313
export function interpolateEnvVars(content: string): string {
14-
return content.replace(/\$\{([^}]+)\}/g, (_, envVar) => {
15-
const value = process.env[envVar.trim()];
16-
if (value === undefined) {
17-
throw new Error(`Environment variable "${envVar.trim()}" is not defined`);
14+
// Process line by line to skip commented lines
15+
const lines = content.split('\n');
16+
const processedLines = lines.map((line) => {
17+
// Check if the line is a YAML comment (starts with # after optional whitespace)
18+
const trimmedLine = line.trimStart();
19+
if (trimmedLine.startsWith('#')) {
20+
// Skip interpolation for comment lines
21+
return line;
1822
}
19-
return value;
23+
24+
// Process environment variables for non-comment lines
25+
return line.replace(/\$\{([^}]+)\}/g, (_, envVar) => {
26+
const value = process.env[envVar.trim()];
27+
if (value === undefined) {
28+
throw new Error(
29+
`Environment variable "${envVar.trim()}" is not defined`,
30+
);
31+
}
32+
return value;
33+
});
2034
});
35+
36+
return processedLines.join('\n');
2137
}
2238

2339
export function parseYamlScript(

packages/web-integration/tests/unit-test/yaml/utils.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@ tasks:
5353
);
5454
});
5555

56+
test('should not throw error for undefined env vars in commented lines', () => {
57+
const yamlContent = `
58+
# DEV_USERNAME="\${UNDEFINED_ENV_VAR}"
59+
target:
60+
url: "https://example.com"
61+
tasks:
62+
- sleep: 1000
63+
# - aiInput: "\${ANOTHER_UNDEFINED_VAR}"
64+
# locate: "input field"
65+
`;
66+
67+
expect(() => parseYamlScript(yamlContent)).not.toThrow();
68+
const result = parseYamlScript(yamlContent);
69+
expect(result.target?.url).toBe('https://example.com');
70+
});
71+
72+
test('should handle mixed commented and uncommented env vars', () => {
73+
process.env.DEFINED_VAR = 'defined_value';
74+
75+
const yamlContent = `
76+
# This is a comment with \${UNDEFINED_VAR_IN_COMMENT}
77+
target:
78+
url: "https://example.com/\${DEFINED_VAR}"
79+
tasks:
80+
- sleep: 1000
81+
# - aiAction: "click \${UNDEFINED_IN_COMMENT}"
82+
`;
83+
84+
const result = parseYamlScript(yamlContent);
85+
expect(result.target?.url).toBe('https://example.com/defined_value');
86+
87+
process.env.DEFINED_VAR = undefined;
88+
});
89+
5690
test('android number-style deviceId', () => {
5791
const yamlContent = `
5892
android:

0 commit comments

Comments
 (0)