{"record":{"id":"15d843a32177894c","repo":"LykosAI/StabilityMatrix","slug":"venv-already-exists-uvvenvrunner","errorCode":null,"errorMessage":"Venv already exists","messagePattern":"Venv already exists","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Python/UvVenvRunner.cs","lineNumber":144,"sourceCode":"\n    /// <returns>True if the venv has a Scripts\\python.exe file</returns>\n    public bool Exists() => PythonPath.Exists;\n\n    private FilePath UvExecutablePath =>\n        new(GlobalConfig.LibraryDir, \"Assets\", \"uv\", Compat.IsWindows ? \"uv.exe\" : \"uv\");\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 ProcessArgsBuilder(\n            \"venv\",\n            RootPath.ToString(),\n            \"--allow-existing\",\n            \"--python\",\n            BaseInstall.PythonExePath\n        );\n\n        var venvProc = ProcessRunner.StartAnsiProcess(\n            UvExecutablePath,\n            args.ToProcessArgs(),\n            WorkingDirectory?.FullPath,","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Python/UvVenvRunner.cs#L126-L162","documentation":"Setup() creates a uv-managed Python virtual environment at RootPath. By default it refuses to overwrite an existing venv: if PythonPath (e.g. <RootPath>/Scripts/python.exe) already exists and existsOk is false, it throws this InvalidOperationException before running uv. This guards against accidentally re-initializing or clobbering a working venv.","triggerScenarios":"Calling Setup() (directly or via MigrateAsync) on a UvVenvRunner whose RootPath already contains a created venv, without passing existsOk: true.","commonSituations":"Re-running a package install/one-click installer after a previous successful setup; app migration logic hitting a venv that already exists from an earlier version; retrying setup after a partial failure that still left python.exe in place.","solutions":["Pass existsOk: true to Setup() if re-creating over the existing venv is intended","Delete the existing venv directory at RootPath before calling Setup()","Check Exists() first and skip Setup() when the venv is already present and healthy"],"exampleFix":"// before\nawait venvRunner.Setup();\n// after\nif (!venvRunner.Exists())\n{\n    await venvRunner.Setup();\n}\n// or, to intentionally rebuild:\nawait venvRunner.Setup(existsOk: true);","handlingStrategy":"validation","validationCode":"if (venvRunner.Exists() && !allowRecreate)\n{\n    return; // venv already set up\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await venvRunner.Setup();\n}\ncatch (InvalidOperationException e) when (e.Message == \"Venv already exists\")\n{\n    logger.LogDebug(\"Venv already present, skipping setup\");\n}","preventionTips":["Always check Exists() before Setup()","Pass existsOk: true explicitly when rebuild-over-existing is intended","Track setup completion state so Setup is only called once per venv path"],"tags":["python","venv","setup","invalid-operation"],"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"}