Skip to content

Commit 4302976

Browse files
Copilotdavidfowl
andcommitted
Use GetExecutablePath to defer venv validation as requested
Co-authored-by: davidfowl <[email protected]>
1 parent fde1d65 commit 4302976

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static IResourceBuilder<PythonAppResource> AddPythonApp(
120120
: Path.Join(appDirectory, virtualEnvironmentPath));
121121

122122
var instrumentationExecutable = virtualEnvironment.GetExecutable("opentelemetry-instrument");
123-
var pythonExecutable = virtualEnvironment.GetExecutable("python") ?? "python";
123+
var pythonExecutable = virtualEnvironment.GetExecutablePath("python");
124124
var appExecutable = instrumentationExecutable ?? pythonExecutable;
125125

126126
var resource = new PythonAppResource(name, appExecutable, appDirectory);

src/Aspire.Hosting.Python/VirtualEnvironment.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,21 @@ public string GetRequiredExecutable(string name)
5555
$"The executable {name} could not be found in the virtual environment at '{virtualEnvironmentPath}' . " +
5656
"Make sure the virtual environment is initialized and the executable is installed.");
5757
}
58+
59+
/// <summary>
60+
/// Gets the expected path for an executable in the virtual environment without validating it exists.
61+
/// </summary>
62+
/// <param name="name">The name of the executable.</param>
63+
/// <returns>The expected path to the executable in the virtual environment.</returns>
64+
public string GetExecutablePath(string name)
65+
{
66+
if (OperatingSystem.IsWindows())
67+
{
68+
return Path.Join(virtualEnvironmentPath, "Scripts", name + ".exe");
69+
}
70+
else
71+
{
72+
return Path.Join(virtualEnvironmentPath, "bin", name);
73+
}
74+
}
5875
}

tests/Aspire.Hosting.Python.Tests/AddPythonAppTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,11 @@ public async Task AddPythonAppWithoutVirtualEnvironment_FallsBackToSystemPython(
261261
Assert.Equal("pythonProject", pythonProjectResource.Name);
262262
Assert.Equal(projectDirectory, pythonProjectResource.WorkingDirectory);
263263

264-
// When venv doesn't exist, should fall back to system "python"
265-
Assert.Equal("python", pythonProjectResource.Command);
264+
// When venv doesn't exist, should still use the expected venv path (not validated)
265+
var expectedPythonPath = OperatingSystem.IsWindows()
266+
? Path.Join(projectDirectory, ".venv", "Scripts", "python.exe")
267+
: Path.Join(projectDirectory, ".venv", "bin", "python");
268+
Assert.Equal(expectedPythonPath, pythonProjectResource.Command);
266269

267270
var commandArguments = await ArgumentEvaluator.GetArgumentListAsync(pythonProjectResource, TestServiceProvider.Instance);
268271

0 commit comments

Comments
 (0)