Smoothest method to iterate on individual lines of a package?

Continuing the discussion from Executing code blocks with REPL workspace:

I think the main workflow I am trying to understand/improve is a way to test and iterate on individual lines in a function in a package without having to copy-paste and without having to change the package code in a way that would need to be undone after testing (e.g. adding debug lines or changing function argument/returns).

MyPackage.jl MWE:

module MyPackage

using Statistics

function f(x, y)
    v = 1:x
    v = v .+ y
    z = mean(v)
    return z
end

end

The problem seems to be inconsistent behavior of Julia: Execute Code in REPL (and Julia: Send Current Line or Selection to REPL) in VSCode.

If Execute Code in REPL decides to execute in MyPackage module, then anything typed into the REPL will not be in the namespace, causing confusing errors.

julia> using MyPackage  # typed

julia> x = 3  # typed
3

julia>     v = 1:x  # sent
ERROR: UndefVarError: `x` not defined

julia> f(3, 1)  # typed
3.0

(You can force typed code to execute in MyPackage with REPL.activate(MyPackage) to fix the above namespacing issue.)

If Execute Code in REPL decides to execute in Main module, then variables will be in scope, but unexported functions and types will not be. You also have to remember to using all your dependencies.

julia> using MyPackage  # typed

julia> x = 3  # typed
3

julia>     v = 1:x  # sent
1:3

julia>     z = mean(v)  # sent
ERROR: UndefVarError: `mean` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

julia> using Statistics  # sent

julia>     z = mean(v)  # sent
2.0

julia> f(3, 1)  # typed
ERROR: UndefVarError: `f` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> MyPackage.f(3,1)  # typed
3.0

(You can force package code to be executed in Main by copying and pasting to the REPL instead of using one of the VSCode commands.)

The most confusing part though is that I don’t know which behavior I am going to get when I Julia: Execute Code in REPL. When creating this MWE, I got the Main module execution behavior, even though VSCode is indicating that the active environment and module are MyPackage.
image
image
OP of the linked posted above got the opposite behavior. I created a Github Issue asking to make the VSCode behavior more consistent. I have a lot of workarounds in my toolbelt now, but I am still confused on how this is intended to behave.


Infiltrator is a very useful tool, but it is difficult to use if it isn’t already a dependency of your package. You can’t just use the @infiltrate macro as advertised even if it is in your default environment. The longer versions are a lot of text to add a remove.

I don’t quite understand how this would be used. How is copying and pasting code to here to then Ctrl+Enter better than copying and pasting it directly to the REPL?

1 Like