org-babel-tangle: Do not allow tangling into self

* lisp/ob-tangle.el (org-babel-tangle): Throw an error when trying to
tangle into the org file we tangle from.
* etc/ORG-NEWS (It is no longer allowed to tangle into the same file
as Org source): Document the breaking change.
* testing/lisp/test-ob-tangle.el (ob-tangle/tangle-to-self): New test.
This commit is contained in:
Ihor Radchenko 2024-01-08 16:58:24 +01:00
parent 13bbe6743d
commit ef23b4706b
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
3 changed files with 35 additions and 1 deletions

View File

@ -13,6 +13,18 @@ Please send Org bug reports to mailto:emacs-orgmode@gnu.org.
* Version 9.7 (not released yet)
** Important announcements and breaking changes
*** It is no longer allowed to tangle into the same file as Org source
Previously, =file.org= with the following contents
: #+begin_src org :tangle file.org
: Text
: #+end_src
would overwrite itself.
Now, an error is thrown.
*** iCalendar export now supports multiline =SUMMARY=, =LOCATION=, and =DESCRIPTION= properties
Previously, it was not possible to specify multi-line location,

View File

@ -256,7 +256,8 @@ matching a regular expression."
(when (equal arg '(16))
(or (cdr (assq :tangle (nth 2 (org-babel-get-src-block-info 'no-eval))))
(user-error "Point is not in a source code block"))))
path-collector)
path-collector
(source-file buffer-file-name))
(mapc ;; map over file-names
(lambda (by-fn)
(let ((file-name (car by-fn)))
@ -313,6 +314,13 @@ matching a regular expression."
(compare-buffer-substrings
nil nil nil
tangle-buf nil nil)))))))
(when (equal (if (file-name-absolute-p file-name)
file-name
(expand-file-name file-name))
(if (file-name-absolute-p source-file)
source-file
(expand-file-name source-file)))
(error "Not allowed to tangle into the same file as self"))
;; We do not erase, but overwrite previous file
;; to preserve any existing symlinks.
(write-region nil nil file-name)

View File

@ -553,6 +553,20 @@ another block
(org-split-string (buffer-string))))
(delete-file file))))))
(ert-deftest ob-tangle/tangle-to-self ()
"Do not allow tangling into self."
(let ((file (make-temp-file "org-tangle-" nil ".org")))
(unwind-protect
(with-current-buffer (find-file-noselect file)
(insert
(format "
#+begin_src elisp :tangle %s
2
#+end_src
" file))
(should-error (org-babel-tangle)))
(delete-file file))))
(ert-deftest ob-tangle/detangle-false-positive ()
"Test handling of false positive link during detangle."
(let (buffer)