Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some confusion regarding the live-reload workflow #44

Open
ohmree opened this issue Nov 4, 2021 · 3 comments
Open

Some confusion regarding the live-reload workflow #44

ohmree opened this issue Nov 4, 2021 · 3 comments

Comments

@ohmree
Copy link

ohmree commented Nov 4, 2021

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).

@ubolonton
Copy link
Owner

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.

@lambdadog
Copy link

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
Copy link

lambdadog commented Feb 20, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants