Skip to content
Draft
Show file tree
Hide file tree
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
20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 38 additions & 10 deletions src/managers/poetry/poetryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ async function findPoetry(): Promise<string | undefined> {
}
}

/**
* Returns the platform-specific default path for Poetry's virtualenvs directory.
* - Linux: ~/.cache/pypoetry/virtualenvs
* - macOS: ~/Library/Caches/pypoetry/virtualenvs
* - Windows: %LOCALAPPDATA%\pypoetry\Cache\virtualenvs (or %APPDATA%\pypoetry\Cache\virtualenvs)
*/
export function getDefaultPoetryVirtualenvsPath(): string | undefined {
if (isWindows()) {
const localAppData = process.env.LOCALAPPDATA;
if (localAppData) {
return path.join(localAppData, 'pypoetry', 'Cache', 'virtualenvs');
}
const appData = process.env.APPDATA;
if (appData) {
return path.join(appData, 'pypoetry', 'Cache', 'virtualenvs');
}
return undefined;
}

const home = getUserHomeDir();
if (!home) {
return undefined;
}

if (process.platform === 'darwin') {
return path.join(home, 'Library', 'Caches', 'pypoetry', 'virtualenvs');
}

// Linux default
return path.join(home, '.cache', 'pypoetry', 'virtualenvs');
}

export const POETRY_GLOBAL = 'Global';

export const POETRY_PATH_KEY = `${ENVS_EXTENSION_ID}:poetry:POETRY_PATH`;
Expand Down Expand Up @@ -182,10 +214,7 @@ export async function getPoetryVirtualenvsPath(poetryExe?: string): Promise<stri
// Poetry might return the path with placeholders like {cache-dir}
// If it doesn't start with / or C:\ etc., assume it's using default
if (!path.isAbsolute(venvPath) || venvPath.includes('{')) {
const home = getUserHomeDir();
if (home) {
poetryVirtualenvsPath = path.join(home, '.cache', 'pypoetry', 'virtualenvs');
}
poetryVirtualenvsPath = getDefaultPoetryVirtualenvsPath();
} else {
poetryVirtualenvsPath = venvPath;
}
Expand All @@ -201,9 +230,8 @@ export async function getPoetryVirtualenvsPath(poetryExe?: string): Promise<stri
}

// Fallback to default location
const home = getUserHomeDir();
if (home) {
poetryVirtualenvsPath = path.join(home, '.cache', 'pypoetry', 'virtualenvs');
poetryVirtualenvsPath = getDefaultPoetryVirtualenvsPath();
if (poetryVirtualenvsPath) {
await state.set(POETRY_VIRTUALENVS_PATH_KEY, poetryVirtualenvsPath);
return poetryVirtualenvsPath;
}
Expand Down Expand Up @@ -257,9 +285,9 @@ async function nativeToPythonEnv(
isGlobalPoetryEnv = normalizedPrefix.startsWith(normalizedVirtualenvsPath);
} else {
// Fall back to checking the default location if we haven't cached the path yet
const homeDir = getUserHomeDir();
if (homeDir) {
const defaultPath = path.normalize(path.join(homeDir, '.cache', 'pypoetry', 'virtualenvs'));
const defaultVirtualenvsPath = getDefaultPoetryVirtualenvsPath();
if (defaultVirtualenvsPath) {
const defaultPath = path.normalize(defaultVirtualenvsPath);
isGlobalPoetryEnv = normalizedPrefix.startsWith(defaultPath);

// Try to get the actual path asynchronously for next time
Expand Down
94 changes: 94 additions & 0 deletions src/test/managers/poetry/poetryUtils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import assert from 'assert';
import * as path from 'path';
import * as sinon from 'sinon';
import * as platformUtils from '../../../common/utils/platformUtils';
import * as pathUtils from '../../../common/utils/pathUtils';
import { getDefaultPoetryVirtualenvsPath } from '../../../managers/poetry/poetryUtils';

suite('Poetry Utils - getDefaultPoetryVirtualenvsPath', () => {
let isWindowsStub: sinon.SinonStub;
let getUserHomeDirStub: sinon.SinonStub;
let originalPlatform: PropertyDescriptor | undefined;
let originalEnv: NodeJS.ProcessEnv;

setup(() => {
isWindowsStub = sinon.stub(platformUtils, 'isWindows');
getUserHomeDirStub = sinon.stub(pathUtils, 'getUserHomeDir');
originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform');
originalEnv = { ...process.env };
});

teardown(() => {
sinon.restore();
if (originalPlatform) {
Object.defineProperty(process, 'platform', originalPlatform);
}
process.env = originalEnv;
});

test('should return Linux path on Linux', () => {
isWindowsStub.returns(false);
Object.defineProperty(process, 'platform', { value: 'linux', configurable: true });
getUserHomeDirStub.returns('/home/testuser');

const result = getDefaultPoetryVirtualenvsPath();

assert.strictEqual(result, path.join('/home/testuser', '.cache', 'pypoetry', 'virtualenvs'));
});

test('should return macOS path on darwin', () => {
isWindowsStub.returns(false);
Object.defineProperty(process, 'platform', { value: 'darwin', configurable: true });
getUserHomeDirStub.returns('/Users/testuser');

const result = getDefaultPoetryVirtualenvsPath();

assert.strictEqual(result, path.join('/Users/testuser', 'Library', 'Caches', 'pypoetry', 'virtualenvs'));
});

test('should return LOCALAPPDATA path on Windows when LOCALAPPDATA is set', () => {
isWindowsStub.returns(true);
process.env.LOCALAPPDATA = 'C:\\Users\\testuser\\AppData\\Local';
process.env.APPDATA = 'C:\\Users\\testuser\\AppData\\Roaming';

const result = getDefaultPoetryVirtualenvsPath();

assert.strictEqual(
result,
path.join('C:\\Users\\testuser\\AppData\\Local', 'pypoetry', 'Cache', 'virtualenvs'),
);
});

test('should fall back to APPDATA on Windows when LOCALAPPDATA is not set', () => {
isWindowsStub.returns(true);
delete process.env.LOCALAPPDATA;
process.env.APPDATA = 'C:\\Users\\testuser\\AppData\\Roaming';

const result = getDefaultPoetryVirtualenvsPath();

assert.strictEqual(
result,
path.join('C:\\Users\\testuser\\AppData\\Roaming', 'pypoetry', 'Cache', 'virtualenvs'),
);
});

test('should return undefined on Windows when neither LOCALAPPDATA nor APPDATA is set', () => {
isWindowsStub.returns(true);
delete process.env.LOCALAPPDATA;
delete process.env.APPDATA;

const result = getDefaultPoetryVirtualenvsPath();

assert.strictEqual(result, undefined);
});

test('should return undefined when home directory is not available on non-Windows', () => {
isWindowsStub.returns(false);
Object.defineProperty(process, 'platform', { value: 'linux', configurable: true });
getUserHomeDirStub.returns('');

const result = getDefaultPoetryVirtualenvsPath();

assert.strictEqual(result, undefined);
});
});