evnp / tmex

tmux-execute – a lightweight tmux command & layout composer
MIT License
87 stars 5 forks source link

"duplicate session" error when re-attaching #4

Closed altano closed 2 years ago

altano commented 2 years ago

Cool package. Thanks for making this.

I have a package.json script: "start": "tmex -r -n dev",

When I do npm start I get an error:

duplicate session:

I'd expect -r to make it so that it attaches to the existing session, avoiding this error. If I do tmux a I can manually reattach.

evnp commented 2 years ago

Hey @altano, thanks for reporting this. Have to admit I'm a bit puzzled by it – when I add this as a package.json script and run it things seem to work as expected. There's a section of code which determines whether to attach-session or kill-session here:

    # either attach to an existing session or kill it based on specified behavior
    if tmux has-session -t "${argsession}" 2>/dev/null; then
      if [[ "${argreattach}" == true ]]; then
        tmux attach-session -t "${argsession}"
        exit 0
      else
        tmux kill-session -t "${argsession}"
      fi
    fi

    # execute constructed tmux command
    tmux "${tmuxargs[@]}"

In my case, ${argsession} is "tmex" (the default if you don't provide a session name) and the first branch of that if statement executes because if tmux has-session -t "${argsession}" is true. However, if I force that to be false, execution skips to the bottom – tmux "${tmuxargs[@]}" – and I see the "duplicate session" error you're running into.

If you manually run this in your terminal, what is the result? I'm wondering if there's some differing behavior between tmux versions I'm unaware of.

$ if tmux has-session -t "tmex"; then echo "session 'tmex' found"; fi
session 'tmex' found
altano commented 2 years ago

Hey @evnp, thanks for looking into this.

I figured out the issue with your pointer.

My package name has periods in it, e.g. "my.package.name". The tmux session getting created is "my_package_name". So tmex can't find it, because it's using "my.package.name".

Workarounds:

If you're feeling generous you could try to fix this in tmex, but I cannot find reference documentation on what tmux considers valid and what it does to automatically convert session names, so I'm not sure what that fix would look like.

I'm going with workaround option 1 for now. Thank you again for your help.

evnp commented 2 years ago

Ah, that makes perfect sense! I was unaware that tmux did any manipulation of session name strings (and had briefly forgotten about the npm package name -> session name feature when writing that response – thanks for figuring out the rest).

Nothing too generous, but I would like to avoid others running into this in the future – what do you think of https://github.com/evnp/tmex/commit/5d3a4ef6fe5360b2d7990768bb6a996b2f77d3fc as a fix? At least for the use-cases I'm aware of, it seems to allow --reattach to work as expected without anything disruptive (eg. raising an error message if troublesome chars are present).

evnp commented 2 years ago

Thanks again @altano, https://github.com/evnp/tmex/releases/tag/v1.0.9 is now published with the fix.