janet-lang / spork

Various Janet utility modules - the official "Contrib" library.
MIT License
117 stars 35 forks source link

No error feedback when coerce-to-env fails for netrepl's server function #162

Closed sogaiu closed 2 months ago

sogaiu commented 8 months ago

The call to coerce-to-env in netrepl's server function can fail because the env argument to coerce-to-env is invoked if it is a function.

janet-netrepl specifies a function value for env in a couple of places and that function can fail because it can call require and/or dofile with values specified by a user at the command line.

In the case of a failure-triggering invocation via janet-netrepl, the client connection is closed after establishment and there is no error feedback regarding the failure provided on the server end.

The following is a demonstration of the described situation (similar to that in #160).


Suppose spork is installed (so janet-netrepl is available).

Start server, requesting non-installed / unavailable library:

$ janet-netrepl -l not-spork
Starting networked repl server on 127.0.0.1, port 9365...

Start client (note the exiting apparent from the second prompt):

$ janet-netrepl -c
$

Observe output for server:

client [127.0.0.1:9365] connected
closing client [127.0.0.1:9365]

With the following diff:

diff --git a/spork/netrepl.janet b/spork/netrepl.janet
index 2556bf5..f72b41c 100644
--- a/spork/netrepl.janet
+++ b/spork/netrepl.janet
@@ -209,7 +209,11 @@
           (set name (string name (gensym))))
         (put name-set name true)
         (eprint "client " name " connected")
-        (def e (coerce-to-env env name stream))
+        (def e
+          (try (coerce-to-env env name stream)
+            ([err fib]
+              (eprint err)
+              (debug/stacktrace fib "coerce-to-env failed" ""))))
         (def p (parser/new))
         # Print welcome message
         (when (and welcome-msg auto-flush)

Server output looks like:

$ janet-netrepl -l not-spork
Starting networked repl server on 127.0.0.1, port 9365...
client [127.0.0.1:9365] connected
could not find module not-spork:
    /not-spork.jimage
    /not-spork.janet
    /not-spork/init.janet
    /not-spork.so
error: coerce-to-env failed
  in require-1 [boot.janet] (tailcall) on line 3024, column 20
  in env-make [/home/user/.local/bin/janet-netrepl] (tailcall) on line 82, column 41
closing client [127.0.0.1:9365]

That seems a bit better.

Note that the values:

    /not-spork.jimage
    /not-spork.janet
    /not-spork/init.janet
    /not-spork.so

start with / with no "intermediate" path because of #160. (The values might change if #160 is addressed, but I don't think that is significant for this issue.)

sogaiu commented 7 months ago

Note that now that #160 has been addressed, with the changes in #170 applied, the paths in the error output are more "fleshed out".

For example, they show up here as:

    /home/user/.local/lib/janet/no-spork.jimage
    /home/user/.local/lib/janet/no-spork.janet
    /home/user/.local/lib/janet/no-spork/init.janet
    /home/user/.local/lib/janet/no-spork.so

instead of:

    /not-spork.jimage
    /not-spork.janet
    /not-spork/init.janet
    /not-spork.so

because locally:

$ janet
Janet 1.33.0-23b0fe9f linux/x64/gcc - '(doc)' for help
repl:1:> (dyn :syspath)
"/home/user/.local/lib/janet"