After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 675987 - stop using chdir / readlink
stop using chdir / readlink
Status: RESOLVED FIXED
Product: gnome-terminal
Classification: Core
Component: general
git master
Other Linux
: Normal normal
: ---
Assigned To: GNOME Terminal Maintainers
GNOME Terminal Maintainers
: 622180 (view as bug list)
Depends on:
Blocks: 677467
 
 
Reported: 2012-05-13 21:44 UTC by Christian Persch
Modified: 2013-04-30 15:52 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
emulation: Add sequence to set cwd (9.82 KB, patch)
2012-06-01 17:44 UTC, Christian Persch
none Details | Review
emulation: Add sequences to set current directory and file (14.53 KB, patch)
2012-06-01 21:44 UTC, Christian Persch
none Details | Review
bash: Add /etc/profile.d/vte.sh script (2.32 KB, patch)
2012-06-04 21:54 UTC, Christian Persch
none Details | Review
server: Stop using chdir and readlink (6.22 KB, patch)
2012-06-04 22:00 UTC, Christian Persch
none Details | Review

Description Christian Persch 2012-05-13 21:44:07 UTC
We shouldn't be using readlink and chdir to get the current cwd. They can cause the process to deadlock (e.g. bug 622180). Instead, we should get the cwd by getting the shell to tell us the cwd via using some specific sequences in the prompt.
Comment 1 Behdad Esfahbod 2012-05-14 12:41:28 UTC
Is this just for showing in the title?  In which case, yes, I think switching to requiring the shell to tell makes more sense.
Comment 2 Christian Persch 2012-05-14 12:43:37 UTC
It's used for a) showing the title, and b) opening new tabs in the current directory.
Comment 3 Behdad Esfahbod 2012-05-14 12:47:19 UTC
For the latter, we can read the process cwd from /proc on Linux.  We can ignore the issue on other systems I guess?  Or just not try to open in the same dir or something.  Opening in the same dir of a frozen filesystem doesn't produce a usable shell, so that's something else to keep in mind too.
Comment 4 Christian Persch 2012-05-28 13:19:47 UTC
As far as I could determine by searching the web, it simply uses a bash PROMPT_COMMAND which prints "OSC 7 ; <URI> BEL" with the cwd as a properly %-encoded file:/// URI.
Comment 5 Behdad Esfahbod 2012-05-28 15:41:55 UTC
Can that be done easily by setting PS1?  If yes, I think we should start accepting that and lobby distros to set it, and stop using chdir() in  later release.
Comment 6 Christian Persch 2012-06-01 17:44:21 UTC
Created attachment 215456 [details] [review]
emulation: Add sequence to set cwd

Add OSC 7 ; URI BEL and OSC 7 ; URI ST sequences to set the cwd as an URI.
Comment 7 Christian Persch 2012-06-01 17:44:30 UTC
I think PROMPT_COMMAND fits better than PS1, since you'll have to use a function to escape the string, and also only do this when running under vte. (I guess we'll need to export some env var for this?)
Comment 8 Behdad Esfahbod 2012-06-01 18:01:14 UTC
Function in PS1 is fine.  This is how everyone sets git-bash-completion up:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\[\033[31m\] $?$(__git_ps1 " (%s)" </dev/null)\[\033[00m\]\$ '
Comment 9 Christian Persch 2012-06-01 18:05:15 UTC
According to http://superuser.com/questions/79972/set-the-title-of-the-terminal-window-to-the-current-directory/321733#321733  there's also OSC 6 used to set the 'represented file'; I think we should add support for that at the same time.
Comment 10 Behdad Esfahbod 2012-06-01 18:31:54 UTC
Also, I think xterm has support for something like this already.  Oh yeah, here it is, from my .bashrc:

# If this is an xterm set the title to user@host:dir 
case "$TERM" in 
xterm*|rxvt*) 
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" 
    ;; 
*) 
    ;; 
esac
Comment 11 Behdad Esfahbod 2012-06-01 18:39:58 UTC
Ok, I see, so Apple added seqs for dir and file specifically.  This actually fixes the old "open in samedir" with symlinked directories too.  Really cool.
Comment 12 Behdad Esfahbod 2012-06-01 18:44:56 UTC
Looks like xterm simply ignores those sequences, so it should be fine advocating using it unconditionally under TERM=xterm.
Comment 13 Christian Persch 2012-06-01 18:57:48 UTC
Yes, OSC 0 just sets title+icon-title, so a) there's no guarantee to the terminal that this is actually a file/dir (must apply heuristics), and b) the URI escaping takes care to accurately represent the filename (which may not be representable as valid UTF-8). 

And yes, this solves the symlink problem too.

I guess we should ship a .bashrc snipplet that uses this new sequence.
Comment 14 Christian Persch 2012-06-01 21:44:19 UTC
Created attachment 215471 [details] [review]
emulation: Add sequences to set current directory and file

Add sequences
  OSC 6 ; URI BEL
  OSC 6 ; URI ST
  OSC 7 ; URI BEL
  OSC 7 ; URI ST
that set the current file (OSC 7) and current directory (OSC 6) as an URI.
Comment 15 Christian Persch 2012-06-04 21:54:12 UTC
Created attachment 215588 [details] [review]
bash: Add /etc/profile.d/vte.sh script

This script exports __vte_ps1 which when used in PS1 will keep the terminal
updated about the current directory. Use like this in ~/.bashrc:

export PS1='\[$(__vte_ps1)\]'$PS1
Comment 16 Christian Persch 2012-06-04 21:55:26 UTC
(In reply to comment #12)
> Looks like xterm simply ignores those sequences, so it should be fine
> advocating using it unconditionally under TERM=xterm.

The only problem is that old (pre-this patch) vte doesn't ignore them... that should only matter for us developers however.
Comment 17 Christian Persch 2012-06-04 22:00:27 UTC
Created attachment 215589 [details] [review]
server: Stop using chdir and readlink

They really don't work, and may hang the process (see bug #622180).
Instead, rely on the shell to keep the terminal updated about the
current directory via vte's new current-directory-uri property that
can be updated via the OSC 7 ; URI BEL escape sequence.
vte helpfully installs a script to /etc/profile.d that does via the
shell prompt. Just put this in your ~/.bashrc:

  export PS1='\[$(__vte_ps1)\]'$PS1

https://bugzilla.gnome.org/show_bug.cgi?id=622180
Comment 18 Christian Persch 2012-06-04 22:10:26 UTC
*** Bug 622180 has been marked as a duplicate of this bug. ***
Comment 19 Christian Persch 2012-06-04 22:14:55 UTC
(In reply to comment #5)
> Can that be done easily by setting PS1?  If yes, I think we should start
> accepting that and lobby distros to set it, and stop using chdir() in  later
> release.

IMHO we should just switch to this *right away*, it's simply better :-) Oh, and the symlinks now work! Also, the only thing that breaks if you don't use the PS1 with the escape sequences is that your new-tab/new-window terminals open in $HOME instead of whereever; so that wouldn't be a huge bug.
Comment 20 Christian Persch 2012-06-05 13:28:47 UTC
Fixed on vte-0-34 and vte-next, and g-t master. Relnoting filed as bug 677467.
Comment 21 Jeremy Bicha 2013-04-01 03:02:29 UTC
Sorry for digging up an old bug, but I think it would be useful if one of you would post to a blog or somewhere why this change is useful/needed now that gnome-terminal 3.8 has been released as stable.

How do you recommend distros handle upgrading users to this version? I'm assuming you aren't recommending everyone should manually add an extra line to their ~/.bashrc to avoid a regression...
Comment 22 Jeremy Bicha 2013-04-01 03:08:23 UTC
(My last post was inspired by http://morefedora.blogspot.com/2013/04/gnome-terminal-38.html but I've been frustrated by this for a few months and I still haven't figured out what Ubuntu or Debian will need to change to get this to work out of the box.)
Comment 23 Christian Persch 2013-04-01 11:52:00 UTC
I don't blog. I agree it's useful to know, which is why it's mentioned in NEWS.

I don't think 'everyone' needs to update their bashrc manually; I envisioned that distributions would patch their default bash settings.

/etc/profile.d/vte.sh is sourced automatically (at least that works here on F17), so alternatively we could make it alter the PS1 accordingly, for interactive shells and on condition test "$COLORTERM" = "gnome-terminal" ? That would exclude other vte-based terminal emulators from using this feature however. Also I didn't do this because I got the idea from git's prompt integration feature, which isn't installed by default either.
Comment 24 Jeremy Nickurak 2013-04-30 15:42:53 UTC
Unless I'm mistaken, /etc/profile.d/vte.sh is only sourced for *login* shells, so it's not present in gnome-terminal shells.
Comment 25 Christian Persch 2013-04-30 15:52:38 UTC
Discussion on that is continuing in bug 697475.