Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Jul 11, 2022
1 parent b5c1ab3 commit 6b51ab7
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 44 deletions.
21 changes: 11 additions & 10 deletions src/kernels/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { SysInfoReason } from '../messageTypes';
import { getNormalizedInterpreterPath, getInterpreterHash } from '../platform/pythonEnvironments/info/interpreter';
import { getTelemetrySafeVersion } from '../platform/telemetry/helpers';
import { EnvironmentType, PythonEnvironment } from '../platform/pythonEnvironments/info';
import { fsPathToUri } from '../platform/vscode-path/utils';
import { deserializePythonEnvironment, serializePythonEnvironment } from '../platform/api/pythonApi';
import { JupyterKernelSpec } from './jupyter/jupyterKernelSpec';
import { Resource } from '../platform/common/types';
Expand Down Expand Up @@ -1080,7 +1079,9 @@ export function getKernelId(spec: IJupyterKernelSpec, interpreter?: PythonEnviro
}
const prefixForRemoteKernels = serverId ? `${serverId}.` : '';
const specPath = getFilePath(
getNormalizedInterpreterPath(fsPathToUri(spec.interpreterPath) || Uri.file(spec.executable))
getNormalizedInterpreterPath(
spec.interpreterPath ? Uri.file(spec.interpreterPath) : Uri.file(spec.executable)
)
);
const interpreterPath = getFilePath(getNormalizedInterpreterPath(interpreter?.uri)) || '';
return `${prefixForRemoteKernels}${
Expand Down Expand Up @@ -1220,18 +1221,18 @@ export function getKernelPathFromKernelConnection(kernelConnection?: KernelConne
kernelConnection.kind === 'startUsingLocalKernelSpec') &&
kernelConnection.kernelSpec.language === PYTHON_LANGUAGE)
) {
return fsPathToUri(
kernelSpec?.metadata?.interpreter?.path || kernelSpec?.interpreterPath || kernelSpec?.executable
);
const pathValue =
kernelSpec?.metadata?.interpreter?.path || kernelSpec?.interpreterPath || kernelSpec?.executable;
return pathValue ? Uri.file(pathValue) : undefined;
} else {
// For non python kernels, give preference to the executable path in the kernelspec
// E.g. if we have a rust kernel, we should show the path to the rust executable not the interpreter (such as conda env that owns the rust runtime).
return fsPathToUri(
const pathValue =
model?.executable ||
kernelSpec?.executable ||
kernelSpec?.metadata?.interpreter?.path ||
kernelSpec?.interpreterPath
);
kernelSpec?.executable ||
kernelSpec?.metadata?.interpreter?.path ||
kernelSpec?.interpreterPath;
return pathValue ? Uri.file(pathValue) : undefined;
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/kernels/raw/finder/jupyterPaths.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { tryGetRealPath } from '../../../platform/common/utils.node';
import { IEnvironmentVariablesProvider } from '../../../platform/common/variables/types';
import { traceDecoratorVerbose } from '../../../platform/logging';
import { OSType } from '../../../platform/common/utils/platform.node';
import { fsPathToUri } from '../../../platform/vscode-path/utils';
import { ResourceMap, ResourceSet } from '../../../platform/vscode-path/map';
import { noop } from '../../../platform/common/utils/misc';
import { PythonEnvironment } from '../../../platform/pythonEnvironments/info';
Expand Down Expand Up @@ -118,7 +117,7 @@ export class JupyterPaths {
runtimeDir = uriPath.joinPath(userHomeDir, macJupyterRuntimePath);
} else {
runtimeDir = process.env['$XDG_RUNTIME_DIR']
? fsPathToUri(path.join(process.env['$XDG_RUNTIME_DIR'], 'jupyter', 'runtime'))
? Uri.file(path.join(process.env['$XDG_RUNTIME_DIR'], 'jupyter', 'runtime'))
: uriPath.joinPath(userHomeDir, '.local', 'share', 'jupyter', 'runtime');
}
}
Expand Down Expand Up @@ -354,14 +353,15 @@ export class JupyterPaths {
: [];

if (jupyterPathVars.length > 0) {
await Promise.all(
jupyterPathVars.map(async (jupyterPath) => {
const realPath = await tryGetRealPath(Uri.file(jupyterPath));
if (realPath) {
paths.add(realPath);
}
})
// Preserve the order of the items.
const jupyterPaths = await Promise.all(
jupyterPathVars.map(async (jupyterPath) => tryGetRealPath(Uri.file(jupyterPath)))
);
jupyterPaths.forEach((jupyterPath) => {
if (jupyterPath) {
paths.add(jupyterPath);
}
});
}

return Array.from(paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { IInterpreterService } from '../../../platform/interpreter/contracts';
import { areInterpreterPathsSame } from '../../../platform/pythonEnvironments/info/interpreter';
import { captureTelemetry, Telemetry } from '../../../telemetry';
import { PythonEnvironment } from '../../../platform/pythonEnvironments/info';
import { fsPathToUri } from '../../../platform/vscode-path/utils';
import { ResourceSet } from '../../../platform/vscode-path/map';

/**
Expand Down Expand Up @@ -384,7 +383,7 @@ export class LocalPythonAndRelatedNonPythonKernelSpecFinder extends LocalKernelS
const matchBasedOnInterpreterPath = interpreters.find((i) => {
if (
kernelSpec.interpreterPath &&
areInterpreterPathsSame(fsPathToUri(kernelSpec.interpreterPath), i.uri)
areInterpreterPathsSame(Uri.file(kernelSpec.interpreterPath), i.uri)
) {
traceVerbose(`Kernel ${kernelSpec.name} matches ${i.displayName} based on interpreter path.`);
return true;
Expand Down
3 changes: 1 addition & 2 deletions src/platform/api/pythonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Licensed under the MIT License.

import { Disposable, EventEmitter, Event, Uri, workspace, ExtensionMode } from 'vscode';
import { fsPathToUri } from '../vscode-path/utils';
import {
IPythonApiProvider,
IPythonExtensionChecker,
Expand Down Expand Up @@ -39,7 +38,7 @@ export function deserializePythonEnvironment(
...pythonVersion,
sysPrefix: pythonVersion.sysPrefix || '',
uri: Uri.file(pythonVersion.path || ''),
envPath: fsPathToUri(pythonVersion.envPath)
envPath: pythonVersion.envPath ? Uri.file(pythonVersion.envPath) : undefined
};

// Cleanup stuff that shouldn't be there.
Expand Down
5 changes: 2 additions & 3 deletions src/platform/common/utils.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import * as path from '../../platform/vscode-path/path';
import * as fsExtra from 'fs-extra';
import { SemVer, parse } from 'semver';
import { Uri } from 'vscode';
import { fsPathToUri } from '../vscode-path/utils';
import { IWorkspaceService } from './application/types';
import { IConfigurationService, Resource } from './types';
import { getOSType, OSType } from './utils/platform';
import { IFileSystem } from './platform/types';
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const untildify = require('untildify');

export async function tryGetRealPath(expectedPath: Uri): Promise<Uri | undefined> {
export async function tryGetRealPath(expectedPath: Uri): Promise<Uri> {
try {
// Real path throws if the expected path is not actually created yet.
let realPath = await fsExtra.realpath(expectedPath.fsPath);
Expand All @@ -24,7 +23,7 @@ export async function tryGetRealPath(expectedPath: Uri): Promise<Uri | undefined
realPath = realPath.replace(/\\/g, '/');
}

return fsPathToUri(realPath);
return Uri.file(realPath);
} catch {
// So if that happens, just return the original path.
return expectedPath;
Expand Down
5 changes: 2 additions & 3 deletions src/platform/common/utils/platform.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import { Uri } from 'vscode';
import * as os from 'os';
import { fsPathToUri } from '../../vscode-path/utils';
import { EnvironmentVariables } from '../variables/types';
import { getOSType, OSType } from './platform';
export * from './platform';
Expand All @@ -24,10 +23,10 @@ export function getPathEnvironmentVariable(): string | undefined {

export function getUserHomeDir(): Uri {
if (getOSType() === OSType.Windows) {
return fsPathToUri(getEnvironmentVariable('USERPROFILE') || homePath)!;
return Uri.file(getEnvironmentVariable('USERPROFILE') || homePath);
}
const homeVar = getEnvironmentVariable('HOME') || getEnvironmentVariable('HOMEPATH') || homePath;

// Make sure if linux, it uses linux separators
return fsPathToUri(homeVar?.replace(/\\/g, '/'))!;
return Uri.file(homeVar?.replace(/\\/g, '/'));
}
6 changes: 0 additions & 6 deletions src/platform/vscode-path/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// Miscellaneous functions from other spots in VS code

import { Uri } from 'vscode';

export function fsPathToUri(path: string | undefined) {
return path ? Uri.file(path) : undefined;
}

export namespace Schemas {
/**
* A schema that is used for models that exist in memory
Expand Down
4 changes: 2 additions & 2 deletions src/test/interpreters/envActivation.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getActivatedEnvVariables } from './index.node';
import { Resource } from '../../platform/common/types';
import { IEnvironmentActivationService } from '../../platform/interpreter/activation/types';
import { PythonEnvironment } from '../../platform/pythonEnvironments/info';
import { fsPathToUri } from '../../platform/vscode-path/utils';
import { Uri } from 'vscode';

@injectable()
export class EnvironmentActivationService implements IEnvironmentActivationService {
Expand All @@ -15,7 +15,7 @@ export class EnvironmentActivationService implements IEnvironmentActivationServi
interpreter?: PythonEnvironment,
_allowExceptions?: boolean
): Promise<NodeJS.ProcessEnv | undefined> {
return getActivatedEnvVariables(interpreter?.uri || fsPathToUri(process.env.CI_PYTHON_PATH || 'python')!);
return getActivatedEnvVariables(interpreter?.uri || Uri.file(process.env.CI_PYTHON_PATH || 'python')!);
}
async hasActivationCommands(_resource: Resource, _interpreter?: PythonEnvironment): Promise<boolean> {
return false;
Expand Down
9 changes: 4 additions & 5 deletions src/test/interpreters/interpreterService.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { IPythonExtensionChecker } from '../../platform/api/types';
import { Resource } from '../../platform/common/types';
import { IInterpreterService } from '../../platform/interpreter/contracts';
import { PythonEnvironment } from '../../platform/pythonEnvironments/info';
import { fsPathToUri } from '../../platform/vscode-path/utils';
import { ResourceSet } from '../../platform/vscode-path/map';

let interpretersCache: Promise<PythonEnvironment[]> | undefined;
Expand Down Expand Up @@ -43,7 +42,7 @@ export class InterpreterService implements IInterpreterService {
this.validatePythonExtension();
const pythonPath = this.customInterpretersPerUri.has(resource?.fsPath || '')
? this.customInterpretersPerUri.get(resource?.fsPath || '')!
: fsPathToUri(process.env.CI_PYTHON_PATH || 'python');
: Uri.file(process.env.CI_PYTHON_PATH || 'python');
return getInterpreterInfo(pythonPath);
}

Expand All @@ -68,9 +67,9 @@ export class InterpreterService implements IInterpreterService {

async function getAllInterpreters(): Promise<PythonEnvironment[]> {
const allInterpreters = await Promise.all([
getInterpreterInfo(fsPathToUri(process.env.CI_PYTHON_PATH)),
getInterpreterInfo(fsPathToUri(process.env.CI_PYTHON_PATH2)),
getInterpreterInfo(fsPathToUri('python'))
getInterpreterInfo(process.env.CI_PYTHON_PATH ? Uri.file(process.env.CI_PYTHON_PATH) : undefined),
getInterpreterInfo(process.env.CI_PYTHON_PATH2 ? Uri.file(process.env.CI_PYTHON_PATH2) : undefined),
getInterpreterInfo(Uri.file('python'))
]);
const interpreters: PythonEnvironment[] = [];
const items = new ResourceSet();
Expand Down
3 changes: 1 addition & 2 deletions src/test/kernels/raw/finder/jupyterPaths.node.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as path from '../../../../platform/vscode-path/path';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../../constants.node';
import { uriEquals } from '../../../datascience/helpers';

suite.only('Jupyter Paths', () => {
suite('Jupyter Paths', () => {
const disposables: IDisposable[] = [];
let jupyterPaths: JupyterPaths;
let platformService: IPlatformService;
Expand Down Expand Up @@ -292,7 +292,6 @@ suite.only('Jupyter Paths', () => {
const dataDirs = await jupyterPaths.getDataDirs({ resource: undefined, interpreter });

assert.strictEqual(dataDirs.length, 6);
assert.strictEqual(dataDirs[0].toString(), JSON.stringify(dataDirs));
assert.strictEqual(dataDirs[0].toString(), Uri.file(jupyter_Paths[0]).toString());
assert.strictEqual(dataDirs[1].toString(), Uri.file(jupyter_Paths[1]).toString());
assert.strictEqual(dataDirs[2].toString(), Uri.file(jupyter_Paths[2]).toString());
Expand Down

0 comments on commit 6b51ab7

Please sign in to comment.