Skip to content

Commit c395790

Browse files
sean-mcmanusbobbrow
authored andcommitted
Fix bugs with compilerPath squiggles and build tasks. (#3228)
1 parent e928bfb commit c395790

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

Extension/src/LanguageServer/configurations.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ export class CppProperties {
756756
let compilerPathAndArgs: util.CompilerPathAndArgs;
757757
if (this.CurrentConfiguration.compilerPath) {
758758
compilerPathAndArgs = util.extractCompilerPathAndArgs(this.CurrentConfiguration.compilerPath);
759-
paths.add(`"${compilerPathAndArgs.compilerPath}`); // It may not end with ".
759+
paths.add(`${compilerPathAndArgs.compilerPath}`); // It may not start or end with ".
760760
}
761761

762762
// Get the start/end for properties that are file-only.
@@ -765,15 +765,15 @@ export class CppProperties {
765765
let compileCommandsStart: number = curText.search(/\s*\"compileCommands\"\s*:\s*\"/);
766766
let compileCommandsEnd: number = compileCommandsStart === -1 ? -1 : curText.indexOf('"', curText.indexOf('"', curText.indexOf(":", compileCommandsStart)) + 1);
767767
let compilerPathStart: number = curText.search(/\s*\"compilerPath\"\s*:\s*\"/);
768-
let compilerPathEnd: number = compilerPathStart === -1 ? -1 : curText.indexOf('"', curText.indexOf('"', curText.indexOf(":", compilerPathStart)) + 1);
768+
let compilerPathEnd: number = compilerPathStart === -1 ? -1 : curText.indexOf('"', curText.indexOf('"', curText.indexOf(":", compilerPathStart)) + 1) + 1;
769769

770770
if (this.prevSquiggleMetrics[this.CurrentConfiguration.name] === undefined) {
771771
this.prevSquiggleMetrics[this.CurrentConfiguration.name] = { PathNonExistent: 0, PathNotAFile: 0, PathNotADirectory: 0 };
772772
}
773773
let newSquiggleMetrics: { [key: string]: number } = { PathNonExistent: 0, PathNotAFile: 0, PathNotADirectory: 0 };
774774

775775
for (let curPath of paths) {
776-
let resolvedPath: string = curPath.substr(1, (curPath.endsWith('"') ? curPath.length - 2 : curPath.length - 1));
776+
let resolvedPath: string = curPath.substr((curPath.startsWith('"') ? 1 : 0), (curPath.endsWith('"') ? curPath.length - 2 : curPath.length));
777777
// Resolve special path cases.
778778
if (resolvedPath === "${default}") {
779779
// TODO: Add squiggles for when the C_Cpp.default.* paths are invalid.
@@ -842,7 +842,8 @@ export class CppProperties {
842842
}
843843
}
844844
let diagnostic: vscode.Diagnostic = new vscode.Diagnostic(
845-
new vscode.Range(document.positionAt(curTextStartOffset + curOffset + 1), document.positionAt(curTextStartOffset + curOffset + (curPath.endsWith('"') ? curPath.length - 1 : curPath.length))),
845+
new vscode.Range(document.positionAt(curTextStartOffset + curOffset),
846+
document.positionAt(curTextStartOffset + curOffset + (curPath.endsWith('"') ? curPath.length - 1 : curPath.length))),
846847
message, vscode.DiagnosticSeverity.Warning);
847848
diagnostics.push(diagnostic);
848849
}

Extension/src/common.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -766,19 +766,34 @@ export function extractCompilerPathAndArgs(inputCompilerPath: string): CompilerP
766766
let compilerPath: string = inputCompilerPath;
767767
let additionalArgs: string[];
768768
if (compilerPath) {
769+
compilerPath = compilerPath.trim();
769770
if (compilerPath.startsWith("\"")) {
770771
let endQuote: number = compilerPath.substr(1).search("\"") + 1;
771772
if (endQuote !== -1) {
772773
additionalArgs = compilerPath.substr(endQuote + 1).split(" ");
773-
additionalArgs = additionalArgs.filter((arg: string) => { return arg.trim().length !== 0; });
774+
additionalArgs = additionalArgs.filter((arg: string) => { return arg.trim().length !== 0; }); // Remove empty args.
774775
compilerPath = compilerPath.substr(1, endQuote - 1);
775776
}
776777
} else {
777-
if (compilerPath.includes(" ") && !fs.existsSync(compilerPath)) {
778-
let argStart: number = compilerPath.search(" ");
779-
additionalArgs = compilerPath.substr(argStart + 1).split(" ");
780-
additionalArgs = additionalArgs.filter((arg: string) => { return arg.trim().length !== 0; });
781-
compilerPath = compilerPath.substr(0, argStart);
778+
// Go from right to left checking if a valid path is to the left of a space.
779+
let spaceStart: number = compilerPath.lastIndexOf(" ");
780+
if (spaceStart !== -1 && !fs.existsSync(compilerPath)) {
781+
let potentialCompilerPath: string = compilerPath.substr(0, spaceStart);
782+
while (!fs.existsSync(potentialCompilerPath)) {
783+
spaceStart = potentialCompilerPath.lastIndexOf(" ");
784+
if (spaceStart === -1) {
785+
// Reached the start without finding a valid path. Use the original value.
786+
potentialCompilerPath = compilerPath;
787+
break;
788+
}
789+
potentialCompilerPath = potentialCompilerPath.substr(0, spaceStart);
790+
}
791+
if (compilerPath !== potentialCompilerPath) {
792+
// Found a valid compilerPath and args.
793+
additionalArgs = compilerPath.substr(spaceStart + 1).split(" ");
794+
additionalArgs = additionalArgs.filter((arg: string) => { return arg.trim().length !== 0; }); // Remove empty args.
795+
compilerPath = potentialCompilerPath;
796+
}
782797
}
783798
}
784799
}

0 commit comments

Comments
 (0)