Unable to run or debug when using VSCode

Using:
Windows 11
Julia 1.11.4
Visual Studio Code 1.98.2
julialang extension 1.127.2

I get an error message whenever I try to run any code from VSCode if it contains a user-defined function. Here’s an example I threw together:

println(x1,x2)

function quadratic_formula(a, b, c)
	discrim = b^2 - 4.0*a*c
	if discrim >= 0.0
		x1 = (-b + sqrt(discrim))/(2.0*a)
		x2 = (-b - sqrt(discrim))/(2.0*a)
	else
		return nothing
	end
	return (x1, x2)
end```

Using CTRL-F5 gives this error message:

ERROR: LoadError: UndefVarError: `quadratic_formula` not defined in `Main`
Stacktrace:
 [1] top-level scope
   @ c:\Users\xxxxx\OneDrive\Documents\xxxxx\Examples\quadratic.jl:1
 [2] include(fname::String)
   @ Main .\sysimg.jl:38
 [3] run(debug_session::VSCodeDebugger.DebugAdapter.DebugSession, error_handler::VSCodeDebugger.var"#3#4"{String})
   @ VSCodeDebugger.DebugAdapter c:\Users\xxxxx\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\DebugAdapter\src\packagedef.jl:122
 [4] startdebugger()
   @ VSCodeDebugger c:\Users\xxxxx\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeDebugger\src\VSCodeDebugger.jl:45
 [5] top-level scope
   @ c:\Users\xxxxx\.vscode\extensions\julialang.language-julia-1.127.2\scripts\debugger\run_debugger.jl:12
 [6] include(mod::Module, _path::String)
   @ Base .\Base.jl:557
 [7] exec_options(opts::Base.JLOptions)
   @ Base .\client.jl:323
 [8] _start()
   @ Base .\client.jl:531
in expression starting at c:\Users\xxxxx\OneDrive\Documents\xxxxx\Examples\quadratic.jl:1

Using F5 gives this message:

Exception has occurred: UndefVarError
UndefVarError: `quadratic_formula` not defined in `Main`

Stacktrace:
 [1] top-level scope
   @ c:\Users\jbmcv\OneDrive\Documents\MasterPC\Examples\quadratic.jl:1

I'm able to evaluate the quadratic_formula function by putting my cursor on the final line and executing "Shift Enter", but this doesn't fix the issue. I've searched the available resources and can't find anything that seems helpful. I'm in the process of porting an old Fortran 77 code of mine and will need to be able to run it in the debugger. Any help would be appreciated!

The message “xxx is not defined in Main” means that Julia does not see the function.

You need to compile it first. You didn’t provide a full Minimal working example (MWE) with your full code but try the following:

  • Put the function in a file, e.g. myfuncs.jl
  • in another file, write include("myfuncs.jl"). Depending on where you put the file you might need to add the path, e.g. If it is in a subfolder “functions” it should be include("functions/myfuncs.jl")
  • Evaluate it/Send it to the REPL. You should see first a round arrow while julia compiles the function. If there is no error in your function you will see a tick mark on vs code on the line

Now julia should sees the function and you can call it.

This workflow is a bit dependent on how exactly you use VS code.

Consider using Revise if you want to change the function, then you use includet() instead of include()

Boris, thank you so much for taking the time to respond to my issue. The fix turned out to be something very simple. I had been putting my calls to the Main function at the very beginning of my files. My previous experience has been primarily in Matlab, Fortran, and Basic, where that is a natural thing to do. Following the project structure suggested in:

seems to enable the debugger to function properly in VSCode. Again, thank you for your time.