Is there a way to know if `Revise` is enabled?

Is it possible to know if when I’m executing some code it is being tracked by Revise.jl?

For example say I have a function where I want to do something different based on this condition e.g. in pseudocode:

function foo()
    if revise_enabled()
        do_1()
    else
        do_2()
    end
end

if this is possible it will probably rely on some internal detail I think though

To try to answer your question directly, I will need more information about what context you are trying to check for Revise.

If you are in a script or the REPL, you might might try check if the Revise isdefined.

julia> isdefined(Main, :Revise)
false

julia> using Revise

julia> isdefined(Main, :Revise)
true

In the more general case, you could replace Main with @__MODULE__.

julia> module Foo
           println(isdefined(@__MODULE__, :Revise))
       end
false
Main.Foo

julia> module Foo
           using Revise
           println(isdefined(@__MODULE__, :Revise))
       end
WARNING: replacing module Foo.
true
Main.Foo

julia> isdefined(Main, :Revise)
false

Checking if Revise is enabled is just the first step though. Do you want to refer to some Revise function within that if statement?

More likely than not, what you actually want is a package extension. This will load some code if Revise and your package is detected.

Depending on what you want to do, we may need Revise.jl’s UUID: "295af30f-e4ad-537b-8983-00126c2a3abe".

julia> const revise_uuid = Base.UUID("295af30f-e4ad-537b-8983-00126c2a3abe")
UUID("295af30f-e4ad-537b-8983-00126c2a3abe")

julia> const revise_pkgid = Base.PkgId(revise_uuid, "Revise")
Revise [295af30f-e4ad-537b-8983-00126c2a3abe]

julia> Base.loaded_modules[revise_pkgid]
Revise
1 Like

thanks, this is helpful, let’s say we are in the package scenario, is there a way to know if Revise is tracking that package and not just if it was loaded (but maybe it is tracking something else)?

We may need to get into Pkg.jl’s internals:

2 Likes

Thank you, specifically it appears to be the watched_files global.

Just one question: On my Mac, it is Dict{String, Revise.WatchList}, with keys being directories. According to Revise docs,

FreeBSD and NFS-mounted systems should watch files, otherwise we prefer to watch directories.

So where the watched files list goes on those systems?

1 Like

The following can check if the module runs “under Revise” - except I don’t know if it would work under FreeBSD and the like.

module Inrev

const revise_uuid = Base.UUID("295af30f-e4ad-537b-8983-00126c2a3abe")
const revise_pkgid = Base.PkgId(revise_uuid, "Revise")

function checkrev()
    revise_pkgid in keys(Base.loaded_modules) || return false 
    d = @__DIR__ 
    f = splitpath(@__FILE__)[end]

    watched_files = getproperty(Main.Revise, :watched_files)
    d in keys(watched_files) || return false

    trackedfiles = watched_files[d].trackedfiles
    return f in keys(trackedfiles)
end

macro inrev(args...)
    println(checkrev())
end
export @inrev

end 
1 Like