# Smoothest method to iterate on individual lines of a package?

**URL:** <https://discourse.julialang.org/t/smoothest-method-to-iterate-on-individual-lines-of-a-package/107781>\
**Category:** VS Code\
**Tags:** package, workflow\
**Created:** [December 18, 2023, 5:56pm UTC](https://discourse.julialang.org/t/smoothest-method-to-iterate-on-individual-lines-of-a-package/107781 "2023-12-18T17:56:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [December 18, 2023, 5:56pm UTC](https://discourse.julialang.org/t/smoothest-method-to-iterate-on-individual-lines-of-a-package/107781/1 "2023-12-18T17:56:30Z")

</div>

Continuing the discussion from [Executing code blocks with REPL workspace](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/13):

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:

```julia
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-repl
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-repl
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](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b158fff4ffebaaf6b70835c052de2491aaab894.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/8/08769c18365bddc9f950fa0fcaef7014222d6274.png)  
OP of the linked posted above got the opposite behavior. I created a [Github Issue](https://github.com/julia-vscode/julia-vscode/issues/3492) 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.

* * *

> [@Executing code blocks with REPL workspace](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/13):
>
> There are also packages such as [GitHub - JuliaDebug/Infiltrator.jl: No-overhead breakpoints in Julia](https://github.com/JuliaDebug/Infiltrator.jl) that you let step into a local environment of a function.

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.

> [@Executing code blocks with REPL workspace](https://discourse.julialang.org/t/executing-code-blocks-with-repl-workspace/107572/14):
>
> A `main()` function inside the module which looks like
> 
> ```julia
> function main()
> @eval Main begin
> ...
> end
> end
> 
> ```

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?
