Repository: reflex-dev/reflex

Issue:
Reflex installs Bun even though BUN_PATH is set 
**Describe the bug**
Reflex installs Bun even though `BUN_PATH` is set.

**To Reproduce**
1. Install Bun manually. However, the version of Bun is different from `constants.Bun.VERSION`.
2. Set an environment variable `BUN_PATH` as the installed bun path.
3. Run `reflex init`.

**Expected behavior**
The installation of Bun would be skipped.

**Additional context**
The condition of Bun installation seems incorrect.

Current implementation:
https://github.com/reflex-dev/reflex/blob/b01c4b6c6a0d5642079b73be6a5c27193730e425/reflex/utils/prerequisites.py#L795-L800

I suppose this should be changed as below:
```python
# Skip if bun is already installed.
bun_path = get_config().bun_path
if bun_path != constants.Bun.DEFAULT_PATH or os.path.exists(bun_path) and get_bun_version() == version.parse(
    constants.Bun.VERSION
):
    console.debug("Skipping bun installation as it is already installed.")
    return
```


Discussion:
PR #4006 is introducing `REFLEX_USE_SYSTEM_BUN` as an env var.
Let us know if this solve the issue for you.
@Lendemor 
Thank you for your response.

There seems to be a new bug in `prerequisites.get_bun_version`.

The version of Bun is resolved at following code, but `REFLEX_USE_SYSTEM_BUN` is not considered.
https://github.com/reflex-dev/reflex/blob/3eab2b6e7d8dab972b9e5714a845777d664e7ecb/reflex/utils/prerequisites.py#L1044
https://github.com/reflex-dev/reflex/blob/3eab2b6e7d8dab972b9e5714a845777d664e7ecb/reflex/utils/prerequisites.py#L196

Compared to `prerequisites.get_node_version`, it seems that `path_ops.get_bun_path()` should be used instead of `get_config().bun_path`.
https://github.com/reflex-dev/reflex/blob/3eab2b6e7d8dab972b9e5714a845777d664e7ecb/reflex/utils/prerequisites.py#L159
Sorry for the delay in getting back to you on this.

Made a PR that should fix the issue with bun_path, let me know if you can give it a try.



Patch:
diff --git a/reflex/utils/exec.py b/reflex/utils/exec.py
index 9a5b7011262..a8b86d30a02 100644
--- a/reflex/utils/exec.py
+++ b/reflex/utils/exec.py
@@ -467,19 +467,19 @@ def output_system_info():
 
     system = platform.system()
 
+    fnm_info = f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]"
+
     if system != "Windows" or (
         system == "Windows" and prerequisites.is_windows_bun_supported()
     ):
         dependencies.extend(
             [
-                f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]",
-                f"[Bun {prerequisites.get_bun_version()} (Expected: {constants.Bun.VERSION}) (PATH: {config.bun_path})]",
+                fnm_info,
+                f"[Bun {prerequisites.get_bun_version()} (Expected: {constants.Bun.VERSION}) (PATH: {path_ops.get_bun_path()})]",
             ],
         )
     else:
-        dependencies.append(
-            f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]",
-        )
+        dependencies.append(fnm_info)
 
     if system == "Linux":
         import distro  # type: ignore
diff --git a/reflex/utils/path_ops.py b/reflex/utils/path_ops.py
index b447718d2ab..b6bb9aade8e 100644
--- a/reflex/utils/path_ops.py
+++ b/reflex/utils/path_ops.py
@@ -9,7 +9,7 @@
 from pathlib import Path
 
 from reflex import constants
-from reflex.config import environment
+from reflex.config import environment, get_config
 
 # Shorthand for join.
 join = os.linesep.join
@@ -118,7 +118,7 @@ def ln(src: str | Path, dest: str | Path, overwrite: bool = False) -> bool:
     return True
 
 
-def which(program: str | Path) -> str | Path | None:
+def which(program: str | Path) -> Path | None:
     """Find the path to an executable.
 
     Args:
@@ -127,7 +127,8 @@ def which(program: str | Path) -> str | Path | None:
     Returns:
         The path to the executable.
     """
-    return shutil.which(str(program))
+    which_result = shutil.which(program)
+    return Path(which_result) if which_result else None
 
 
 def use_system_node() -> bool:
@@ -156,12 +157,12 @@ def get_node_bin_path() -> Path | None:
     """
     bin_path = Path(constants.Node.BIN_PATH)
     if not bin_path.exists():
-        str_path = which("node")
-        return Path(str_path).parent.resolve() if str_path else None
-    return bin_path.resolve()
+        path = which("node")
+        return path.parent.absolute() if path else None
+    return bin_path.absolute()
 
 
-def get_node_path() -> str | None:
+def get_node_path() -> Path | None:
     """Get the node binary path.
 
     Returns:
@@ -169,9 +170,8 @@ def get_node_path() -> str | None:
     """
     node_path = Path(constants.Node.PATH)
     if use_system_node() or not node_path.exists():
-        system_node_path = which("node")
-        return str(system_node_path) if system_node_path else None
-    return str(node_path)
+        node_path = which("node")
+    return node_path
 
 
 def get_npm_path() -> Path | None:
@@ -182,11 +182,22 @@ def get_npm_path() -> Path | None:
     """
     npm_path = Path(constants.Node.NPM_PATH)
     if use_system_node() or not npm_path.exists():
-        system_npm_path = which("npm")
-        npm_path = Path(system_npm_path) if system_npm_path else None
+        npm_path = which("npm")
     return npm_path.absolute() if npm_path else None
 
 
+def get_bun_path() -> Path | None:
+    """Get bun binary path.
+
+    Returns:
+        The path to the bun binary file.
+    """
+    bun_path = get_config().bun_path
+    if use_system_bun() or not bun_path.exists():
+        bun_path = which("bun")
+    return bun_path.absolute() if bun_path else None
+
+
 def update_json_file(file_path: str | Path, update_dict: dict[str, int | str]):
     """Update the contents of a json file.
 
diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py
index 94d8f8fbd1a..7927eb4ea80 100644
--- a/reflex/utils/prerequisites.py
+++ b/reflex/utils/prerequisites.py
@@ -194,10 +194,13 @@ def get_bun_version() -> version.Version | None:
     Returns:
         The version of bun.
     """
+    bun_path = path_ops.get_bun_path()
+    if bun_path is None:
+        return None
     try:
         # Run the bun -v command and capture the output
-        result = processes.new_process([str(get_config().bun_path), "-v"], run=True)
-        return version.parse(result.stdout)  # type: ignore
+        result = processes.new_process([str(bun_path), "-v"], run=True)
+        return version.parse(str(result.stdout))
     except FileNotFoundError:
         return None
     except version.InvalidVersion as e:
@@ -1051,9 +1054,7 @@ def install_bun():
             )
 
     # Skip if bun is already installed.
-    if Path(get_config().bun_path).exists() and get_bun_version() == version.parse(
-        constants.Bun.VERSION
-    ):
+    if get_bun_version() == version.parse(constants.Bun.VERSION):
         console.debug("Skipping bun installation as it is already installed.")
         return
 
@@ -1074,8 +1075,7 @@ def install_bun():
             show_logs=console.is_debug(),
         )
     else:
-        unzip_path = path_ops.which("unzip")
-        if unzip_path is None:
+        if path_ops.which("unzip") is None:
             raise SystemPackageMissingError("unzip")
 
         # Run the bun install script.
@@ -1279,12 +1279,9 @@ def validate_bun():
     Raises:
         Exit: If custom specified bun does not exist or does not meet requirements.
     """
-    # if a custom bun path is provided, make sure its valid
-    # This is specific to non-FHS OS
-    bun_path = get_config().bun_path
-    if path_ops.use_system_bun():
-        bun_path = path_ops.which("bun")
-    if bun_path != constants.Bun.DEFAULT_PATH:
+    bun_path = path_ops.get_bun_path()
+
+    if bun_path and bun_path.samefile(constants.Bun.DEFAULT_PATH):
         console.info(f"Using custom Bun path: {bun_path}")
         bun_version = get_bun_version()
         if not bun_version:


Test patch:
diff --git a/tests/units/utils/test_utils.py b/tests/units/utils/test_utils.py
index f8573111ce7..65e77f4a77d 100644
--- a/tests/units/utils/test_utils.py
+++ b/tests/units/utils/test_utils.py
@@ -122,13 +122,13 @@ def test_validate_invalid_bun_path(mocker):
     Args:
         mocker: Pytest mocker object.
     """
-    mock = mocker.Mock()
-    mocker.patch.object(mock, "bun_path", return_value="/mock/path")
-    mocker.patch("reflex.utils.prerequisites.get_config", mock)
+    mock_path = mocker.Mock()
+    mocker.patch("reflex.utils.path_ops.get_bun_path", return_value=mock_path)
     mocker.patch("reflex.utils.prerequisites.get_bun_version", return_value=None)
 
     with pytest.raises(typer.Exit):
         prerequisites.validate_bun()
+    mock_path.samefile.assert_called_once()
 
 
 def test_validate_bun_path_incompatible_version(mocker):
@@ -137,9 +137,8 @@ def test_validate_bun_path_incompatible_version(mocker):
     Args:
         mocker: Pytest mocker object.
     """
-    mock = mocker.Mock()
-    mocker.patch.object(mock, "bun_path", return_value="/mock/path")
-    mocker.patch("reflex.utils.prerequisites.get_config", mock)
+    mock_path = mocker.Mock()
+    mocker.patch("reflex.utils.path_ops.get_bun_path", return_value=mock_path)
     mocker.patch(
         "reflex.utils.prerequisites.get_bun_version",
         return_value=version.parse("0.6.5"),