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

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


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ahnlabb