Skip to content

Commit

Permalink
Attempt to handle pixi error more gracefully (#23937)
Browse files Browse the repository at this point in the history
For: #23911 and
#23906

(For virtual/remote scenario) Locating pixi environment, regardless of
presence of pixi environment, is leading to crash. Hoping to address
this and handle errors more gracefully so program does not terminate.

/cc @baszalmstra
  • Loading branch information
anthonykim1 authored Aug 12, 2024
1 parent 3fea993 commit f417024
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions src/client/pythonEnvironments/common/environmentManagers/pixi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,31 +169,47 @@ export class Pixi {
*/
@cache(1_000, true, 1_000)
public async getPixiInfo(cwd: string): Promise<PixiInfo | undefined> {
const infoOutput = await exec(this.command, ['info', '--json'], {
cwd,
throwOnStdErr: false,
}).catch(traceError);
if (!infoOutput) {
try {
const infoOutput = await exec(this.command, ['info', '--json'], {
cwd,
throwOnStdErr: false,
});

if (!infoOutput || !infoOutput.stdout) {
return undefined;
}

const pixiInfo: PixiInfo = JSON.parse(infoOutput.stdout);
return pixiInfo;
} catch (error) {
traceError(`Failed to get pixi info for ${cwd}`, error);
return undefined;
}

const pixiInfo: PixiInfo = JSON.parse(infoOutput.stdout);
return pixiInfo;
}

/**
* Runs `pixi --version` and returns the version part of the output.
*/
@cache(30_000, true, 10_000)
public async getVersion(): Promise<string | undefined> {
const versionOutput = await exec(this.command, ['--version'], {
throwOnStdErr: false,
}).catch(traceError);
if (!versionOutput) {
try {
const versionOutput = await exec(this.command, ['--version'], {
throwOnStdErr: false,
});
if (!versionOutput || !versionOutput.stdout) {
return undefined;
}

const versionParts = versionOutput.stdout.split(' ');
if (versionParts.length < 2) {
return undefined;
}

return versionParts[1].trim();
} catch (error) {
traceError(`Failed to get pixi version`, error);
return undefined;
}

return versionOutput.stdout.split(' ')[1].trim();
}

/**
Expand Down Expand Up @@ -279,13 +295,24 @@ export async function getPixiEnvironmentFromInterpreter(

// Usually the pixi environments are stored under `<projectDir>/.pixi/envs/<environment>/`. So,
// we walk backwards to determine the project directory.
const envName = path.basename(prefix);
const envsDir = path.dirname(prefix);
const dotPixiDir = path.dirname(envsDir);
const pixiProjectDir = path.dirname(dotPixiDir);
let envName: string | undefined;
let envsDir: string;
let dotPixiDir: string;
let pixiProjectDir: string;
let pixiInfo: PixiInfo | undefined;

try {
envName = path.basename(prefix);
envsDir = path.dirname(prefix);
dotPixiDir = path.dirname(envsDir);
pixiProjectDir = path.dirname(dotPixiDir);

// Invoke pixi to get information about the pixi project
pixiInfo = await pixi.getPixiInfo(pixiProjectDir);
} catch (error) {
traceWarn('Error processing paths or getting Pixi Info:', error);
}

// Invoke pixi to get information about the pixi project
const pixiInfo = await pixi.getPixiInfo(pixiProjectDir);
if (!pixiInfo || !pixiInfo.project_info) {
traceWarn(`failed to determine pixi project information for the interpreter at ${interpreterPath}`);
return undefined;
Expand Down

0 comments on commit f417024

Please sign in to comment.