@@ -17,6 +17,19 @@ import {
1717} from '../common/nativePythonFinder' ;
1818import { getShellActivationCommands , shortVersion , sortEnvironments } from '../common/utils' ;
1919
20+ /**
21+ * Checks if the POETRY_VIRTUALENVS_IN_PROJECT environment variable is set to a truthy value.
22+ * When true, Poetry creates virtualenvs in the project's `.venv` directory.
23+ * Mirrors the PET server logic in `pet-poetry/src/env_variables.rs`.
24+ */
25+ export function isPoetryVirtualenvsInProject ( ) : boolean {
26+ const value = process . env . POETRY_VIRTUALENVS_IN_PROJECT ;
27+ if ( value === undefined ) {
28+ return false ;
29+ }
30+ return value === '1' || value . toLowerCase ( ) === 'true' ;
31+ }
32+
2033async function findPoetry ( ) : Promise < string | undefined > {
2134 try {
2235 return await which ( 'poetry' ) ;
@@ -251,19 +264,28 @@ async function nativeToPythonEnv(
251264
252265 // Determine if the environment is in Poetry's global virtualenvs directory
253266 let isGlobalPoetryEnv = false ;
254- const virtualenvsPath = poetryVirtualenvsPath ; // Use the cached value if available
255- if ( virtualenvsPath ) {
256- const normalizedVirtualenvsPath = path . normalize ( virtualenvsPath ) ;
257- isGlobalPoetryEnv = normalizedPrefix . startsWith ( normalizedVirtualenvsPath ) ;
267+
268+ // If POETRY_VIRTUALENVS_IN_PROJECT is set, environments are created in-project (.venv)
269+ // and should not be classified as global
270+ if ( isPoetryVirtualenvsInProject ( ) && info . project ) {
271+ isGlobalPoetryEnv = false ;
258272 } else {
259- // Fall back to checking the default location if we haven't cached the path yet
260- const homeDir = getUserHomeDir ( ) ;
261- if ( homeDir ) {
262- const defaultPath = path . normalize ( path . join ( homeDir , '.cache' , 'pypoetry' , 'virtualenvs' ) ) ;
263- isGlobalPoetryEnv = normalizedPrefix . startsWith ( defaultPath ) ;
264-
265- // Try to get the actual path asynchronously for next time
266- getPoetryVirtualenvsPath ( _poetry ) . catch ( ( e ) => traceError ( `Error getting Poetry virtualenvs path: ${ e } ` ) ) ;
273+ const virtualenvsPath = poetryVirtualenvsPath ; // Use the cached value if available
274+ if ( virtualenvsPath ) {
275+ const normalizedVirtualenvsPath = path . normalize ( virtualenvsPath ) ;
276+ isGlobalPoetryEnv = normalizedPrefix . startsWith ( normalizedVirtualenvsPath ) ;
277+ } else {
278+ // Fall back to checking the default location if we haven't cached the path yet
279+ const homeDir = getUserHomeDir ( ) ;
280+ if ( homeDir ) {
281+ const defaultPath = path . normalize ( path . join ( homeDir , '.cache' , 'pypoetry' , 'virtualenvs' ) ) ;
282+ isGlobalPoetryEnv = normalizedPrefix . startsWith ( defaultPath ) ;
283+
284+ // Try to get the actual path asynchronously for next time
285+ getPoetryVirtualenvsPath ( _poetry ) . catch ( ( e ) =>
286+ traceError ( `Error getting Poetry virtualenvs path: ${ e } ` ) ,
287+ ) ;
288+ }
267289 }
268290 }
269291
0 commit comments