emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] ob-shell: :shebang changes interpretation of :cmdline
@ 2023-11-18 15:54 Max Nikulin
  2023-11-18 18:09 ` Matt
  2023-11-18 18:20 ` [BUG] ob-shell: :cmdline fails with single argument (was Re: [BUG] ob-shell: :shebang changes interpretation of :cmdline) Matt
  0 siblings, 2 replies; 5+ messages in thread
From: Max Nikulin @ 2023-11-18 15:54 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

Trying to figure out the origin of the confusion with
"bash -c bash /path/to/file-containing-the-source-code.sh"
I have faced an inconsistency with :cmdline treatment in ob-shell.el. I 
expect same results in the following cases:

#+begin_src bash :cmdline 1 2 3
   printf "%s\n" "$1"
#+end_src

#+RESULTS:
: 1

#+begin_src bash :cmdline 1 2 3 :shebang #!/bin/bash
   printf "%s\n" "$1"
#+end_src

#+RESULTS:
: 1 2 3

Emacs-28, Org is the current git HEAD.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] ob-shell: :shebang changes interpretation of :cmdline
  2023-11-18 15:54 [BUG] ob-shell: :shebang changes interpretation of :cmdline Max Nikulin
@ 2023-11-18 18:09 ` Matt
  2023-11-18 18:20 ` [BUG] ob-shell: :cmdline fails with single argument (was Re: [BUG] ob-shell: :shebang changes interpretation of :cmdline) Matt
  1 sibling, 0 replies; 5+ messages in thread
From: Matt @ 2023-11-18 18:09 UTC (permalink / raw)
  To: Max Nikulin; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 857 bytes --]


 ---- On Sat, 18 Nov 2023 16:54:39 +0100  Max Nikulin  wrote --- 

 > I have faced an inconsistency with :cmdline treatment in ob-shell.el. I 
 > expect same results in the following cases:
 > 
 > #+begin_src bash :cmdline 1 2 3
 >    printf "%s\n" "$1"
 > #+end_src
 > 
 > #+RESULTS:
 > : 1
 > 
 > #+begin_src bash :cmdline 1 2 3 :shebang #!/bin/bash
 >    printf "%s\n" "$1"
 > #+end_src
 > 
 > #+RESULTS:
 > : 1 2 3

Thank you!  This makes a good test case.

Ihor or Bastion, Craig and my employer reached agreement on the disclaimer language.  I've sent a signed copy to Craig and haven't heard back yet with the go-ahead to continue contributing.  I sent the paperwork only a week or so ago, so I'm sure he'll get back to me in time.  Because of this, I'm including patches rather than pushing (which assumes I still have access (I haven't checked)).

[-- Attachment #2: test-cmdline-alone-and-with-shebang-have-same-result.patch --]
[-- Type: application/octet-stream, Size: 971 bytes --]

diff --git a/testing/lisp/test-ob-shell.el b/testing/lisp/test-ob-shell.el
index 879555af0..8f6180153 100644
--- a/testing/lisp/test-ob-shell.el
+++ b/testing/lisp/test-ob-shell.el
@@ -436,6 +436,26 @@ exiting with a non-zero return code multiple times."
                          (buffer-string)))))
       (kill-buffer "*Org-Babel Error Output*")))
 
+\f
+;;; cmdline
+
+(ert-deftest test-cmdline-alone-and-with-shebang-have-same-result ()
+  "Executing a block with the same shebang as the shell language
+should produce the same output."
+  (should (equal
+           (org-test-with-temp-text
+               "#+begin_src sh :cmdline 1 2 3
+echo \"$1\"
+<point>
+#+end_src"
+             (org-babel-execute-src-block))
+           (org-test-with-temp-text
+               "#+begin_src sh :cmdline \"1\" :shebang #!/bin/bash
+echo \"$1\"
+<point>
+#+end_src"
+             (org-babel-execute-src-block)))))
+
 (provide 'test-ob-shell)
 
 ;;; test-ob-shell.el ends here

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [BUG] ob-shell: :cmdline fails with single argument (was Re: [BUG] ob-shell: :shebang changes interpretation of :cmdline)
  2023-11-18 15:54 [BUG] ob-shell: :shebang changes interpretation of :cmdline Max Nikulin
  2023-11-18 18:09 ` Matt
@ 2023-11-18 18:20 ` Matt
  2023-11-19  6:57   ` Max Nikulin
  1 sibling, 1 reply; 5+ messages in thread
From: Matt @ 2023-11-18 18:20 UTC (permalink / raw)
  To: Max Nikulin; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 951 bytes --]


 ---- On Sat, 18 Nov 2023 16:54:39 +0100  Max Nikulin  wrote --- 

 > I have faced an inconsistency with :cmdline treatment in ob-shell.el. 

These are sadly easy to find.  

If you run:

#+begin_src bash :cmdline 1 
echo "$1"
#+end_src

Then it fails with 

list: Wrong type argument: sequencep, 1

However, running this works:

#+begin_src bash :cmdline "1" 
echo "$1"
#+end_src

I didn't dig too much into it, but it looks like :cmdline expects a sequence.  When multiple arguments are passed, such as :cmdline 1 2 3, then "1 2 3" is passed into process-file.  (sequencep "1 2 3") is t.  (sequencep 1) is nil.  So, to work around this a single :cmdline argument must be surrounded by quotes to make it a sequence.  (sequencep "1") is t.  Obviously, this should be fixed.

Attached is a patch to test for this whenever we're ready to tackle making execution mutually consistent.  I'm still reviewing the library and am not quite ready for that yet.

[-- Attachment #2: test-cmdline-with-single-argument-shouldnt-require-quotes.patch --]
[-- Type: application/octet-stream, Size: 861 bytes --]

diff --git a/testing/lisp/test-ob-shell.el b/testing/lisp/test-ob-shell.el
index 879555af0..d41387e57 100644
--- a/testing/lisp/test-ob-shell.el
+++ b/testing/lisp/test-ob-shell.el
@@ -436,6 +436,19 @@ exiting with a non-zero return code multiple times."
                          (buffer-string)))))
       (kill-buffer "*Org-Babel Error Output*")))
 
+(ert-deftest test-cmdline-with-single-argument-shouldnt-require-quotes ()
+  "The original implementation of :cmdline required a single element
+to be surrounded by goes to work."
+  (should
+   (equal 1
+          (org-test-with-temp-text
+              ;; "#+begin_src sh :cmdline \"1\" :shebang #!/bin/bash
+              "#+begin_src sh :cmdline 1 #!/bin/bash
+echo \"$1\"
+<point>
+#+end_src"
+            (org-babel-execute-src-block)))))
+
 (provide 'test-ob-shell)
 
 ;;; test-ob-shell.el ends here

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [BUG] ob-shell: :cmdline fails with single argument (was Re: [BUG] ob-shell: :shebang changes interpretation of :cmdline)
  2023-11-18 18:20 ` [BUG] ob-shell: :cmdline fails with single argument (was Re: [BUG] ob-shell: :shebang changes interpretation of :cmdline) Matt
@ 2023-11-19  6:57   ` Max Nikulin
  2023-11-19  7:57     ` Matt
  0 siblings, 1 reply; 5+ messages in thread
From: Max Nikulin @ 2023-11-19  6:57 UTC (permalink / raw)
  To: emacs-orgmode

On 19/11/2023 01:20, Matt wrote:
> #+begin_src bash :cmdline 1
> echo "$1"
> #+end_src
> 
> Then it fails with
> 
> list: Wrong type argument: sequencep, 1

I would say that :cmdline is treated in a different way in comparison to 
:var:

#+header: :results verbatim
#+begin_src bash :var arr='(1 2 3) :cmdline '(97 98 99)
   printf '$1:%s\n' "$1"
   declare -p arr
#+end_src

#+RESULTS:
: $1:abc
: declare -a arr=([0]="1" [1]="2" [2]="3")

I would expect more consistent results since script arguments is an 
array (positional arguments). $1=97, $2=98, $3=99



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] ob-shell: :cmdline fails with single argument (was Re: [BUG] ob-shell: :shebang changes interpretation of :cmdline)
  2023-11-19  6:57   ` Max Nikulin
@ 2023-11-19  7:57     ` Matt
  0 siblings, 0 replies; 5+ messages in thread
From: Matt @ 2023-11-19  7:57 UTC (permalink / raw)
  To: Max Nikulin; +Cc: emacs-orgmode


 ---- On Sun, 19 Nov 2023 07:57:26 +0100  Max Nikulin  wrote --- 

 > I would say that :cmdline is treated in a different way in comparison to 
 > :var:

It most definitely is.  It's a completely separate process from :var.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-11-19  7:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-18 15:54 [BUG] ob-shell: :shebang changes interpretation of :cmdline Max Nikulin
2023-11-18 18:09 ` Matt
2023-11-18 18:20 ` [BUG] ob-shell: :cmdline fails with single argument (was Re: [BUG] ob-shell: :shebang changes interpretation of :cmdline) Matt
2023-11-19  6:57   ` Max Nikulin
2023-11-19  7:57     ` Matt

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).