{"record":{"id":"32e6e3b035bf1266","repo":"LykosAI/StabilityMatrix","slug":"venv-already-exists","errorCode":null,"errorMessage":"Venv already exists","messagePattern":"Venv already exists","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Python/PyVenvRunner.cs","lineNumber":144,"sourceCode":"    {\n        EnvironmentVariables = env(EnvironmentVariables);\n    }\n\n    /// <returns>True if the venv has a Scripts\\python.exe file</returns>\n    public bool Exists() => PythonPath.Exists;\n\n    /// <summary>\n    /// Creates a venv at the configured path.\n    /// </summary>\n    public async Task Setup(\n        bool existsOk = false,\n        Action<ProcessOutput>? onConsoleOutput = null,\n        CancellationToken cancellationToken = default\n    )\n    {\n        if (!existsOk && Exists())\n        {\n            throw new InvalidOperationException(\"Venv already exists\");\n        }\n\n        // Create RootPath if it doesn't exist\n        RootPath.Create();\n\n        // Create venv (copy mode if windows)\n        var args = new string[] { \"-m\", \"virtualenv\", Compat.IsWindows ? \"--always-copy\" : \"\", RootPath };\n\n        var venvProc = ProcessRunner.StartAnsiProcess(\n            BaseInstall.PythonExePath,\n            args,\n            WorkingDirectory?.FullPath,\n            onConsoleOutput\n        );\n\n        try\n        {\n            await venvProc.WaitForExitAsync(cancellationToken).ConfigureAwait(false);","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Python/PyVenvRunner.cs#L126-L162","documentation":"Setup() throws this InvalidOperationException when a virtual environment already exists at RootPath and the existsOk parameter was not set to true. The library refuses to run `python -m venv` over an existing venv to avoid corrupting or partially re-creating one. Callers must pass existsOk: true or delete the venv first.","triggerScenarios":"Calling PyVenvRunner.Setup() when Exists() is true (RootPath/pyvenv.cfg or the venv dir already present) without existsOk=true. Seen in practice via MigrateAsync re-running setup on an already-created venv.","commonSituations":"Re-running a package migration or install step after a previous successful venv creation; retrying setup after a partially failed run that still created the folder; pointing a new PyVenvRunner at a path that already holds a venv.","solutions":["Delete the existing venv directory at RootPath before calling Setup, or call PyVenvRunner.DeleteAsync().","Pass existsOk: true if re-using an existing venv is intentional.","Check Exists() before Setup and skip creation when it returns true."],"exampleFix":"// before\nawait venvRunner.Setup(null, onConsoleOutput, ct);\n// after\nif (venvRunner.Exists())\n{\n    await venvRunner.DeleteAsync(ct);\n}\nawait venvRunner.Setup(null, onConsoleOutput, ct);","handlingStrategy":"validation","validationCode":"if (venvRunner.Exists())\n{\n    // skip setup or delete first\n    await venvRunner.DeleteAsync(ct);\n}","typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException) when (venvRunner.Exists()) { /* venv already provisioned; continue */ }","preventionTips":["Always check Exists() before Setup.","Make setup idempotent by passing existsOk: true where re-use is safe.","Delete stale/partial venvs in error-recovery paths."],"tags":["python","venv","state"],"backgroundTag":"file-already-exists","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}