@from (from Fromfile) does not work inside begin-en for local module workflow

I have been trying to get my “local modules” workflow from previous {either include or push loadpath} to using excellent @Roger-luo FromFile. But the case I was using it get me trouble, specifically it fails when included inside begin ...end

For an MWE : I have following file tmod.jl

module tmod
function hello()
    println("hello from tmod")
function __init__()

If, in REPL, doing

using FromFile
@from "tmod.jl" using tmod

it works, if doing

using FromFile
@from "tmod.jl" using tmod

I get

ERROR: LoadError: UndefVarError: @from not defined
in expression starting at REPL[1]:3

Why is that ?
For the record, the two others way of doing it

if !@isdefined tmod; include("tmod.jl"); using .tmod; end


using tmod

when used inside begin/end do not suffer with the same trouble.
So, why is that the @from does not work when is same block as using FromFile?


  • Include solution seems a bit “tormented” for me, and puts the module in the Main namespace while the two other ones put it in the Global namespace IIUC.

  • LOAD_PATH manipulation makes me fears that it is not really “Julia-esque” and may be could fail for later Julia versions - may be I should not worry ? Also, will vs-code referencing work with this way of doing things ? (because of the pop of loadpath makes it loose the path to the module?)

  • This is an aside question but not some kind of XY problem, I was trying to get timing info for my "using"s, by way of TimerOutputs, so put those usings in a begin/end block for that. Then it failed ! I know I can can put the using FromFileat the beginning of the file and then it should work, but I would like to understand why when this using and the@from are in the same block, it does not work.

Thanks for comment about my question, and possibly, also, more generally, about best/better local module workflow)

hmm…this does not seems to be a FromFile issue, but a Julia issue

julia> begin
       using LinearAlgebra
ERROR: LoadError: UndefVarError: LinearAlgebra not defined
in expression starting at REPL[1]:3

I created an issue here: https://github.com/JuliaLang/julia/issues/44104

as pointed out in the issue

Those two aren’t equivalent. Macro expansion will always happen on the whole top-level expression before any code is evaluated.

so this is the limitation of FromFile we will need a proper implementation within the loading
to have the behavior properly, which is unlikely to happen any time soon. Unless someone can convince the rest of the community about it.

I assume you are using Pluto which cause you hit this? I’m not sure if @fonsp has any idea of a workaround in Pluto, perhaps no.

Thanks for your effort. I understand that the question here is a general issue with macro parsing, not specific to FromFile. So I will keep that in mind (I have learn something!) for later.

Regarding your question, no I am not using Pluto for this specific case. But I agree that it could be, as Pluto is a wonderful product! And so your yours. Cheers!