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 758452 - Set $PWD from vte
Set $PWD from vte
Status: RESOLVED FIXED
Product: vte
Classification: Core
Component: general
git master
Other Linux
: Normal minor
: ---
Assigned To: VTE Maintainers
VTE Maintainers
Depends on:
Blocks:
 
 
Reported: 2015-11-21 12:40 UTC by Egmont Koblinger
Modified: 2018-02-06 18:25 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
vte bits: set $PWD (1.41 KB, patch)
2017-10-05 21:01 UTC, Egmont Koblinger
committed Details | Review
g-t bits: don't set $PWD (1.26 KB, patch)
2017-10-05 21:02 UTC, Egmont Koblinger
committed Details | Review

Description Egmont Koblinger 2015-11-21 12:40:55 UTC
This is a pretty core Unix feature, even built in to basic library methods such as get_current_dir_name().

It is required for preserving symlink components in the working directory for new terminals.

The rest of the story (tracking OSC 7) is done by vte. So I'm wondering if this should also be done down there in vte_pty_child_setup(), rather than separately in each vte-based app.

(Inspired by https://bugs.launchpad.net/terminator/+bug/1518554)
Comment 1 Egmont Koblinger 2017-10-05 21:01:57 UTC
Created attachment 360997 [details] [review]
vte bits: set $PWD
Comment 2 Egmont Koblinger 2017-10-05 21:02:21 UTC
Created attachment 360998 [details] [review]
g-t bits: don't set $PWD
Comment 3 Egmont Koblinger 2017-10-05 21:13:11 UTC
vte:

Env vars are set in pty.cc, at two different places: vte_pty_child_setup() and __vte_pty_merge_environ(). It's not clear to me what goes where, e.g. TERM and VTE_VERSION are set at both, and COLORTERM lives a completely different life than TERM. Maybe this could be cleaned up, whatever. I went with whichever seemed to be simpler.

__vte_pty_spawn(), after constructing the array of env vars, calls vte_spawn_async_with_pipes_cancellable() first with the desired directory, and if it fails then again with NULL. Ideally we'd set PWD only for the first, and probably leave unchanged (at its original value) for the second. So we
either construct two different sets of envp upfront, or bother with
saving/restoring PWD (or lack thereof). Alternatively, set PWD in vtespawn.cc
vte_spawn_async_with_pipes_cancellable() which is far away from setting all
other env vars, and works on the totally inconvenient gchar** array rather
than the convenient hash table used in __vte_pty_merge_environ(). All these approaches are truly ugly and cumbersome to implement and no fun at all.

So I just went with the simplest, which is by the way what happens currently when gnome-terminal (and other vte-based frontends) manually set PWD: it's also there for the fallback spawn.

I think it's good enough because PWD is ignored by bash (and other apps) if it doesn't match the actual working directory. The only role of PWD is to add the extra bit of info about the symlinks traversed to get to the cwd.

g-t:

There's something I'm unsure about, see the patch. I guess I'll just leave that line unchanged.
Comment 4 Christian Persch 2017-12-31 11:57:57 UTC
Comment on attachment 360997 [details] [review]
vte bits: set $PWD

Looks good, but please add a short text to the vte_terminal_spawn_sync() and spawn_async() docs that explain that the caller should have used g_get_current_dir() (from glib) or get_current_dir_name() (from glibc) to get the  @working_directory (those 2 return $PWD if it's correct) instead of plain getcwd(), so that symlinks are preserved.
Comment 5 Christian Persch 2017-12-31 11:59:12 UTC
Comment on attachment 360998 [details] [review]
g-t bits: don't set $PWD

The line in terminal-client-utils just filters which env vars are forwarded to the server, so keeping PWD in the list is harmless (and since the PWD env will be unused, there's no point forwarding it). So keep that part as-is.

master only, since this requires the vte change. Thanks!
Comment 6 Egmont Koblinger 2018-02-06 18:25:37 UTC
Fixed.