Skip to content

Commit

Permalink
Do not resolve symbolic links in posix locator if they exceed the cou…
Browse files Browse the repository at this point in the history
…nt limit (#21658)

Closes #21310

Fixes interpreter discovery running forever for non-Windows OS
  • Loading branch information
Kartik Raj authored Jul 19, 2023
1 parent c256678 commit be334bd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class PosixKnownPathsLocator extends Locator<BasicEnvInfo> {
// those binaries as environments.
const knownDirs = (await commonPosixBinPaths()).filter((dirname) => !isPyenvShimDir(dirname));
let pythonBinaries = await getPythonBinFromPosixPaths(knownDirs);
traceVerbose(`Found ${pythonBinaries.length} python binaries in posix paths`);

// Filter out MacOS system installs of Python 2 if necessary.
if (isMacPython2Deprecated) {
Expand Down
11 changes: 8 additions & 3 deletions src/client/pythonEnvironments/common/externalDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IDisposable, IConfigurationService } from '../../common/types';
import { chain, iterable } from '../../common/utils/async';
import { getOSType, OSType } from '../../common/utils/platform';
import { IServiceContainer } from '../../ioc/types';
import { traceVerbose } from '../../logging';
import { traceError, traceVerbose } from '../../logging';

let internalServiceContainer: IServiceContainer;
export function initializeExternalDependencies(serviceContainer: IServiceContainer): void {
Expand Down Expand Up @@ -93,16 +93,21 @@ export function arePathsSame(path1: string, path2: string): boolean {
return normCasePath(path1) === normCasePath(path2);
}

export async function resolveSymbolicLink(absPath: string, stats?: fsapi.Stats): Promise<string> {
export async function resolveSymbolicLink(absPath: string, stats?: fsapi.Stats, count?: number): Promise<string> {
stats = stats ?? (await fsapi.lstat(absPath));
if (stats.isSymbolicLink()) {
if (count && count > 5) {
traceError(`Detected a potential symbolic link loop at ${absPath}, terminating resolution.`);
return absPath;
}
const link = await fsapi.readlink(absPath);
// Result from readlink is not guaranteed to be an absolute path. For eg. on Mac it resolves
// /usr/local/bin/python3.9 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9
//
// The resultant path is reported relative to the symlink directory we resolve. Convert that to absolute path.
const absLinkPath = path.isAbsolute(link) ? link : path.resolve(path.dirname(absPath), link);
return resolveSymbolicLink(absLinkPath);
count = count ? count + 1 : 1;
return resolveSymbolicLink(absLinkPath, undefined, count);
}
return absPath;
}
Expand Down
3 changes: 2 additions & 1 deletion src/client/pythonEnvironments/common/posixUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as path from 'path';
import { uniq } from 'lodash';
import { getSearchPathEntries } from '../../common/utils/exec';
import { resolveSymbolicLink } from './externalDependencies';
import { traceError, traceInfo } from '../../logging';
import { traceError, traceInfo, traceVerbose } from '../../logging';

/**
* Determine if the given filename looks like the simplest Python executable.
Expand Down Expand Up @@ -123,6 +123,7 @@ export async function getPythonBinFromPosixPaths(searchDirs: string[]): Promise<
// Ensure that we have a collection of unique global binaries by
// resolving all symlinks to the target binaries.
try {
traceVerbose(`Attempting to resolve symbolic link: ${filepath}`);
const resolvedBin = await resolveSymbolicLink(filepath);
if (binToLinkMap.has(resolvedBin)) {
binToLinkMap.get(resolvedBin)?.push(filepath);
Expand Down

0 comments on commit be334bd

Please sign in to comment.