markdown-exec icon indicating copy to clipboard operation
markdown-exec copied to clipboard

bug: Directory change dos not persist between code blocks executed on the same session

Open NikosAlexandris opened this issue 1 year ago • 3 comments

Description of the bug

Given that the current working directory is part of the state when executing code in a shell, then the state does not persist between executed code blocks when changing to another directory.

To Reproduce

Enter in a directory, print its name and list files inside it :

```
bash exec="true" result="ansi" session="somesession" source="above"
cd somedirectory/
pwd
```

OR

``` bash exec="true" result="ansi" session="somesession" source="above"
cd somedirectory/
ls .
```

Print the current directory's name (or list its content), expected to be somedirectory (or the same content) from the previous commands :

``` bash exec="true" result="ansi" session="somesession" source="above"
pwd
```

Expected behavior

Given that the current working directory is part of the state, executing subsequent code blocks on the same session should remember previously executed code blocks that change the directory.

Environment information

python -m markdown_exec.debug
  • System: Linux-6.7.0-arch3-1-x86_64-with-glibc2.38
  • Python: cpython 3.11.6
  • Environment variables:
  • Installed packages:
    • markdown-exec v1.8.0

NikosAlexandris avatar Jan 16 '24 23:01 NikosAlexandris

Hello, thanks for the report.

So, actually, sessions are not supported for the sh or bash languages, as mentioned in the docs.

To bring support, we would have to run a process in the background and feed it input. I believe this would maintain the CWD.

pawamoy avatar Jan 17 '24 06:01 pawamoy

I didn't pay attention to the notice this works for Python and Pycon only :-/. Anyhow, it would be useful to stick the current working directory across code blocks running in the same session. However, for my use case, I prefer a single command output only. That is, show the last command's output.

ps- Reading in the unedited comment about hiding, though, gives me an idea to work-around my current use-case without using a session. In a How-To, I am trying to say something like :

  • we navigate to ...

    ``` bash exec="true" source="above"
    cd somedirectory/
    ```
    
  • we next run this command

    ```bash exec="true" source="above"
     somecommand . --some-option
     ```
    

So, for now, maybe the second command can be

```bash exec="true" source="above"
cd somedirectory/  # markdown-exec: hide
somecommand . --some-option
```

while hiding the first line (?).

Will try.

NikosAlexandris avatar Jan 17 '24 11:01 NikosAlexandris

So, for now, maybe the second command can be

``` bash exec="true" source="above"
cd somedirectory/  # markdown-exec: hide
somecommand . --some-option
```

while hiding the first line (?).

Will try.

That seems to work nicely for my use case. Needless to repeat, this plugin is very useful! Thank you @pawamoy.

NikosAlexandris avatar Jan 17 '24 11:01 NikosAlexandris

Since sessions are not yet supported for Bash code blocks, I'll close this issue in favor of https://github.com/pawamoy/markdown-exec/issues/53.

pawamoy avatar Jun 12 '24 23:06 pawamoy