{"record":{"id":"78be122d82997286","repo":"oraios/serena","slug":"failed-to-detect-ocaml-version-e-stderr-please","errorCode":null,"errorMessage":"Failed to detect OCaml version: {e.stderr}\nPlease ensure OCaml is installed and opam is configured:\n  opam switch show\n  opam exec -- ocaml -version","messagePattern":"Failed to detect OCaml version: (.+?)\nPlease ensure OCaml is installed and opam is configured:\n  opam switch show\n  opam exec -- ocaml -version","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/solidlsp/language_servers/ocaml_lsp_server.py","lineNumber":94,"sourceCode":"                patch = int(version_match.group(3))\n                version_tuple = (major, minor, patch)\n                version_str = f\"{major}.{minor}.{patch}\"\n                log.info(f\"OCaml version: {version_str}\")\n\n                if version_tuple == (5, 1, 0):\n                    raise RuntimeError(\n                        f\"OCaml {version_str} is incompatible with ocaml-lsp-server.\\n\"\n                        \"Please use OCaml < 5.1 or >= 5.1.1.\\n\"\n                        \"Consider creating a new opam switch:\\n\"\n                        \"  opam switch create <name> ocaml-base-compiler.4.14.2\"\n                    )\n                return version_tuple\n            raise RuntimeError(\n                f\"Could not parse OCaml version from output: {result.stdout.strip()}\\n\"\n                \"Please ensure OCaml is properly installed: opam exec -- ocaml -version\"\n            )\n        except subprocess.CalledProcessError as e:\n            raise RuntimeError(\n                f\"Failed to detect OCaml version: {e.stderr}\\n\"\n                \"Please ensure OCaml is installed and opam is configured:\\n\"\n                \"  opam switch show\\n\"\n                \"  opam exec -- ocaml -version\"\n            ) from e\n        except FileNotFoundError as e:\n            raise RuntimeError(\n                \"OCaml not found. Please install OCaml via opam:\\n\"\n                \"  opam switch create <name> ocaml-base-compiler.4.14.2\\n\"\n                \"  eval $(opam env)\"\n            ) from e\n\n    @staticmethod\n    def _detect_lsp_version(repository_root_path: str) -> tuple[int, int, int]:\n        \"\"\"\n        Detect and return the ocaml-lsp-server version as a tuple (major, minor, patch).\n        Raises RuntimeError if version cannot be determined.\n        \"\"\"","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/solidlsp/language_servers/ocaml_lsp_server.py#L76-L112","documentation":"This RuntimeError is raised when the subprocess call used to detect the OCaml version exits with a non-zero status (subprocess.CalledProcessError). _detect_ocaml_version converts it into an actionable message including the subprocess stderr. It indicates opam is present but the version probe ('opam exec -- ocaml -version') failed.","triggerScenarios":"Constructing the OCaml language server when 'opam exec -- ocaml -version' returns a non-zero exit code, e.g. no opam switch initialized/selected, broken switch, or ocaml not installed inside the current switch.","commonSituations":"opam installed but never 'opam init'-ed; empty opam root with no switches; switch exists but the compiler package was removed or the switch is corrupted; OPAMROOT pointing to a stale/incomplete directory in CI.","solutions":["Run 'opam init' (or 'opam switch create <name> ocaml-base-compiler.4.14.2') if no switch exists.","Activate a valid switch with 'opam switch <name>' then 'eval $(opam env)'.","Check stderr in the error message for the concrete opam failure and fix that underlying issue.","Verify the probe works: 'opam exec -- ocaml -version' should succeed before starting the server."],"exampleFix":"// before (CI)\n- opam install ocaml-lsp-server   # fails: no switch\n// after\n- opam init -y\n- opam switch create default ocaml-base-compiler.5.1.1\n- opam install -y ocaml-lsp-server\n- eval $(opam env)","handlingStrategy":"validation","validationCode":"import subprocess\n\ndef check_opam_switch_ready() -> None:\n    subprocess.run([\"opam\", \"switch\", \"show\"], check=True, capture_output=True)\n    subprocess.run(\n        [\"opam\", \"exec\", \"--\", \"ocaml\", \"-version\"],\n        check=True, capture_output=True,\n    )  # non-zero exit here means an uninitialized/broken switch","typeGuard":null,"tryCatchPattern":"try:\n    server = OcamlLanguageServer(**kwargs)\nexcept RuntimeError as e:\n    if \"Failed to detect OCaml version\" in str(e):\n        logger.error(\"opam probe failed: %s. Run 'opam init' / 'opam switch create' and 'eval $(opam env)', then retry.\", e)\n        raise\n    raise","preventionTips":["Always run 'opam init' after installing opam, including in CI and Dockerfiles.","Set OPAMROOT consistently in CI so the same opam root is used across steps.","Source 'eval $(opam env)' in the process environment, not only interactive shells.","Include stderr from failed probes in your own error reports to speed diagnosis."],"tags":["ocaml","opam","subprocess","environment"],"backgroundTag":"subprocess-failed","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}