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.
This commit is contained in:
outfoxxed 2025-05-29 22:35:17 -07:00
parent 4a0f6382b0
commit 2773e5468f
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
5 changed files with 25 additions and 17 deletions

View file

@ -1,9 +1,11 @@
#include "common.hpp" #include "common.hpp"
#include <qdatetime.h> #include <qdatetime.h>
#include <qprocess.h>
namespace qs { namespace qs {
const QDateTime Common::LAUNCH_TIME = QDateTime::currentDateTime(); const QDateTime Common::LAUNCH_TIME = QDateTime::currentDateTime();
QProcessEnvironment Common::INITIAL_ENVIRONMENT = {}; // NOLINT
} } // namespace qs

View file

@ -1,11 +1,13 @@
#pragma once #pragma once
#include <qdatetime.h> #include <qdatetime.h>
#include <qprocess.h>
namespace qs { namespace qs {
struct Common { struct Common {
static const QDateTime LAUNCH_TIME; static const QDateTime LAUNCH_TIME;
static QProcessEnvironment INITIAL_ENVIRONMENT; // NOLINT
}; };
} // namespace qs } // namespace qs

View file

@ -16,6 +16,7 @@
#include <qtenvironmentvariables.h> #include <qtenvironmentvariables.h>
#include <ranges> #include <ranges>
#include "common.hpp"
#include "model.hpp" #include "model.hpp"
namespace { namespace {
@ -260,6 +261,7 @@ void DesktopEntry::doExec(const QString& execString, const QString& workingDirec
process.setProgram(args.at(0)); process.setProgram(args.at(0));
process.setArguments(args.sliced(1)); process.setArguments(args.sliced(1));
if (!workingDirectory.isEmpty()) process.setWorkingDirectory(workingDirectory); if (!workingDirectory.isEmpty()) process.setWorkingDirectory(workingDirectory);
process.setProcessEnvironment(qs::Common::INITIAL_ENVIRONMENT);
process.startDetached(); process.startDetached();
} }

View file

@ -13,6 +13,7 @@
#include <qtypes.h> #include <qtypes.h>
#include <qvariant.h> #include <qvariant.h>
#include "../core/common.hpp"
#include "../core/generation.hpp" #include "../core/generation.hpp"
#include "../core/qmlglobal.hpp" #include "../core/qmlglobal.hpp"
#include "datastream.hpp" #include "datastream.hpp"
@ -223,26 +224,24 @@ void Process::setupEnvironment(QProcess* process) {
process->setWorkingDirectory(this->mWorkingDirectory); process->setWorkingDirectory(this->mWorkingDirectory);
} }
if (!this->mEnvironment.isEmpty() || this->mClearEnvironment) { const auto& sysenv = qs::Common::INITIAL_ENVIRONMENT;
auto sysenv = QProcessEnvironment::systemEnvironment(); auto env = this->mClearEnvironment ? QProcessEnvironment() : sysenv;
auto env = this->mClearEnvironment ? QProcessEnvironment() : sysenv;
for (auto& name: this->mEnvironment.keys()) { for (auto& name: this->mEnvironment.keys()) {
auto value = this->mEnvironment.value(name); auto value = this->mEnvironment.value(name);
if (!value.isValid()) continue; if (!value.isValid()) continue;
if (this->mClearEnvironment) { if (this->mClearEnvironment) {
if (value.isNull()) { if (value.isNull()) {
if (sysenv.contains(name)) env.insert(name, sysenv.value(name)); if (sysenv.contains(name)) env.insert(name, sysenv.value(name));
} else env.insert(name, value.toString()); } else env.insert(name, value.toString());
} else { } else {
if (value.isNull()) env.remove(name); if (value.isNull()) env.remove(name);
else env.insert(name, value.toString()); else env.insert(name, value.toString());
}
} }
process->setProcessEnvironment(env);
} }
process->setProcessEnvironment(env);
} }
void Process::onStarted() { void Process::onStarted() {

View file

@ -9,6 +9,7 @@
#include <qlist.h> #include <qlist.h>
#include <qlogging.h> #include <qlogging.h>
#include <qnamespace.h> #include <qnamespace.h>
#include <qprocess.h>
#include <qqmldebug.h> #include <qqmldebug.h>
#include <qquickwindow.h> #include <qquickwindow.h>
#include <qstring.h> #include <qstring.h>
@ -151,6 +152,8 @@ int launch(const LaunchArgs& args, char** argv, QCoreApplication* coreApplicatio
QsPaths::instance()->linkPathDir(); QsPaths::instance()->linkPathDir();
LogManager::initFs(); LogManager::initFs();
Common::INITIAL_ENVIRONMENT = QProcessEnvironment::systemEnvironment();
for (auto [var, val]: pragmas.envOverrides.asKeyValueRange()) { for (auto [var, val]: pragmas.envOverrides.asKeyValueRange()) {
qputenv(var.toUtf8(), val.toUtf8()); qputenv(var.toUtf8(), val.toUtf8());
} }