I’ve never worked out how to deal with the following situation where I’m developing a script which includes some other files in the REPL, but I also want the LSP to recognise any included functions.
Problem
.
├── scripts
│ └── example_script.jl
└── src
└── utils.jl
# scripts/example_script.jl
include("src/utils.jl")
example_function() # imported from utils.jl
# src/utils.jl
example_function() = println("Hello")
If from the working directory .
, I open up scripts/example_script.jl
and the REPL, I can send code using Shift+Enter and it works fine. But the LSP does not recognise any of the definitions in src/utils.jl
. I guess this is because when Julia runs a script all paths are treated as relative to the script’s own directory not the working directory that Julia is invoked from.
Solutions
- I can instead use
include("../src/utils.jl")
and change the REPL working directory to./scripts
. Is this what everyone else does? It is a little annoying because: (a) I always need to make sure the REPL working directory is the same as whatever script I’m working on; (b) if I move around/reorganize thescripts
directory then I need to change all the relative paths; (c) it’s just less clear to me to have potentially a few layers of../..
on all the paths than having all paths be relative to the main project directory. - I can do something like the following, but it seems a little hacky?
if isinteractive()
include("src/utils.jl")
else
include("../src/utils.jl")
end
- Turn
src/utils.jl
into a package andadd
ordev
it into the project environment. This seems overkill if the utilities are specific to this project and has the disadvantage of requiring a separate Git repo (I think?).
I’d be very grateful to hear what everyone else does! And sorry if this question has been asked before.