Skip to content

Commit

Permalink
Fix squiggles with WSL compilerPath with args. (#3313)
Browse files Browse the repository at this point in the history
* Fix squiggles with WSL compilerPath with args.
* Normalize path separators.
  • Loading branch information
sean-mcmanus authored Mar 19, 2019
1 parent 0c61ee9 commit 8aadd40
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions Extension/src/LanguageServer/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,15 +753,9 @@ export class CppProperties {
paths.add(`"${this.CurrentConfiguration.compileCommands}"`);
}

const isWindows: boolean = os.platform() === 'win32';
if (this.CurrentConfiguration.compilerPath) {
let compilerPathAndArgs: util.CompilerPathAndArgs;
compilerPathAndArgs = util.extractCompilerPathAndArgs(this.CurrentConfiguration.compilerPath);
if (!(isWindows && compilerPathAndArgs.compilerPath.endsWith("cl.exe"))) {
// Unlike other cases, compilerPath may not start or end with " due to trimming of whitespace.
// This is checked to determine if the path is a compilerPath later on.
paths.add(`${compilerPathAndArgs.compilerPath}`);
}
// Unlike other cases, compilerPath may not start or end with " due to trimming of whitespace and the possibility of compiler args.
paths.add(`${this.CurrentConfiguration.compilerPath}`);
}

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

for (let curPath of paths) {
const isCompilerPath: boolean = !curPath.startsWith('"'); // This check probably will need to change later.
let resolvedPath: string = curPath.substr((!isCompilerPath ? 1 : 0), curPath.length + (!isCompilerPath ? - 2 : 0));
const isCompilerPath: boolean = curPath === this.CurrentConfiguration.compilerPath;
let resolvedPath: string = isCompilerPath ? curPath : curPath.substr(1, curPath.length - 2); // Remove the surrounding quotes.
// Resolve special path cases.
if (resolvedPath === "${default}") {
// TODO: Add squiggles for when the C_Cpp.default.* paths are invalid.
Expand All @@ -799,6 +794,8 @@ export class CppProperties {
resolvedPath = resolvedPath.replace(/\*/g, "");
}

// TODO: Invalid paths created from environment variables are not detected.

// Handle WSL paths.
const isWSL: boolean = isWindows && resolvedPath.startsWith("/");
if (isWSL) {
Expand All @@ -808,11 +805,19 @@ export class CppProperties {
resolvedPath = resolvedPath.substr(0, 1) + ":" + resolvedPath.substr(1);
} else if (this.rootfs && this.rootfs.length > 0) {
resolvedPath = this.rootfs + resolvedPath.substr(1);
resolvedPath = resolvedPath.replace(/\//g, path.sep);
// TODO: Handle WSL symlinks.
}
}

if (isCompilerPath) {
let compilerPathAndArgs: util.CompilerPathAndArgs = util.extractCompilerPathAndArgs(resolvedPath);
if (isWindows && compilerPathAndArgs.compilerPath.endsWith("cl.exe")) {
continue; // Don't squiggle invalid cl.exe paths because it could be for an older preview build.
}
resolvedPath = compilerPathAndArgs.compilerPath;
curPath = curPath.replace(/\"/g, `\\"`);
}

let pathExists: boolean = true;
let existsWithExeAdded: (path: string) => boolean = (path: string) => {
return isCompilerPath && isWindows && !isWSL && fs.existsSync(path + ".exe");
Expand All @@ -835,6 +840,13 @@ export class CppProperties {
}
}

// Normalize path separators.
if (path.sep === "/") {
resolvedPath = resolvedPath.replace(/\\/g, path.sep);
} else {
resolvedPath = resolvedPath.replace(/\//g, path.sep);
}

// Iterate through the text and apply squiggles.
for (let curOffset: number = curText.indexOf(curPath); curOffset !== -1; curOffset = curText.indexOf(curPath, curOffset + curPath.length)) {
let message: string;
Expand All @@ -861,7 +873,7 @@ export class CppProperties {
}
let diagnostic: vscode.Diagnostic = new vscode.Diagnostic(
new vscode.Range(document.positionAt(curTextStartOffset + curOffset),
document.positionAt(curTextStartOffset + curOffset + curPath.length + (!isCompilerPath ? - 1 : 0))),
document.positionAt(curTextStartOffset + curOffset + curPath.length + (!isCompilerPath ? -1 : 0))),
message, vscode.DiagnosticSeverity.Warning);
diagnostics.push(diagnostic);
}
Expand Down

0 comments on commit 8aadd40

Please sign in to comment.