# Inconsistent UndefVarError

**URL:** <https://discourse.julialang.org/t/inconsistent-undefvarerror/83831>\
**Category:** New to Julia\
**Tags:** error\
**Created:** [July 6, 2022, 11:02am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831 "2022-07-06T11:02:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![fsmith001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fsmith001/32/37173_2.png) [@fsmith001](https://discourse.julialang.org/u/fsmith001)\
**Post date:** [July 6, 2022, 11:02am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/1 "2022-07-06T11:02:53Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Amval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amval/32/6217_2.png) [@Amval](https://discourse.julialang.org/u/Amval)\
**Post date:** [July 6, 2022, 11:08am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/2 "2022-07-06T11:08:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 6, 2022, 11:48am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/3 "2022-07-06T11:48:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fsmith001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fsmith001/32/37173_2.png) [@fsmith001](https://discourse.julialang.org/u/fsmith001)\
**Post date:** [July 6, 2022, 12:02pm UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/4 "2022-07-06T12:02:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 6, 2022, 2:17pm UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/5 "2022-07-06T14:17:04Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![fsmith001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fsmith001/32/37173_2.png) [@fsmith001](https://discourse.julialang.org/u/fsmith001)\
**Post date:** [July 7, 2022, 5:33am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/6 "2022-07-07T05:33:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fsmith001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fsmith001/32/37173_2.png) [@fsmith001](https://discourse.julialang.org/u/fsmith001)\
**Post date:** [July 7, 2022, 6:05am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/7 "2022-07-07T06:05:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [July 7, 2022, 8:11am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/8 "2022-07-07T08:11:00Z")

</div>

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

```julia
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).

---

<div class="post-metadata">

**Author:** ![fsmith001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fsmith001/32/37173_2.png) [@fsmith001](https://discourse.julialang.org/u/fsmith001)\
**Post date:** [July 7, 2022, 8:45am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/9 "2022-07-07T08:45:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [July 7, 2022, 9:00am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/10 "2022-07-07T09:00:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [July 7, 2022, 9:05am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/11 "2022-07-07T09:05:51Z")

</div>

Do you have it in a public repro somewhere.

That would make it much easier to help

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [July 7, 2022, 9:07am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/12 "2022-07-07T09:07:21Z")

</div>

I assumed it was [GitHub - Collegeville/JuliaPetra.jl: An implementation of the Petra Object Model in Julia](https://github.com/Collegeville/JuliaPetra.jl) 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](https://docs.julialang.org/en/v1/manual/modules/), it could help as well.

---

<div class="post-metadata">

**Author:** ![fsmith001](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fsmith001/32/37173_2.png) [@fsmith001](https://discourse.julialang.org/u/fsmith001)\
**Post date:** [July 7, 2022, 9:18am UTC](https://discourse.julialang.org/t/inconsistent-undefvarerror/83831/13 "2022-07-07T09:18:47Z")

</div>

thank you for the help!
