Skip to content

fix(cli/self-update): fix Nushell-related suggestions and scripts #4265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
//! and racy on Windows.

use std::borrow::Cow;
use std::env::consts::EXE_SUFFIX;
use std::env::{self, consts::EXE_SUFFIX};
use std::fmt;
use std::fs;
use std::io::Write;
use std::path::{Component, MAIN_SEPARATOR, Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::{env, fmt};

use anyhow::{Context, Result, anyhow};
use cfg_if::cfg_if;
Expand Down Expand Up @@ -77,6 +77,9 @@ use unix::{delete_rustup_and_cargo_home, do_add_to_path, do_remove_from_path};
#[cfg(unix)]
pub(crate) use unix::{run_update, self_replace};

#[cfg(unix)]
use self::shell::{Nu, UnixShell};

#[cfg(windows)]
mod windows;
#[cfg(windows)]
Expand Down Expand Up @@ -384,7 +387,7 @@ the corresponding `env` file under {cargo_home}.
This is usually done by running one of the following (note the leading DOT):
. "{cargo_home}/env" # For sh/bash/zsh/ash/dash/pdksh
source "{cargo_home}/env.fish" # For fish
source "{cargo_home}/env.nu" # For nushell
source $"{cargo_home_nushell}/env.nu" # For nushell
"#
};
}
Expand Down Expand Up @@ -578,13 +581,20 @@ pub(crate) async fn install(
format!(post_install_msg_win!(), cargo_home = cargo_home)
};
#[cfg(not(windows))]
let cargo_home_nushell = Nu.cargo_home_str(process)?;
#[cfg(not(windows))]
let msg = if no_modify_path {
format!(
post_install_msg_unix_no_modify_path!(),
cargo_home = cargo_home
cargo_home = cargo_home,
cargo_home_nushell = cargo_home_nushell,
)
} else {
format!(post_install_msg_unix!(), cargo_home = cargo_home)
format!(
post_install_msg_unix!(),
cargo_home = cargo_home,
cargo_home_nushell = cargo_home_nushell,
)
};
md(&mut term, msg);

Expand Down
5 changes: 2 additions & 3 deletions src/cli/self_update/env.nu
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
if ("{cargo_bin}" not-in ($env.Path | split row (char esep))) {
$env.Path = ($env.Path | prepend "{cargo_bin}")
}
use std/util "path add"
path add $"{cargo_bin}"
75 changes: 45 additions & 30 deletions src/cli/self_update/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,16 @@ pub(crate) struct ShellScript {
name: &'static str,
}

impl ShellScript {
pub(crate) fn write(&self, process: &Process) -> Result<()> {
let home = process.cargo_home()?;
let cargo_bin = format!("{}/bin", cargo_home_str(process)?);
let env_name = home.join(self.name);
let env_file = self.content.replace("{cargo_bin}", &cargo_bin);
utils::write_file(self.name, &env_name, &env_file)?;
Ok(())
}
}

// TODO: Update into a bytestring.
pub(crate) fn cargo_home_str(process: &Process) -> Result<Cow<'static, str>> {
fn cargo_home_str_with_home(home: &str, process: &Process) -> Result<Cow<'static, str>> {
let path = process.cargo_home()?;

let default_cargo_home = process
.home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".cargo");
Ok(if default_cargo_home == path {
"$HOME/.cargo".into()
Cow::Owned(format!("{home}/.cargo"))
} else {
match path.to_str() {
Some(p) => p.to_owned().into(),
Expand Down Expand Up @@ -107,12 +96,30 @@ pub(crate) trait UnixShell {
}
}

fn cargo_home_str(&self, process: &Process) -> Result<Cow<'static, str>> {
#[cfg(windows)]
let home = "%USERPROFILE%";
#[cfg(not(windows))]
let home = "$HOME";
cargo_home_str_with_home(home, process)
}

fn source_string(&self, process: &Process) -> Result<String> {
Ok(format!(r#". "{}/env""#, cargo_home_str(process)?))
Ok(format!(r#". "{}/env""#, self.cargo_home_str(process)?))
}

fn write_script(&self, script: &ShellScript, process: &Process) -> Result<()> {
let home = process.cargo_home()?;
let cargo_bin = format!("{}/bin", self.cargo_home_str(process)?);
let env_name = home.join(script.name);
let env_file = script.content.replace("{cargo_bin}", &cargo_bin);
utils::write_file(script.name, &env_name, &env_file)?;
Ok(())
}
}

struct Posix;
pub(super) struct Posix;

impl UnixShell for Posix {
fn does_exist(&self, _: &Process) -> bool {
true
Expand Down Expand Up @@ -252,11 +259,14 @@ impl UnixShell for Fish {
}

fn source_string(&self, process: &Process) -> Result<String> {
Ok(format!(r#"source "{}/env.fish""#, cargo_home_str(process)?))
Ok(format!(
r#"source "{}/env.fish""#,
self.cargo_home_str(process)?
))
}
}

struct Nu;
pub(super) struct Nu;

impl UnixShell for Nu {
fn does_exist(&self, process: &Process) -> bool {
Expand All @@ -269,26 +279,24 @@ impl UnixShell for Nu {
let mut paths = vec![];

if let Ok(p) = process.var("XDG_CONFIG_HOME") {
let path = PathBuf::from(p).join("nushell/");
paths.push(path.join("env.nu"));
paths.push(path.join("config.nu"));
let mut p = PathBuf::from(p);
p.extend(["nushell", "config.nu"]);
paths.push(p)
}

if let Some(p) = process.home_dir() {
let path = p.join(".config/nushell/");
paths.push(path.join("env.nu"));
paths.push(path.join("config.nu"));
if let Some(mut p) = process.home_dir() {
p.extend([".config", "nushell", "config.nu"]);
paths.push(p)
}
paths
}

fn update_rcs(&self, process: &Process) -> Vec<PathBuf> {
let mut rcs = self.rcfiles(process);
if rcs.len() == 4 {
// The first two rcfile takes precedence (XDG_CONFIG_HOME).
rcs.truncate(2);
// The first rcfile in XDG_CONFIG_HOME takes precedence.
match self.rcfiles(process).into_iter().next() {
Some(path) => vec![path],
None => vec![],
}
rcs
}

fn env_script(&self) -> ShellScript {
Expand All @@ -299,7 +307,14 @@ impl UnixShell for Nu {
}

fn source_string(&self, process: &Process) -> Result<String> {
Ok(format!(r#"source "{}/env.nu""#, cargo_home_str(process)?))
Ok(format!(
r#"source $"{}/env.nu""#,
self.cargo_home_str(process)?
))
}

fn cargo_home_str(&self, process: &Process) -> Result<Cow<'static, str>> {
cargo_home_str_with_home("($nu.home-path)", process)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/cli/self_update/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{Context, Result, bail};
use tracing::{error, warn};

use super::install_bins;
use super::shell;
use super::shell::{self, Posix, UnixShell};
use crate::process::Process;
use crate::utils::{self, Notification};

Expand Down Expand Up @@ -115,7 +115,7 @@ pub(crate) fn do_write_env_files(process: &Process) -> Result<()> {
let script = sh.env_script();
// Only write each possible script once.
if !written.contains(&script) {
script.write(process)?;
sh.write_script(&script, process)?;
written.push(script);
}
}
Expand Down Expand Up @@ -174,15 +174,15 @@ fn remove_legacy_paths(process: &Process) -> Result<()> {
remove_legacy_source_command(
format!(
"export PATH=\"{}/bin:$PATH\"\n",
shell::cargo_home_str(process)?
Posix.cargo_home_str(process)?
),
process,
)?;
// Unfortunately in 1.23, we accidentally used `source` rather than `.`
// which, while widely supported, isn't actually POSIX, so we also
// clean that up here. This issue was filed as #2623.
remove_legacy_source_command(
format!("source \"{}/env\"\n", shell::cargo_home_str(process)?),
format!("source \"{}/env\"\n", Posix.cargo_home_str(process)?),
process,
)?;

Expand Down