Skip to content

Commit 139248a

Browse files
committed
wip
1 parent 15e8947 commit 139248a

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

  • crates/vite_global_cli/src/commands/env

crates/vite_global_cli/src/commands/env/setup.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ complete -c vpr --keep-order --exclusive --arguments "(__vpr_complete)"
624624
// Completions delegate to Fish dynamically (VP_COMPLETE=fish) because clap_complete_nushell
625625
// generates multiple rest params (e.g. for `vp install`), which Nushell does not support.
626626
const ENV_TEMPLATE_NU: &str = r#"# Vite+ environment setup (https://viteplus.dev)
627-
$env.VP_HOME = "__VP_HOME__"
627+
$env.VP_HOME = ("__VP_HOME__" | path expand --no-symlink)
628628
$env.PATH = ($env.PATH | where { $in != "__VP_BIN__" } | prepend "__VP_BIN__")
629629
630630
# Shell function wrapper: intercepts `vp env use` to parse its stdout,
@@ -764,6 +764,14 @@ fn render_home_relative_path(path: &std::path::Path, home_dir: Option<&std::path
764764
.unwrap_or_else(|| path.display().to_string().replace('\\', "/"))
765765
}
766766

767+
fn render_nu_path_ref(path_ref: &str) -> String {
768+
match path_ref.strip_prefix("$HOME") {
769+
Some("") => "~".to_string(),
770+
Some(suffix) if suffix.starts_with('/') => format!("~{suffix}"),
771+
_ => path_ref.to_string(),
772+
}
773+
}
774+
767775
/// Render the env-file content for `shell` against `vite_plus_home`.
768776
fn render_env_content(shell: EnvShell, vite_plus_home: &vite_path::AbsolutePath) -> String {
769777
let bin_path = vite_plus_home.join("bin");
@@ -782,11 +790,8 @@ fn render_env_content(shell: EnvShell, vite_plus_home: &vite_path::AbsolutePath)
782790
EnvShell::Nu => {
783791
// Nushell requires `~` instead of `$HOME` in string literals — `$HOME` is not
784792
// expanded at parse time, so PATH entries would contain a literal "$HOME/...".
785-
let bin_path_ref_nu = bin_path_ref.replace("$HOME/", "~/");
786-
// `~` is only expanded in Nushell path-literal positions, not general
787-
// strings, so a `~/...` VP_HOME would leak literally into the env var;
788-
// bake the absolute path instead (`None` skips $HOME-relativization).
789-
let home_path_ref_nu = render_home_relative_path(vite_plus_home.as_path(), None);
793+
let home_path_ref_nu = render_nu_path_ref(&home_path_ref);
794+
let bin_path_ref_nu = render_nu_path_ref(&bin_path_ref);
790795
ENV_TEMPLATE_NU
791796
.replace("__VP_HOME__", &home_path_ref_nu)
792797
.replace("__VP_BIN__", &bin_path_ref_nu)
@@ -958,8 +963,9 @@ mod tests {
958963
#[tokio::test]
959964
async fn test_create_env_files_replaces_placeholder_with_home_relative_path() {
960965
let temp_dir = TempDir::new().unwrap();
961-
let home = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
966+
let home = AbsolutePathBuf::new(temp_dir.path().join("vp_home")).unwrap();
962967
let _guard = home_guard(temp_dir.path());
968+
tokio::fs::create_dir_all(&home).await.unwrap();
963969

964970
create_env_files(&home).await.unwrap();
965971

@@ -988,28 +994,31 @@ mod tests {
988994

989995
// Should use $HOME-relative path since install dir is under HOME
990996
assert!(
991-
env_content.contains("$HOME/bin"),
992-
"env file should reference $HOME/bin, got: {env_content}"
997+
env_content.contains("$HOME/vp_home/bin"),
998+
"env file should reference $HOME/vp_home/bin, got: {env_content}"
993999
);
9941000
assert!(
995-
fish_content.contains("$HOME/bin"),
996-
"env.fish file should reference $HOME/bin, got: {fish_content}"
1001+
fish_content.contains("$HOME/vp_home/bin"),
1002+
"env.fish file should reference $HOME/vp_home/bin, got: {fish_content}"
9971003
);
9981004
assert!(
999-
env_content.contains("export VP_HOME=\"$HOME\""),
1005+
env_content.contains("export VP_HOME=\"$HOME/vp_home\""),
10001006
"env file should export VP_HOME, got: {env_content}"
10011007
);
10021008
assert!(
1003-
fish_content.contains("set -gx VP_HOME \"$HOME\""),
1009+
fish_content.contains("set -gx VP_HOME \"$HOME/vp_home\""),
10041010
"env.fish file should export VP_HOME, got: {fish_content}"
10051011
);
1006-
1007-
let expected_home = home.as_path().display().to_string();
1008-
let expected_home_nu = expected_home.replace('\\', "/");
10091012
assert!(
1010-
nu_content.contains(&format!("$env.VP_HOME = \"{expected_home_nu}\"")),
1011-
"env.nu file should set VP_HOME, got: {nu_content}"
1013+
nu_content.contains("$env.VP_HOME = (\"~/vp_home\" | path expand --no-symlink)"),
1014+
"env.nu file should set home-relative VP_HOME, got: {nu_content}"
1015+
);
1016+
assert!(
1017+
nu_content.contains("~/vp_home/bin"),
1018+
"env.nu file should reference ~/vp_home/bin, got: {nu_content}"
10121019
);
1020+
1021+
let expected_home = home.as_path().display().to_string();
10131022
assert!(
10141023
ps1_content.contains(&format!("$env:VP_HOME = \"{expected_home}\"")),
10151024
"env.ps1 file should set VP_HOME, got: {ps1_content}"

0 commit comments

Comments
 (0)