From 2773e5468f237599f792f04a09b20211a5a7b9f3 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Thu, 29 May 2025 22:35:17 -0700 Subject: [PATCH] core/process: ignore environment changes made by the Env pragma This pragma ends up used to set things like QQC theme which shouldn't be cascaded into child processes. --- src/core/common.cpp | 4 +++- src/core/common.hpp | 2 ++ src/core/desktopentry.cpp | 2 ++ src/io/process.cpp | 31 +++++++++++++++---------------- src/launch/launch.cpp | 3 +++ 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/core/common.cpp b/src/core/common.cpp index d09661f1..5928e470 100644 --- a/src/core/common.cpp +++ b/src/core/common.cpp @@ -1,9 +1,11 @@ #include "common.hpp" #include +#include namespace qs { const QDateTime Common::LAUNCH_TIME = QDateTime::currentDateTime(); +QProcessEnvironment Common::INITIAL_ENVIRONMENT = {}; // NOLINT -} +} // namespace qs diff --git a/src/core/common.hpp b/src/core/common.hpp index 36094f89..f2a01bc1 100644 --- a/src/core/common.hpp +++ b/src/core/common.hpp @@ -1,11 +1,13 @@ #pragma once #include +#include namespace qs { struct Common { static const QDateTime LAUNCH_TIME; + static QProcessEnvironment INITIAL_ENVIRONMENT; // NOLINT }; } // namespace qs diff --git a/src/core/desktopentry.cpp b/src/core/desktopentry.cpp index 77a16388..541207ee 100644 --- a/src/core/desktopentry.cpp +++ b/src/core/desktopentry.cpp @@ -16,6 +16,7 @@ #include #include +#include "common.hpp" #include "model.hpp" namespace { @@ -260,6 +261,7 @@ void DesktopEntry::doExec(const QString& execString, const QString& workingDirec process.setProgram(args.at(0)); process.setArguments(args.sliced(1)); if (!workingDirectory.isEmpty()) process.setWorkingDirectory(workingDirectory); + process.setProcessEnvironment(qs::Common::INITIAL_ENVIRONMENT); process.startDetached(); } diff --git a/src/io/process.cpp b/src/io/process.cpp index a532ac8f..143fdec8 100644 --- a/src/io/process.cpp +++ b/src/io/process.cpp @@ -13,6 +13,7 @@ #include #include +#include "../core/common.hpp" #include "../core/generation.hpp" #include "../core/qmlglobal.hpp" #include "datastream.hpp" @@ -223,26 +224,24 @@ void Process::setupEnvironment(QProcess* process) { process->setWorkingDirectory(this->mWorkingDirectory); } - if (!this->mEnvironment.isEmpty() || this->mClearEnvironment) { - auto sysenv = QProcessEnvironment::systemEnvironment(); - auto env = this->mClearEnvironment ? QProcessEnvironment() : sysenv; + const auto& sysenv = qs::Common::INITIAL_ENVIRONMENT; + auto env = this->mClearEnvironment ? QProcessEnvironment() : sysenv; - for (auto& name: this->mEnvironment.keys()) { - auto value = this->mEnvironment.value(name); - if (!value.isValid()) continue; + for (auto& name: this->mEnvironment.keys()) { + auto value = this->mEnvironment.value(name); + if (!value.isValid()) continue; - if (this->mClearEnvironment) { - if (value.isNull()) { - if (sysenv.contains(name)) env.insert(name, sysenv.value(name)); - } else env.insert(name, value.toString()); - } else { - if (value.isNull()) env.remove(name); - else env.insert(name, value.toString()); - } + if (this->mClearEnvironment) { + if (value.isNull()) { + if (sysenv.contains(name)) env.insert(name, sysenv.value(name)); + } else env.insert(name, value.toString()); + } else { + if (value.isNull()) env.remove(name); + else env.insert(name, value.toString()); } - - process->setProcessEnvironment(env); } + + process->setProcessEnvironment(env); } void Process::onStarted() { diff --git a/src/launch/launch.cpp b/src/launch/launch.cpp index 5d265442..fe28a942 100644 --- a/src/launch/launch.cpp +++ b/src/launch/launch.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -151,6 +152,8 @@ int launch(const LaunchArgs& args, char** argv, QCoreApplication* coreApplicatio QsPaths::instance()->linkPathDir(); LogManager::initFs(); + Common::INITIAL_ENVIRONMENT = QProcessEnvironment::systemEnvironment(); + for (auto [var, val]: pragmas.envOverrides.asKeyValueRange()) { qputenv(var.toUtf8(), val.toUtf8()); }