From 20d9f3d18ceb9901c3d89cdcc69981a096248b80 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Sat, 17 Jul 2021 17:57:04 +0530 Subject: [PATCH] Fix init error in PowerShell strict mode (#233) --- CHANGELOG.md | 1 + src/shell.rs | 3 ++- templates/powershell.txt | 7 +++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb20168..dba3e81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Nushell: errors on 0.33.0. +- PowerShell: errors when initializing in `StrictMode`. ## [0.7.2] - 2021-06-10 diff --git a/src/shell.rs b/src/shell.rs index 8a3ef78..dc7efa7 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -280,7 +280,8 @@ mod tests { #[values(false, true)] resolve_symlinks: bool, ) { let opts = Opts { cmd, hook, echo, resolve_symlinks }; - let source = Powershell(&opts).render().unwrap(); + let mut source = "Set-StrictMode -Version latest\n".to_string(); + Powershell(&opts).render_into(&mut source).unwrap(); Command::new("pwsh") .args(&["-NoLogo", "-NonInteractive", "-NoProfile", "-Command", &source]) diff --git a/templates/powershell.txt b/templates/powershell.txt index 46d7baf..ede8619 100644 --- a/templates/powershell.txt +++ b/templates/powershell.txt @@ -34,8 +34,11 @@ function __zoxide_hook { } # Initialize hook. -if ($__zoxide_hooked -ne '1') { - $__zoxide_hooked = '1' +{# Initialize $__zoxide_hooked if it does not exist. Removing this will cause + # an unset variable error in StrictMode. #} +$__zoxide_hooked = (Get-Variable __zoxide_hooked -ValueOnly -ErrorAction silentlycontinue) +if ($__zoxide_hooked -ne 1) { + $__zoxide_hooked = 1 {%- match hook %} {%- when InitHook::None %} {{ not_configured }}