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

Parse envFile and pass to debugger #1951

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,11 @@
"description": "Environment variables passed to the program.",
"default": {}
},
"envFile": {
"type": "string",
"description": "Environment variables passed to the program by a file.",
"default": "${workspaceFolder}/.env"
},
"console": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -1884,6 +1889,11 @@
"description": "Environment variables passed to the program.",
"default": {}
},
"envFile": {
"type": "string",
"description": "Environment variables passed to the program by a file.",
"default": "${workspaceFolder}/.env"
},
"console": {
"type": "string",
"enum": [
Expand Down
68 changes: 68 additions & 0 deletions src/configurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,79 @@ export class CSharpConfigurationProvider implements vscode.DebugConfigurationPro
});
}

/**
* Parse envFile and add to config.env
*/
private parseEnvFile(envFile: string, config: vscode.DebugConfiguration): vscode.DebugConfiguration {
if(envFile) {
try {
let content: string = fs.readFileSync(envFile, "utf8");
let parseErrors: string[] = [];

// Remove UTF-8 BOM if present
if(content.charAt(0) === '\uFEFF') {
content = content.substr(1);
}

content.split("\n").forEach(line => {
const r: RegExpMatchArray = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);

if (r !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we provide some sort of error message if this is null?

Copy link
Contributor

Choose a reason for hiding this comment

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

(Excluding lines that start with '#')


In reply to: 203184365 [](ancestors = 203184365)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also done!

const key: string = r[1];
let value: string = r[2] || "";
if ((value.length > 0) && (value.charAt(0) === '"') && (value.charAt(value.length - 1) === '"')) {
value = value.replace(/\\n/gm, "\n");
}

value = value.replace(/(^['"]|['"]$)/g, "");

if(!config.env) {
config.env = {};
}
config.env[key] = value;
}
else {
// Blank lines and lines starting with # are no parse errors
const comments: RegExp = new RegExp(/^\s*(#|$)/);
if (!comments.test(line)) {
parseErrors.push(line);
}
}
});

// show error message if single lines cannot get parsed
if(parseErrors.length !== 0) {
let parseError: string = "Ignoring non-parseable lines in envFile " + envFile + ": ";
parseErrors.forEach(function (value, idx, array) {
parseError += "\"" + value + "\"" + ((idx !== array.length - 1) ? ", " : ".");
});
showErrorMessage(vscode, parseError);
}
}
catch (e) {
throw new Error("Can't parse envFile " + envFile);
}
}

// remove envFile from config after parsing
if(config.envFile) {
delete config.envFile;
}

return config;
}

/**
* Try to add all missing attributes to the debug configuration being launched.
*/
resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration> {
// vsdbg does the error checking

// read from envFile and set config.env
if(config.envFile) {
config = this.parseEnvFile(config.envFile.replace(/\${workspaceFolder}/g, folder.uri.path), config);
}

return config;
}
}
5 changes: 5 additions & 0 deletions src/tools/OptionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@
"description": "Environment variables passed to the program.",
"default": {}
},
"envFile": {
"type": "string",
"description": "Environment variables passed to the program by a file.",
"default": "${workspaceFolder}/.env"
},
"console": {
"type": "string",
"enum": [ "internalConsole", "integratedTerminal", "externalTerminal" ],
Expand Down