{"record":{"id":"93b775263dd1de6a","repo":"microsoft/aspire","slug":"cannot-update-virtual-environment-python-entrypoint","errorCode":null,"errorMessage":"Cannot update virtual environment: Python entrypoint annotation not found.","messagePattern":"Cannot update virtual environment: Python entrypoint annotation not found\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs","lineNumber":879,"sourceCode":"    /// </example>\n    [AspireExport]\n    public static IResourceBuilder<T> WithVirtualEnvironment<T>(\n        this IResourceBuilder<T> builder, string virtualEnvironmentPath, bool createIfNotExists = true) where T : PythonAppResource\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(virtualEnvironmentPath);\n\n        // Use the provided path verbatim - resolve relative paths against the app working directory\n        var resolvedPath = Path.IsPathRooted(virtualEnvironmentPath)\n            ? virtualEnvironmentPath\n            : Path.GetFullPath(virtualEnvironmentPath, builder.Resource.WorkingDirectory);\n\n        var virtualEnvironment = new VirtualEnvironment(resolvedPath);\n\n        // Get the entrypoint annotation to determine how to update the command\n        if (!builder.Resource.TryGetLastAnnotation<PythonEntrypointAnnotation>(out var entrypointAnnotation))\n        {\n            throw new InvalidOperationException(\"Cannot update virtual environment: Python entrypoint annotation not found.\");\n        }\n\n        // Update the command based on entrypoint type\n        string command = entrypointAnnotation.Type switch\n        {\n            EntrypointType.Executable => virtualEnvironment.GetExecutable(entrypointAnnotation.Entrypoint),\n            EntrypointType.Script or EntrypointType.Module => virtualEnvironment.GetExecutable(\"python\"),\n            _ => throw new InvalidOperationException($\"Unsupported entrypoint type: {entrypointAnnotation.Type}\")\n        };\n\n        builder.WithCommand(command);\n        builder.WithPythonEnvironment(env =>\n        {\n            env.VirtualEnvironment = virtualEnvironment;\n            env.CreateVenvIfNotExists = createIfNotExists;\n        });\n\n        // If createIfNotExists is false, remove venv creator","sourceCodeStart":861,"sourceCodeEnd":897,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs#L861-L897","documentation":"WithVirtualEnvironment rewrites the resource's command to point at the virtual environment's interpreter/executable. It first looks up the PythonEntrypointAnnotation on the resource; if the annotation is absent it throws InvalidOperationException because it cannot know how to rebase the command.","triggerScenarios":"Calling WithVirtualEnvironment on a builder whose resource was not created via the Python app entrypoint APIs (AddPythonApp/AddPythonExecutable), so no PythonEntrypointAnnotation exists.","commonSituations":"Creating a PythonResource manually with AddResource plus annotations; calling WithVirtualEnvironment before AddPythonApp ran; applying it to a different resource type that lacks the entrypoint annotation.","solutions":["Call AddPythonApp or AddPythonExecutable first so the PythonEntrypointAnnotation is registered, then call WithVirtualEnvironment.","Verify you are chaining on the correct builder/resource instance.","If constructing the resource manually, add a PythonEntrypointAnnotation with the correct EntrypointType before calling WithVirtualEnvironment.","Check ordering: WithVirtualEnvironment depends on prior entrypoint configuration."],"exampleFix":"// before\nvar py = builder.AddPythonApp(\"app\", \"main.py\"); // then WithVirtualEnvironment on wrong resource\n// after\nvar py = builder.AddPythonApp(\"app\", \"main.py\");\npy.WithVirtualEnvironment(\".venv\"); // chained on the same builder","handlingStrategy":"type-guard","validationCode":"bool canSetVenv = pyApp.Resource.HasAnnotation<PythonEntrypointAnnotation>();","typeGuard":"static bool HasEntrypointAnnotation(IResource resource) => resource.TryGetLastAnnotation<PythonEntrypointAnnotation>(out _);","tryCatchPattern":"try\n{\n    pyApp.WithVirtualEnvironment(\".venv\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"entrypoint annotation not found\"))\n{\n    logger.LogError(ex, \"Resource was not created via AddPythonApp/AddPythonExecutable.\");\n}","preventionTips":["Only call WithVirtualEnvironment on builders created by AddPythonApp/AddPythonExecutable.","Chain WithVirtualEnvironment on the same builder instance used for entrypoint setup.","Keep entrypoint configuration before virtual environment configuration."],"tags":["csharp","aspire","python","missing-annotation"],"backgroundTag":"missing-dependency","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}