Bug 420085 - User autostart scripts sourced after system-level scripts
Summary: User autostart scripts sourced after system-level scripts
Status: RESOLVED FIXED
Alias: None
Product: plasmashell
Classification: Plasma
Component: general (show other bugs)
Version: 5.18.4
Platform: Other Linux
: NOR normal
Target Milestone: 1.0
Assignee: David Edmundson
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-04-14 16:36 UTC by Francis T
Modified: 2020-04-28 13:53 UTC (History)
4 users (show)

See Also:
Latest Commit:
Version Fixed In: 5.19.0


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Francis T 2020-04-14 16:36:17 UTC
SUMMARY

I am trying to use a shell script placed in ~/.config/plasma-workspace/env/ to set my `$EDITOR` variable, as described in the KDE docs here: https://userbase.kde.org/Session_Environment_Variables

The script is correctly executed during login, and $EDITOR is correctly set at one point. However, later on it is overwritten by the value specified in /etc/xdg/plasma-workspace/env/envars.sh. This file is provided by the Manjaro distribution to provide default values of common environment variables (such as PATH and EDITOR).

User preferences should prevail over system defaults. I would expect that the $EDITOR value I set in my user-specific session autostart scripts should be the final, active value set after session startup is complete.

STEPS TO REPRODUCE

1. Create the user vars file:

echo "export EDITOR=/usr/bin/nvim" > $HOME/.config/plasma-workspace/env/uservars.sh
echo "export EDITOR2=bar" >> $HOME/.config/plasma-workspace/env/uservars.sh
chmod +x $HOME/.config/plasma-workspace/env/uservars.sh

2. Edit (or create) the system level envars file:

Edit the file /etc/xdg/plasma-workspace/env/envars.sh. Change the line "export EDITOR=/usr/bin/nano"  to "export EDITOR=FOO".

3. Log out and log back in.

OBSERVED RESULT

The EDITOR2 variable is set, indicating that the user script was executed. However, the EDITOR variable was overwritten by the system-level script:

$> echo $EDITOR
FOO

$> echo $EDITOR2
bar

FINAL NOTE

I am not sure which script/process executes these files. It seems the order is wrong. The KDE docs (linked above) say that the "startkde" script runs them, yet it appears that this script was replaced by a binary named "startplasma-x11". I searched KDE's phabricator but was not able to find the source for this file. If someone can point me to the source file, I can grep through it with my half-understanding of C++/Qt and attempt to find the source of the bug.

I should also note that according to pacman, /etc/xdg/plasma-workspace/env/envars.sh belongs to the package `manjaro-kde-settings-19.0 20200408-2`.
Comment 1 Rex Dieter 2020-04-14 16:42:05 UTC
I'd encourage you to also submit this issue to your distribution.  They could easily adjust their script to not override any existing definition of the EDITOR variable.
Comment 2 David Edmundson 2020-04-14 16:47:23 UTC
startplasma.cpp:
runEnvironmentScripts()

They are run alphabetically, which at a minimum is usable as a workaround for you, 

I think it's also the correct approach, especially if people follow the convention common for merged config files 00-foo  10-run-after 99-foolast
Comment 3 Nate Graham 2020-04-14 21:17:24 UTC
Alphabetical within each scope makes sense to me, but I agree that user config should always take precedence over system config when both are present.
Comment 4 Francis T 2020-04-15 00:37:19 UTC
Odd, I thought I had replied previously.

Anyways, I had a look at startplasma.cpp here: https://phabricator.kde.org/source/plasma-workspace/browse/master/startkde/startplasma.cpp$193

I don't think anything in the function in question sorts the scripts before sourcing. entryInfoList has an optional argument for sorting (which is not used, and the default is NoSort), but even if we used it, we'd be sorting each directory separately, which is not very useful.

Adding a sort call before sourcefiles appears to be a 1-line fix. However, I personally agree with Nate that user scripts should be sourced after system scripts.

I can try and put together a patch. However, C++ (and qt) is really not my forte, so if anyone wants to take a stab, please have at it.
Comment 5 Nate Graham 2020-04-15 02:27:06 UTC
Sounds like you've already done all the investigation. Please feel free! Our documentation is here, in case it helps: https://community.kde.org/Get_Involved/development
Comment 6 Francis T 2020-04-18 13:41:37 UTC
Patch submmitted here: https://phabricator.kde.org/D28941
Comment 7 Nate Graham 2020-04-28 13:53:06 UTC
Git commit ed09a3baf3e00b6f831132ae55391548130f1d48 by Nate Graham, on behalf of Francis Thérien.
Committed on 28/04/2020 at 13:52.
Pushed by ngraham into branch 'master'.

startplasma: Sort environment scripts prior to sourcing

Summary:
Sort environment scripts before they are sourced. User scripts should run after system scripts to ensure that user preferences take precendence over system defaults. Scripts in each location (user and system) are then separately sorted in lexical order to ensure deterministic source order.
FIXED-IN: 5.19.0

Reviewers: #plasma, davidedmundson, apol

Reviewed By: apol

Subscribers: cfeck, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D28941

M  +17   -4    startkde/startplasma.cpp

https://commits.kde.org/plasma-workspace/ed09a3baf3e00b6f831132ae55391548130f1d48