bakpakin / Fennel

Lua Lisp Language
https://fennel-lang.org
MIT License
2.42k stars 124 forks source link

Emacs: reloading `require` lines #446

Closed fosskers closed 1 year ago

fosskers commented 1 year ago

I'm getting more used to fennel-mode and enjoying myself, so thank you.

I've noticed that executing C-c C-e (lisp-eval-defun) on a require line only "works" the first time. That is, if the underlying library changes and I reeval the require line, autocomplete doesn't show new functions, nor are they usable when I attempt to use them within the file. Here is a quick example:

(local t (require :transducers))

(fn sum-1 [tensor]
  "Sum the elements of a Rank-1 tensor."
  (t.transduce t.pass t.add tensor))

(sum-1 [1 2 3])

I had added add on-the-fly when I noticed + isn't a function in Fennel, but upon reevaluating the entire buffer, I found that it was claiming that add didn't exist. Indeed it was being passed to t.transduce as nil, in the Lua way.

Any thoughts about what might be happening here? Cheers!

P.S. I would have opened this issue within the fennel-mode repo, but couldn't figure out how. :pray:

technomancy commented 1 year ago

Try M-x fennel-reload or C-c C-k.

fosskers commented 1 year ago

I had tried that, but it doesn't seem to work either. In executing sum-1 manually in the REPL or via C-c C-e directly in the buffer (which I'm more oft to do), it is convinced that t.add doesn't exist.

technomancy commented 1 year ago

Did you reload the module that you added the function to, or the module that's calling the one that changed?

If it's the first one, it could be a bug. Can you provide a repro case? Also try reloading with ,reload transducers in the repl; it should be the same thing as M-x fennel-reload.

fosskers commented 1 year ago

I think that the library with the function added was never actually loaded into the REPL - I guess it had only been done so transitively via the require call to it. Let me reproduce this and get back to you.

technomancy commented 1 year ago

I think that the library with the function added was never actually loaded into the REPL - I guess it had only been done so transitively via the require call to it.

There's no difference between these two situations; if a module is loaded transitively or directly it behaves the same either way.

fosskers commented 1 year ago

Okay, there seem to be multiple things happening here.

Setup

  1. Have a symlink to https://github.com/fosskers/transducers.fnl/blob/master/transducers.fnl in the local testing directory.
  2. Add the following to a testing file:
    
    (local t (require :transducers))

(fn sum-1 [tensor] "Sum the elements of a Rank-1 tensor." (t.transduce t.pass t.add tensor))

(sum-1 [1 2 3])

{:sum-1 sum-1}


3. Comment out the `add` export within the transducers file.

This is the starting setup for both scenarios below. I completely restart Emacs between each scenario to ensure a clean slate.

### Scenario 1: The REPL

1. Open testing file.
2. C-c C-k
3. Completion does not work for `t` symbol.
4. Bind this module to a local in the REPL.
5. Call sum-1 in REPL; fails, since t.add doesn't exist yet.
6. Open transducers buffer and export `add`.
7. C-c C-k once again in testing file.
8. Can call sum-1 in REPL without needing to rebind the local (convenient!)
9. But, error in REPL implying t.add is nil.

### Scenario 2: The Buffer

This is more often how I work with Lisps; local one-liners that I execute for testing without typing things in the REPL manually.

1. Open testing file.
2. Attempt C-c C-e on local one-liner for testing.
3. It fails: sum-1 is not loaded.
4. C-c C-e on sum-1: it fails, transducers hasn't been loaded.
5. C-c C-e on `require` line, then sum-1 again.
6. C-c C-e on the one-liner: fails, `add` isn't exported yet.
7. Open transducers buffer and export `add`.
8. C-c C-e on the `require` doesn't overwrite what the Lisp process already holds; `add` continues not to exist.

### Lines of Inquiry

1. Could the symlink have anything to do with it?
2. How come the REPL (the Lisp process?) seems to get in different states between `C-c C-k` and individual `C-c C-e` calls? (re: completions)
fosskers commented 1 year ago

Also try reloading with ,reload transducers in the repl;

This works, by the way (for Scenario 2).

technomancy commented 1 year ago

In scenario 1, you said you're only reloading the testing file. You need to reload the file that changed in order to have the changes reflected in the repl.

In scenario 2 it sounds like you're trying to run code that refers to things that haven't been loaded yet. Re-running require has no effect; you have to reload in order to get the new changes in a module to show.

fosskers commented 1 year ago

Thanks. After playing around more last night, I think the proper mental model (and the one you're hinting at) is to use a single REPL session across multiple Fennel projects (if they're interdependent), and make sure everything is C-c C-k'd properly.

I'll close this for now, cheers.