Refactor sudo handling for package installation#83
Refactor sudo handling for package installation#83tannevaled wants to merge 3 commits intopkgxdev:mainfrom
Conversation
proposal to fix sudo handling
There was a problem hiding this comment.
Pull request overview
This PR proposes to refactor how pkgm decides when to invoke pkgx directly vs wrapping it with sudo, specifically around installs targeting /usr/local.
Changes:
- Introduces explicit
isRootandSUDO_USERdetection inquery_pkgx. - Emits a warning when installing to
/usr/localas root without being invoked viasudo. - Changes the conditions under which
/usr/bin/sudois used and how thesudoarguments are constructed.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const needs_sudo_backwards = | ||
| install_prefix().string == "/usr/local" && !isRoot && !sudoUser; | ||
|
|
||
| let cmd = pkgx; | ||
|
|
||
| if (needs_sudo_backwards) { | ||
| cmd = "/usr/bin/sudo"; | ||
| const user = Deno.env.get("USER")!; | ||
| args.unshift("-u", user, pkgx); |
There was a problem hiding this comment.
this one seems like the problem; we need drop privileges so we don't mess up a user's permissions, i believe.
|
|
||
| if (needs_sudo_backwards) { | ||
| cmd = "/usr/bin/sudo"; | ||
| const user = Deno.env.get("USER")!; | ||
| args.unshift("-u", user, pkgx); | ||
| } | ||
|
|
||
| const proc = new Deno.Command(cmd, { | ||
| args: [...args, "--json=v1"], |
There was a problem hiding this comment.
this seems like a lateral fix that was either always needed, or isn't now needed.
|
|
||
| if (needs_sudo_backwards) { | ||
| cmd = "/usr/bin/sudo"; | ||
| const user = Deno.env.get("USER")!; |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const needs_sudo_backwards = | ||
| prefix == "/usr/local" && !isRoot && !sudoUser; | ||
|
|
||
| let cmd = pkgx; | ||
|
|
||
| if (needs_sudo_backwards) { | ||
| cmd = "/usr/bin/sudo"; | ||
| const user = Deno.env.get("USER")!; | ||
| args.unshift("-u", user, pkgx); | ||
| } | ||
|
|
| const user = Deno.env.get("USER")!; | ||
| args.unshift("-u", user, pkgx); |
|
|
||
| if (needs_sudo_backwards) { | ||
| cmd = "/usr/bin/sudo"; | ||
| const user = Deno.env.get("USER")!; | ||
| args.unshift("-u", user, pkgx); | ||
| } | ||
|
|
||
| const proc = new Deno.Command(cmd, { | ||
| args: [...args, "--json=v1"], |
|
|
||
| if (needs_sudo_backwards) { | ||
| cmd = "/usr/bin/sudo"; | ||
| const user = Deno.env.get("USER")!; |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const needs_sudo_backwards = | ||
| prefix == "/usr/local" && !isRoot && !sudoUser; | ||
|
|
||
| let cmd = pkgx; | ||
|
|
||
| if (needs_sudo_backwards) { | ||
| if (!Deno.env.get("SUDO_USER")) { | ||
| if (Deno.uid() == 0) { | ||
| console.error( | ||
| "%cwarning", | ||
| "color:yellow", | ||
| "installing as root; installing via `sudo` is preferred", | ||
| ); | ||
| } | ||
| cmd = pkgx; | ||
| } else { | ||
| args.unshift("-u", Deno.env.get("SUDO_USER")!, pkgx); | ||
| } | ||
| cmd = "/usr/bin/sudo"; | ||
| const user = Deno.env.get("USER")!; | ||
| args.unshift("-u", user, pkgx); |
| cmd = "/usr/bin/sudo"; | ||
| const user = Deno.env.get("USER")!; | ||
| args.unshift("-u", user, pkgx); | ||
| } |
proposal to fix sudo handling