Skip to content

Commit 8aadd40

Browse files
authored
Fix squiggles with WSL compilerPath with args. (#3313)
* Fix squiggles with WSL compilerPath with args. * Normalize path separators.
1 parent 0c61ee9 commit 8aadd40

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

Extension/src/LanguageServer/configurations.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -753,15 +753,9 @@ export class CppProperties {
753753
paths.add(`"${this.CurrentConfiguration.compileCommands}"`);
754754
}
755755

756-
const isWindows: boolean = os.platform() === 'win32';
757756
if (this.CurrentConfiguration.compilerPath) {
758-
let compilerPathAndArgs: util.CompilerPathAndArgs;
759-
compilerPathAndArgs = util.extractCompilerPathAndArgs(this.CurrentConfiguration.compilerPath);
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-
}
757+
// Unlike other cases, compilerPath may not start or end with " due to trimming of whitespace and the possibility of compiler args.
758+
paths.add(`${this.CurrentConfiguration.compilerPath}`);
765759
}
766760

767761
// Get the start/end for properties that are file-only.
@@ -776,10 +770,11 @@ export class CppProperties {
776770
this.prevSquiggleMetrics[this.CurrentConfiguration.name] = { PathNonExistent: 0, PathNotAFile: 0, PathNotADirectory: 0 };
777771
}
778772
let newSquiggleMetrics: { [key: string]: number } = { PathNonExistent: 0, PathNotAFile: 0, PathNotADirectory: 0 };
773+
const isWindows: boolean = os.platform() === 'win32';
779774

780775
for (let curPath of paths) {
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));
776+
const isCompilerPath: boolean = curPath === this.CurrentConfiguration.compilerPath;
777+
let resolvedPath: string = isCompilerPath ? curPath : curPath.substr(1, curPath.length - 2); // Remove the surrounding quotes.
783778
// Resolve special path cases.
784779
if (resolvedPath === "${default}") {
785780
// TODO: Add squiggles for when the C_Cpp.default.* paths are invalid.
@@ -799,6 +794,8 @@ export class CppProperties {
799794
resolvedPath = resolvedPath.replace(/\*/g, "");
800795
}
801796

797+
// TODO: Invalid paths created from environment variables are not detected.
798+
802799
// Handle WSL paths.
803800
const isWSL: boolean = isWindows && resolvedPath.startsWith("/");
804801
if (isWSL) {
@@ -808,11 +805,19 @@ export class CppProperties {
808805
resolvedPath = resolvedPath.substr(0, 1) + ":" + resolvedPath.substr(1);
809806
} else if (this.rootfs && this.rootfs.length > 0) {
810807
resolvedPath = this.rootfs + resolvedPath.substr(1);
811-
resolvedPath = resolvedPath.replace(/\//g, path.sep);
812808
// TODO: Handle WSL symlinks.
813809
}
814810
}
815811

812+
if (isCompilerPath) {
813+
let compilerPathAndArgs: util.CompilerPathAndArgs = util.extractCompilerPathAndArgs(resolvedPath);
814+
if (isWindows && compilerPathAndArgs.compilerPath.endsWith("cl.exe")) {
815+
continue; // Don't squiggle invalid cl.exe paths because it could be for an older preview build.
816+
}
817+
resolvedPath = compilerPathAndArgs.compilerPath;
818+
curPath = curPath.replace(/\"/g, `\\"`);
819+
}
820+
816821
let pathExists: boolean = true;
817822
let existsWithExeAdded: (path: string) => boolean = (path: string) => {
818823
return isCompilerPath && isWindows && !isWSL && fs.existsSync(path + ".exe");
@@ -835,6 +840,13 @@ export class CppProperties {
835840
}
836841
}
837842

843+
// Normalize path separators.
844+
if (path.sep === "/") {
845+
resolvedPath = resolvedPath.replace(/\\/g, path.sep);
846+
} else {
847+
resolvedPath = resolvedPath.replace(/\//g, path.sep);
848+
}
849+
838850
// Iterate through the text and apply squiggles.
839851
for (let curOffset: number = curText.indexOf(curPath); curOffset !== -1; curOffset = curText.indexOf(curPath, curOffset + curPath.length)) {
840852
let message: string;
@@ -861,7 +873,7 @@ export class CppProperties {
861873
}
862874
let diagnostic: vscode.Diagnostic = new vscode.Diagnostic(
863875
new vscode.Range(document.positionAt(curTextStartOffset + curOffset),
864-
document.positionAt(curTextStartOffset + curOffset + curPath.length + (!isCompilerPath ? - 1 : 0))),
876+
document.positionAt(curTextStartOffset + curOffset + curPath.length + (!isCompilerPath ? -1 : 0))),
865877
message, vscode.DiagnosticSeverity.Warning);
866878
diagnostics.push(diagnostic);
867879
}

0 commit comments

Comments
 (0)