5

I upgraded to Linux Mint 21 (based on Ubuntu 22), having number of small issues, one of which is in VS Code, not in Terminal (gnome-terminal), running ls alias:

bash: printf: `Y': invalid format character

While I have defined these aliases:

# Base `ls` alias contains:
# - escape for it could be defined already
# - use colors when appropriate
# - group directories first
# - date format YYYY-Mmm-DD
alias ls="\ls --color=auto --group-directories-first --time-style=+'%Y-%b-%d'"

I did not notice this behavior in Focal, maybe I just look for errors now, or is this truly some error (mine, whoever)?

Notes:

https://www.gnu.org/software/coreutils/manual/html_node/Date-conversion-specifiers.html


$ type -a ls
ls is aliased to `\ls --color=auto --group-directories-first --time-style=+'%Y-%b-%d''
ls is /usr/bin/ls
ls is /bin/ls

Both in Terminal and VS Code.

printf %q\\n "PS1=$PS1" "PS2=$PS2" "PS3=$PS3" "PS4=$PS4" "PROMPT_COMMAND=$PROMPT_COMMAND"
PS1=\\\[\\e\]0\;\ \\w\ \\a\\\]\\\[\$color_green\\\]\\\$\ \\\[\$color_reset\\\]
PS2=\>\ 
PS3=
PS4=+\ 
PROMPT_COMMAND=

Only in VS Code (nothing in Terminal):

trap -p DEBUG
trap -- '__vsc_preexec_only "$_"' DEBUG

Image re-post from the original revision:

enter image description here

Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309
  • So it's not the prompt. Another related possibility would be a debug trap: what's the output of `trap -p DEBUG` ? And do you see the expected output from `ls` or not (actually, I think depending on the configuration the command might not be executed if the DEBUG trap fails, so that would still be consistent)? – Gilles 'SO- stop being evil' Aug 07 '22 at 09:03
  • @Gilles'SO-stopbeingevil' You just hit something, have no idea what it is though... – Vlastimil Burián Aug 07 '22 at 09:36
  • 4
    That vscode [bug](https://github.com/microsoft/vscode/issues/157226) was [fixed 2 days ago](https://github.com/microsoft/vscode/commit/5b23b086ffde3e3927db87186f56372e8fce7ff3), but that code is still very buggy. Looks like they need to read [When is double-quoting necessary?](//unix.stackexchange.com/q/68694) and [Why is printf better than echo?](//unix.stackexchange.com/q/65803) at least). – Stéphane Chazelas Aug 07 '22 at 10:17
  • 3
    and instead of fixing it to use `printf "... %s ..." "$__vsc_current_command"`, they've added a call to `sed` to escape the `%` signs. Sigh. Well, at least [someone has noted that in the bug report](https://github.com/microsoft/vscode/issues/157226#issuecomment-1207357558). (Also it doesn't look like `__vsc_preexec_only` uses the argument it gets anyway, hmm) – ilkkachu Aug 07 '22 at 10:21
  • The initial version of this question included a screenshot (image) showing the error occurring.  While we dislike images of text, the correct remedy would be to transcribe the image as text into the body of the question, optionally adding more information *but not subtracting relevant information.*  The screenshot showed that you were getting normal `ls` output ***after*** the error message.  You should [edit] your question to restore that information. – G-Man Says 'Reinstate Monica' Aug 14 '22 at 19:08

1 Answers1

8

Bash can process the text of the current command before running it by enabling the DEBUG trap. This was originally intended for debugging, but it is somewhat commonly used to set the window title to the text of the command while it's running.

The extdebug bash option tweaks the way the DEBUG trap works. One of its effects is that if the code of the DEBUG trap returns a nonzero status, the command is not executed.

Your configuration in VSCode has a DEBUG trap set (trap -p DEBUG shows the code if set). This is very likely done to set the window title to the text of the running command. It appears that this code has a bug, and tries to pass the command in a printf format. This breaks when the command contains a backslash or a percent sign. Because the extdebug option is apparently enabled (I don't know why), the following command is not executed.

The solution is to either fix the buggy code, unset the trap (trap - DEBUG) or override the trap with other, non-buggy code (trap 'other, non-buggy code' DEBUG).

This is a bug in the __vsc_preexec_only function or another function that it calls. Thus, presumably, it's a bug in VSCode. As noted by Stéphane Chazelas, that bug has already been reported (and at the time of writing, there's a fix, but the fix is still buggy, it looks like the maintainers of that code don't understand what they're doing).

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 1
    See the [vscode bug](https://github.com/microsoft/vscode/issues/157226) actually [fixed 2 days ago](https://github.com/microsoft/vscode/commit/5b23b086ffde3e3927db87186f56372e8fce7ff3) (though that code is still very buggy. Looks like they need to read [When is double-quoting necessary?](//unix.stackexchange.com/q/68694) and [Why is printf better than echo?](//unix.stackexchange.com/q/65803) at least and maybe realise that bash has builtin text substitution support and learn how to properly use `printf`). – Stéphane Chazelas Aug 07 '22 at 10:21
  • The initial version of the OP's question had a screenshot showing that the `ls` command was run even after `printf` printed that error. – Stéphane Chazelas Aug 07 '22 at 10:22