Hello,

Here is an org document setting out my question. It should export cleanly
to HTML, without any dependencies other than Jujutsu.

#+title: Noweb source/result evaluation order

#+OPTIONS: toc:nil creator:nil author:nil timestamp:nil num:t validate:nil

**Warning**: the code below executes =rm /tmp/time-travel-wtf/ -rf=.

Make a Jujutsu (=jj=) repository that looks like this

#+begin_src sh :eval export :exports results :results output code
rm /tmp/time-travel-wtf/ -rf
mkdir /tmp/time-travel-wtf
jj git init --colocate /tmp/time-travel-wtf/branching
cd  /tmp/time-travel-wtf/branching
jj config set --repo user.name "Your Name"
jj config set --repo user.email "y...@example.com"
echo 1 >> numbers
jj commit -m "Base commit"
echo 2 >> numbers
jj commit -m "Right commit"
jj new -m "Left commit" -r @--
echo 3 >> numbers
jj new
jj
#+end_src

We can identify the unique prefix of the head on the non-current branch with:
#+name: other-head
#+begin_src sh :eval export :exports both :dir /tmp/time-travel-wtf/branching
jj log -r 'visible_heads() ~ @' -T 'change_id.shortest()' --no-graph
#+end_src
#+results: other-head

We want to demonstrate how to use this ID to switch branch with =jj new <ID>=. 
We use noweb to generate the code containing the =jj= instruction containing 
the specific ID, and add some noise (=echo A=) to help us visually link input 
to corresponding output. First we only export the code, we do not execute it, 
so there is no output anywhere containing the =A= marker
#+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

Observations:

1. The code contains =echo B <ID of LEFT branch>= but the output shows =B <id 
of RIGHT branch>=: the output does not match literal constants printed by the 
source!
2. The side-effect of the =jj new ...= line seems to take place before that 
line is constructed.

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!

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 ?

Reply via email to