From 42c0096ce75f8cd7733661dd99e365a852f26fa4 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 5 Feb 2026 19:45:30 -0600 Subject: [PATCH] Allow simpler loading of modules. E.g. (import project/file.janet :as file) (import ./project/file.janet :as file) (import /home/me/project/file.janet :as file) Will now do what you expect. When loading with a file extension, this will first check directly if the relative (or absolute) path exists. Also expands `module/find` to be able to show all matches for a given module name without needing to try and catch import errors. Lastly, one can also override the module/find functionality by passing `:loader` directly. E.g. (import ./macoslib.dylib :loader :native :as my-thing) Which will allow easily bypassing all module/find logic; "./macoslib.dylib" will be passed directly to the :native loader `(get module/loaders :native)`. The module system was previously left open to customization but with defaults to encourage a more typical style. However, these qol improvements are more than worth it and can in some cases let people stop fighting the module system. --- src/boot/boot.janet | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index d2a33e24..5f7afcff 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2924,6 +2924,7 @@ (array/insert mp sys-index [(string ":sys:/:all:" ext) loader check-is-dep]) (def curall-index (find-prefix ":cur:/:all:")) (array/insert mp curall-index [(string ":cur:/:all:" ext) loader check-relative]) + (array/insert mp 0 [":all:" loader (fn :check-ext [x] (string/has-suffix? ext x))]) mp) # Don't expose this externally yet - could break if custom module/paths is setup. @@ -2976,20 +2977,22 @@ or :image if the module is found, otherwise a tuple with nil followed by an error message. ``` - [path] + [path &opt find-all] (var ret nil) (def mp (dyn *module-paths* module/paths)) + (def all-matches (if find-all @[])) (each [p mod-kind checker] mp (when (mod-filter checker path) (if (function? p) (when-let [res (p path)] (set ret [res mod-kind]) - (break)) + (if find-all (array/push all-matches ret) (break))) (do (def fullpath (string (module/expand-path path p))) (when (fexists fullpath) (set ret [fullpath mod-kind]) - (break)))))) + (if find-all (array/push all-matches ret) (break))))))) + (if find-all (break all-matches)) (if ret ret (let [expander (fn :expander [[t _ chk]] (when (string? t) @@ -3164,7 +3167,10 @@ (defn- require-1 [path args kargs] - (def [fullpath mod-kind] (module/find path)) + (def [fullpath mod-kind] + (if-let [loader (get kargs :loader)] + [path loader] + (module/find path))) (unless fullpath (error mod-kind)) (def mc (dyn *module-cache* module/cache)) (def ml (dyn *module-loading* module/loading))