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

Change variable scope from "local" to "function" #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

frazar
Copy link
Contributor

@frazar frazar commented Sep 9, 2024

Inside the __done_run_powershell_script function, the if test $status -ne 0 block would set the powershell_exe variable with scope -l (--local), meaning that its value would be erased at the end of the if block.

done/conf.d/done.fish

Lines 32 to 36 in eb32ade

if test $status -ne 0
and command --search wslvar
set -l powershell_exe (wslpath (wslvar windir)/System32/WindowsPowerShell/v1.0/powershell.exe)
end

However, the updated value would then not be available in the subsequent part of the function and (in particular) would result in the following if block not being evaluated:

done/conf.d/done.fish

Lines 38 to 44 in eb32ade

if string length --quiet "$powershell_exe"
and test -x "$powershell_exe"
set cmd (string escape $argv)
eval "$powershell_exe -Command $cmd"
end

To verify this, I ran the function with set_fish_trace enabled

$ set fish_trace 1 ; __done_windows_notification test notif
> __done_windows_notification test notif
--> if
--> test 1 -eq 1
---> set soundopt '<audio silent="false" src="ms-winsoundevent:Notification.Default" />'
--> end if
--> __done_run_powershell_script \n\[Windows.Data.Xml.Dom.XmlDocument,\ Windows.Data.Xml.Dom.XmlDocument,\ ContentType\ =\ WindowsRuntime\]\ \|\ Out-Null\n\[Windows.UI.Notifications.ToastNotification,\ Windows.UI.Notifications,\ ContentType\ =\ WindowsRuntime\]\ \|\ Out-Null\n\n\$toast_xml_source\ =\ @\"\n\ \ \ \ \<toast\>\n\ \ \ \ \ \ \ \ \<audio\ silent=\"false\"\ src=\"ms-winsoundevent:Notification.Default\"\ /\>\n\ \ \ \ \ \ \ \ \<visual\>\n\ \ \ \ \ \ \ \ \ \ \ \ \<binding\ template=\"ToastText02\"\>\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \<text\ id=\"1\"\>test\</text\>\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \<text\ id=\"2\"\>notif\</text\>\n\ \ \ \ \ \ \ \ \ \ \ \ \</binding\>\n\ \ \ \ \ \ \ \ \</visual\>\n\ \ \ \ \</toast\>\n\"@\n\n\$toast_xml\ =\ New-Object\ Windows.Data.Xml.Dom.XmlDocument\n\$toast_xml.loadXml\(\$toast_xml_source\)\n\n\$toast\ =\ New-Object\ Windows.UI.Notifications.ToastNotification\ \$toast_xml\n\n\[Windows.UI.Notifications.ToastNotificationManager\]::CreateToastNotifier\(\"fish\"\).Show\(\$toast\)\n
-----> command --search powershell.exe
----> set -l powershell_exe
----> if
----> test 127 -ne 0
----> command --search wslvar
/usr/bin/wslvar
-------> wslvar windir
------> wslpath C:\\Windows/System32/WindowsPowerShell/v1.0/powershell.exe
-----> set -l powershell_exe /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe 
----> end if
----> if
----> string length --quiet ''  
----> end if

As seen above, the if evaluates the wrong value of the powershell_exe value (i.e., '') instead of the updated one (i.e., /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe).

Setting the variable scope to -f (--function) fixes the issue by making the new value of the powershell_exe variable being available in the whole function block.

> __done_windows_notification test notif
--> if
--> test 1 -eq 1
---> set soundopt '<audio silent="false" src="ms-winsoundevent:Notification.Default" />'
--> end if
--> __done_run_powershell_script \n\[Windows.Data.Xml.Dom.XmlDocument,\ Windows.Data.Xml.Dom.XmlDocument,\ ContentType\ =\ WindowsRuntime\]\ \|\ Out-Null\n\[Windows.UI.Notifications.ToastNotification,\ Windows.UI.Notifications,\ ContentType\ =\ WindowsRuntime\]\ \|\ Out-Null\n\n\$toast_xml_source\ =\ @\"\n\ \ \ \ \<toast\>\n\ \ \ \ \ \ \ \ \<audio\ silent=\"false\"\ src=\"ms-winsoundevent:Notification.Default\"\ /\>\n\ \ \ \ \ \ \ \ \<visual\>\n\ \ \ \ \ \ \ \ \ \ \ \ \<binding\ template=\"ToastText02\"\>\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \<text\ id=\"1\"\>test\</text\>\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \<text\ id=\"2\"\>notif\</text\>\n\ \ \ \ \ \ \ \ \ \ \ \ \</binding\>\n\ \ \ \ \ \ \ \ \</visual\>\n\ \ \ \ \</toast\>\n\"@\n\n\$toast_xml\ =\ New-Object\ Windows.Data.Xml.Dom.XmlDocument\n\$toast_xml.loadXml\(\$toast_xml_source\)\n\n\$toast\ =\ New-Object\ Windows.UI.Notifications.ToastNotification\ \$toast_xml\n\n\[Windows.UI.Notifications.ToastNotificationManager\]::CreateToastNotifier\(\"fish\"\).Show\(\$toast\)\n
-----> command --search powershell.exe
----> set -f powershell_exe
----> if
----> test 127 -ne 0
----> command --search wslvar
/usr/bin/wslvar
-------> wslvar windir
------> wslpath C:\\Windows/System32/WindowsPowerShell/v1.0/powershell.exe
-----> set -f powershell_exe /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
----> end if
----> if
----> string length --quiet /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
----> test -x /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
------> string escape \n\[Windows.Data.Xml.Dom.XmlDocument,\ Windows.Data.Xml.Dom.XmlDocument,\ ContentType\ =\ WindowsRuntime\]\ \|\ Out-Null\n\[Windows.UI.Notifications.ToastNotification,\ Windows.UI.Notifications,\ ContentType\ =\ WindowsRuntime\]\ \|\ Out-Null\n\n\$toast_xml_source\ =\ @\"\n\ \ \ \ \<toast\>\n\ \ \ \ \ \ \ \ \<audio\ silent=\"false\"\ src=\"ms-winsoundevent:Notification.Default\"\ /\>\n\ \ \ \ \ \ \ \ \<visual\>\n\ \ \ \ \ \ \ \ \ \ \ \ \<binding\ template=\"ToastText02\"\>\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \<text\ id=\"1\"\>test\</text\>\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \<text\ id=\"2\"\>notif\</text\>\n\ \ \ \ \ \ \ \ \ \ \ \ \</binding\>\n\ \ \ \ \ \ \ \ \</visual\>\n\ \ \ \ \</toast\>\n\"@\n\n\$toast_xml\ =\ New-Object\ Windows.Data.Xml.Dom.XmlDocument\n\$toast_xml.loadXml\(\$toast_xml_source\)\n\n\$toast\ =\ New-Object\ Windows.UI.Notifications.ToastNotification\ \$toast_xml\n\n\[Windows.UI.Notifications.ToastNotificationManager\]::CreateToastNotifier\(\"fish\"\).Show\(\$toast\)\n
-----> set cmd \\n\\\[Windows.Data.Xml.Dom.XmlDocument,\\\ Windows.Data.Xml.Dom.XmlDocument,\\\ ContentType\\\ =\\\ WindowsRuntime\\\]\\\ \\\|\\\ Out-Null\\n\\\[Windows.UI.Notifications.ToastNotification,\\\ Windows.UI.Notifications,\\\ ContentType\\\ =\\\ WindowsRuntime\\\]\\\ \\\|\\\ Out-Null\\n\\n\\\$toast_xml_source\\\ =\\\ @\\\"\\n\\\ \\\ \\\ \\\ \\\<toast\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<audio\\\ silent=\\\"false\\\"\\\ src=\\\"ms-winsoundevent:Notification.Default\\\"\\\ /\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<visual\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<binding\\\ template=\\\"ToastText02\\\"\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<text\\\ id=\\\"1\\\"\\\>test\\\</text\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<text\\\ id=\\\"2\\\"\\\>notif\\\</text\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\</binding\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\</visual\\\>\\n\\\ \\\ \\\ \\\ \\\</toast\\\>\\n\\\"@\\n\\n\\\$toast_xml\\\ =\\\ New-Object\\\ Windows.Data.Xml.Dom.XmlDocument\\n\\\$toast_xml.loadXml\\\(\\\$toast_xml_source\\\)\\n\\n\\\$toast\\\ =\\\ New-Object\\\ Windows.UI.Notifications.ToastNotification\\\ \\\$toast_xml\\n\\n\\\[Windows.UI.Notifications.ToastNotificationManager\\\]::CreateToastNotifier\\\(\\\"fish\\\"\\\).Show\\\(\\\$toast\\\)\\n
-----> eval /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe\ -Command\ \\n\\\[Windows.Data.Xml.Dom.XmlDocument,\\\ Windows.Data.Xml.Dom.XmlDocument,\\\ ContentType\\\ =\\\ WindowsRuntime\\\]\\\ \\\|\\\ Out-Null\\n\\\[Windows.UI.Notifications.ToastNotification,\\\ Windows.UI.Notifications,\\\ ContentType\\\ =\\\ WindowsRuntime\\\]\\\ \\\|\\\ Out-Null\\n\\n\\\$toast_xml_source\\\ =\\\ @\\\"\\n\\\ \\\ \\\ \\\ \\\<toast\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<audio\\\ silent=\\\"false\\\"\\\ src=\\\"ms-winsoundevent:Notification.Default\\\"\\\ /\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<visual\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<binding\\\ template=\\\"ToastText02\\\"\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<text\\\ id=\\\"1\\\"\\\>test\\\</text\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\<text\\\ id=\\\"2\\\"\\\>notif\\\</text\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\</binding\\\>\\n\\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\ \\\</visual\\\>\\n\\\ \\\ \\\ \\\ \\\</toast\\\>\\n\\\"@\\n\\n\\\$toast_xml\\\ =\\\ New-Object\\\ Windows.Data.Xml.Dom.XmlDocument\\n\\\$toast_xml.loadXml\\\(\\\$toast_xml_source\\\)\\n\\n\\\$toast\\\ =\\\ New-Object\\\ Windows.UI.Notifications.ToastNotification\\\ \\\$toast_xml\\n\\n\\\[Windows.UI.Notifications.ToastNotificationManager\\\]::CreateToastNotifier\\\(\\\"fish\\\"\\\).Show\\\(\\\$toast\\\)\\n
------> /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command \n\[Windows.Data.Xml.Dom.XmlDocument,\ Windows.Data.Xml.Dom.XmlDocument,\ ContentType\ =\ WindowsRuntime\]\ \|\ Out-Null\n\[Windows.UI.Notifications.ToastNotification,\ Windows.UI.Notifications,\ ContentType\ =\ WindowsRuntime\]\ \|\ Out-Null\n\n\$toast_xml_source\ =\ @\"\n\ \ \ \ \<toast\>\n\ \ \ \ \ \ \ \ \<audio\ silent=\"false\"\ src=\"ms-winsoundevent:Notification.Default\"\ /\>\n\ \ \ \ \ \ \ \ \<visual\>\n\ \ \ \ \ \ \ \ \ \ \ \ \<binding\ template=\"ToastText02\"\>\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \<text\ id=\"1\"\>test\</text\>\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \<text\ id=\"2\"\>notif\</text\>\n\ \ \ \ \ \ \ \ \ \ \ \ \</binding\>\n\ \ \ \ \ \ \ \ \</visual\>\n\ \ \ \ \</toast\>\n\"@\n\n\$toast_xml\ =\ New-Object\ Windows.Data.Xml.Dom.XmlDocument\n\$toast_xml.loadXml\(\$toast_xml_source\)\n\n\$toast\ =\ New-Object\ Windows.UI.Notifications.ToastNotification\ \$toast_xml\n\n\[Windows.UI.Notifications.ToastNotificationManager\]::CreateToastNotifier\(\"fish\"\).Show\(\$toast\)\n
----> end if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant