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

Directory that binary executed from #509

Open
barslmn opened this issue May 23, 2023 · 1 comment · May be fixed by #535 or emacs-rustic/rustic#9
Open

Directory that binary executed from #509

barslmn opened this issue May 23, 2023 · 1 comment · May be fixed by #535 or emacs-rustic/rustic#9

Comments

@barslmn
Copy link

barslmn commented May 23, 2023

Hi,
System Info:
Debian sid
Doomemacs
rustic version: 3.4

I am using rustic from orgmode by compiling rust code blocks. I have the following files:

❯ ls -1
aoc2022.org
input1

And I have the following code block in the org file:

#+BEGIN_SRC rust :results output
use std::fs;

fn main() {
    let file_path = "input1";
    let contents = fs::read_to_string(file_path)
        .expect("Should have been able to read the file");
}
#+END_SRC

#+RESULTS:
: thread 'main' panicked at 'Should have been able to read the file: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:7:10

Executing this block from org file gives the no such file error. In order to investigate I run the following block:

#+BEGIN_SRC rust 
use std::env;
fn main() {
    println!("{:?}", env::current_dir());
}
#+END_SRC

#+RESULTS:
: Ok("/tmp/babel-C1Nmcb/cargozPIbXS")

My question is how do I change the directory that binary is executed from /tmp/babel-XXXXXX/cargoXXXXXX to where the org file is? This does not happen with other languages (I tried C, python, shell) that's why I think rustic repo is the right place ask.
Thanks, B

@ed9w2in6
Copy link

ed9w2in6 commented Oct 12, 2023

I face this issue too, which is a bug in my opinion as most if not all other babel executors all honours executing the binary in the directory specified by buffer local variable default-directory.

AFAIK, the current flow of source block execution is:

  1. rustic ultimately uses rustic-babel-eval to build the compiled binary for the src block.
  2. rustic-bable-eval uses rustic--inheritenv which does tries to retain some environment settings.
  3. when constructing command for make-process, rustic-babel-build-sentinel is used for :sentinel later async execution
  4. Finally, another make-process call in rustic-babel-build-sentinel will use '("cargo" toolchain "run" "--quiet") as command, in which toolchain is specified by :toolchain

From 2, we can see that this project does aim to preserve local environment when executing the compiled binary with org-babel framework. So I hope we can agree that we should fix this issue.

Here, the culprit is at step 4. By default, Emacs will start new process with default-directory as current working directory. However, in rustic-babel-build-sentinel we modified it for cargo to build correctly, since rust uses cargo to manage dependencies. This breaks the flow when calling cargo run.

IMO, the correct high level logic is:

  1. somehow change directory to the original default-directory
  2. call make-process with :command as '("cargo" toolchain "run" "--manifest-path" rustic-babel-dir "--quiet")

For 2., I believe just modifying 1 line in rustic-babel-build-sentinel will suffice.
For 1., there are multiple ways to do this, but I believe temporarily setting default-directory before make-process is better.

Please note that there are still edge cases for this method, since I do NOT understand them completely and it should NOT affect org-babel (READ: it MAY affect cargo build, so only change this for org-babel!) For details, see: rust-lang/cargo#6142

FYR, the implementation for C, C++ and D is at org-babel-C-execute, which compiled to a specified path and execute the said path WITHOUT ever changing default-directory.

ed9w2in6 added a commit to ed9w2in6/rustic that referenced this issue Oct 13, 2023
In an org file, the src block's code should be interpreted, OR the binary compiled from it SHOULD execute with PWD as `default-directory` of the org file.

Summary of changes: 
1. save the original default-directory then later restores it.
2. supply `--manifest-path` option to `cargo run` to execute the target `Cargo.toml` binary compiled from the src block.

For reference:
Try to execute a C++, C, js (node), python block.
In particular, see implementation of `org-babel-C-execute` or `org-babel-python-evaluate-external-process`.  May be we can re-write like them in the future.

fixes: brotzeit#509
CeleritasCelery pushed a commit to emacs-rustic/rustic that referenced this issue May 17, 2024
In an org file, the src block's code should be interpreted, OR the binary compiled from it SHOULD execute with PWD as `default-directory` of the org file.

Summary of changes: 
1. save the original default-directory then later restores it.
2. supply `--manifest-path` option to `cargo run` to execute the target `Cargo.toml` binary compiled from the src block.

For reference:
Try to execute a C++, C, js (node), python block.
In particular, see implementation of `org-babel-C-execute` or `org-babel-python-evaluate-external-process`.  May be we can re-write like them in the future.

fixes: brotzeit/rustic#509
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants