Stream: helpdesk (published)

Topic: Force macro re-expansion of `includet`'d file?


view this post on Zulip Mason Protter (Jul 30 2025 at 05:56):

Is there any way to make Revise.jl force the re-expansion of macros in tracked files? For example, if I have a file foo.jl with was includet'd:

bar() = @__LINE__

and I evaluate bar() I'd get foo.jl:1, but if I then edit the file so that bar is on a different line, i.e.

# some comment
bar() = @__LINE__

then those changes are not reflected in the result of bar(), it'll still return 1 instead of 2.

It seems that I can force Revise to re-expand the macro if bar is inside a package and I do revise(Mod::Module; force=true), but that requires Mod to be loaded through Pkg, it won't work with e.g. Main.

view this post on Zulip Daniel Wennberg (Jul 30 2025 at 07:00):

Does it work if you define __revise_mode__ = :eval before calling includet? https://timholy.github.io/Revise.jl/stable/config/#Configuring-the-revise-mode

view this post on Zulip Mason Protter (Jul 30 2025 at 18:00):

Unfortunately not, but I managed to hack my away around it using some Revise internals reverse engineered from Revise.revise (not super happy with that but oh well for now). What I did was

const revision_keys = Dict{String, Symbol}()

function add_force_revise_callback(file::AbstractString, mod::Module)
    key = get!(revision_keys, file) do
        gensym(file)
    end
    Revise.add_callback((file,); key) do
        force_revise_cells(file, mod)
    end
end
add_force_revise_callback(::Nothing, ::Module) = nothing

function force_revise_cells(file::AbstractString, mod::Module)
    for (mod, exsigs) in Revise.parse_source(file, mod)
        for def in keys(exsigs)
            ex = def.ex
            exuw = Revise.unwrap(ex)
            if Base.isexpr(exuw, :macrocall) && exuw.args[1] == Symbol("@cell")
                Core.eval(mod, ex)
            end
        end
    end
end

which means that all my @cell macros will get re-evaled whenever the specified file is updated.


Last updated: Aug 14 2025 at 04:51 UTC