Skip to content

Commit fca4795

Browse files
authored
lib: fail gracefully if we can't find the username (#2375)
1 parent 07e9d7c commit fca4795

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/find-python.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,35 @@ const win = process.platform === 'win32'
88
const logWithPrefix = require('./util').logWithPrefix
99

1010
const systemDrive = process.env.SystemDrive || 'C:'
11-
const username = process.env.USERNAME || process.env.USER || require('os').userInfo().username
11+
const username = process.env.USERNAME || process.env.USER || getOsUserInfo()
1212
const localAppData = process.env.LOCALAPPDATA || `${systemDrive}\\${username}\\AppData\\Local`
13+
const foundLocalAppData = process.env.LOCALAPPDATA || username
1314
const programFiles = process.env.ProgramW6432 || process.env.ProgramFiles || `${systemDrive}\\Program Files`
1415
const programFilesX86 = process.env['ProgramFiles(x86)'] || `${programFiles} (x86)`
1516

1617
const winDefaultLocationsArray = []
1718
for (const majorMinor of ['39', '38', '37', '36']) {
18-
winDefaultLocationsArray.push(
19-
`${localAppData}\\Programs\\Python\\Python${majorMinor}\\python.exe`,
20-
`${programFiles}\\Python${majorMinor}\\python.exe`,
21-
`${localAppData}\\Programs\\Python\\Python${majorMinor}-32\\python.exe`,
22-
`${programFiles}\\Python${majorMinor}-32\\python.exe`,
23-
`${programFilesX86}\\Python${majorMinor}-32\\python.exe`
24-
)
19+
if (foundLocalAppData) {
20+
winDefaultLocationsArray.push(
21+
`${localAppData}\\Programs\\Python\\Python${majorMinor}\\python.exe`,
22+
`${programFiles}\\Python${majorMinor}\\python.exe`,
23+
`${localAppData}\\Programs\\Python\\Python${majorMinor}-32\\python.exe`,
24+
`${programFiles}\\Python${majorMinor}-32\\python.exe`,
25+
`${programFilesX86}\\Python${majorMinor}-32\\python.exe`
26+
)
27+
} else {
28+
winDefaultLocationsArray.push(
29+
`${programFiles}\\Python${majorMinor}\\python.exe`,
30+
`${programFiles}\\Python${majorMinor}-32\\python.exe`,
31+
`${programFilesX86}\\Python${majorMinor}-32\\python.exe`
32+
)
33+
}
34+
}
35+
36+
function getOsUserInfo () {
37+
try {
38+
return require('os').userInfo().username
39+
} catch {}
2540
}
2641

2742
function PythonFinder (configPython, callback) {

0 commit comments

Comments
 (0)