Skip to content

Add stable hook API for modulefile and modulerc evaluation - #668

Merged
xdelaruelle merged 8 commits into
envmodules:mainfrom
xdelaruelle:issue607
Aug 7, 2026
Merged

Add stable hook API for modulefile and modulerc evaluation#668
xdelaruelle merged 8 commits into
envmodules:mainfrom
xdelaruelle:issue607

Conversation

@xdelaruelle

@xdelaruelle xdelaruelle commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator
  • Add an add-hook <event> <procedure> siteconfig command with four initial events (before-modulefile-eval, after-modulefile-eval, before-modulerc-eval, after-modulerc-eval), replacing the trace/procedure-rename techniques for most needs without removing them
  • Document the design (doc/source/design/hook-api.rst), the command/events (module.rst), the new-feature highlight (MIGRATING.rst), and a contributor guide for adding further hook events (doc/source/devel/add-new-hook-event.rst)
  • Add a cookbook recipe (sync-remote-appdir) demonstrating a real-world use of the hook to sync an application directory from a network share on first load

Fixes #607

Describes an add-hook command with 4 initial events (before/after
modulefile and modulerc evaluation) to replace the trace/proc-rename
technique currently needed for site code to run around evaluation,
which breaks on internal changes across upgrades.

Relates to envmodules#607

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Add an add-hook <event> <procedure> command, usable from
siteconfig.tcl, to register site procedures to run around modulefile
and modulerc evaluation. This replaces the trace/rename techniques
described in the Hooks section of module.rst for the events it
covers, without removing them: they remain available for anything
add-hook does not (yet) support.

Four events are introduced: before-modulefile-eval,
after-modulefile-eval, before-modulerc-eval and after-modulerc-eval.
Several procedures can register on the same event; they run in their
registration order. A hook procedure error is caught and reported but
does not abort the running module command nor prevent other
registered procedures from running, since these hooks fire on nearly
every module invocation.

The hook registration list, add-hook command and dispatch helper live
in init.tcl.in rather than interp.tcl.in: the mechanism itself is
generic, not tied to modulefile/modulerc evaluation, those are just
the first 4 events it supports.

The two modulefile events also carry modspec, the module
specification as it was passed to the internal evaluation call prior
to resolution, alongside the already-resolved modname/modnamevr.

Fixes envmodules#607

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Test error handling around add-hook and the four hook events (unknown
event name, hook procedure called with too few or too many arguments,
Tcl code error in a hook procedure, error on one procedure not
affecting others registered on the same event or the wrapped
evaluation), the modulefile/modulerc argument contract, hooks running
across the different module evaluation modes on both success and
failure, hooks firing for global/user rc files as modulefile events
rather than modulerc events, and the examples given in module.rst.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Document add-hook and its four events (before-modulefile-eval,
after-modulefile-eval, before-modulerc-eval, after-modulerc-eval) in
the Hooks section of module.rst, using the new mhook Sphinx object
type; demote the existing trace/rename technique to an advanced,
lower-level fallback for what add-hook does not cover. Frame the
design doc and siteconfig.tcl example generically around the Hook API
rather than around these first 4 events, add a hook-events-per-release
table to changes.rst, and record the feature in NEWS.rst.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Add a v5.7 entry describing the new add-hook siteconfig command and
its four events, alongside the other new-feature highlights for this
release. Note that these four events are only the first ones
introduced, with more expected as real site needs come up.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Add doc/source/devel/add-new-hook-event.rst, following the existing
add-new-sub-command.rst/add-new-config-option.rst howtos, to guide
contributors through adding a new add-hook event: declaring it,
picking its touch point and argument contract, documenting it, and
covering it in the testsuite. Reference it from the developer notes
list in CONTRIBUTING.rst, from the Hook API entry in MIGRATING.rst,
and record it in NEWS.rst.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Add a cookbook recipe showing a before-modulefile-eval hook that
copies, with rsync, the application directory of a module hosted on a
network share to local disk the first time it is loaded, tracked with
a marker file so later loads skip the copy. The modulepath root
.modulerc tags a module 'remote' while its application directory has
not been synced yet, and the 'remote' tag is added to
non_exportable_tags so it does not stick once the module is actually
loaded.

Configuration for this tag (abbreviation, color, non_exportable_tags)
is set with module config calls in the initrc configuration file,
evaluated once when the module shell function initializes with
autoinit -- the only context a global rc file evaluated by Modules can
call module config from, since such a file is otherwise re-evaluated
ahead of every other module command afterward.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
@xdelaruelle
xdelaruelle merged commit a29f261 into envmodules:main Aug 7, 2026
19 checks passed
@xdelaruelle
xdelaruelle deleted the issue607 branch August 7, 2026 09:35
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.

Stable hook API for modulefile/modulerc evaluation

1 participant