Skip to content
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

feat(shim): Windows shim add hardlink & symlink mode #4409

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,11 @@
"type": "string"
}
},
"windows_shim_mode": {
"default": "file",
"description": "Shim file mode for Windows. Options: `file`, `hardlink`, `symlink`.",
"type": "string"
},
"yes": {
"description": "This will automatically answer yes or no to prompts. This is useful for scripting.",
"type": "boolean"
Expand Down
11 changes: 11 additions & 0 deletions settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,17 @@ default = ["exe", "bat", "cmd", "com", "ps1", "vbs"]
parse_env = "list_by_comma"
description = "List of executable extensions for Windows. For example, `exe` for .exe files, `bat` for .bat files, and so on."

[windows_shim_mode]
env = "MISE_WINDOWS_SHIM_MODE"
type = "String"
default = "file"
description = "Shim file mode for Windows. Options: `file`, `hardlink`, `symlink`."
docs = """
`file`: Creates a file with the content `mise exec`.
`hardlink`: Uses Windows NTFS Hardlink, required on same filesystems.
`symlink`: Uses Windows NTFS SymbolicLink. Requires Windows Vista or later with admin privileges or enabling "Developer Mode" in Windows 10/11.
"""

[yes]
env = "MISE_YES"
type = "Bool"
Expand Down
85 changes: 57 additions & 28 deletions src/shims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,38 +128,59 @@ pub fn reshim(ts: &Toolset, force: bool) -> Result<()> {

#[cfg(windows)]
fn add_shim(mise_bin: &Path, symlink_path: &Path, shim: &str) -> Result<()> {
let shim = shim.trim_end_matches(".cmd");
// write a shim file without extension for use in Git Bash/Cygwin
file::write(
symlink_path.with_extension(""),
formatdoc! {r#"
match SETTINGS.windows_shim_mode.as_ref() {
"file" => {
let shim = shim.trim_end_matches(".cmd");
// write a shim file without extension for use in Git Bash/Cygwin
file::write(
symlink_path.with_extension(""),
formatdoc! {r#"
#!/bin/bash

exec mise x -- {shim} "$@"
"#},
)
.wrap_err_with(|| {
eyre!(
"Failed to create symlink from {} to {}",
display_path(mise_bin),
display_path(symlink_path)
)
})?;
file::write(
symlink_path.with_extension("cmd"),
formatdoc! {r#"
)
.wrap_err_with(|| {
eyre!(
"Failed to create symlink from {} to {}",
display_path(mise_bin),
display_path(symlink_path)
)
})?;
file::write(
symlink_path.with_extension("cmd"),
formatdoc! {r#"
@echo off
setlocal
mise x -- {shim} %*
"#},
)
.wrap_err_with(|| {
eyre!(
"Failed to create symlink from {} to {}",
display_path(mise_bin),
display_path(symlink_path)
)
})
)
.wrap_err_with(|| {
eyre!(
"Failed to create symlink from {} to {}",
display_path(mise_bin),
display_path(symlink_path)
)
})
}
"hardlink" => fs::hard_link(mise_bin, symlink_path).wrap_err_with(|| {
eyre!(
"Failed to create hardlink from {} to {}",
display_path(mise_bin),
display_path(symlink_path)
)
}),
"symlink" => {
std::os::windows::fs::symlink_file(mise_bin, symlink_path).wrap_err_with(|| {
eyre!(
"Failed to create symlink from {} to {}",
display_path(mise_bin),
display_path(symlink_path)
)
})
}
_ => panic!("Unknown shim mode"),
}
}

#[cfg(unix)]
Expand Down Expand Up @@ -261,10 +282,18 @@ fn get_desired_shims(toolset: &Toolset) -> Result<HashSet<String>> {
bins.into_iter()
.flat_map(|b| {
let p = PathBuf::from(&b);
vec![
p.with_extension("").to_string_lossy().to_string(),
p.with_extension("cmd").to_string_lossy().to_string(),
]
match SETTINGS.windows_shim_mode.as_ref() {
"hardlink" | "symlink" => {
vec![p.with_extension("exe").to_string_lossy().to_string()]
}
"file" => {
vec![
p.with_extension("").to_string_lossy().to_string(),
p.with_extension("cmd").to_string_lossy().to_string(),
]
}
_ => panic!("Unknown shim mode"),
}
})
.collect()
} else if cfg!(macos) {
Expand Down
Loading