microsoft / vscode-cmake-tools

CMake integration in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
MIT License
1.46k stars 446 forks source link

Fatal Bug: TypeError: str.replace is not a function #2399

Open KagaJiankui opened 2 years ago

KagaJiankui commented 2 years ago

Brief Issue Summary

The CMake Tools extension on the VSCode on WSL cannot run at all due to the error TypeError: str.replace is not a function.

Reproduce the bug

  1. Install the VSCode v1.64(x64 Windows) and start a WSL connection to the WSL2 on the same computer.
  2. Install cmake-tools v1.9.2 extension.
  3. There is no any CMake options either in the command palette or in the sidebar.

CMake Tools Diagnostics

The CMake: Log Diagnostics cannot run at all since the extension can't load.

image

[2022-02-24 20:38:28.707] [exthost] [error] Activating extension ms-vscode.cmake-tools failed due to an error:
[2022-02-24 20:38:28.708] [exthost] [error] TypeError: str.replace is not a function
    at Object.replaceAll (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:62770:16)
    at /home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:53765:32
    at Map.forEach (<anonymous>)
    at expandStringHelper (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:53763:10)
    at Object.expandString (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:53653:33)
    at CMakeTools._init (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:46317:81)
    at Function.create (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:46547:20)
    at Function.createForDirectory (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:46559:27)
    at CMakeToolsFolderController._loadCMakeToolsForWorkspaceFolder (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:55710:38)
    at CMakeToolsFolderController._addFolder (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:55725:36)
    at CMakeToolsFolderController.loadAllCurrent (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:55679:28)
    at ExtensionManager._init (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:54050:33)
    at Function.create (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:54102:20)
    at setup (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:55194:55)
    at activate (/home/lexington/.vscode-server/extensions/ms-vscode.cmake-tools-1.9.2/dist/main.js:55363:12)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Promise.all (index 0)
    at async w.$activate (/home/lexington/.vscode-server/bin/f80445acd5a3dadef24aa209168452a3d97cc326/out/vs/workbench/services/extensions/node/extensionHostProcess.js:98:21810)

Debug Log

There isn't debug log since the extension cannot load due to the error trace.

<!-- Paste the debug log contents HERE -->

Installed Extensions and Activated Extensions

  1. ms-vscode.cmake-tools v1.9.2
  2. cheshirekow.cmake-format v0.6.11
  3. josetr.cmake-language-support-vscode v0.0.4
KagaJiankui commented 2 years ago

This bug can be resolved by adding toString() in the erroneous functions listed below:

utils.ts/normalizePath

export function normalizePath(p: string, opt: PathNormalizationOptions): string {
    const normCase: NormalizationSetting = opt ? opt.normCase ? opt.normCase : 'never' : 'never';
    const normUnicode: NormalizationSetting = opt ? opt.normUnicode ? opt.normUnicode : 'never' : 'never';
    let norm = path.normalize(p.toString()); // p->p.toString()
    while (path.sep !== path.posix.sep && norm.includes(path.sep)) {
        norm = norm.replace(path.sep, path.posix.sep);
    }
//...
}

utils.ts/replaceAll

export function replaceAll(str: string, needle: string, what: string) {
    const pattern = escapeStringForRegex(needle);
    const re = new RegExp(pattern, 'g');
    return str.toString().replace(re, what); //str.replace()->str.toString().replace()
}

utils.ts/escapeStringForRegex

export function escapeStringForRegex(str: string): string {
    return str.toString().replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');  //str.replace()->str.toString().replace()
}

utils.ts/fixPaths

export function fixPaths(str: string | undefined) {
    if (str === undefined) {
        return undefined;
    }
    const fix_paths = /[A-Z]:(\\((?![<>:\"\/\\|\?\*]).)+)*\\?(?!\\)/gi;
    let pathmatch: RegExpMatchArray | null = null;
    let newstr = str;
    while ((pathmatch = fix_paths.exec(str))) {
        const pathfull = pathmatch[0];
        const fixslash = pathfull.replace(/\\/g, '/');
        newstr = newstr.toString().replace(pathfull, fixslash);  //str.replace()->str.toString().replace()
    }
    return newstr;
}
bobbrow commented 2 years ago

I think there's a bigger problem here if the parameters typed as "string" are not actually "strings". We'll need to try to reproduce this error under a debugger and see what the problem is.

KagaJiankui commented 2 years ago

This problem pops up only on the WSL version of VSCode so far, and there is no problem on Windows version temporarily.

KagaJiankui commented 2 years ago

By casting the parameters passed into the suspicious functions into string can also resolve the problem temporarily on WSL2 version of the VSCode.

chausner commented 2 years ago

Hmm, can't reproduce the problem on my WSL2 instance running Ubuntu 22.04.