Skip to content

Commit 57a0fff

Browse files
committed
Only update shell startup files in user mode on macOS
Like on Linux. Cf. krlmlr/scriptlets#27.
1 parent 53de302 commit 57a0fff

4 files changed

Lines changed: 50 additions & 10 deletions

File tree

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
* rig now warns instead of failing when it encounters a broken R
4242
installation (#346).
4343

44+
* rig now does not edit the shell startup files in admin mode,
45+
like rig 0.8.1 did on macOS. rig now only does this in user mode.
46+
4447
# rig 0.8.1
4548

4649
* The `RIG_PLATFORM` environment variable works correctly again (#325).

src/alias.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ pub fn get_alias(args: &ArgMatches) -> Option<String> {
7474

7575
#[cfg(target_os = "macos")]
7676
pub fn add_alias(ver: &str, alias: &str) -> Result<(), Box<dyn Error>> {
77+
let mode = crate::utils::get_mode()?;
7778
let msg = "Adding R-".to_string() + alias + " alias";
78-
if crate::utils::get_mode()? == crate::utils::Mode::Admin {
79+
if mode == crate::utils::Mode::Admin {
7980
escalate(&msg)?;
81+
} else {
82+
let binary_dir = get_binary_dir()?;
83+
std::fs::create_dir_all(&binary_dir)?;
8084
}
8185

8286
check_local_bin_path()?;

src/macos.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,12 @@ pub fn sc_rm(args: &ArgMatches) -> Result<(), Box<dyn Error>> {
740740
pub fn sc_system_make_links() -> Result<(), Box<dyn Error>> {
741741
let binary_dir = get_binary_dir()?;
742742
let mode = get_mode()?;
743-
if mode == crate::utils::Mode::Admin && access(binary_dir.as_str(), AccessFlags::W_OK).is_err()
744-
{
745-
escalate("making R-* quick links")?;
743+
if mode == crate::utils::Mode::Admin {
744+
if access(binary_dir.as_str(), AccessFlags::W_OK).is_err() {
745+
escalate("making R-* quick links")?;
746+
}
747+
} else {
748+
std::fs::create_dir_all(&binary_dir)?;
746749
}
747750
check_local_bin_path()?;
748751
let vers = sc_get_list()?;
@@ -1864,8 +1867,11 @@ pub fn sc_set_default(ver: &str) -> Result<(), Box<dyn Error>> {
18641867
}
18651868
};
18661869

1867-
check_local_bin_path()?;
18681870
let binary_dir = get_binary_dir()?;
1871+
if get_mode()? == crate::utils::Mode::User {
1872+
std::fs::create_dir_all(&binary_dir)?;
1873+
}
1874+
check_local_bin_path()?;
18691875

18701876
let r = Path::new(&binary_dir).join("R");
18711877
if !r.exists() {

src/utils.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,18 +590,25 @@ pub fn check_local_bin_path() -> Result<(), Box<dyn Error>> {
590590
usr_local_bin_idx
591591
);
592592

593-
let needs_add = match (local_bin_idx, usr_local_bin_idx) {
594-
(None, _) => true,
595-
(Some(l), Some(u)) if l > u => true,
596-
_ => false,
597-
};
593+
// Only user mode puts rig's quick links into ~/.local/bin, so only user
594+
// mode has a reason to edit the user's shell startup files. In admin mode
595+
// we merely warn below if the binary directory is not on the PATH.
596+
let mode = get_mode()?;
597+
let needs_add = mode == Mode::User
598+
&& match (local_bin_idx, usr_local_bin_idx) {
599+
(None, _) => true,
600+
(Some(l), Some(u)) if l > u => true,
601+
_ => false,
602+
};
598603

599604
if needs_add && !ADD_DONE.swap(true, Ordering::Relaxed) {
600605
debug!(
601606
"Updating shell profiles to put {} on PATH",
602607
local_bin.display()
603608
);
604609
add_local_bin_to_path()?;
610+
} else if mode == Mode::Admin {
611+
debug!("Admin mode, not touching shell startup files");
605612
} else if !needs_add {
606613
debug!(
607614
"{} is already correctly placed on PATH",
@@ -814,6 +821,26 @@ mod tests {
814821
});
815822
}
816823

824+
#[test]
825+
fn test_check_local_bin_path_admin_mode_does_not_touch_dotfiles() {
826+
with_temp_home(|home| {
827+
let zshrc = home.join(".zshrc");
828+
fs::write(&zshrc, "# existing\n").unwrap();
829+
unsafe {
830+
std::env::set_var("PATH", "/usr/local/bin:/usr/bin:/bin");
831+
}
832+
set_mode(Mode::Admin).unwrap();
833+
834+
check_local_bin_path().unwrap();
835+
836+
// ~/.local/bin is not on the PATH here, but in admin mode rig has
837+
// no business editing the user's shell startup files.
838+
assert_eq!(fs::read_to_string(&zshrc).unwrap(), "# existing\n");
839+
assert!(!home.join(".local/bin/rigenv").exists());
840+
assert!(!home.join(".profile").exists());
841+
});
842+
}
843+
817844
#[test]
818845
fn os_converts_to_osstring() {
819846
assert_eq!(os("hello"), OsString::from("hello"));

0 commit comments

Comments
 (0)