External Julia REPL in VSCode

Someone recently asked in Slack if it was possible to to launch the Julia REPL process in a separate window (i.e. a separate terminal) and have that hooked up with the Julia extension such that you can send code to it from the editor. This can be pretty useful, for example, if you have a second monitor where you want to keep the REPL and thus make it possible to maximize the editor window on the first monitor.

I figured out a way of doing it and just wanted to share this since it might be useful for others. It might have to be tailored a bit for other terminals/systems, so perhaps that can be crowd sourced in the comments. I used Ubuntu with the default GNOME Terminal.

Anyway, here is what I did; Create a wrapper script (e.g. julia-wrapper-external-repl) with the following content:

#!/bin/bash

# Wrapper script for launching an external Julia REPL in 
# VSCode using the Julia extension.
# Configured for Linux (Ubuntu) with the default GNOME Terminal

## Path to julia executable
JULIA=/opt/julia/julia-1.5/bin/julia

if [ -z "${JULIA_LANGUAGESERVER}" ]; then
    ## User-process; launch a new terminal window and exec julia
    cmd="${JULIA} "$@""
    exec gnome-terminal --disable-factory -e "$cmd"
else
    ## Language server process; exec julia
    exec ${JULIA} "$@"
fi

and make it executable. Next, point the Julia: Executable Path extension setting to this script and voilà!

This works by inspecting the JULIA_LANGUAGESERVER variable, which is only set when the Julia extension is launching its Language Server process. If this variable is empty we launch an external terminal window and start julia in it, and if it is set we exec normally.

Hope some of you find it useful!

26 Likes

Thank you for this great work, good idea indeed! – I was looking for something similar to find out why vscode-julia doesn’t work with intel-mkl compiled julia binary – which seems to be rooted in how MKL manages shared objects.

Used your idea to preload/source the Intel MKL scripts, below is the modified copy. The first julia start flashes a terminal up super fast and fails (can’t read error, too fast for my eyes…), then the second try, same session, starts the terminal with julia (stays on screen, fully functional prompt) then each line is reflected in the vscode terminal (not in the julia one) and nothing else happens.

Is there a way to slow/pause the startup process – so I could read the error message? or an idea how to capture/redirect error message – perhaps with tmux?

best wishes: steven

#!/bin/bash

# Wrapper script for launching an external Julia REPL in
# VSCode using the Julia extension.
# Configured for Linux (Ubuntu) with the default GNOME Terminal

source /opt/intel/bin/compilervars.sh intel64
source /opt/intel/parallel_studio_xe_2020/bin/psxevars.sh &> /dev/null
## Path to julia executable
JULIA=/usr/local/bin/julia

if [ -z "${JULIA_LANGUAGESERVER}" ]; then
    ## User-process; launch a new terminal window and exec julia
    cmd="${JULIA} "$@""
    exec mate-terminal --disable-factory -e "$cmd"
else
    ## Language server process; exec julia
    exec ${JULIA} "$@"
fi

The error is just us starting (what we think is) Julia once to get version info and verify at actually works.

Don’t try to evaluate code with Ctrl-Enter (default), use Alt-Enter instead. The former relies on the VSCode APIs to insert text into the REPL, while the latter sends the code you want to execute over a pipe.

4 Likes

Has anyone managed to get this to work on Windows.

I have tried something like

@echo off
set JULIA=C:\Tools\Julia\julia-latest\bin\julia.exe
setlocal enableDelayedExpansion

IF "%JULIA_LANGUAGESERVER%"=="" (start wt %JULIA% %*) ELSE (start %JULIA% %*)
2 Likes

I am trying to get this working on my system, and it appears as though JULIA_LANGUAGESERVER is not reliably set. In my wrapper, the first thing I do is printenv > /Users/darsnack/julialog. Sometimes, the log is full of environment variables, and it is clear that the process is being launch under my user with my full shell environment + rc (zsh). Notably, JULIA_LANGUAGESERVER is unset, and things try to launch in a new terminal window with failed results. Other times, the log is only a few lines long, but JULIA_LANGUAGESERVER is set and everything starts up fine.

Weirdly, I can open up two totally separate repos at the same time in separate VS Code windows, and I get different results for each one.

I use WSL 2 with the Vscode extension. I’d love it if someone can make it work for WSL2. Technically the julia processes are being launched within the Linux OS, but the terminal is Windows Terminal, so I don’t know whether I need a bash script or a cmd script.

1 Like

Any update on making this work on Windows?

1 Like

This is so smart! I can now use remote development with julia-vscode on MKL+1.7 and still have a fully functional Julia Language Server! Thanks a lot.

I think the latest version of the Julia extension has native support for this so this hack isn’t necessary anymore IIUC.

Oh, really? It was @pfitzseb himself (thanks!) that pointed me to this idea of yours on Slack. I was asking him how one could use julia 1.7 with the current language server, that only works under 1.6 AFAIU, and he mentioned this. I’m using v1.2.3 of the julia-vscode extension. Perhaps you mean the master version currently in development?

Ah sorry, I thought you were talking about an external REPL.

My mistake, I didn’t specify the problem. Indeed, it would perhaps be nice to have two "executable path"s in the future, one for the language server and another for the runtime.