diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cc9b768f0b7..ad021cb1d54 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -59,12 +59,15 @@ jobs: - shell: pwsh run: | cargo build + cargo build -p mise-shim Add-Content $env:GITHUB_PATH "$env:GITHUB_WORKSPACE\target\debug" - run: mise -v - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 with: name: mise-windows-latest - path: target/debug/mise.exe + path: | + target/debug/mise.exe + target/debug/mise-shim.exe - uses: ./.github/actions/mise-tools unit: diff --git a/Cargo.lock b/Cargo.lock index fc167d7aa74..6b7d49002c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5178,6 +5178,10 @@ dependencies = [ "xx", ] +[[package]] +name = "mise-shim" +version = "2026.2.5" + [[package]] name = "mlua" version = "0.11.6" diff --git a/Cargo.toml b/Cargo.toml index ddb8736d273..c18f6342817 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ "crates/vfox", "crates/aqua-registry", "crates/mise-interactive-config", + "crates/mise-shim", ] [package] diff --git a/crates/mise-shim/Cargo.toml b/crates/mise-shim/Cargo.toml new file mode 100644 index 00000000000..bfc848c15fe --- /dev/null +++ b/crates/mise-shim/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "mise-shim" +version = "2026.2.5" +edition = "2024" +license = "MIT" +description = "Native executable shim for mise" +homepage = "https://github.com/jdx/mise" +repository = "https://github.com/jdx/mise" + +[[bin]] +name = "mise-shim" +path = "src/main.rs" + +[dependencies] +# zero external dependencies — only std diff --git a/crates/mise-shim/LICENSE b/crates/mise-shim/LICENSE new file mode 100644 index 00000000000..d5824c80871 --- /dev/null +++ b/crates/mise-shim/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2026 Jeff Dickey +Copyright (c) 2026 Jan Killian + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/mise-shim/src/main.rs b/crates/mise-shim/src/main.rs new file mode 100644 index 00000000000..1f4a84a4626 --- /dev/null +++ b/crates/mise-shim/src/main.rs @@ -0,0 +1,37 @@ +// Based on https://github.com/iki/mise-shim by Jan Killian (MIT License) + +use std::env; +use std::process::{Command, exit}; + +fn main() { + let exe = env::current_exe().unwrap_or_else(|e| { + eprintln!("mise-shim: failed to determine executable path: {e}"); + exit(1); + }); + let tool = exe + .file_stem() + .unwrap_or_else(|| { + eprintln!("mise-shim: failed to determine tool name from executable path"); + exit(1); + }) + .to_os_string(); + + let args = env::args_os().skip(1); + + let status = Command::new("mise") + .arg("x") + .arg("--") + .arg(&tool) + .args(args) + .status(); + + match status { + Ok(s) => exit(s.code().unwrap_or(1)), + Err(e) => { + eprintln!("mise-shim: failed to execute mise: {e}"); + eprintln!("Ensure `mise` is installed and available on your PATH."); + eprintln!("See https://mise.jdx.dev for installation instructions."); + exit(1); + } + } +} diff --git a/docs/faq.md b/docs/faq.md index 43ed762ffa3..f495b728706 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -202,7 +202,7 @@ It handles daemon management with features like automatic restarts on failure, s In VSCode, many extensions will throw an "error spawn EINVAL" due to a [Node.js security fix](https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2#command-injection-via-args-parameter-of-child_processspawn-without-shell-option-enabled-on-windows-cve-2024-27980---high). -You can change [windows_shim_mode](https://mise.jdx.dev/configuration/settings.html#windows_shim_mode) to `hardlink` or `symlink` +The default `exe` shim mode should resolve this. If you're using an older mode, you can change [windows_shim_mode](https://mise.jdx.dev/configuration/settings.html#windows_shim_mode) to `exe`, `hardlink`, or `symlink`. ## How does mise versioning work? diff --git a/e2e-win/shim.Tests.ps1 b/e2e-win/shim.Tests.ps1 index e70e200e915..c13eaa29186 100644 --- a/e2e-win/shim.Tests.ps1 +++ b/e2e-win/shim.Tests.ps1 @@ -35,6 +35,19 @@ Describe 'shim_mode' { (Get-Item -Path (Join-Path -Path $shimPath -ChildPath go.cmd)).LinkType | Should -Be $null } + It 'run on exe' { + changeShimMode "exe" + + $wherePath = mise x go@1.23.3 -- where go + $LASTEXITCODE | Should -Be 0 + $wherePath | Should -BeLike "*go.exe" + mise x go@1.23.3 -- go version | Should -BeLike "go version go1.23.3 windows/*" + + $goShim = Get-Item -Path (Join-Path -Path $shimPath -ChildPath go.exe) + $goShim.LinkType | Should -Be $null + $goShim.Length | Should -BeGreaterThan 0 + } + It 'run on hardlink' { mise settings windows_shim_mode "hardlink" diff --git a/schema/mise.json b/schema/mise.json index 299707b57c6..c87880a51dc 100644 --- a/schema/mise.json +++ b/schema/mise.json @@ -1381,8 +1381,8 @@ } }, "windows_shim_mode": { - "default": "file", - "description": "Shim file mode for Windows. Options: `file`, `hardlink`, `symlink`.", + "default": "exe", + "description": "Shim file mode for Windows. Options: `exe`, `file`, `hardlink`, `symlink`.", "type": "string" }, "yes": { diff --git a/scripts/build-tarball.ps1 b/scripts/build-tarball.ps1 index 8d0a367c412..74e9d9241d1 100755 --- a/scripts/build-tarball.ps1 +++ b/scripts/build-tarball.ps1 @@ -7,8 +7,10 @@ $BaseName = "mise-v$Version-$Env:OS-$Env:ARCH" # TODO: use "serious" feature cargo build --release --features rustls-native-roots,openssl/vendored --target "$Target" +cargo build --release -p mise-shim --target "$Target" mkdir -p dist/mise/bin cp "target/$Target/release/mise.exe" dist/mise/bin/mise.exe +cp "target/$Target/release/mise-shim.exe" dist/mise/bin/mise-shim.exe cp README.md dist/mise/README.md cp LICENSE dist/mise/LICENSE Set-Location dist diff --git a/settings.toml b/settings.toml index 8b793fda8d3..cf6896a0cf6 100644 --- a/settings.toml +++ b/settings.toml @@ -1763,11 +1763,12 @@ rust_type = "Vec" type = "ListString" [windows_shim_mode] -default = "file" -description = "Shim file mode for Windows. Options: `file`, `hardlink`, `symlink`." +default = "exe" +description = "Shim file mode for Windows. Options: `exe`, `file`, `hardlink`, `symlink`." docs = """ * values: - * `file`: Creates a file with the content `mise exec`. + * `exe`: Copies a native executable shim (`mise-shim.exe`) as `.exe`. Recommended. Works with all shells, package managers, and `where.exe`. Requires `mise-shim.exe` alongside `mise.exe`. + * `file`: Creates a `.cmd` batch file shim and an extensionless bash script for Git Bash/Cygwin. * `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. """ diff --git a/src/shims.rs b/src/shims.rs index 3792bb33f28..26f77796742 100644 --- a/src/shims.rs +++ b/src/shims.rs @@ -105,11 +105,27 @@ pub async fn reshim(config: &Arc, ts: &Toolset, force: bool) -> Result<( let mise_bin = file::which("mise").unwrap_or(env::MISE_BIN.clone()); let mise_bin = mise_bin.absolutize()?; // relative paths don't work as shims - let is_windows_hardlink = cfg!(windows) && Settings::get().windows_shim_mode.eq("hardlink"); - if force || is_windows_hardlink { + #[cfg(windows)] + let shim_mode = effective_shim_mode(&mise_bin); + #[cfg(not(windows))] + let shim_mode = String::new(); + let shim_mode_changed = cfg!(windows) && { + let mode_file = dirs::SHIMS.join(".mode"); + mode_file + .exists() + .then(|| fs::read_to_string(&mode_file).unwrap_or_default()) + .is_some_and(|prev| prev.trim() != shim_mode) + }; + let is_windows_hardlink_or_exe = + cfg!(windows) && (shim_mode == "hardlink" || shim_mode == "exe"); + if force || is_windows_hardlink_or_exe || shim_mode_changed { file::remove_all(*dirs::SHIMS)?; } file::create_dir_all(*dirs::SHIMS)?; + if cfg!(windows) { + let mode_file = dirs::SHIMS.join(".mode"); + file::write(&mode_file, &shim_mode)?; + } let (shims_to_add, shims_to_remove) = get_shim_diffs(config, &mise_bin, ts).await?; @@ -143,9 +159,74 @@ pub async fn reshim(config: &Arc, ts: &Toolset, force: bool) -> Result<( Ok(()) } +#[cfg(windows)] +fn find_mise_shim_bin(mise_bin: &Path) -> Option { + // Look next to the mise binary first + if let Some(parent) = mise_bin.parent() { + let candidate = parent.join("mise-shim.exe"); + if candidate.is_file() { + return Some(candidate); + } + } + // Fall back to searching PATH + // Note: file::which on Windows checks extension only, not file existence, + // so we must verify the file actually exists. + file::which("mise-shim.exe").filter(|p| p.is_file()) +} + +/// Resolve the effective Windows shim mode, falling back to "file" if "exe" is +/// requested but mise-shim.exe is not available. +#[cfg(windows)] +fn effective_shim_mode(mise_bin: &Path) -> String { + let mode = Settings::get().windows_shim_mode.clone(); + if mode == "exe" && find_mise_shim_bin(mise_bin).is_none() { + warn!( + "mise-shim.exe not found next to {} or on PATH, falling back to \"file\" shim mode", + display_path(mise_bin) + ); + return "file".to_string(); + } + mode +} + #[cfg(windows)] fn add_shim(mise_bin: &Path, symlink_path: &Path, shim: &str) -> Result<()> { - match Settings::get().windows_shim_mode.as_ref() { + match effective_shim_mode(mise_bin).as_ref() { + "exe" => { + if symlink_path.extension().and_then(|s| s.to_str()) == Some("exe") { + let mise_shim_bin = + find_mise_shim_bin(mise_bin).ok_or_else(|| eyre!("mise-shim.exe not found"))?; + // Copy mise-shim.exe as .exe + fs::copy(&mise_shim_bin, symlink_path).wrap_err_with(|| { + eyre!( + "Failed to copy {} to {}", + display_path(&mise_shim_bin), + display_path(symlink_path) + ) + })?; + Ok(()) + } else { + let shim_name = symlink_path + .file_name() + .unwrap_or_default() + .to_string_lossy(); + // Create extensionless bash script for Git Bash/Cygwin + file::write( + symlink_path, + formatdoc! {r#" + #!/bin/bash + + exec mise x -- {shim_name} "$@" + "#}, + ) + .wrap_err_with(|| { + eyre!( + "Failed to create shim script for {}", + display_path(symlink_path) + ) + }) + } + } "file" => { let shim = shim.trim_end_matches(".cmd"); // write a shim file without extension for use in Git Bash/Cygwin @@ -223,7 +304,7 @@ pub async fn get_shim_diffs( let mise_bin = mise_bin.as_ref(); let (actual_shims, desired_shims) = tokio::join!( get_actual_shims(mise_bin), - get_desired_shims(config, toolset) + get_desired_shims(config, mise_bin, toolset) ); let (actual_shims, desired_shims) = (actual_shims?, desired_shims?); let out: (BTreeSet, BTreeSet) = ( @@ -272,6 +353,11 @@ fn list_shims() -> Result> { .read_dir()? .map(|bin| { let bin = bin?; + let name = bin.file_name(); + // skip dotfiles (e.g. .mode) — these are metadata, not shims + if name.to_string_lossy().starts_with('.') { + return Ok(None); + } // files and symlinks which are executable or extensionless files (Git Bash/Cygwin) if (file::is_executable(&bin.path()) || bin.path().extension().is_none()) && (bin.file_type()?.is_file() || bin.file_type()?.is_symlink()) @@ -287,7 +373,12 @@ fn list_shims() -> Result> { .collect()) } -async fn get_desired_shims(config: &Arc, toolset: &Toolset) -> Result> { +async fn get_desired_shims( + config: &Arc, + mise_bin: &Path, + toolset: &Toolset, +) -> Result> { + let _mise_bin = mise_bin; // used on Windows only let mut shims = HashSet::new(); for (t, tv) in toolset.list_installed_versions(config).await? { let bins = list_tool_bins(config, t.clone(), &tv) @@ -297,12 +388,22 @@ async fn get_desired_shims(config: &Arc, toolset: &Toolset) -> Result { vec![p.with_extension("exe").to_string_lossy().to_string()] } + "exe" => { + vec![ + p.with_extension("exe").to_string_lossy().to_string(), + p.with_extension("").to_string_lossy().to_string(), + ] + } "file" => { vec![ p.with_extension("").to_string_lossy().to_string(),