Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: stop the Windows Terminal from creating nul files through Git Bash (#17010)

Daniel Hancdf27f4b1b7d
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 387bfd037d6..913f02c9c43 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -9470,6 +9470,111 @@ def _shell_is_posix() -> bool:
return sys.platform != "win32" or _windows_bash() is not None
+# cmd's `>nul` idiom; `<<` is matched first by the heredoc pattern, and `x->nul` / `a=>nul` are not redirects.
+_NUL_REDIRECT_RE = re.compile(
+ r"(?<![-=<>])((?:[0-9]+|&)?(?:>>?|>\||>&)|<)([ \t]*)nul(?=[\s;&|()<>]|$)", re.IGNORECASE
+)
+_HEREDOC_RE = re.compile(r"<<(-?)[ \t]*(['\"]?)([^\s'\"<>;&|()]+)\2")
+
+
+def _substitution_end(command: str, i: int) -> int:
+ depth, n = 1, len(command)
+ while i < n:
+ c = command[i]
+ if c == "\\":
+ i += 2
+ continue
+ if c in "'\"":
+ j = command.find(c, i + 1)
+ i = n if j < 0 else j + 1
+ continue
+ if c == "(":
+ depth += 1
+ elif c == ")":
+ depth -= 1
+ if depth == 0:
+ return i
+ i += 1
+ return n
+
+
+def _bash_nul_to_dev_null(command: str) -> str:
+ """Git Bash turns `2>nul` into a real file named nul that Explorer cannot delete (#16991).
+ Quoted text and heredoc bodies (a .bat written through cat) keep their spelling."""
+ if "nul" not in command.lower():
+ return command
+ out: list[str] = []
+ heredocs: list[tuple[bool, str]] = []
+ i, n = 0, len(command)
+ while i < n:
+ c = command[i]
+ if c == "\\":
+ out.append(command[i : i + 2])
+ i += 2
+ continue
+ if c == "'":
+ j = command.find("'", i + 1)
+ j = n if j < 0 else j + 1
+ out.append(command[i:j])
+ i = j
+ continue
+ if c == '"':
+ # A $( ) inside double quotes still runs: `x="$(cmd 2>nul)"`.
+ out.append(c)
+ i += 1
+ while i < n and command[i] != '"':
+ if command.startswith("$(", i):
+ end = _substitution_end(command, i + 2)
+ out.append("$(" + _bash_nul_to_dev_null(command[i + 2 : end]))
+ i = end
+ continue
+ step = 2 if command[i] == "\\" else 1
+ out.append(command[i : i + step])
+ i += step
+ out.append(command[i : i + 1])
+ i += 1
+ continue
+ if c == "#" and (i == 0 or command[i - 1] in " \t\n;&|()"):
+ j = command.find("\n", i)
+ j = n if j < 0 else j
+ out.append(command[i:j])
+ i = j
+ continue
+ if c == "\n" and heredocs:
+ i += 1
+ out.append("\n")
+ for strip_tabs, delim in heredocs:
+ while i < n:
+ j = command.find("\n", i)
+ j = n if j < 0 else j + 1
+ line = command[i:j].rstrip("\n")
+ out.append(command[i:j])
+ i = j
+ if (line.lstrip("\t") if strip_tabs else line) == delim:
+ break
+ heredocs = []
+ continue
+ if command.startswith("<<<", i):
+ out.append("<<<")
+ i += 3
+ continue
+ if command.startswith("<<", i):
+ m = _HEREDOC_RE.match(command, i)
+ if m:
+ heredocs.append((m.group(1) == "-", m.group(3)))
+ out.append(m.group(0))
+ i = m.end()
+ continue
+ m = _NUL_REDIRECT_RE.match(command, i)
+ if m:
+ out.append(f"{m.group(1)}{m.group(2)}/dev/null")
+ i = m.end()
+ continue
+ out.append(c)
+ i += 1
+ return "".join(out)
+
+
def _get_shell_cmd(command: str) -> list[str]:
"""Return the platform-appropriate shell invocation for a command string."""
if sys.platform == "win32":
@@ -22415,6 +22520,8 @@ def _bash_exec(
# Chosen once, so the blocklist, env and argv all agree on the shell that will run this call.
# Sandbox Low runs on the host shell: cmd is only picked to stay inside MXC.
profile = _terminal_profile(disable_sandbox or tool_execution_mode == "software")
+ if profile == "bash" and sys.platform == "win32":
+ command = _bash_nul_to_dev_null(command)
if profile == "cmd_isolated":
# Models often end a command with a newline; cmd /s /c cannot carry one.
command = command.strip()
diff --git a/studio/backend/tests/test_windows_bash_nul_redirect.py b/studio/backend/tests/test_windows_bash_nul_redirect.py
new file mode 100644
index 00000000000..fa1a717e1c1
--- /dev/null
+++ b/studio/backend/tests/test_windows_bash_nul_redirect.py
@@ -0,0 +1,125 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The Windows Terminal runs Git Bash, where cmd's `2>nul` makes a real file named nul (#16991)."""
+
+from __future__ import annotations
+
+import os
+import shutil
+import subprocess
+import sys
+
+import pytest
+
+from core.inference import tools
+
+_rewrite = tools._bash_nul_to_dev_null
+
+
+ "command, expected",
+ [
+ ("dir 2>nul", "dir 2>/dev/null"),
+ ("echo hi > nul", "echo hi > /dev/null"),
+ ("cmd >nul 2>&1", "cmd >/dev/null 2>&1"),
+ ("cmd > NUL 2>&1 || echo no", "cmd > /dev/null 2>&1 || echo no"),
+ ("cmd &>nul; ls", "cmd &>/dev/null; ls"),
+ ("cmd >>nul", "cmd >>/dev/null"),
+ ("cmd >|nul", "cmd >|/dev/null"),
+ ("cmd >&nul", "cmd >&/dev/null"),
+ ("read x <nul", "read x </dev/null"),
+ ("(where git 2>nul)", "(where git 2>/dev/null)"),
+ ('x="$(git rev-parse HEAD 2>nul)"', 'x="$(git rev-parse HEAD 2>/dev/null)"'),
+ ('echo "v: $(python -V 2>nul) ok"', 'echo "v: $(python -V 2>/dev/null) ok"'),
+ ('x="$(echo "$(ls 2>nul)")"', 'x="$(echo "$(ls 2>/dev/null)")"'),
+ (
+ "cd sub && python a.py 2>nul\nls 2>nul",
+ "cd sub && python a.py 2>/dev/null\nls 2>/dev/null",
+ ),
+ ],
+)
+def test_cmd_style_nul_redirects_go_to_dev_null(command, expected):
+ assert _rewrite(command) == expected
+
+
+ "command",
+ [
+ "echo hi",
+ "cat nul.txt",
+ "ls > null",
+ "ls > nul.txt",
+ "ls > nullable",
+ "ls 2>/dev/null",
+ "grep '2>nul' run.bat",
+ 'git commit -m "send output >nul"',
+ "echo \"$(echo '2>nul')\"",
+ "echo \\>nul",
+ "ls # 2>nul",
+ "node -e 'x=>nul'",
+ "cat <<< nul",
+ "cat <<EOF > run.bat\n@echo off\ndir 2>nul\nEOF",
+ "cat <<-'EOF' > run.bat\n\tdir 2>nul\n\tEOF",
+ ],
+)
+def test_other_text_is_untouched(command):
+ assert _rewrite(command) == command
+
+
+def test_redirects_after_a_heredoc_are_still_rewritten():
+ command = "cat <<EOF > run.bat\ndir 2>nul\nEOF\nrun.bat 2>nul"
+ assert _rewrite(command) == "cat <<EOF > run.bat\ndir 2>nul\nEOF\nrun.bat 2>/dev/null"
+
+
+# System32 bash.exe on Windows is the WSL launcher; use the Git Bash the Terminal runs.
+_BASH = tools._windows_bash() if sys.platform == "win32" else shutil.which("bash")
+
+
[email protected](_BASH is None, reason = "needs bash")
+def test_rewritten_command_leaves_no_file_and_keeps_its_output(tmp_path):
+ out = subprocess.run(
+ [_BASH, "-c", _rewrite("echo hidden > nul 2>&1; ls missing 2>nul; echo shown")],
+ cwd = tmp_path,
+ capture_output = True,
+ text = True,
+ timeout = 60,
+ )
+ assert out.stdout == "shown\n"
+ assert os.listdir(tmp_path) == []
+
+
+ "platform, profile, rewritten",
+ [("win32", "bash", True), ("win32", "cmd_fallback", False), ("linux", "bash", False)],
+)
+def test_only_the_windows_bash_terminal_rewrites(monkeypatch, platform, profile, rewritten):
+ seen = []
+ monkeypatch.setattr(tools, "_bash_nul_to_dev_null", lambda c: seen.append(c) or c)
+ monkeypatch.setattr(tools, "_terminal_profile", lambda *_a, **_k: profile)
+
+ def stop(*_a, **_k):
+ raise RuntimeError("stop")
+
+ monkeypatch.setattr(tools, "_get_workdir", stop)
+ monkeypatch.setattr(tools, "_references_studio_credential_here", lambda *_a, **_k: False)
+ monkeypatch.setattr(tools, "_harden_parent_against_proc_env_leak", lambda: True)
+ monkeypatch.setattr(sys, "platform", platform)
+ out = tools._bash_exec("echo hi > nul", None, 5, "t", disable_sandbox = True)
+ assert out == "Execution error: stop"
+ assert seen == (["echo hi > nul"] if rewritten else [])
+
+
[email protected](sys.platform != "win32", reason = "needs Git Bash on Windows")
+def test_terminal_on_git_bash_leaves_no_nul_file():
+ if tools._windows_bash() is None:
+ pytest.skip(
+ reason = "no trusted Git Bash: the Terminal runs cmd.exe, where > nul is the device"
+ )
+ session = "nul-redirect-16991"
+ out = tools._bash_exec(
+ "echo hidden > nul 2>&1; echo shown", None, 60, session, disable_sandbox = True
+ )
+ assert "shown" in out and "hidden" not in out
+ workdir = tools._get_workdir(session)
+ assert "nul" not in [name.lower() for name in os.listdir("\\\\?\\" + os.path.abspath(workdir))]