emacs-module-rs icon indicating copy to clipboard operation
emacs-module-rs copied to clipboard

Some confusion regarding the live-reload workflow

Open ohmree opened this issue 4 years ago • 3 comments

Hi, I'm trying to write some elisp bindings to a rust library and would like to benefit from live reloading.

Looking at the workflow shown in magit-libgit2, I've:

  • Copied bin/load.sh and modified the file names for my project
  • Added emacs-rs-module to my dev dependencies
  • Tried to run the script using cargo-watch as shown in the aforementioned project's readme

But it doesn't seem to work - find "$root" -iname "*emacs_rs_module*.$ext" returns nothing.

Furthermore, I can't find any file that looks like *emacs_rs_module*.so anywhere in my project (after a build of course).

Is this workflow just broken at the moment?

Maybe some build script machinery could be used to have a more ergonomic workflow? I'm not sure I know enough about how emacs (and linux I guess) loads dynamic libraries to work on it myself, but I feel like it could possibly make this a bit more ergonomic (e.g. automate the whole .so/.dylib symlinking thing, generate elisp files that modify the load path and require the module).

ohmree avatar Nov 04 '21 01:11 ohmree

Yeah, live reloading support was generally a hack that accidentally worked on some versions of Linux/macOS. I think we should remove that part from the doc.

ubolonton avatar Dec 28 '21 15:12 ubolonton

I'm currently experimenting with live reloading (and have gotten it to almost work) but have bumped against an error. On first load everything loads correctly, but on second load I hit a panic during initialization.

Saving file /Users/lambdadog/devel/data-search.el/data_search_native/src/lib.rs...
Wrote /Users/lambdadog/devel/data-search.el/data_search_native/src/lib.rs
[rs-module]: defined functions...
[/Users/lambdadog/devel/data-search.el/data_search_native/target/debug/libdata_search_native.dylib]: not loaded yet
[/Users/lambdadog/devel/data-search.el/data_search_native/target/debug/libdata_search_native.dylib]: loading...
[/Users/lambdadog/devel/data-search.el/data_search_native/target/debug/libdata_search_native.dylib]: initializing...
[/Users/lambdadog/devel/data-search.el/data_search_native/target/debug/libdata_search_native.dylib]: loaded and initialized
Saving file /Users/lambdadog/devel/data-search.el/data_search_native/src/lib.rs...
Wrote /Users/lambdadog/devel/data-search.el/data_search_native/src/lib.rs
[/Users/lambdadog/devel/data-search.el/data_search_native/target/debug/libdata_search_native.dylib]: unloaded Library@0x7fbee8875700...
[/Users/lambdadog/devel/data-search.el/data_search_native/target/debug/libdata_search_native.dylib]: loading...
[/Users/lambdadog/devel/data-search.el/data_search_native/target/debug/libdata_search_native.dylib]: initializing...
Panic during initialization: Any { .. }
[/Users/lambdadog/devel/data-search.el/data_search_native/target/debug/libdata_search_native.dylib]: loaded and initialized

Files

script/live-reload.sh

#!/usr/bin/env bash

here=`cd $(dirname $BASH_SOURCE); pwd`
cd $here/../data_search_native
cargo watch -s $here/on-reload.sh -x 'build --features live-reload'

script/on-reload.sh

#!/usr/bin/env bash

# (Re)load the dynamic module into a running Emacs instance.

system=`uname`
if [[ $system == "Linux" ]]; then
    ext="so"
elif [[ $system == "Darwin" ]]; then
    ext="dylib"
else
    echo "Unsupported system: $system"
    exit 1
fi

here=`cd $(dirname $BASH_SOURCE); pwd`
root=`cd $here/..; pwd`
RS_MODULE=$(find $root -iname "libemacs_rs_module-*.$ext" | head -n 1)
MODULE=$root/data_search_native/target/debug/libdata_search_native.$ext
MODULE_X=$root/data-search.el

read -r -d '' expr <<EOF
(progn
  (unless (featurep 'rs-module ;'
                              )
    (module-load "$RS_MODULE"))
  (rs-module/load "$MODULE")
  (unless (featurep 'data-search ;'
                                    )
    (load "$MODULE_X")))
EOF

emacsclient -e "$expr"

echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

data_search_native/Cargo.toml

[package]
name = "data_search_native"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
emacs = "0.18"
milli = { git = "https://github.com/meilisearch/milli", tag = "v0.22.2" }
heed = { git = "https://github.com/Kerollmops/heed", tag = "v0.12.1" }

emacs-rs-module = { version = "0.18.0", optional = true }

[features]
live-reload = ["emacs-rs-module"]

lambdadog avatar Feb 20 '22 00:02 lambdadog

Regarding @ohmree's issue, I was able to solve that by having emacs-rs-module be gated behind a feature rather than it being a dev dependency (which didn't behave the way the on-reload script needed and I'm not experienced enough with rust to figure out a way to make it do so while still being a dev-dependency) but as far as I can tell, at least on my system, the emacs_rs_module_init init route used by emacs-rs-module is broken in some way, at least on second load.

I presume it's something not being undone properly on unloading, otherwise it wouldn't load correctly the first time around.

lambdadog avatar Feb 20 '22 00:02 lambdadog