Fatal Bug: TypeError: str.replace is not a function
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
- Install the VSCode v1.64(x64 Windows) and start a WSL connection to the WSL2 on the same computer.
- Install
cmake-tools v1.9.2extension. - 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.

[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
- ms-vscode.cmake-tools v1.9.2
- cheshirekow.cmake-format v0.6.11
- josetr.cmake-language-support-vscode v0.0.4
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;
}
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.
This problem pops up only on the WSL version of VSCode so far, and there is no problem on Windows version temporarily.
By casting the parameters passed into the suspicious functions into string can also resolve the problem temporarily on WSL2 version of the VSCode.
Hmm, can't reproduce the problem on my WSL2 instance running Ubuntu 22.04.