Inconsistent UndefVarError

Hello everyone. I have a Julia project I have been working with in VSCode and it has been running just fine. I closed VSCode, reopened the project, and tried to run one of my files without making any changes and the following error came up: “UndefVarError: CSRMatrix not defined.” CSRMatrix is a file in the same directory of the file I tried to run. This problem has come up before, and nearly every time it is a different thing that is not defined (i.e., it is commonly “UndefVarError: Comm not defined” and Comm is another file in this same directory. This error seems to be very inconsistent so I wonder if there is something wrong with Julia or if I need some sort of update? I’m not sure what is wrong but I would appreciate any insight.

Thank you!

To me that sounds like the order in which you have included files is not correct, but once the project is running you are running or including those and they are available.

Make sure you have no circular dependencies between files or modules, like file_a.jl needing a struct defined in file_b.jl and being included at a later point.

Okay, but the files actually include each other? How is your project structured? Julia do not simply look for files with the same name as modules in the working folder when it does not find something.

The files do not all include one another. The thing I am struggling with is how everything ran perfectly before I closed and reopened VSCode, but when I did so, it stopped working.

In the project, there is a src folder and a tests folder. I am working in the src folder, trying to run a file to test out constructing and running functions on objects of types that are constructed in files in the src folder. Does that make sense?

Not much to be honest.

If you have a project that have a src and a tests, are you sure you do not have a Package (instead of just a project)?

Why are you doing tests in the src folder and not the tests folder?

I do not know how VSCode works (I do not use it), but it should not replace Julia mechanisms for including/combining code. If it is a package then the main file (with the same name as the parent of the src folder) would probable include all the other files inside it by means of include (and all these files could be oblivious to each other).

Yes you are right, I am working with a Package. And yes, I see how the main file includes all the other files. Does that mean I do or do not need to include all of the other ones within each other?

I am not running actual @test tests, rather just getting used to the objects in this project by constructing them, etc. I am new to the project, most of it was written by someone else and now I am continuing their work.

Additionally, I have tried to do the following commands:
import Pkg
Pkg.add(“Comm”) -->Comm is the object type it says is not defined and is the name of a file I have
To that, I get the following error: "The following package names could not be resolved:

  • Comm (not found in project or manifest)"

Any suggestions for this/does this help at all?

It would help to have a clear view of your package structure. Is it something like the following?

MyPackage
├── src
│   ├── MyPackage.jl
│   └── ...
├── test
│   ├── runtests.jl
│   └── ...
├── Project.toml
└── ...

and if so, what is the name of MyPackage? (in particular, is it Comm as I think I understand?).
The fact that something works within VSCode but not outside looks like an environment issue. In particular, when you open the Julia REPL in VSCode (in the setup that works), what do you see if you simply type the ] character at the beginning of a line? The prompt should change to either something like (@v1.7) pkg> where “1.7” is your julia version, or it can be (MyPackage) pkg> with “MyPackage” being the package under development.
If it’s the former, I’m not sure what’s happening, so I would need the name of MyPackage to understand better. If it’s the latter, then, when you are not in VSCode, try adding --project=/path/to/MyPackage to your julia command when launching julia and check if that works: if so, you can use the --project flag each time. Alternatively, without the --project flag, launch julia as you would usually do, then type the ] character to enter pkg> mode and enter dev /path/to/MyPackage (you only have to do this once).

Here is what the structure looks like:

JuliaPetra
├── src
│ ├── JuliaPetra.jl
│ └── main.jl
│ └── Comm.jl
│ └── …
├── test
│ ├── runtests.jl
│ └── …
├── Project.toml
└── …

main.jl is the file I am running some code with that is producing the error. My Julia env is JuliaPetra.jl

OK! So, for context, what happens when you load your package, i.e. when you do using JuliaPetra is that the JuliaPetra.jl file is executed, and that’s basically it. Looking at this file, it includes a bunch of other files (that’s the typical organization for a big package) so you probably want to include them as well in your main.jl file, especially Comm.jl since it includes the definition of the Comm type, without which there is no way julia can guess what Comm refers to (hence the UndefVarError).

Now, since it looks like you want to explore the JuliaPetra package, what I think should be easier is to move main.jl somewhere else, out of the JuliaPetra directory, and just write using JuliaPetra somewhere at the top: that way, everything exported by JuliaPetra will be imported in your main.jl script, and you can use them all for your experiments. You don’t need to do any other include(...).
If you want to modify the files in the JuliaPetra package and see the effect in your main.jl script, note that you will have to do pkg> dev /your/path/to/JuliaPetra in your REPL (just once) so that julia knows that the JuliaPetra package it must look at is not the registered JuliaPetra but the local one on your computer.

1 Like

Do you have it in a public repro somewhere.

That would make it much easier to help

I assumed it was GitHub - Collegeville/JuliaPetra.jl: An implementation of the Petra Object Model in Julia but correct me if I’m wrong.

By the way, if you are not familiar with modules in Julia, I recommend reading the documentation: Modules · The Julia Language, it could help as well.

thank you for the help!