Skip to content

The agent that grows with you

fix(install): never prompt for sudo under --non-interactive

kshitijk4poor847acaf1fbd2
diff --git a/scripts/install.sh b/scripts/install.sh
index e0ec5b7fc92..13497c91fb0 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -444,7 +444,7 @@ stage_prerequisites() {
# glibc Node links libatomic.so.1, absent on minimal Debian/RHEL hosts. PM
# installs the distro package with `sudo -n` under its install lock, so
# cache sudo credentials now, while the terminal can answer the prompt.
- if [[ "$_target" == linux-* && "$_target" != *-musl ]] \
+ if [ "$NON_INTERACTIVE" != true ] && [[ "$_target" == linux-* && "$_target" != *-musl ]] \
&& [ "$(id -u)" -ne 0 ] && command -v sudo >/dev/null 2>&1 && has_terminal \
&& ! { ldconfig -p 2>/dev/null || /sbin/ldconfig -p 2>/dev/null; } | grep -q 'libatomic\.so\.1'; then
log "Node.js needs libatomic.so.1; sudo may ask for your password to install it"
diff --git a/tests/pm/test_node_libatomic_install_boundary.py b/tests/pm/test_node_libatomic_install_boundary.py
index 60b97de7a05..181363b3952 100644
--- a/tests/pm/test_node_libatomic_install_boundary.py
+++ b/tests/pm/test_node_libatomic_install_boundary.py
@@ -63,3 +63,34 @@ def test_auto_repair_never_reads_a_tty(monkeypatch):
assert attempted is False
assert captured["argv"][:2] == ["/usr/bin/sudo", "-n"]
assert captured["stdin"] is libatomic.subprocess.DEVNULL
+
+
[email protected]("flags", ["", "NON_INTERACTIVE=true"])
+def test_installer_sudo_warmup_honours_non_interactive(tmp_path, flags):
+ """The prerequisites warm-up may prompt for sudo, but never under --non-interactive."""
+ import os
+ import pty
+
+ script = Path(__file__).resolve().parents[2] / "scripts" / "install.sh"
+ bin_dir = tmp_path / "bin"
+ log = tmp_path / "sudo.log"
+ _node_script(bin_dir / "ldconfig", "#!/bin/sh\nexit 0\n")
+ _node_script(bin_dir / "id", "#!/bin/sh\necho 1000\n")
+ _node_script(bin_dir / "sudo", f'#!/bin/sh\necho "$*" >> "{log}"\nexit 0\n')
+ probe = f'source "$1" --manifest\n{flags}\nuv_bootstrap_target() {{ echo linux-x64; }}\nstage_prerequisites\n'
+ env = dict(os.environ, PATH=f"{bin_dir}{os.pathsep}{os.environ['PATH']}", HOME=str(tmp_path))
+ # A real controlling terminal: has_terminal() and `sudo -v </dev/tty` open it.
+ pid, fd = pty.fork()
+ if pid == 0:
+ os.execvpe("bash", ["bash", "-c", probe, "probe", str(script)], env)
+ while True:
+ try:
+ if not os.read(fd, 4096):
+ break
+ except OSError:
+ break
+ os.waitpid(pid, 0)
+
+ prompted = log.is_file() and "-v" in log.read_text().split()
+ assert prompted is (flags == "")