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

REPL shell mode for Windows #33427

Closed
wants to merge 4 commits into from
Closed
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
24 changes: 20 additions & 4 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ stackframe_lineinfo_color() = repl_color("JULIA_STACKFRAME_LINEINFO_COLOR", :bol
stackframe_function_color() = repl_color("JULIA_STACKFRAME_FUNCTION_COLOR", :bold)

function repl_cmd(cmd, out)
shell = shell_split(get(ENV, "JULIA_SHELL", get(ENV, "SHELL", "/bin/sh")))
shell = shell_split(get(ENV, "JULIA_SHELL", Sys.iswindows() ? "cmd" : get(ENV, "SHELL", "/bin/sh")))
shell_name = Base.basename(shell[1])
if Sys.iswindows()
shell_name = lowercase(splitext(shell_name)[1]) # canonicalize for comparisons below
end

# Immediately expand all arguments, so that typing e.g. ~/bin/foo works.
cmd.exec .= expanduser.(cmd.exec)
Expand Down Expand Up @@ -66,15 +69,28 @@ function repl_cmd(cmd, out)
ENV["OLDPWD"] = new_oldpwd
println(out, pwd())
else
@static if !Sys.iswindows()
local command::Cmd
if Sys.iswindows()
if shell_name == ""
command = cmd
elseif shell_name == "cmd"
command = Cmd(`$shell /s /c $(string('"', join(cmd.exec, ' '), '"'))`, windows_verbatim=true)
elseif shell_name == "powershell" || shell_name == "pwsh"
command = Cmd(`$shell -Command $(join(cmd.exec, ' '))`, windows_verbatim=true)
elseif shell_name == "busybox"
command = `$shell sh -c $(shell_escape_posixly(cmd))`
else
command = `$shell $cmd`
end
else
if shell_name == "fish"
shell_escape_cmd = "begin; $(shell_escape_posixly(cmd)); and true; end"
else
shell_escape_cmd = "($(shell_escape_posixly(cmd))) && true"
end
cmd = `$shell -c $shell_escape_cmd`
command = `$shell -c $shell_escape_cmd`
end
run(ignorestatus(cmd))
run(ignorestatus(command))
end
nothing
end
Expand Down
7 changes: 3 additions & 4 deletions doc/src/manual/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,9 @@ The absolute path of the shell with which Julia should execute external commands
(via `Base.repl_cmd()`). Defaults to the environment variable `$SHELL`, and
falls back to `/bin/sh` if `$SHELL` is unset.

!!! note

On Windows, this environment variable is ignored, and external commands are
executed directly.
On Windows, `$JULIA_SHELL` can be set to `cmd`, `powershell`, `busybox` or `""`.
If set to `""` external commands are executed directly. Defaults to `cmd` if
`$JULIA_SHELL` is not set.

### `JULIA_EDITOR`

Expand Down
1 change: 1 addition & 0 deletions stdlib/REPL/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ julia> ; # upon typing ;, the prompt changes (in place) to: shell>
shell> echo hello
hello
```
See `JULIA_SHELL` in the Environment Variables section of the Julia manual.

### Search modes

Expand Down