Use Infiltrator.jl on package code without adding Infitrator to Project.toml

Viewed 132

Is it possible to use @infiltrate from Infiltrator.jl to inspect a function in a package without adding Infiltrator to the Project.toml file? I have Infiltrator installed in my 1.7 environment, so I was hoping that stacked environments would come into play, but apparently not:

julia> using PackageFoo
[ Info: Precompiling PackageFoo [b7d5e62f-924e-4906-b2b5-430ba4531d91]
ERROR: LoadError: UndefVarError: @infiltrate not defined
2 Answers

One possibility would be to use a system image. In the following code I use PackageCompiler.jl to create a system image that includes Infiltrator.jl, that way Main, the top-level module, will always include Infiltrator as a submodule:

julia> using PackageCompiler

julia> create_sysimage(["Infiltrator"]; sysimage_path="debug_image.so")

I save the new system image to debug_image.so and then start julia with this image instead of the default one, the NoInfil project does not have Infiltrator in its deps:

$ julia --project=NoInfil -Jdebug_image.so                                                                                                                                                                                                                                 [21:52:23]
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.7.1 (2021-12-22)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using NoInfil

julia> NoInfil.greet()
Infiltrating greet() at NoInfil.jl:3:

infil>

This is what NoInfil/src/NoInfil.jl looks like:

module NoInfil

greet() = (Main.Infiltrator.@infiltrate; print("Hello World!"))

end # module

The current working solution is to do

Main.@infiltrate

from within your package.

I keep

using Infiltrator

in my startup.jl so that it's always pulled from the 1.7 environment regardless of which package or other environment I'm using.

Note that you need to first load your package without that line, then rely on Revise to notice when you add the Main.@infiltrate line to your package.

Related