Skip to content

Commit d31b870

Browse files
Kartik Rajkarthiknadig
Kartik Raj
authored andcommitted
Revert "Remove old code for folder support in interpreter path setting" (#22638)
Reverts #22413 #22618 Turns out we still need this code for old deprecated APIs that we expose, one of which is used in testing.
1 parent 780870a commit d31b870

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

src/client/common/configSettings.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// eslint-disable-next-line camelcase
44
import * as path from 'path';
5+
import * as fs from 'fs';
56
import {
67
ConfigurationChangeEvent,
78
ConfigurationTarget,
@@ -35,6 +36,8 @@ import {
3536
} from './types';
3637
import { debounceSync } from './utils/decorators';
3738
import { SystemVariables } from './variables/systemVariables';
39+
import { getOSType, OSType } from './utils/platform';
40+
import { isWindows } from './platform/platformService';
3841

3942
const untildify = require('untildify');
4043

@@ -391,7 +394,7 @@ export class PythonSettings implements IPythonSettings {
391394

392395
// eslint-disable-next-line class-methods-use-this
393396
protected getPythonExecutable(pythonPath: string): string {
394-
return untildify(pythonPath);
397+
return getPythonExecutable(pythonPath);
395398
}
396399

397400
protected onWorkspaceFoldersChanged(): void {
@@ -490,3 +493,63 @@ function getAbsolutePath(pathToCheck: string, rootDir: string | undefined): stri
490493
}
491494
return path.isAbsolute(pathToCheck) ? pathToCheck : path.resolve(rootDir, pathToCheck);
492495
}
496+
497+
function getPythonExecutable(pythonPath: string): string {
498+
pythonPath = untildify(pythonPath) as string;
499+
500+
// If only 'python'.
501+
if (
502+
pythonPath === 'python' ||
503+
pythonPath.indexOf(path.sep) === -1 ||
504+
path.basename(pythonPath) === path.dirname(pythonPath)
505+
) {
506+
return pythonPath;
507+
}
508+
509+
if (isValidPythonPath(pythonPath)) {
510+
return pythonPath;
511+
}
512+
// Keep python right on top, for backwards compatibility.
513+
514+
const KnownPythonExecutables = [
515+
'python',
516+
'python4',
517+
'python3.6',
518+
'python3.5',
519+
'python3',
520+
'python2.7',
521+
'python2',
522+
'python3.7',
523+
'python3.8',
524+
'python3.9',
525+
];
526+
527+
for (let executableName of KnownPythonExecutables) {
528+
// Suffix with 'python' for linux and 'osx', and 'python.exe' for 'windows'.
529+
if (isWindows()) {
530+
executableName = `${executableName}.exe`;
531+
if (isValidPythonPath(path.join(pythonPath, executableName))) {
532+
return path.join(pythonPath, executableName);
533+
}
534+
if (isValidPythonPath(path.join(pythonPath, 'Scripts', executableName))) {
535+
return path.join(pythonPath, 'Scripts', executableName);
536+
}
537+
} else {
538+
if (isValidPythonPath(path.join(pythonPath, executableName))) {
539+
return path.join(pythonPath, executableName);
540+
}
541+
if (isValidPythonPath(path.join(pythonPath, 'bin', executableName))) {
542+
return path.join(pythonPath, 'bin', executableName);
543+
}
544+
}
545+
}
546+
547+
return pythonPath;
548+
}
549+
550+
function isValidPythonPath(pythonPath: string): boolean {
551+
return (
552+
fs.existsSync(pythonPath) &&
553+
path.basename(getOSType() === OSType.Windows ? pythonPath.toLowerCase() : pythonPath).startsWith('python')
554+
);
555+
}

0 commit comments

Comments
 (0)