Problems with module redefinition from vscode

Module redefinition leads to inconsistent behaviour, at least when run in code blocks with Shift+Enter from vscode. Can anyone confirm this? I made a file and run it in cycle from top to bottom: #1, #2, THEN AGAIN #1:

#1
module Module1
    mutable struct Foo
        x::Int
    end
end # module

using .Module1
foo(x::Module1.Foo) = x.x
x1 = Module1.Foo(1)
foo(x1)

#2
module Module1
    mutable struct Foo
        s::String
    end
end # module

using .Module1
foo(x::Module1.Foo) = x.s
y1 = Module1.Foo("hi") # error
foo(y1)

At second run of #1 I get error:

ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String

Closest candidates are:
  convert(::Type{String}, ::String)
   @ Base essentials.jl:298
  convert(::Type{T}, ::T) where T<:AbstractString
   @ Base strings\basic.jl:231
  convert(::Type{T}, ::AbstractString) where T<:AbstractString
   @ Base strings\basic.jl:232
  ...

Stacktrace:
 [1] Main.Module1.Foo(s::Int64)
   @ Main.Module1 c:\Users\gvg\YandexDisk\my_course\course-2023-1\wtf.jl:16
 [2] top-level scope
   @ c:\Users\gvg\YandexDisk\my_course\course-2023-1\wtf.jl:10

Meanwhile I never get the same error if just copy-paste these code blocks in the same order in julia REPL. What is going on here? What is the difference between running from vscode and REPL?

Interestingly, if I copy-paste #1 after #2 into #3 and run in cycle: #1, #2, #3, the problem disappears at some point. But I don’t understand the particular order, so it is just confusing to have the same error appearing and disappearing from time to time, depending on file line for the same code…

Tried this with Julia 1.9.0-rc1 and 1.8.5 on Windows 10 and 11, with vscode Julia plugin both release and pre-releave versions

UPDATE: Record

vscode_bug

The issue here is your cursor position when evaluating the first module definition. You can see that it’s getting defined as Main.Module1.Module1, which means that the module isn’t being overwritten as intended.

AFAICT this happens only if the cursor is on the last column of the module Module1 line – you can see what module the current code block will be evaluated into on the far right side of the status bar.

Ouch!

I’ve done this for quite a time and it confused me a lot, and it made me think there are some problems on the Julia side with module redefinition. Didn’t ever thought that I can get different behaviour depending on the position of the cursor at the beginning or at the end of the line.

FWIW, this is just a bug in the extension, basically.

I would expect it always position cursor at the front of the line and run maybe a multiline block of code right behind.

The automatic cursor position (at the end of the block) is always correct, I think. You were just manually clicking on the end of the first line in the module definition.

But yeah, as I said, we should use the outer module here. It’s a fairly easy mistake to make, because technically the whitespace following the module Foo is inside of the module…