~technomancy/fennel#250: 
searcher: don't check module-name as-is before converting `.` to path separator

see: https://git.sr.ht/~technomancy/fennel/tree/c057f58216c47a968709c08d4907685d2cff77c3/item/src/fennel/specials.fnl#L1247

(let [pathsepesc (escapepat pkg-config.pathsep)
        pattern (: "([^%s]*)%s" :format pathsepesc pathsepesc)
        no-dot-module (modulename:gsub "%." pkg-config.dirsep)
        fullpath (.. (or ?pathstring utils.fennel-module.path)
                     pkg-config.pathsep)]
    (fn try-path [path]
      (let [filename (path:gsub (escapepat pkg-config.pathmark) no-dot-module)
            filename2 (path:gsub (escapepat pkg-config.pathmark) modulename)]
        (case (or (io.open filename) (io.open filename2))
          file (do
                 (file:close)
                 filename)
          _ (values nil (.. "no file '" filename "'")))))

it checks modulename which was the value of that in (require :modulename) as well as as no-dot-module, but returns only the filename for no-dot-module if either of them works.

this presents an issue when the module contains a dot. on requiring module.name, it checks module/name.fnl, and module.name.fnl if only the latter exists, the filename is passed to dofile, which then raises runtime error on failing to io.open it.

checking module.name.fnl (rather than just module/name.fnl is also inconsistent with lua's behaviour.

use case: custom support for importing modules with dot in their names: https://git.sr.ht/~hedy/fssg/tree/67b30cdac878cc3b8809379abe1d2f23751f854b/item/fix-searcher.fnl

Status
RESOLVED FIXED
Submitter
~hedy
Assigned to
No-one
Submitted
a month ago
Updated
a month ago
Labels
bug

~technomancy REPORTED FIXED a month ago

I see what you mean! There's not any reason ever to check filename2, because you'll only ever get false positives. This is a good catch; thanks. I've fixed it in c8e5f05.

~hedy a month ago

awesome. thank you!

Register here or Log in to comment, or comment via email.