Skip to content

Conversation

@jpthiele
Copy link
Contributor

@jpthiele jpthiele commented Jan 5, 2026

On the same day as I submitted ExtensionHelpers to General the package WeakDepHelpers was also submitted using an alternative approach based on error hints.

@jpthiele jpthiele marked this pull request as draft January 5, 2026 16:42
@jpthiele
Copy link
Contributor Author

jpthiele commented Jan 5, 2026

@Krastanov When I load it in a small dummy project that devs this branch I only get the MethodError without any hints.
Any idea why this might be?

The calls to test hinting

using ExtendableGrids
simplexgrid_from_gmsh("test.msh")

Precompilation passes and I get auto completion for the function but only the bare error and no WeakDepError

@Krastanov
Copy link

I will try to look into this later today

using WeakDepHelpers
import Graphs

const WEAKDEP_METHOD_ERROR_HINT_CACHE = WeakDepCache()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need the __init__ hook manually added.

https://github.com/QuantumSavory/QuantumClifford.jl/blob/master/src/init.jl#L19-L21

function __init__()
    if isdefined(Base.Experimental, :register_error_hint)
        Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
            method_error_hint_callback(WEAKDEP_METHOD_ERROR_HINT_CACHE, io, exc, argtypes, kwargs)
        end
    end
end

Let me know if this seems distasteful and we can figure out a cleaner API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a lot of boilerplate for every code wanting to use WeakDepHelpers.
I have implemented and tested it and it works now.
I have also moved everything within init into a register_weakdep_cache function that receives the cache singleton. I think it might be helpful to add this to WeakDepHelpers to increase usability

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur @jpthiele .

I am happy to take care of the implementation. Could you post an issue on the WeakDepHelpers tracker with an example boilerplate that you would find "reasonably pleasant / unobtrusive"? Or do you think that just a pre-defined register_weakdep_cache would be sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this predefined function might be sufficient. Makes it easier to use for those not too familiar with the hinting system while still being easy enough to be copied and modified by expert users if needed.

@jpthiele
Copy link
Contributor Author

jpthiele commented Jan 6, 2026

@j-fu in comparison to ExtensionErrors this works with error hints which so far are expermental (for a while..).
But removal of that is guarded in the init function, so it would simply not show the hint anymore.

Between the usual MethodError text and the stacktrace this now shows the following (in 'info' blue)

HINT: `simplexgrid_from_gmsh` depends on the package(s) `Gmsh` but you have not installed or imported them yet. 
Immediately after an `import Gmsh`, `simplexgrid_from_gmsh` will be available.

@Krastanov
Copy link

Just for a bit of extra context, keeping the original MethodError (instead of hiding it behind a different error type) was an explicit design goal of WeakDepHelpers. Unlike other error types, MethodError has a "privileged" position, coming from deep inside of the runtime, hooked into IDE tools, related to the basic paradigm of how Julia works, thus various design guides advise on not hiding it (and it even has special handling from the runtime with additional context being printed about it in recent versions of julia).

Another minor advantage of this approach is that it avoid invalidations. There is no custom-error-throwing method defined, so there is nothing to invalidate when the extension is loaded.

A (big) drawback of this approach is that it requires an __init__ function, which, to my knowledge, messes up the new ahead-of-time trimmed compilation capabilities from juliac. That might be fixable with the newly introduced OncePerX capabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants