From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id uEYuD9hvyWP/twAAbAwnHQ (envelope-from ) for ; Thu, 19 Jan 2023 17:29:12 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id GCUxD9hvyWPtlAAA9RJhRA (envelope-from ) for ; Thu, 19 Jan 2023 17:29:12 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C4E303DEE7 for ; Thu, 19 Jan 2023 17:29:11 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pIXmD-0002qh-A9; Thu, 19 Jan 2023 11:28:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pIXm9-0002qZ-U2 for emacs-orgmode@gnu.org; Thu, 19 Jan 2023 11:28:30 -0500 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pIXm4-0004xh-Ed for emacs-orgmode@gnu.org; Thu, 19 Jan 2023 11:28:29 -0500 Received: by mail-pf1-x42d.google.com with SMTP id c85so1889597pfc.8 for ; Thu, 19 Jan 2023 08:28:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=cTPmTSgrlEBXz1n70HB/HvOS7UhZT8N/zE9FHPXZbZU=; b=aOi9bMd3jaUn/Jdyrde50OysaC54en+UCADXwErvgzbLLqafpgLX7dl6R4afE0vuT5 OLxwYoBq4YjgBIfmsQevtqN+ECZjCxd7KlSKab+kREOAB98Wx1om094waKOn9op0K6Vq jUcJBXeppB4d7G2frnckXJKT2v/WERMe0iUli156MOiOZFavEbRcJ5rpOES+aVWn3ozw IZ46kMPFyXgeC/w3m4x4zMiyIIj4Pe0zbbNGlEJG8UWGtmTp/h07k29Bqyu/CKnVVrTs WI0bKrOPW03R941CPSaulIRroeEa74JAVw9+EguPXlUY44JbMfvMahG8q25zNVPQF0e/ K+ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cTPmTSgrlEBXz1n70HB/HvOS7UhZT8N/zE9FHPXZbZU=; b=INdQq9onF2fO9WKVQeai9C4nu1IHLFEt6lh5gsKFtM/TVq+USnG1tF/zmO7vzJzH/q oeVOC+zGCXyD7pVPPejLc34XvULj9UCTZTMvzjHSjjqjcTsd8mF0FfVZ70ZTRHp3OpXN buyoojd5XfuRDxktamP/ySGvJM/vsuYC5pqXV3RiHNJ/k7hbnvTq/M7kkqqETXoaphas 54gKIV7FDRTN4GmkRltJlAP0MypCpC986EPa5jy7j6KfTG3IwPnNH+KIamzNVXRvACVh yf2nrerUx3DK2nb9jS8tU0BB5o+xCMFPRV0yK0cRLzzpCwqm4GeyApg0NIBJsNKgKaS6 pjfQ== X-Gm-Message-State: AFqh2kre+pqvHvg0+cSvDgQJBSLQyyzIWjfnkOdiq5tX7poe2SjSwCN3 QRuDdv7iclIvlsUdw8fW+Gr5c+uLfnEgdpYNjzhFK25BE7s= X-Google-Smtp-Source: AMrXdXsBmS179wVOAc4BvUDso5jFWopUJNyYqBOmvSwAViU+zQxC6EJxc0S7OhAe4iqocmxgmU4x58LIfxYTq8ElwCs= X-Received: by 2002:a62:144a:0:b0:582:e939:183d with SMTP id 71-20020a62144a000000b00582e939183dmr1300409pfu.63.1674145701132; Thu, 19 Jan 2023 08:28:21 -0800 (PST) MIME-Version: 1.0 References: <185bc86c826.11ae860d1190626.407710511075709301@excalamus.com> <185bd43e452.bb3f5419220019.823265614059778953@excalamus.com> <185c34814a4.bcdd7b94578193.327779579906739164@excalamus.com> <87ilh48b6p.fsf@localhost> In-Reply-To: <87ilh48b6p.fsf@localhost> From: Osher Jacob Date: Thu, 19 Jan 2023 18:28:09 +0200 Message-ID: Subject: Re: [BUG] ob-shell doesn't evaluate last line on Windows (cmd/cmdproxy) [9.6.1 ( @ c:/Users/Osher/AppData/Roaming/.emacs.d/elpa/org-9.6.1/)] To: Ihor Radchenko , Matt Cc: emacs-orgmode Content-Type: multipart/alternative; boundary="000000000000d1804205f2a06b58" Received-SPF: pass client-ip=2607:f8b0:4864:20::42d; envelope-from=osherz5@gmail.com; helo=mail-pf1-x42d.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN ARC-Seal: i=1; s=key1; d=yhetil.org; t=1674145751; a=rsa-sha256; cv=none; b=cS+PEe+r8mMUCcpl5j1GhWK27LskJc0XiBuaDzXehKED0dkPb5omYwwb2Ci0AlsgQ6MfOO wdbNEHSw6AMtSq1I65ERBBxLjYi5d5bUpDQ20HQuU6ybjKxQBK3PTNxRIqUcCuDL0wUmcm LEXy79sF06TVLIeoaRoBhKvmLYM1zJ15b4x/mVbb/lb9fFlonpXtPwfxPZdIMh9XI7c+kL EnV+AzhFxYADsmJ+TMulsSrqSIvOH3e3HRkSgFG2J+FTfx5n/oeJs+z3D0SJimzsC+iuGQ /av9ILV/UZs1kkwpttu4/KKbSUMZztT92X2Qn5cJYNG4YQVVtM/uwAbj8CX7lw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=aOi9bMd3; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1674145751; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=cTPmTSgrlEBXz1n70HB/HvOS7UhZT8N/zE9FHPXZbZU=; b=ml8Y2NtsoSFeEUoFg6Q1wm5WSRZV0w9+6pK9RnHHTvo6MiU9X4wdNtXyUZm/0QWEms8qhn Ndj59nzb0/5sCll05Hwq8kjRTcSD27ITnRYyoFA9k6XezpxDAyoOR8hj+wQieHCuGck41y nk95gNeOCLwsNxPcng1UCGP1KFaKYSm0tQPRani4NbF0Zn5MMzZh4FU+uBKFHpR9Q72Po1 x9mu33McngTH1htKA/pYBgME9HzJmAwOqNgDHja+6gyMe8l8OBOO3S+bZeA2LxZmvVDW3A MvjfLFOofKtu+2rt94uX8B8yBkQGBgMvp2QRmI444hPCx3EE1mv8bz4M1LAUBA== X-Spam-Score: -9.79 X-Migadu-Queue-Id: C4E303DEE7 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=aOi9bMd3; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Scanner: scn0.migadu.com X-Migadu-Spam-Score: -9.79 X-TUID: OBhoBN/2XO9D --000000000000d1804205f2a06b58 Content-Type: text/plain; charset="UTF-8" Thanks for the suggestions! On Wed, Jan 18, 2023 at 7:09 AM Matt wrote: > > 1. Another naive work around attempt. Again, I'm going from memory, > documentation, and what I have previously written. > > I have in my init a command to open a terminal when working on Windows > that looks like: > > (start-process "cmd" nil "cmd.exe" "/C" "start" "cmd.exe" "/K" "cd" > dir) > > This starts a cmd process with /c which terminates the prompt after the > command that follows is run. The command That follows starts another cmd > process with /k which changes the directory and leaves the terminal open. > > I have another command to open QGIS that does the same thing: > > (start-process "cmd" nil "cmd.exe" "/C" "start" "\"qgis\"" "cmd.exe" > "/K" "C:\\Program Files\\QGIS 3.22.3\\bin\\qgis.bat") > > It's not clear to me why the extra call to a second cmd with /k is > needed. Maybe it's copy-pasta or Windows being Windows. > > Anyway, I mention these because I wonder if we might be able to do > something similar. > > Try changing `shell-file-name' to "cmd.exe" (ideally, the full path) and > `shell-command-switch' to "/k" (or maybe "-k"). My hope would be that the > final call would look something like, > > cmd.exe /k cmd.exe /k > > Given my analysis, I'm not sure if there's a way we could trick the call > into being `cmd.exe /c cmd.exe /k input-file'. > On Wed, Jan 18, 2023 at 11:05 AM Ihor Radchenko wrote: > If running cmdproxy.exe /c cmdproxy.exe /path/to/input is not wrong, the > problem is on Emacs side. > Osher, could you try putting your example script into a file and running > the command line directly? What will it output? > Unfortunately, it seems like cmdproxy.exe and cmd.exe cannot accept an input file as a command-line argument and execute it. In the case of running : cmdproxy.exe /c cmdproxy.exe /c C:/tmp/inp We get: cmdproxy.exe /c cmdproxy.exe /c C:/tmp/inp 'C:/tmp/inp' is not recognized as an internal or external command, operable program or batch file. If we're insistent on passing the input through the command line arguments, I can think of two ways to go about this, but both seem undesirable: - Dumping the content of the input buffer into a ".bat" file and then passing it as an argument. That way we end up executing "cmdproxy.exe /c cmdproxy.exe input.bat", but then batch files produce a slightly different behaviour than running in the context of an interactive shell. or - Concating the lines together with the "&" in between them. This would look like 'cmdproxy.exe /c cmdproxy.exe /c "echo 1 & echo 2 & echo 3", which is also a pretty nasty solution in my opinion. This leads me to your next point, which sounds like the most natural way to go about it. 2. We could write some ob-shell code to explicitly handle Windows cmd and > powershell. For example, call `process-file' similar to the stdin/cmdline > branch of `org-babel-sh-evaluate'. > > I'm open to this, but, like I've said, I don't have a Windows machine to > work on right now. I'd not be able to verify it. I, and I'm sure others, > would be happy to guide you if that's something you'd like to help > implement. > > I think it could be enough to make sure the input ends with a newline, in which case it could be done the way I proposed in the first message, that is changing: (t (org-babel-eval shell-file-name (org-trim body)))))) to (t (org-babel-eval shell-file-name (concat (org-trim body) "\n")))))) I think as long as this change doesn't break the code in non-Windows operating systems (How would we go about verifying this?), it would be preferable due to its simplicity and minimality. If for any other reason you would rather not change the org-babel-sh-evaluate, and would like to implement a separate function for Windows, that's also a viable option. I am willing to try and go down that path, although I'm not a very experienced lisper. Do any of these options sound like they could work well? --000000000000d1804205f2a06b58 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks for the suggestions!

On Wed, Jan 18, 2023 at 7:09 AM Matt <= matt@excalamus.com<= /a>> wrote:

1= . Another naive work around attempt.=C2=A0 Again, I'm going from memory= , documentation, and what I have previously written.=C2=A0

I have in= my init a command to open a terminal when working on Windows that looks li= ke:

=C2=A0 =C2=A0 (start-process "cmd" nil "cmd.exe&q= uot; "/C" "start" "cmd.exe" "/K" &q= uot;cd" dir)

This starts a cmd process with /c which terminates= the prompt after the command that follows is run.=C2=A0 The command=C2=A0 = That follows starts another cmd process with /k which changes the directory= and leaves the terminal open.

I have another command to open QGIS t= hat does the same thing:

=C2=A0 =C2=A0 (start-process "cmd"= ; nil "cmd.exe" "/C" "start" "\"qgi= s\"" "cmd.exe" "/K" "C:\\Program Files\\= QGIS 3.22.3\\bin\\qgis.bat")

It's not clear to me why the e= xtra call to a second cmd with /k is needed.=C2=A0 Maybe it's copy-past= a or Windows being Windows.

Anyway, I mention these because I wonder= if we might be able to do something similar.

Try changing `shell-fi= le-name' to "cmd.exe" (ideally, the full path) and `shell-com= mand-switch' to "/k" (or maybe "-k").=C2=A0 My hope= would be that the final call would look something like,

cmd.exe /k = cmd.exe /k <executes-input-file-as-stdin-or-whatever>

Given my= analysis, I'm not sure if there's a way we could trick the call in= to being `cmd.exe /c cmd.exe /k input-file'.

<= /div>
If running cmdproxy.exe /c cmdproxy.exe /path/to= /input is not wrong, the
problem is on Emacs side.

Osher, could you try putting your exa= mple script into a file and running
the command line directly? What will= it output?

Unfortunat= ely, it seems like cmdproxy.exe and cmd.exe cannot accept an input file as = a command-line argument and execute it.

In the cas= e of running :
cmdproxy.exe /c cmdproxy.exe /c C:/tmp/inp

We get:
cmdproxy.exe /c cmdproxy.exe /c C= :/tmp/inp
'C:/tmp/inp' is not recognized as an internal or exter= nal command,
operable program or batch file.


If we're insistent=C2=A0on passing the inpu= t through=C2=A0the command line arguments, I can think of two ways to go ab= out this,=C2=A0but both seem undesirable:
- Dumping the content o= f the input buffer into a ".bat" file and then passing it as an a= rgument. That way we end up executing "cmdproxy.exe /c cmdproxy.exe in= put.bat", but then batch files produce a slightly different behaviour = than running in the context of an interactive shell.
or
- Concating the lines together with the "&" in between them.= This would look like 'cmdproxy.exe /c cmdproxy.exe /c "echo 1 &am= p; echo 2 & echo 3", which is also a pretty nasty solution in my o= pinion.

This leads me to your next point, which so= unds like the most natural way to go about it.

2. We could write some ob-shell c= ode to explicitly handle Windows cmd and powershell.=C2=A0 For example, cal= l `process-file' similar to the stdin/cmdline branch of `org-babel-sh-e= valuate'.

I'm open to this, but, like I've said, I don&#= 39;t have a Windows machine to work on right now.=C2=A0 I'd not be able= to verify it.=C2=A0 I, and I'm sure others, would be happy to guide yo= u if that's something you'd like to help implement.

=C2=A0
I think it could be enough to make sure th= e input ends with a newline, in which case it could be done the way I propo= sed in the first message, that is changing:
(t (org-babel-eval sh= ell-file-name (org-trim body))))))
to
(t (org-babel= -eval shell-file-name (concat (org-trim body) "\n"))))))

I think as long as this change doesn't break the c= ode in non-Windows operating systems (How would we go about verifying this?= ), it would be preferable due to its=C2=A0simplicity and minimality.
<= div>
If for any other reason you would rather not change the= =C2=A0org-babel-sh-evaluate, and would like to implement a separate functio= n for Windows, that's also a viable option.
I am willing to t= ry and go down that path, although I'm not a very experienced lisper.

Do any of these op= tions sound like they could work well?
=C2=A0
--000000000000d1804205f2a06b58--