Skip to content

Commit 0c77b9e

Browse files
authored
Fix "cl.exe" in the compilerPath squiggle. (#3237)
* Fix "cl.exe" and missing ".exe" in the compilerPath squiggle.
1 parent c395790 commit 0c77b9e

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

Extension/src/LanguageServer/configurations.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -716,20 +716,20 @@ export class CppProperties {
716716
// Get the text of the current configuration.
717717
let curText: string = document.getText();
718718
let curTextStartOffset: number = 0;
719-
let configStart: number = curText.search(new RegExp(`{\\s*"name"\\s*:\\s*"${this.CurrentConfiguration.name}"`));
719+
const configStart: number = curText.search(new RegExp(`{\\s*"name"\\s*:\\s*"${this.CurrentConfiguration.name}"`));
720720
if (configStart === -1) {
721721
telemetry.logLanguageServerEvent("ConfigSquiggles", { "error": "config name not first" });
722722
return;
723723
}
724724
curTextStartOffset = configStart + 1;
725725
curText = curText.substr(curTextStartOffset); // Remove earlier configs.
726-
let nameEnd: number = curText.indexOf(":");
726+
const nameEnd: number = curText.indexOf(":");
727727
curTextStartOffset += nameEnd + 1;
728728
curText = curText.substr(nameEnd + 1);
729-
let nextNameStart: number = curText.search(new RegExp('"name"\\s*:\\s*"'));
729+
const nextNameStart: number = curText.search(new RegExp('"name"\\s*:\\s*"'));
730730
if (nextNameStart !== -1) {
731731
curText = curText.substr(0, nextNameStart + 6); // Remove later configs.
732-
let nextNameStart2: number = curText.search(new RegExp('\\s*}\\s*,\\s*{\\s*"name"'));
732+
const nextNameStart2: number = curText.search(new RegExp('\\s*}\\s*,\\s*{\\s*"name"'));
733733
if (nextNameStart2 === -1) {
734734
telemetry.logLanguageServerEvent("ConfigSquiggles", { "error": "next config name not first" });
735735
return;
@@ -753,27 +753,33 @@ export class CppProperties {
753753
paths.add(`"${this.CurrentConfiguration.compileCommands}"`);
754754
}
755755

756-
let compilerPathAndArgs: util.CompilerPathAndArgs;
756+
const isWindows: boolean = os.platform() === 'win32';
757757
if (this.CurrentConfiguration.compilerPath) {
758+
let compilerPathAndArgs: util.CompilerPathAndArgs;
758759
compilerPathAndArgs = util.extractCompilerPathAndArgs(this.CurrentConfiguration.compilerPath);
759-
paths.add(`${compilerPathAndArgs.compilerPath}`); // It may not start or end with ".
760+
if (!(isWindows && compilerPathAndArgs.compilerPath.endsWith("cl.exe"))) {
761+
// Unlike other cases, compilerPath may not start or end with " due to trimming of whitespace.
762+
// This is checked to determine if the path is a compilerPath later on.
763+
paths.add(`${compilerPathAndArgs.compilerPath}`);
764+
}
760765
}
761766

762767
// Get the start/end for properties that are file-only.
763-
let forcedIncludeStart: number = curText.search(/\s*\"forcedInclude\"\s*:\s*\[/);
764-
let forcedeIncludeEnd: number = forcedIncludeStart === -1 ? -1 : curText.indexOf("]", forcedIncludeStart);
765-
let compileCommandsStart: number = curText.search(/\s*\"compileCommands\"\s*:\s*\"/);
766-
let compileCommandsEnd: number = compileCommandsStart === -1 ? -1 : curText.indexOf('"', curText.indexOf('"', curText.indexOf(":", compileCommandsStart)) + 1);
767-
let compilerPathStart: number = curText.search(/\s*\"compilerPath\"\s*:\s*\"/);
768-
let compilerPathEnd: number = compilerPathStart === -1 ? -1 : curText.indexOf('"', curText.indexOf('"', curText.indexOf(":", compilerPathStart)) + 1) + 1;
768+
const forcedIncludeStart: number = curText.search(/\s*\"forcedInclude\"\s*:\s*\[/);
769+
const forcedeIncludeEnd: number = forcedIncludeStart === -1 ? -1 : curText.indexOf("]", forcedIncludeStart);
770+
const compileCommandsStart: number = curText.search(/\s*\"compileCommands\"\s*:\s*\"/);
771+
const compileCommandsEnd: number = compileCommandsStart === -1 ? -1 : curText.indexOf('"', curText.indexOf('"', curText.indexOf(":", compileCommandsStart)) + 1);
772+
const compilerPathStart: number = curText.search(/\s*\"compilerPath\"\s*:\s*\"/);
773+
const compilerPathEnd: number = compilerPathStart === -1 ? -1 : curText.indexOf('"', curText.indexOf('"', curText.indexOf(":", compilerPathStart)) + 1) + 1;
769774

770775
if (this.prevSquiggleMetrics[this.CurrentConfiguration.name] === undefined) {
771776
this.prevSquiggleMetrics[this.CurrentConfiguration.name] = { PathNonExistent: 0, PathNotAFile: 0, PathNotADirectory: 0 };
772777
}
773778
let newSquiggleMetrics: { [key: string]: number } = { PathNonExistent: 0, PathNotAFile: 0, PathNotADirectory: 0 };
774779

775780
for (let curPath of paths) {
776-
let resolvedPath: string = curPath.substr((curPath.startsWith('"') ? 1 : 0), (curPath.endsWith('"') ? curPath.length - 2 : curPath.length));
781+
const isCompilerPath: boolean = !curPath.startsWith('"'); // This check probably will need to change later.
782+
let resolvedPath: string = curPath.substr((!isCompilerPath ? 1 : 0), curPath.length + (!isCompilerPath ? - 2 : 0));
777783
// Resolve special path cases.
778784
if (resolvedPath === "${default}") {
779785
// TODO: Add squiggles for when the C_Cpp.default.* paths are invalid.
@@ -794,7 +800,8 @@ export class CppProperties {
794800
}
795801

796802
// Handle WSL paths.
797-
if (resolvedPath.startsWith("/") && os.platform() === 'win32') {
803+
const isWSL: boolean = isWindows && resolvedPath.startsWith("/");
804+
if (isWSL) {
798805
const mntStr: string = "/mnt/";
799806
if (resolvedPath.length > "/mnt/c/".length && resolvedPath.substr(0, mntStr.length) === mntStr) {
800807
resolvedPath = resolvedPath.substr(mntStr.length);
@@ -807,13 +814,24 @@ export class CppProperties {
807814
}
808815

809816
let pathExists: boolean = true;
817+
let existsWithExeAdded: (path: string) => boolean = (path: string) => {
818+
return isCompilerPath && isWindows && !isWSL && fs.existsSync(path + ".exe");
819+
};
810820
if (!fs.existsSync(resolvedPath)) {
811-
// Check again for a relative path.
812-
let relativePath: string = this.rootUri.fsPath + path.sep + resolvedPath;
813-
if (!fs.existsSync(relativePath)) {
814-
pathExists = false;
821+
if (existsWithExeAdded(resolvedPath)) {
822+
resolvedPath += ".exe";
815823
} else {
816-
resolvedPath = relativePath;
824+
// Check again for a relative path.
825+
const relativePath: string = this.rootUri.fsPath + path.sep + resolvedPath;
826+
if (!fs.existsSync(relativePath)) {
827+
if (existsWithExeAdded(resolvedPath)) {
828+
resolvedPath += ".exe";
829+
} else {
830+
pathExists = false;
831+
}
832+
} else {
833+
resolvedPath = relativePath;
834+
}
817835
}
818836
}
819837

@@ -843,7 +861,7 @@ export class CppProperties {
843861
}
844862
let diagnostic: vscode.Diagnostic = new vscode.Diagnostic(
845863
new vscode.Range(document.positionAt(curTextStartOffset + curOffset),
846-
document.positionAt(curTextStartOffset + curOffset + (curPath.endsWith('"') ? curPath.length - 1 : curPath.length))),
864+
document.positionAt(curTextStartOffset + curOffset + curPath.length + (!isCompilerPath ? - 1 : 0))),
847865
message, vscode.DiagnosticSeverity.Warning);
848866
diagnostics.push(diagnostic);
849867
}

Extension/src/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ export interface CompilerPathAndArgs {
765765
export function extractCompilerPathAndArgs(inputCompilerPath: string): CompilerPathAndArgs {
766766
let compilerPath: string = inputCompilerPath;
767767
let additionalArgs: string[];
768+
let isWindows: boolean = os.platform() === 'win32';
768769
if (compilerPath) {
769770
compilerPath = compilerPath.trim();
770771
if (compilerPath.startsWith("\"")) {
@@ -779,7 +780,7 @@ export function extractCompilerPathAndArgs(inputCompilerPath: string): CompilerP
779780
let spaceStart: number = compilerPath.lastIndexOf(" ");
780781
if (spaceStart !== -1 && !fs.existsSync(compilerPath)) {
781782
let potentialCompilerPath: string = compilerPath.substr(0, spaceStart);
782-
while (!fs.existsSync(potentialCompilerPath)) {
783+
while ((!isWindows || !potentialCompilerPath.endsWith("cl.exe")) && !fs.existsSync(potentialCompilerPath)) {
783784
spaceStart = potentialCompilerPath.lastIndexOf(" ");
784785
if (spaceStart === -1) {
785786
// Reached the start without finding a valid path. Use the original value.

0 commit comments

Comments
 (0)