# Redefining modules in VS Code

**URL:** <https://discourse.julialang.org/t/redefining-modules-in-vs-code/49978>\
**Category:** VS Code\
**Created:** [November 11, 2020, 12:39pm UTC](https://discourse.julialang.org/t/redefining-modules-in-vs-code/49978 "2020-11-11T12:39:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [November 11, 2020, 12:39pm UTC](https://discourse.julialang.org/t/redefining-modules-in-vs-code/49978/1 "2020-11-11T12:39:15Z")

</div>

I have a tiny file defining a module:

```julia
module A
using Dates: now
println("Module $(@ __MODULE__ ) defined at $(now())")
end

```

If I do `Julia: Execute File` three times, the REPL output is like so:

```julia
Module Main.A defined at 2020-11-11T07:32:24.427
Main.A

WARNING: replacing module A.
Module Main.A.A defined at 2020-11-11T07:32:30.564
Main.A.A

WARNING: replacing module A.
Module Main.A.A defined at 2020-11-11T07:35:32.017
Main.A.A

```

I find this really confusing! Several questions:

1. On the second execution of the file, why is module `Main.A.A` defined, rather than `Main.A` **re** -defined?
2. Given that `Main.A.A` is defined, why is there _also_ a warning about replacing module `A`? Isn’t `Main.A` left intact?
3. Why does the third execution of the file not define `Main.A.A.A`?

Very frequently I’ll execute a file which defines module `A`, do some interactive work in the REPL, modify the file, re-execute, and find that for continued REPL work with the updated file, I actually need `A_for_real = Main.A.A`. Which always surprises me, would love to learn why this happens.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 11, 2020, 3:08pm UTC](https://discourse.julialang.org/t/redefining-modules-in-vs-code/49978/2 "2020-11-11T15:08:25Z")

</div>

That’s an unfortunate bug. We determine the module we’re going to evaluate the file in by looking at the `(0,1)` position in the text document (which is a hacky fix for [other](https://github.com/julia-vscode/LanguageServer.jl/issues/771) [issues](https://github.com/julia-vscode/julia-vscode/issues/1500)), so in this case

```julia
m|odule A

```

This obviously returns `A` as the containing module, instead of e.g. `Main`. The first time around `A` doesn’t exist, so we fall back to `Main`. The second time (and every time after that) we just replace `A.A`. AFAICT looking at `(0,0)` instead has the same issue.

A quick hack around this is to insert _something_ before the `module` statement, e.g. a new line or a comment or whatever.  
I’ll look into fixing that soon(-ish) though.
