Jacek Generowicz <jg-orgm...@my-post-office.net> writes:

> Here is an org document setting out my question. It should export cleanly
> to HTML, without any dependencies other than Jujutsu.
> ...
> #+begin_src sh :noweb yes :exports code :dir /tmp/time-travel-wtf/branching
> echo A <<other-head()>>
> jj new <<other-head()>>
> echo A <<other-head()>>
> #+end_src
> So far so good. Now we repeat the block with two small variations:
> 1. Replace the =A= marker with =B=
> 2. Execute the code and render its output.
>
> This time there should be some output containing the =B= marker.
> #+begin_src sh :noweb yes :exports both :results output code :dir 
> /tmp/time-travel-wtf/branching
> echo B <<other-head()>>
> jj new <<other-head()>>
> echo B <<other-head()>>
> #+end_src
> ...
>
> By using a simpler example (one which increments a counter stored in a file), 
> I have discovered that =other-head()= is executed 9 (nine) times:
> + the first three times (I guess) when rendering the =echo A= source block
> + three more times when rendering the **result** of the =echo B= block
> + a further three times when rendering the *source* of =echo B= block.
>
> Yes, the occurrences of =other_head()= in the rendering of the source 
> evaluate *after* the occurrences in the rendering of that source code's 
> result!

Your observation is correct.
Org mode first evaluates the code, then processes the code for export
(including optionally expanding noweb references), and finally moves on
to process the results. See `org-babel-exp-do-export'.

> This unexpected order (cause being rendered after effect) would explain the 
> mismatch between the IDs shown in the =echo B= source code and its result.

> How can we work around this behaviour, in order to
>
> 1. render a code snippet containing a specific, *literal* =jj= change ID,
> 2. execute that code for its side effect,
> 3. render the output of that code ?

You would probably need to modify `org-babel-exp-do-export' and its
caller. The current behavior is simply inner working details of the
implementation. The order is such simply because it is easier to do via
the current code logic.

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to