From 20b099c926508bc7a758a39d9c4e1bfb8d72c12e Mon Sep 17 00:00:00 2001
From: Mingtong Lin <mt.oss@fastmail.com>
Date: Thu, 27 Nov 2025 18:11:51 -0500
Subject: [PATCH 2/6] lisp/ob-tangle.el: Fix incorrect trimming behavior on
 tangled blocks lisp/ob-emacs-lisp.el: Fix undesired trailing empty line in
 expanded body testing/lisp/test-ob-tangle.el: Add test.

* ob-tangle.el (org-babel-tangle-single-block): Remove incorrect
  org-trim calls.
* ob-emacs-lisp.el (org-babel-expand-body:elisp): Remove the trailing
  emtpy line.

All the trailing empty lines in tangled blocks are trimmed by
org-babel-tangle-single-block, which I believe is due to the fix on
trailing empty lines created by org-babel-expand-body:xxx calls.

In fact, it is the org-babel-expand-body:xxx functions that should be
fixed, rather than an overkilling remedy at tangle time.

Before:
,#+begin_src emacs-lisp :tangle xxx.el
"hello"

,#+end_src

tangles to

,#+begin_src emacs-lisp
"hello"
,#+end_src

Now:

,#+begin_src emacs-lisp
"hello"

,#+end_src

Reported-by: "Mingtong Lin" <mt.oss@fastmail.com>
Link: https://list.orgmode.org/f43360bb-dc8f-41bb-b40e-dfdd38ebb87b@app.fastmail.com/
---
 lisp/ob-emacs-lisp.el          |  2 +-
 lisp/ob-tangle.el              |  4 ++--
 testing/lisp/test-ob-tangle.el | 22 ++++++++++++++++++++++
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-emacs-lisp.el b/lisp/ob-emacs-lisp.el
index 93d49db32..edb0954c0 100644
--- a/lisp/ob-emacs-lisp.el
+++ b/lisp/ob-emacs-lisp.el
@@ -51,7 +51,7 @@ by `org-edit-src-code'.")
 	(print-length nil)
         (prologue (cdr (assq :prologue params)))
         (epilogue (cdr (assq :epilogue params))))
-    (if (null vars) (concat body "\n")
+    (if (null vars) body
       (format "(let (%s)\n%s%s%s\n)"
 	      (mapconcat
 	       (lambda (var)
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 5d54721a5..022cc04bd 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -624,8 +624,8 @@ non-nil, return the full association list to be used by
 		link
 		source-name
 		params
-		(if (org-src-preserve-indentation-p) (org-trim body t)
-		  (org-trim (org-remove-indentation body)))
+		(if (org-src-preserve-indentation-p) body
+		  (org-remove-indentation body))
 		comment)))
     (if only-this-block
         (let* ((file-name (org-babel-effective-tangled-filename
diff --git a/testing/lisp/test-ob-tangle.el b/testing/lisp/test-ob-tangle.el
index 640d910b5..cd45c1160 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -52,6 +52,28 @@
 ;;      (should-not (exp-p "no"))
 ;;      (should (exp-p "tangle"))))))
 
+(ert-deftest ob-tangle/tangle-preserve-trailing-empty-lines ()
+  "Test tangle with trailing empty lines."
+  (should
+   (equal
+    "1
+
+"
+    (org-test-with-temp-text-in-file
+    "
+#+header: :tangle \"test-ob-tangle.el\"
+#+begin_src emacs-lisp
+1
+
+#+end_src"
+    (unwind-protect
+        (progn
+          (org-babel-tangle)
+          (with-temp-buffer
+            (insert-file-contents "test-ob-tangle.el")
+            (buffer-string)))
+      (delete-file "test-ob-tangle.el"))))))
+
 (ert-deftest ob-tangle/no-excessive-id-insertion-on-tangle ()
   "Don't add IDs to headings without tangling code blocks."
   (org-test-at-id "ef06fd7f-012b-4fde-87a2-2ae91504ea7e"
-- 
2.39.5 (Apple Git-154)

