jupytext icon indicating copy to clipboard operation
jupytext copied to clipboard

Bug: `--set-formats` is not writing the formats to the text files in some cases depending on the timestamps

Open caenrigen opened this issue 5 months ago • 11 comments

Hi @mwouts! Thanks a lot for the work on this package

I run into a strange bug while I was debugging my extension (https://github.com/caenrigen/vscode-jupytext-sync) for VSCode-based IDEs, basically a jupytext wrapper (+ raw cells support).

It seems a bit tricky to reproduce but seems related to the timestamps.

I used here in the example v1.16.7 but the same happens with the latest v1.17.1

╰[✔] g n23 ~/ ‹provisioner●› 
[25-05-08 13:20:52] $ stat -r bug.md bug.py
16777231 462850286 0100644 1 501 20 0 293 1746702847 1746702845 1746702845 1746701256 4096 8 0 bug.md
16777231 462850278 0100644 1 501 20 0 322 1746702847 1746702844 1746702844 1746701255 4096 8 0 bug.py
╰[✔] g n23 ~/ ‹provisioner●› 
[25-05-08 13:20:57] $ cat bug.py bug.md
# ---
# jupyter:
#   jupytext:
#     formats: py:percent,md
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.16.7
#   kernelspec:
#     display_name: g
#     language: python
#     name: python3
# ---
---
jupyter:
  jupytext:
    formats: py:percent,md
    text_representation:
      extension: .md
      format_name: markdown
      format_version: '1.3'
      jupytext_version: 1.16.7
  kernelspec:
    display_name: g
    language: python
    name: python3
---
╰[✔] g n23 ~/ ‹provisioner●› 
[25-05-08 13:21:07] $ jupytext --set-formats "ipynb,py:percent,md" bug.py
[jupytext] Reading bug.py in format py
[jupytext] Updating notebook metadata with '{"jupytext": {"formats": "ipynb,py:percent,md"}}'
[jupytext] Loading bug.md
[jupytext] Updating bug.ipynb
[jupytext] Unchanged bug.md
[jupytext] Unchanged bug.py
╰[✔] g n23 ~/ ‹provisioner●› 
[25-05-08 13:21:12] $ jupytext --sync bug.ipynb
[jupytext] Reading bug.ipynb in format ipynb
Traceback (most recent call last):
  File "/opt/homebrew/anaconda3/envs/g/bin/jupytext", line 8, in <module>
    sys.exit(jupytext())
             ~~~~~~~~^^
  File "/opt/homebrew/anaconda3/envs/g/lib/python3.13/site-packages/jupytext/cli.py", line 497, in jupytext
    exit_code += jupytext_single_file(nb_file, args, log)
                 ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/anaconda3/envs/g/lib/python3.13/site-packages/jupytext/cli.py", line 649, in jupytext_single_file
    notebook, inputs_nb_file, outputs_nb_file = load_paired_notebook(
                                                ~~~~~~~~~~~~~~~~~~~~^
        notebook, fmt, config, formats, nb_file, log, args.pre_commit_mode
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/opt/homebrew/anaconda3/envs/g/lib/python3.13/site-packages/jupytext/cli.py", line 1089, in load_paired_notebook
    _, fmt_with_prefix_suffix = find_base_path_and_format(nb_file, formats)
                                ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/anaconda3/envs/g/lib/python3.13/site-packages/jupytext/paired_paths.py", line 239, in find_base_path_and_format
    raise InconsistentPath(
    ...<5 lines>...
    )
jupytext.paired_paths.InconsistentPath: Path 'bug.ipynb' matches none of the export formats. Please make sure that jupytext.formats covers the current file (e.g. add 'ipynb' to the export formats)

The problem seems to be this:

[25-05-08 13:21:07] $ jupytext --set-formats "ipynb,py:percent,md" bug.py
[jupytext] Reading bug.py in format py
[jupytext] Updating notebook metadata with '{"jupytext": {"formats": "ipynb,py:percent,md"}}'
[jupytext] Loading bug.md
[jupytext] Updating bug.ipynb
[jupytext] Unchanged bug.md  <--------------------------------------------------------------------
[jupytext] Unchanged bug.py <---------------------------------------------------------------------

I suppose jupytext is not validating the formats in the files but using some timestamp-based logic.

caenrigen avatar May 08 '25 11:05 caenrigen