janet-lang / janet-lang.org

Website for janet
https://janet-lang.org
MIT License
90 stars 58 forks source link

Document `init.janet` for `import`. #207

Open amano-kenji opened 7 months ago

amano-kenji commented 7 months ago
> (import ./ok)
error: could not find module ./ok:
    ok.jimage
    ok.janet
    ok/init.janet
    ok.so
  in require-1 [boot.janet] on line 3025, column 20
  in import* [boot.janet] on line 3064, column 15
  in _thunk [repl] (tailcall) on line 2, column 1

In the above error, I saw that (import ./ok) tries to load ok/init.janet after trying to load ok.janet.

This behavior is not documented on https://janet-lang.org/docs/modules.html.

This is significant for module organization.

sogaiu commented 7 months ago

Although there is mention of init.janet at https://janet-lang.org/docs/modules.html, reading the text there didn't lead me to deduce the current behavior.

For reference, I think a relevant line of source in boot.janet is this one:

(module/add-paths "/init.janet" :source)

May be this section of the website docs would be a good place to mention behavior with respect to init.janet.

sogaiu commented 6 months ago

If something is going to be added to the website docs about /init.janet, may be it makes sense to mention the other items that are searched for (and the order as well).

IIUC, for (import foo), at least four things might be looked for in the following order:

  1. foo.jimage
  2. foo.janet
  3. foo/init.janet
  4. foo.<native-extension>

(Here, <native-extension> is likely to be .so on a Linux machine or .dll on Windows.)

I think the search is done in the above order and the first match "wins". That is, once a match is found, the search ends and an attempt is made to "import" the matched item.

For reference, here are some relevant-looking lines from boot.janet.