Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to handle pixi error more gracefully #23937

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading