3

Each time I open Terminal, I get an error:

/Users/myname/.zsh_sessions/SESSIONID.session:2: command not found: Saving

in each of the windows I set to open by default. This seems to have appeared a week ago possibly with an OS update (Monterey)

I haven't touched anything in Terminal though.

This seems to be related to an error in the way the session file SESSIONID.session is written in /Users/myname/.zsh_sessions/ when quitting Terminal

    (blank line)
    Saving session...echo Restored session: "$(/bin/date -r 1699309069)"
    (blank line)

Maybe a bug in etc/zshrc_Apple_Terminal or a configuration problem

Any idea?

EDIT: I commented out the echo -ne line in etc/zshrc_Apple_Terminal and the problem disappeared

 shell_session_save() {
    # Save the current state.
    if [ -n "$SHELL_SESSION_FILE" ]; then
        # echo -ne '\nSaving session...' >&2
        (umask 077; echo 'echo Restored session: "$(/bin/date -r '$(/bin/date +%s)')"' >| "$SHELL_SESSION_FILE")

Hugues
  • 523
  • 2
    A similar (same?) problem was reported here: https://apple.stackexchange.com/q/465930/30895 – Martin R Nov 07 '23 at 08:50
  • Yes, it is related, there seems to be an error in etc/zshrc_Apple_Terminal, which I can't identify, possibly due to a personalised configuration or lack of. – Hugues Nov 08 '23 at 00:14
  • One question. If you make a new user on the Mac, when you log out of the first and open a new graphical log in on the second, does the bug happen from the default setup and settings? – bmike Nov 08 '23 at 00:54
  • Also, be sure to put your solution in the answer section. Even if you don’t accept it it will help many – bmike Nov 08 '23 at 01:52
  • hello bmike, if I revert to the original zshrc_Apple_Terminal and log in another user, the problem is gone. It seems there is a problem how STDERR is handled on my main account – Hugues Nov 08 '23 at 02:04
  • Yes, it’s essentially the same problem on bash in /etc/bashrc_Apple_Terminal. Session save/restore has been in Terminal.app forever. What changed??? Do all of us seeing it has something similar (but not default) in our setups? Python? Homebrew? VS Code? … – Andrew Jaffe Nov 08 '23 at 06:20
  • (FWIW, I don’t consider the other issue “solved”!) If this Q is closed, perhaps upvote that one for more attention? – Andrew Jaffe Nov 08 '23 at 07:23
  • commented out the echo -ne line .... and the problem disappeared : The line by itself can't cause this error message. You can test it by putting it back in and manually calling shell_session_save from the command line. To make this echo in the end produce that error, it means that some process must collect the standard error of that function and execute it as command. – user1934428 Nov 08 '23 at 10:08
  • 1
    @user1934428: the problem is more specifically that stderr from that line is being (incorrectly) written to $SHELL_SESSION_FILE while the following (…) subshell is running; that file is then run (as it should) on the restart of Terminal.app, but with these incorrect characters in it. – Andrew Jaffe Nov 08 '23 at 10:11
  • @AndrewJaffe : This is clear, but this would mean that the caller of the function must have set up a redirection for file descriptor 2 to point to the session file. But even in this case, the >| of the subsequent line would recreate the session file. I simply don't see how this effect can be achieved by the code posted here. – user1934428 Nov 08 '23 at 10:18
  • @Hugues: One thing which I would try is to replace the echo line by print -u 2 $'\nSaving session ...', and then see whether the error is the same. Or maybe doing a echo .... >$(tty) instead. The latter really should not be redirected to any file, even if FD 2 is messed up somehow. – user1934428 Nov 08 '23 at 10:21
  • @user1934428, and yet it is! (Look at the other thread for a little more information.) In particular this code is run via trap shell_session_update EXIT, and the error seems to occurs only when the terminal exits because the Terminal.app is quit, but not by an explicit exit command (nor when various signals are sent by hand to the shell, either). – Andrew Jaffe Nov 08 '23 at 10:43
  • Your edit seems to be rather an answer, can you please post it as such beneath the other question? – nohillside Nov 08 '23 at 11:24
  • @user1934428: commenting out the line actually makes the problem disappear; tested multiple times on 2 accounts. Your suggestion print -u 2 $'\nSaving session ...' also does. – Hugues Nov 08 '23 at 14:12
  • @nohillside I'm not familiar with the usages in this forum, but for me it is not an answer but a workaround. As this seems to be a symptom of another problem. Anyway, the question having being closed, I can't add the answer now. – Hugues Nov 08 '23 at 14:14
  • Please add the answer to https://apple.stackexchange.com/questions/465930/apple-terminal-startup-errors-session-save-restore – nohillside Nov 08 '23 at 14:42
  • OK. I've done it – Hugues Nov 08 '23 at 15:06

0 Answers0