Redefining modules in VS Code

I have a tiny file defining a module:

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:

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.

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 issues), so in this case

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.

1 Like