jolt/test/integration/deps-cache-args-test.janet
Dmitri Sotnikov 307b65b45b
Fix -m arg drop under whole-program cache (jolt-4mui) + RFC 0003 sync (#138)
* Bind *command-line-args* after the deps-image cache swap (jolt-4mui)

Under whole-program (deps-image cache active), `jolt -m NS ARG` dropped ARG:
run-main set *command-line-args* on the current ctx, but a cache HIT then
replaced ctx with the saved image (via `set ctx cached`), whose *command-line-
args* was whatever got baked when the image was saved. The stale binding won at
`(apply NS/-main *command-line-args*)`, so -main ran with the wrong (usually
default) args — silently, for any optimized -m program.

Move set-command-line-args to AFTER the cache swap so it binds on the final ctx.
Repro/regression in deps-cache-args-test.janet: first run builds the image
(arg "first"), second run (cache hit) must echo "second", not the baked "first".

* docs: RFC 0003 — phm is a HAMT, sorted colls a red-black tree

The transients RFC described phm as "bucket-based copy-on-write" and mused about
"if it ever becomes a HAMT" — it is one now (jolt-684u), and sorted maps/sets are
a red-black tree (jolt-0hbr). Update the deviation/future-work notes accordingly.

---------

Co-authored-by: Yogthos <yogthos@gmail.com>
2026-06-16 13:34:08 +00:00

41 lines
2 KiB
Text

# The deps-image cache must not drop -main's command-line args under
# whole-program (jolt-4mui). A cache HIT swaps the running ctx for the saved
# image; *command-line-args* must be (re)bound from the CURRENT invocation's
# argv, not the stale value baked into the image. Drives the built binary:
# first run builds the cache (arg "first"), second run (cache hit) passes "second"
# — both must echo their own arg. Skips if build/jolt is absent.
(def repo (os/cwd))
(def jolt (string repo "/build/jolt"))
(if-not (os/stat jolt)
(do (print "deps-cache-args: SKIP (no build/jolt)") (os/exit 0)))
(def dir (string (or (os/getenv "TMPDIR") "/tmp") "/jolt-dca-" (os/time)))
(os/mkdir dir)
(spit (string dir "/echoargs.clj")
"(ns echoargs)\n(defn -main [& args] (println \"GOT\" (pr-str (vec args))))\n")
# fresh per-run image cache dir so the first run is a guaranteed miss
(def imgdir (string dir "/imgcache")) (os/mkdir imgdir)
(each [k v] [["JOLT_DIRECT_LINK" "1"] ["JOLT_WHOLE_PROGRAM" "1"]
["JOLT_PATH" dir] ["JOLT_APP_PATHS" dir] ["JOLT_IMAGE_CACHE_DIR" imgdir]]
(os/setenv k v))
(defn- run [arg]
(def p (os/spawn [jolt "-m" "echoargs" arg] :p {:out :pipe :err :pipe}))
(def out (:read (p :out) :all))
(os/proc-wait p)
(string (or out "")))
(var fails 0)
(defn check [label got want]
(if (string/find want got) (print " ok " label)
(do (++ fails) (printf " FAIL %s: want %p in %p" label want got))))
(def r1 (run "first")) # cache MISS — builds + saves the image
(check "first run passes its arg" r1 `GOT ["first"]`)
(def r2 (run "second")) # cache HIT — must use the NEW arg, not the baked one
(check "second run (cache hit) passes its arg, not the baked one" r2 `GOT ["second"]`)
(defn rmrf [p] (when (os/stat p) (if (= :directory (os/stat p :mode)) (do (each e (os/dir p) (rmrf (string p "/" e))) (os/rmdir p)) (os/rm p))))
(rmrf dir)
(if (> fails 0) (do (printf "deps-cache-args: %d FAILED" fails) (os/exit 1))
(print "deps-cache-args (jolt-4mui) passed!"))