Skip to content

Commit 7881363

Browse files
Copilotdavidfowl
andcommitted
Walk up parent directories from Python app, stop at AppHost's parent
Co-authored-by: davidfowl <[email protected]>
1 parent c866034 commit 7881363

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -651,29 +651,39 @@ private static string ResolveDefaultVirtualEnvironmentPath(IDistributedApplicati
651651
{
652652
var appDirectoryFullPath = Path.GetFullPath(appDirectory, builder.AppHostDirectory);
653653

654-
// Priority 1: Check if the virtual environment exists in the app directory
655-
var appDirVenvPath = Path.GetFullPath(virtualEnvironmentPath, appDirectoryFullPath);
656-
if (Directory.Exists(appDirVenvPath))
657-
{
658-
return appDirVenvPath;
659-
}
660-
661-
// Priority 2: Check the AppHost directory if the Python app is a child or at the same level
662-
// We only look up to the AppHost's direct parent, not beyond
663-
var appHostDirVenvPath = Path.GetFullPath(virtualEnvironmentPath, builder.AppHostDirectory);
654+
// Walk up from the Python app directory looking for the virtual environment
655+
// Stop at the AppHost's parent directory to avoid picking up unrelated venvs
656+
var appHostParentDirectory = Path.GetDirectoryName(builder.AppHostDirectory);
657+
var currentDirectory = appDirectoryFullPath;
664658

665-
// Check if the app directory is within the AppHost's tree (child or same directory)
666-
// The relative path should not start with ".." which would indicate going up beyond the AppHost
667-
var appDirRelativeToAppHost = Path.GetRelativePath(builder.AppHostDirectory, appDirectoryFullPath);
668-
var isAppDirWithinAppHostTree = !appDirRelativeToAppHost.StartsWith("..", StringComparison.Ordinal);
669-
670-
if (isAppDirWithinAppHostTree && Directory.Exists(appHostDirVenvPath))
659+
while (currentDirectory != null)
671660
{
672-
return appHostDirVenvPath;
661+
var venvPath = Path.Combine(currentDirectory, virtualEnvironmentPath);
662+
if (Directory.Exists(venvPath))
663+
{
664+
return venvPath;
665+
}
666+
667+
// Stop if we've reached the AppHost's parent directory
668+
if (string.Equals(currentDirectory, appHostParentDirectory, StringComparison.OrdinalIgnoreCase))
669+
{
670+
break;
671+
}
672+
673+
// Move up to the parent directory
674+
var parentDirectory = Path.GetDirectoryName(currentDirectory);
675+
676+
// Stop if we can't go up anymore or if we've gone beyond the AppHost's parent
677+
if (parentDirectory == null || parentDirectory == currentDirectory)
678+
{
679+
break;
680+
}
681+
682+
currentDirectory = parentDirectory;
673683
}
674684

675685
// Default: Return app directory path (for cases where the venv will be created later)
676-
return appDirVenvPath;
686+
return Path.Combine(appDirectoryFullPath, virtualEnvironmentPath);
677687
}
678688

679689
/// <summary>

0 commit comments

Comments
 (0)