Page MenuHomePhabricator

"vi" and "vim" may exit with errors even though edits succeed
Closed, ResolvedPublic

Description

See PHI1279. This is an awful mess.

At least on macOS, vi and vim may exit with an error code even though the edit succeeded. On my system (Mojave 10.4.4), this reproduces for vi but not for vim. This flow reproduces the issue:

$ touch x
$ vi x
> Type "Escape".
> Type ":Q<enter>", producing an error message because this isn't a valid command.
> Type ":q<enter>", exiting the editor.
$ echo $?
1

The behavior of the vi binary seems highly suspicious here.

When invoked as vim x, the same sequence of commands exits with code 0.

These issues report similar problems, but none seem to identify a real cause or reason.

https://unix.stackexchange.com/questions/14497/why-would-vim-return-non-zero-exit-code-if-i-exit-immediately-after-opening
https://stackoverflow.com/questions/41719802/vi-is-aliased-to-vim-but-exit-with-code-1
https://vi.stackexchange.com/questions/16382/why-does-git-think-vi-quit-in-error

I also can't find a reason for this in man vim, etc. The actual binary behavior is highly dependent on how it is invoked in argv[0]:

vim/src/main.c
/*
 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]  (sort of)
 * If the executable name starts with "r" we disable shell commands.
 * If the next character is "e" we run in Easy mode.
 * If the next character is "g" we run the GUI version.
 * If the next characters are "view" we start in readonly mode.
 * If the next characters are "diff" or "vimdiff" we start in diff mode.
 * If the next characters are "ex" we start in Ex mode.  If it's followed
 * by "im" use improved Ex mode.
 */
    static void
parse_command_name(mparm_T *parmp)
{

Invoking with vi -V9... to produce a debug log doesn't do much of anything.

In PHI1279, the reporting user sees the same behavior from vim, although I can't reproduce this locally.

Event Timeline

epriestley triaged this task as Low priority.May 24 2019, 8:15 PM
epriestley created this task.
avivey added a subscriber: avivey.May 24 2019, 9:03 PM

Running the thing on my ubuntu exists with 0...

$ vi --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Apr 10 2018 21:31:58)
Included patches: 1-1453
.....
.....
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -Wdate-time  -g -O2 -fdebug-prefix-map=/build/vim-NQEcoP/vim-8.0.1453=. -fstack-protector-strong -Wformat -Werror=format-security -DTINY_VIMRC -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim    -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE  -lm -ltinfo  -lselinux -lacl -lattr -ldl

Here's a more concise test:

my-laptop (macOS) $ vi +Q +q; echo vi $?; vim +Q +q; echo vim $?
vi 1
vim 0
secure001 (Ubuntu 14) $ vi +Q +q; echo vi $?; vim +Q +q; echo vim $?;
vi 0
vim 0

Both are vim 8.0.

It is amazing that software works even some of the time.

epriestley closed this task as Resolved.May 30 2019, 11:47 PM

I'm just going to close this in the hope it fades quietly into the night now that it has been "fixed".

iiam