configure cider to use PowerShell 7.x and not Windows PowerShell 5.1 on windows
shishini opened this issue · 23 comments
Cider fails to jack in a repl , and this initially seem to be because its trying to start the repl using
c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
instead of
C:\Program Files\PowerShell\7\pwsh.exe
I installed Clojure CLI in a path that is only accessible for PowerShell 7.x not for Windows PowerShell 5.1
So how can i configure cider (or emacs) to use pwsh (which is the command for Powershell 7.x) not powershell (which is the command for the older Windows Powershell 5.1)
I can confirm that adding the clojure cli module to a Windows Powershell 5.1 modules path, fixes my initial issue
so i just want the ability to configure either cider or emacs to use pwsh not powershell
Also you might want to check this calva issue BetterThanTomorrow/calva#1000 for suggestion on how to solve this, since this is basically the same issue but in emacs
I am really not that experience
Can you give more detail on how to use this feature to fix the problem
what do i need to add in my .emacs for example ?
Yes, please add something like (setq cider-clojure-cli-command "pwsh")
in your Emacs init file. After a restart, that should be it
thanks i did just that but now i am getting this error
error in process sentinel: nrepl-server-sentinel: Could not start nREPL server: Error while parsing option "--config-data {:deps {nrepl/nrepl {:mvn/version \"\"1.0.0\"\"} cider/cider-nrepl {:mvn/version \"\"0.43.3\"\"}} :aliases {:cider/nrepl {:main-opts [\"\"-m\"\" \"\"nrepl.cmdline\"\" \"\"--middleware\"\" \"\"[cider.nrepl/cider-middleware]\"\"]}}}": java.lang.NumberFormatException: Invalid number: 1.0.0
("finished")
error in process sentinel: Could not start nREPL server: Error while parsing option "--config-data {:deps {nrepl/nrepl {:mvn/version \"\"1.0.0\"\"} cider/cider-nrepl {:mvn/version \"\"0.43.3\"\"}} :aliases {:cider/nrepl {:main-opts [\"\"-m\"\" \"\"nrepl.cmdline\"\" \"\"--middleware\"\" \"\"[cider.nrepl/cider-middleware]\"\"]}}}": java.lang.NumberFormatException: Invalid number: 1.0.0
setting this variable to use powershell, removes the error, but i am back to using powershell not pwsh
It looks like a quoting problem. Quoting can be tricky on windows, although we specifically handle it if cider-clojure-cli-command
equals "powershell"
or "pwsh"
For reference, this is our quoting code:
Lines 801 to 810 in 5261886
What I would suggest is:
- note in the
*Messages*
buffer the entire "jack in" command - paste it into your terminal (not emacs)
- it should also fail with
Invalid number: 1.0.0
- it should also fail with
- try fixing it
- probably there's insufficient or excessive quoting
- chatgpt may help!
Not sure how i can help but
PS C:\> clojure -Sdeps " {:deps {nrepl/nrepl {:mvn/version ""1.0.0""} cider/cider-nrepl {:mvn/version ""0.43.3""}} :aliases {:cider/nrepl {:main-opts [""-m"" ""nrepl.cmdline"" ""--middleware"" ""[cider.nrepl/cider-middleware]""]}}} " Clojure 1.11.1 user=>
Kinda works I think, does this help
I see! That's promising, I guess.
Could you paste the exact output shown in *Messages*
?
Example on Mac:
[nREPL] Starting server via /Users/vemv/bin/lein update-in :dependencies conj \[nrepl/nrepl\ \"1.0.0\"\] -- update-in :dependencies conj \[refactor-nrepl/refactor-nrepl\ \"3.9.0\"\] -- update-in :plugins conj \[refactor-nrepl/refactor-nrepl\ \"3.9.0\"\] -- update-in :plugins conj \[cider/cider-nrepl\ \"0.43.3\"\] -- with-profile "+async-checking,+test,+dev,+emacs-backend,+emacs-backend-init,+yourkit,+g1gc,+cider-nrepl-latest" repl :headless :host localhost
When I use powershell (which succeed) this is what is in Messages*
[nREPL] Starting server via "c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" -encodedCommand YwBsAG8AagB1AHIAZQAgAC0AUwBkAGUAcABzACAAJwB7ADoAZABlAHAAcwAgAHsAbgByAGUAcABsAC8AbgByAGUAcABsACAAewA6AG0AdgBuAC8AdgBlAHIAcwBpAG8AbgAgACIAIgAxAC4AMAAuADAAIgAiAH0AIABjAGkAZABlAHIALwBjAGkAZABlAHIALQBuAHIAZQBwAGwAIAB7ADoAbQB2AG4ALwB2AGUAcgBzAGkAbwBuACAAIgAiADAALgA0ADMALgAzACIAIgB9AH0AIAA6AGEAbABpAGEAcwBlAHMAIAB7ADoAYwBpAGQAZQByAC8AbgByAGUAcABsACAAewA6AG0AYQBpAG4ALQBvAHAAdABzACAAWwAiACIALQBtACIAIgAgACIAIgBuAHIAZQBwAGwALgBjAG0AZABsAGkAbgBlACIAIgAgACIAIgAtAC0AbQBpAGQAZABsAGUAdwBhAHIAZQAiACIAIAAiACIAWwBjAGkAZABlAHIALgBuAHIAZQBwAGwALwBjAGkAZABlAHIALQBtAGkAZABkAGwAZQB3AGEAcgBlAF0AIgAiAF0AfQB9AH0AJwAgAC0ATQA6AGMAaQBkAGUAcgAvAG4AcgBlAHAAbAA=
when I use pwsh this is the Messages
[nREPL] Starting server via "c:/Program Files/PowerShell/7/pwsh.exe" -encodedCommand YwBsAG8AagB1AHIAZQAgAC0AUwBkAGUAcABzACAAJwB7ADoAZABlAHAAcwAgAHsAbgByAGUAcABsAC8AbgByAGUAcABsACAAewA6AG0AdgBuAC8AdgBlAHIAcwBpAG8AbgAgACIAIgAxAC4AMAAuADAAIgAiAH0AIABjAGkAZABlAHIALwBjAGkAZABlAHIALQBuAHIAZQBwAGwAIAB7ADoAbQB2AG4ALwB2AGUAcgBzAGkAbwBuACAAIgAiADAALgA0ADMALgAzACIAIgB9AH0AIAA6AGEAbABpAGEAcwBlAHMAIAB7ADoAYwBpAGQAZQByAC8AbgByAGUAcABsACAAewA6AG0AYQBpAG4ALQBvAHAAdABzACAAWwAiACIALQBtACIAIgAgACIAIgBuAHIAZQBwAGwALgBjAG0AZABsAGkAbgBlACIAIgAgACIAIgAtAC0AbQBpAGQAZABsAGUAdwBhAHIAZQAiACIAIAAiACIAWwBjAGkAZABlAHIALgBuAHIAZQBwAGwALwBjAGkAZABlAHIALQBtAGkAZABkAGwAZQB3AGEAcgBlAF0AIgAiAF0AfQB9AH0AJwAgAC0ATQA6AGMAaQBkAGUAcgAvAG4AcgBlAHAAbAA= error in process sentinel: nrepl-server-sentinel: Could not start nREPL server: Error while parsing option "--config-data {:deps {nrepl/nrepl {:mvn/version \"\"1.0.0\"\"} cider/cider-nrepl {:mvn/version \"\"0.43.3\"\"}} :aliases {:cider/nrepl {:main-opts [\"\"-m\"\" \"\"nrepl.cmdline\"\" \"\"--middleware\"\" \"\"[cider.nrepl/cider-middleware]\"\"]}}}": java.lang.NumberFormatException: Invalid number: 1.0.0 ("finished")
Now I need to add that I research if there was any quoting rules changes between pwsh and powershell, and there should not be, the languages should be the same, the differences are in the underlying .net framework uses, which mainly only impact the availability of some modules , but quoting rules should be the same, so this issue should not exist
It may be worth trying to skip the double quotations.
Maybe (and this is speculation of mine), with pwsh it suffices to encode the command.
Please try the following
- add this to your emacs init
(require 'cider) ;; Important - make sure we're actually overriding the defun
(defun cider--shell-quote-argument (argument &optional command)
"Patch to skip the quoting on windows"
(if (cider--jack-in-cmd-powershell-p command)
argument
(shell-quote-argument argument)))
-
Restart Emacs
-
Jack-in again
Sorry didnt work
Now I get this error
[nREPL] Starting server via "c:/Program Files/PowerShell/7/pwsh.exe" -encodedCommand YwBsAG8AagB1AHIAZQAgAC0AUwBkAGUAcABzACAAewA6AGQAZQBwAHMAIAB7AG4AcgBlAHAAbAAvAG4AcgBlAHAAbAAgAHsAOgBtAHYAbgAvAHYAZQByAHMAaQBvAG4AIAAiADEALgAwAC4AMAAiAH0AIABjAGkAZABlAHIALwBjAGkAZABlAHIALQBuAHIAZQBwAGwAIAB7ADoAbQB2AG4ALwB2AGUAcgBzAGkAbwBuACAAIgAwAC4ANAAzAC4AMwAiAH0AfQAgADoAYQBsAGkAYQBzAGUAcwAgAHsAOgBjAGkAZABlAHIALwBuAHIAZQBwAGwAIAB7ADoAbQBhAGkAbgAtAG8AcAB0AHMAIABbACIALQBtACIAIAAiAG4AcgBlAHAAbAAuAGMAbQBkAGwAaQBuAGUAIgAgACIALQAtAG0AaQBkAGQAbABlAHcAYQByAGUAIgAgACIAWwBjAGkAZABlAHIALgBuAHIAZQBwAGwALwBjAGkAZABlAHIALQBtAGkAZABkAGwAZQB3AGEAcgBlAF0AIgBdAH0AfQB9ACAALQBNADoAYwBpAGQAZQByAC8AbgByAGUAcABsAA== error in process sentinel: nrepl-server-sentinel: Could not start nREPL server: Unknown option: "-i" Unknown option: "-n" Unknown option: "-p" Unknown option: "-u" Unknown option: "-t" Unknown option: "-F" Unknown option: "-o" Unknown option: "-r" Unknown option: "-m" Unknown option: "-a" Unknown option: "-t" Unknown option: "-o" Unknown option: "-u" Unknown option: "-t" Unknown option: "-p" Unknown option: "-u" Unknown option: "-t" Unknown option: "-F" Unknown option: "-o" Unknown option: "-r" Unknown option: "-m" Unknown option: "-a" Unknown option: "-t"
Now this is probably not it, but could the space in te folder name Program Files
be causing the issue, the path for powershell have no spaces
Now this is probably not it, but could the space in te folder name Program Files be causing the issue, the path for powershell have no spaces
In #3588 (comment), it is shown that we got a JVM-level exception, so Program files
isn't causing problems
According to chatgpt:
The -EncodedCommand parameter expects the command to be Base64 encoded. The encoding should be compatible with the version of PowerShell you are using. Windows PowerShell typically uses UTF-16LE encoding for Base64 strings, while PowerShell Core may use UTF-8. If you encoded your command for one version, it might not be correctly interpreted by the other.
In our implementation, we only encode as 'utf-16le
: (encode-coding-string command 'utf-16le)
So please undo #3588 (comment) and try:
(require 'cider) ;; Important - make sure we're actually overriding the defun
(defun cider--powershell-encode-command (cmd-params)
"Experiment"
(let* ((quoted-params cmd-params)
(command (format "clojure %s" quoted-params))
(utf-8-command (encode-coding-string command 'utf-8)))
(format "-encodedCommand %s" (base64-encode-string utf-8-command t))))
Now I am getting this error
[nREPL] Starting server via "c:/Program Files/PowerShell/7/pwsh.exe" -encodedCommand Y2xvanVyZSAtU2RlcHMgJ3s6ZGVwcyB7bnJlcGwvbnJlcGwgezptdm4vdmVyc2lvbiAiIjEuMC4wIiJ9IGNpZGVyL2NpZGVyLW5yZXBsIHs6bXZuL3ZlcnNpb24gIiIwLjQzLjMiIn19IDphbGlhc2VzIHs6Y2lkZXIvbnJlcGwgezptYWluLW9wdHMgWyIiLW0iIiAiIm5yZXBsLmNtZGxpbmUiIiAiIi0tbWlkZGxld2FyZSIiICIiW2NpZGVyLm5yZXBsL2NpZGVyLW1pZGRsZXdhcmVdIiJdfX19JyAtTTpjaWRlci9ucmVwbA==
error in process sentinel: nrepl-server-sentinel: Could not start nREPL server: #< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">??????????????????????????????????????????????????????????????????????????????????????????= ???????????????????????????: The term '??????????????????????????????????????????????????????????????????????????????????????????= ???????????????????????????' is not recognized as a name of a cmdlet, function, script file, or executable program._x000D__x000A_</S><S S="Error">Check the spelling of the name, or if a path was included, verify that the path is correct and try again._x000D__x000A_</S></Objs> ("exited abnormally with code 1")
error in process sentinel: Could not start nREPL server: #< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">??????????????????????????????????????????????????????????????????????????????????????????= ???????????????????????????: The term '??????????????????????????????????????????????????????????????????????????????????????????= ???????????????????????????' is not recognized as a name of a cmdlet, function, script file, or executable program._x000D__x000A_</S><S S="Error">Check the spelling of the name, or if a path was included, verify that the path is correct and try again._x000D__x000A_</S></Objs> ("exited abnormally with code 1")
At least we tried :)
What are your thoughts on starting a cider-nrepl server from the terminal, and then cider-connect
ing to it from Emacs?
i will use powershell for now , just a weird bug, might be a bug in pwsh who knows :)
well hopefully in the future clojure cli will become stand alone, and maybe this will help by pass the powershell / pwsh quoting issues
Alright, happy that you have an alternative and that we documented some attempts!
Do you think , we should keep this issue open, for future attempters
and for extra documentation
I am using
PS C:\> $psversionTable
Name Value
---- -----
PSVersion 7.4.0
PSEdition Core
GitCommitId 7.4.0
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Generally we're not keen on having open issues that we don't plan to work on - it keeps the expectations aligned.
@ikappaki might be interested in going over this thread.
Sorry to coming back to this but
Now this is the command cider runs
[nREPL] Starting server via "c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" -encodedCommand YwBsAG8AagB1AHIAZQAgAC0AUwBkAGUAcABzACAAJwB7ADoAZABlAHAAcwAgAHsAbgByAGUAcABsAC8AbgByAGUAcABsACAAewA6AG0AdgBuAC8AdgBlAHIAcwBpAG8AbgAgACIAIgAxAC4AMAAuADAAIgAiAH0AIABjAGkAZABlAHIALwBjAGkAZABlAHIALQBuAHIAZQBwAGwAIAB7ADoAbQB2AG4ALwB2AGUAcgBzAGkAbwBuACAAIgAiADAALgA0ADMALgAzACIAIgB9AH0AIAA6AGEAbABpAGEAcwBlAHMAIAB7ADoAYwBpAGQAZQByAC8AbgByAGUAcABsACAAewA6AG0AYQBpAG4ALQBvAHAAdABzACAAWwAiACIALQBtACIAIgAgACIAIgBuAHIAZQBwAGwALgBjAG0AZABsAGkAbgBlACIAIgAgACIAIgAtAC0AbQBpAGQAZABsAGUAdwBhAHIAZQAiACIAIAAiACIAWwBjAGkAZABlAHIALgBuAHIAZQBwAGwALwBjAGkAZABlAHIALQBtAGkAZABkAGwAZQB3AGEAcgBlAF0AIgAiAF0AfQB9AH0AJwAgAC0ATQA6AGMAaQBkAGUAcgAvAG4AcgBlAHAAbAA=
Kinda late , but better late then never
I decoded this command
$encodedCommand = 'YwBsAG8AagB1AHIAZQAgAC0AUwBkAGUAcABzACAAJwB7ADoAZABlAHAAcwAgAHsAbgByAGUAcABsAC8AbgByAGUAcABsACAAewA6AG0AdgBuAC8AdgBlAHIAcwBpAG8AbgAgACIAIgAxAC4AMAAuADAAIgAiAH0AIABjAGkAZABlAHIALwBjAGkAZABlAHIALQBuAHIAZQBwAGwAIAB7ADoAbQB2AG4ALwB2AGUAcgBzAGkAbwBuACAAIgAiADAALgA0ADMALgAzACIAIgB9AH0AIAA6AGEAbABpAGEAcwBlAHMAIAB7ADoAYwBpAGQAZQByAC8AbgByAGUAcABsACAAewA6AG0AYQBpAG4ALQBvAHAAdABzACAAWwAiACIALQBtACIAIgAgACIAIgBuAHIAZQBwAGwALgBjAG0AZABsAGkAbgBlACIAIgAgACIAIgAtAC0AbQBpAGQAZABsAGUAdwBhAHIAZQAiACIAIAAiACIAWwBjAGkAZABlAHIALgBuAHIAZQBwAGwALwBjAGkAZABlAHIALQBtAGkAZABkAGwAZQB3AGEAcgBlAF0AIgAiAF0AfQB9AH0AJwAgAC0ATQA6AGMAaQBkAGUAcgAvAG4AcgBlAHAAbAA='
$commandBytes = [System.Convert]::FromBase64String($encodedCommand)
$decodedCommand = [System.Text.Encoding]::Unicode.GetString($commandBytes)
$decodedCommand have this string in it let call this Command1
clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version ""1.0.0""} cider/cider-nrepl {:mvn/version ""0.43.3""}} :aliases {:cider/nrepl {:main-opts [""-m"" ""nrepl.cmdline"" ""--middleware"" ""[cider.nrepl/cider-middleware]""]}}}' -M:cider/nrepl
This command Command1, raise en error when run by pwsh, but run successfully when run by powershell. pwsh being the new portable dotnet powershell and powerhsell being the older 5,1 windows only powershell
This command, lets call it Command2, run successfully on pwsh but fails with an error on powershell (i removed the double double quotes
clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version "1.0.0"} cider/cider-nrepl {:mvn/version "0.43.3"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[cider.nrepl/cider-middleware]"]}}}' -M:cider/nrepl
So for now at least we cannot treat pwsh and powershell as equal, while i cant find a nicer fix, or any documentation of these differences, i think cider should have two different commands prepared one for pwsh with a single double quote and the old or current command can remain to be used by powershell
Thanks much for persevering!
If the quotations were problematic, why did our attempt of skippking them not improve things?
Feel free to try it again, with variations. You could create a PR out of it or simply let me know what to change.
The decoded command from that fix looks like this
clojure -Sdeps {:deps {nrepl/nrepl {:mvn/version "1.0.0"} cider/cider-nrepl {:mvn/version "0.43.3"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[cider.nrepl/cider-middleware]"]}}} -M:cider/nrepl
the pwsh command that you need to produce looks like this
clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version "1.0.0"} cider/cider-nrepl {:mvn/version "0.43.3"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[cider.nrepl/cider-middleware]"]}}}' -M:cider/nrepl
I reviewed your code to the best of my abilities, i dont see why it strips the single '
quotes around the first argument
I found this issue online, might be related and might be a pwsh bug removing the signle quotation , but not sure yet
https://stackoverflow.com/questions/59036580/pwsh-command-is-removing-quotation-marks
This works, I am not an elisp coder, so this is not necessarily the best code, but it works
(defun cider--jack-in-cmd-pwsh-p (command)
"Returns whether COMMAND is pwsh."
(string-equal command "pwsh"))
(defun cider--jack-in-cmd-powershell-p (command)
"Returns whether COMMAND is Powershell."
(string-equal command "powershell"))
(defun cider--shell-quote-argument (argument &optional command)
"Patch to skip the quoting on windows"
(cond ((cider--jack-in-cmd-pwsh-p command) (format "'%s'" argument))
((cider--jack-in-cmd-powershell-p command) (format "'%s'" (replace-regexp-in-string "\"" "\"\"" argument)))
(t (shell-quote-argument argument))))
(defun cider--format-cmd (command-resolved command cmd-params)
"Format COMMAND-RESOLVED or COMMAND followed by CMD-PARAMS."
(format "%s %s" command-resolved
(if (or (cider--jack-in-cmd-pwsh-p command) (cider--jack-in-cmd-powershell-p command))
(cider--powershell-encode-command cmd-params)
cmd-params)))
Proof
;; Startup: "c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" -encodedCommand YwBsAG8AagB1AHIAZQAgAC0AUwBkAGUAcABzACAAJwB7ADoAZABlAHAAcwAgAHsAbgByAGUAcABsAC8AbgByAGUAcABsACAAewA6AG0AdgBuAC8AdgBlAHIAcwBpAG8AbgAgACIAIgAxAC4AMAAuADAAIgAiAH0AIABjAGkAZABlAHIALwBjAGkAZABlAHIALQBuAHIAZQBwAGwAIAB7ADoAbQB2AG4ALwB2AGUAcgBzAGkAbwBuACAAIgAiADAALgA0ADQALgAwACIAIgB9AH0AIAA6AGEAbABpAGEAcwBlAHMAIAB7ADoAYwBpAGQAZQByAC8AbgByAGUAcABsACAAewA6AG0AYQBpAG4ALQBvAHAAdABzACAAWwAiACIALQBtACIAIgAgACIAIgBuAHIAZQBwAGwALgBjAG0AZABsAGkAbgBlACIAIgAgACIAIgAtAC0AbQBpAGQAZABsAGUAdwBhAHIAZQAiACIAIAAiACIAWwBjAGkAZABlAHIALgBuAHIAZQBwAGwALwBjAGkAZABlAHIALQBtAGkAZABkAGwAZQB3AGEAcgBlAF0AIgAiAF0AfQB9AH0AJwAgAC0ATQA6AGMAaQBkAGUAcgAvAG4AcgBlAHAAbAA=
user>
;; Startup: "c:/Program Files/PowerShell/7/pwsh.exe" -encodedCommand YwBsAG8AagB1AHIAZQAgAC0AUwBkAGUAcABzACAAJwB7ADoAZABlAHAAcwAgAHsAbgByAGUAcABsAC8AbgByAGUAcABsACAAewA6AG0AdgBuAC8AdgBlAHIAcwBpAG8AbgAgACIAMQAuADAALgAwACIAfQAgAGMAaQBkAGUAcgAvAGMAaQBkAGUAcgAtAG4AcgBlAHAAbAAgAHsAOgBtAHYAbgAvAHYAZQByAHMAaQBvAG4AIAAiADAALgA0ADQALgAwACIAfQB9ACAAOgBhAGwAaQBhAHMAZQBzACAAewA6AGMAaQBkAGUAcgAvAG4AcgBlAHAAbAAgAHsAOgBtAGEAaQBuAC0AbwBwAHQAcwAgAFsAIgAtAG0AIgAgACIAbgByAGUAcABsAC4AYwBtAGQAbABpAG4AZQAiACAAIgAtAC0AbQBpAGQAZABsAGUAdwBhAHIAZQAiACAAIgBbAGMAaQBkAGUAcgAuAG4AcgBlAHAAbAAvAGMAaQBkAGUAcgAtAG0AaQBkAGQAbABlAHcAYQByAGUAXQAiAF0AfQB9AH0AJwAgAC0ATQA6AGMAaQBkAGUAcgAvAG4AcgBlAHAAbAA=
user>
This is my first pull request ever, i feel afraid