Skip to content

Commit 88413d5

Browse files
authored
Switch to a simpler module for reading from registry (#2410)
The @vscode/windows-registry module is maintained by Microsoft and is described like so: This module only has what is needed to support VS Code and is intended to be a lightweight module. Seems like a great fit for our purposes.
1 parent 63d3a8f commit 88413d5

File tree

3 files changed

+20
-37
lines changed

3 files changed

+20
-37
lines changed

extensions/positron-r/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,11 @@
516516
"vsce": "^2.11.0"
517517
},
518518
"dependencies": {
519+
"@vscode/windows-registry": "^1.1.0",
519520
"p-queue": "^6.6.2",
520521
"split2": "^4.2.0",
521522
"vscode-languageclient": "^9.0.1",
522-
"web-tree-sitter": "^0.20.8",
523-
"winreg": "^1.2.5"
523+
"web-tree-sitter": "^0.20.8"
524524
},
525525
"repository": {
526526
"type": "git",

extensions/positron-r/src/provider.ts

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import * as vscode from 'vscode';
1010
import * as which from 'which';
1111
import * as positron from 'positron';
1212
import * as crypto from 'crypto';
13-
import * as winreg from 'winreg';
1413

1514
import { RInstallation, getRHomePath } from './r-installation';
1615
import { RRuntime, createJupyterKernelExtra, createJupyterKernelSpec } from './runtime';
1716
import { RRuntimeManager } from './runtime-manager';
1817
import { Logger } from './extension';
1918
import { readLines } from './util';
19+
import { HKEY } from '@vscode/windows-registry';
2020

2121
const initialDynState = {
2222
inputPrompt: '>',
@@ -362,8 +362,8 @@ async function findCurrentRBinary(): Promise<string | undefined> {
362362
}
363363

364364
async function findCurrentRBinaryFromRegistry(): Promise<string | undefined> {
365-
const userPath = await getRegistryInstallPath(winreg.HKCU);
366-
const machinePath = await getRegistryInstallPath(winreg.HKLM);
365+
const userPath = await getRegistryInstallPath('HKEY_CURRENT_USER');
366+
const machinePath = await getRegistryInstallPath('HKEY_LOCAL_MACHINE');
367367
if (!userPath && !machinePath) {
368368
return undefined;
369369
}
@@ -378,34 +378,17 @@ async function findCurrentRBinaryFromRegistry(): Promise<string | undefined> {
378378
return binPath;
379379
}
380380

381-
async function getRegistryInstallPath(hive: string): Promise<string | undefined> {
382-
try {
383-
const key = new winreg({
384-
hive: hive as keyof typeof winreg,
385-
// 'R64' here is another place where we explicitly ignore 32-bit R
386-
key: '\\Software\\R-Core\\R64',
387-
});
388-
389-
Logger.info(`Checking for 'InstallPath' in registry key ${key.key} for hive ${key.hive}`);
381+
async function getRegistryInstallPath(hive: 'HKEY_CURRENT_USER' | 'HKEY_LOCAL_MACHINE'): Promise<string | undefined> {
382+
// 'R64' here is another place where we explicitly ignore 32-bit R
383+
const R64_KEY: string = 'Software\\R-Core\\R64';
384+
const registry = await import('@vscode/windows-registry');
390385

391-
const result = await new Promise<{ value: string }>((resolve, reject) => {
392-
key.get('InstallPath', (error, result) => {
393-
if (error) {
394-
reject(error);
395-
} else {
396-
resolve(result);
397-
}
398-
});
399-
});
400-
401-
if (!result || typeof result.value !== 'string') {
402-
Logger.info(`Invalid value of 'InstallPath'`);
403-
return undefined;
404-
}
405-
406-
return result.value;
407-
} catch (error: any) {
408-
Logger.info(`Unable to get value of 'InstallPath': ${error.message}`);
386+
try {
387+
Logger.info(`Checking for 'InstallPath' in registry key ${R64_KEY} for hive ${hive}`);
388+
const pth = registry.GetStringRegKey(hive as HKEY, R64_KEY, 'InstallPath') || '';
389+
return pth;
390+
} catch (err) {
391+
Logger.info(err as string);
409392
return undefined;
410393
}
411394
}

extensions/positron-r/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@
244244
rimraf "^3.0.2"
245245
unzipper "^0.10.11"
246246

247+
"@vscode/windows-registry@^1.1.0":
248+
version "1.1.0"
249+
resolved "https://registry.yarnpkg.com/@vscode/windows-registry/-/windows-registry-1.1.0.tgz#03dace7c29c46f658588b9885b9580e453ad21f9"
250+
integrity sha512-5AZzuWJpGscyiMOed0IuyEwt6iKmV5Us7zuwCDCFYMIq7tsvooO9BUiciywsvuthGz6UG4LSpeDeCxvgMVhnIw==
251+
247252
acorn-jsx@^5.3.1:
248253
version "5.3.2"
249254
resolved "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz"
@@ -2285,11 +2290,6 @@ [email protected], which@^2.0.1:
22852290
dependencies:
22862291
isexe "^2.0.0"
22872292

2288-
winreg@^1.2.5:
2289-
version "1.2.5"
2290-
resolved "https://registry.yarnpkg.com/winreg/-/winreg-1.2.5.tgz#b650383e89278952494b5d113ba049a5a4fa96d8"
2291-
integrity sha512-uf7tHf+tw0B1y+x+mKTLHkykBgK2KMs3g+KlzmyMbLvICSHQyB/xOFjTT8qZ3oeTFyU7Bbj4FzXitGG6jvKhYw==
2292-
22932293
word-wrap@^1.2.3:
22942294
version "1.2.4"
22952295
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.4.tgz#cb4b50ec9aca570abd1f52f33cd45b6c61739a9f"

0 commit comments

Comments
 (0)