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

Merged
merged 5 commits into from
Feb 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
55 changes: 55 additions & 0 deletions e2e-win/shim.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Describe 'shim_mode' {

BeforeAll {
function changeShimMode {
param (
[string]$mode
)

mise settings windows_shim_mode $mode
mise reshim --force
}

$shimPath = Join-Path -Path $env:MISE_DATA_DIR -ChildPath "shims"
}

AfterAll {
mise settings unset windows_shim_mode
}

It 'run on symlink' {
changeShimMode "symlink"

mise x [email protected] -- where go
mise x [email protected] -- go version | Should -BeLike "go version go1.23.3 windows/*"

(Get-Item -Path (Join-Path -Path $shimPath -ChildPath go.exe)).LinkType | Should -Be "SymbolicLink"
}

It 'run on file' {
changeShimMode "file"

mise x [email protected] -- where go
mise x [email protected] -- go version | Should -BeLike "go version go1.23.3 windows/*"

(Get-Item -Path (Join-Path -Path $shimPath -ChildPath go.cmd)).LinkType | Should -Be $null
}

It 'run on hardlink' {
mise settings windows_shim_mode "hardlink"

# make mise is on same filesystem
$misePath = (Get-Command -Type Application mise -all | Select-Object -First 1).Source
$binPath = (Join-Path -Path $env:MISE_DATA_DIR -ChildPath "bin")
$newMisePath = (Join-Path -Path $binPath -ChildPath "mise.exe")
New-Item -ItemType Directory -Path $binPath -Force
Copy-Item $misePath $newMisePath

&$newMisePath reshim --force

&$newMisePath x [email protected] -- where go
&$newMisePath x [email protected] -- go version | Should -BeLike "go version go1.23.3 windows/*"

(Get-Item -Path (Join-Path -Path $shimPath -ChildPath go.exe)).LinkType | Should -Be "HardLink"
}
}
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. Need run `mise reshim --force` after upgrade mise
`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