Python: Avoid polluting global or virtual environments with pylsp

Install the language server into the Qt Creator resource directory
instead. Reuse the server for all interpreters with the same
version. This also reduces the amount of editor toolbars asking the user
to install a language server for a specific interpreter.

Change-Id: I48ef4ad30fe0097ee8d2b855b0f278e98be5ce57
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2023-11-27 14:15:29 +01:00
parent 1200419059
commit fbe054116a
6 changed files with 68 additions and 53 deletions

View File

@@ -55,6 +55,11 @@ void PipInstallTask::setPackages(const QList<PipPackage> &packages)
m_packages = packages;
}
void PipInstallTask::setTargetPath(const Utils::FilePath &targetPath)
{
m_targetPath = targetPath;
}
void PipInstallTask::run()
{
if (m_packages.isEmpty() && m_requirementsFile.isEmpty()) {
@@ -75,9 +80,10 @@ void PipInstallTask::run()
}
}
// add --user to global pythons, but skip it for venv pythons
if (!QDir(m_python.parentDir().toString()).exists("activate"))
arguments << "--user";
if (!m_targetPath.isEmpty())
arguments << "-t" << m_targetPath.toString();
else if (!QDir(m_python.parentDir().toString()).exists("activate"))
arguments << "--user"; // add --user to global pythons, but skip it for venv pythons
m_process.setCommand({m_python, arguments});
m_process.setTerminalMode(TerminalMode::Run);

View File

@@ -67,6 +67,7 @@ public:
void setWorkingDirectory(const Utils::FilePath &workingDirectory);
void addPackage(const PipPackage &package);
void setPackages(const QList<PipPackage> &packages);
void setTargetPath(const Utils::FilePath &targetPath);
void run();
signals:
@@ -83,6 +84,7 @@ private:
const Utils::FilePath m_python;
QList<PipPackage> m_packages;
Utils::FilePath m_requirementsFile;
Utils::FilePath m_targetPath;
Utils::Process m_process;
QFutureInterface<void> m_future;
QFutureWatcher<void> m_watcher;

View File

@@ -72,63 +72,32 @@ static QHash<FilePath, PyLSClient*> &pythonClients()
return clients;
}
FilePath getPylsModulePath(CommandLine pylsCommand)
static FilePath pyLspPath(const FilePath &python)
{
static QMutex mutex; // protect the access to the cache
QMutexLocker locker(&mutex);
static QMap<FilePath, FilePath> cache;
const FilePath &modulePath = cache.value(pylsCommand.executable());
if (!modulePath.isEmpty())
return modulePath;
pylsCommand.addArg("-h");
Process pythonProcess;
Environment env = pythonProcess.environment();
env.set("PYTHONVERBOSE", "x");
pythonProcess.setEnvironment(env);
pythonProcess.setCommand(pylsCommand);
pythonProcess.runBlocking();
static const QString pylsInitPattern = "(.*)"
+ QRegularExpression::escape(
QDir::toNativeSeparators("/pylsp/__init__.py"))
+ '$';
static const QRegularExpression regexCached(" matches " + pylsInitPattern,
QRegularExpression::MultilineOption);
static const QRegularExpression regexNotCached(" code object from " + pylsInitPattern,
QRegularExpression::MultilineOption);
const QString output = pythonProcess.allOutput();
for (const auto &regex : {regexCached, regexNotCached}) {
const QRegularExpressionMatch result = regex.match(output);
if (result.hasMatch()) {
const FilePath &modulePath = FilePath::fromUserInput(result.captured(1));
cache[pylsCommand.executable()] = modulePath;
return modulePath;
}
}
if (python.needsDevice())
return {};
const QString version = pythonVersion(python);
if (version.isEmpty())
return {};
return Core::ICore::userResourcePath("pylsp") / FileUtils::fileSystemFriendlyName(version);
}
static PythonLanguageServerState checkPythonLanguageServer(const FilePath &python)
{
using namespace LanguageClient;
const CommandLine pythonLShelpCommand(python, {"-m", "pylsp", "-h"});
const FilePath &modulePath = getPylsModulePath(pythonLShelpCommand);
auto lspPath = pyLspPath(python);
if (lspPath.isEmpty())
return {PythonLanguageServerState::CanNotBeInstalled, FilePath()};
if (lspPath.pathAppended("bin").pathAppended("pylsp").withExecutableSuffix().exists())
return {PythonLanguageServerState::AlreadyInstalled, lspPath};
Process pythonProcess;
pythonProcess.setTimeoutS(2);
pythonProcess.setCommand(pythonLShelpCommand);
pythonProcess.runBlocking();
if (pythonProcess.allOutput().contains("Python Language Server"))
return {PythonLanguageServerState::AlreadyInstalled, modulePath};
pythonProcess.setCommand({python, {"-m", "pip", "-V"}});
pythonProcess.runBlocking();
if (pythonProcess.allOutput().startsWith("pip "))
return {PythonLanguageServerState::CanBeInstalled, FilePath()};
else
return {PythonLanguageServerState::CanBeInstalled, lspPath};
return {PythonLanguageServerState::CanNotBeInstalled, FilePath()};
}
@@ -150,6 +119,12 @@ protected:
env.appendOrSet("PYTHONPATH",
m_extraPythonPath.path().toString(),
OsSpecificAspects::pathListSeparator(env.osType()));
const FilePath lspPath = pyLspPath(m_cmd.executable());
if (!lspPath.isEmpty() && lspPath.exists()) {
env.appendOrSet("PYTHONPATH",
pyLspPath(m_cmd.executable()).toString(),
OsSpecificAspects::pathListSeparator(env.osType()));
}
setEnvironment(env);
}
StdIOClientInterface::startImpl();
@@ -293,7 +268,8 @@ PyLSConfigureAssistant *PyLSConfigureAssistant::instance()
}
void PyLSConfigureAssistant::installPythonLanguageServer(const FilePath &python,
QPointer<TextEditor::TextDocument> document)
QPointer<TextEditor::TextDocument> document,
const FilePath &pylsPath)
{
document->infoBar()->removeInfo(installPylsInfoBarId);
@@ -317,6 +293,7 @@ void PyLSConfigureAssistant::installPythonLanguageServer(const FilePath &python,
install->deleteLater();
});
install->setTargetPath(pylsPath);
install->setPackages({PipPackage{"python-lsp-server[all]", "Python Language Server"}});
install->run();
}
@@ -376,8 +353,9 @@ void PyLSConfigureAssistant::handlePyLSState(const FilePath &python,
Utils::InfoBarEntry info(installPylsInfoBarId,
message,
Utils::InfoBarEntry::GlobalSuppression::Enabled);
info.addCustomButton(Tr::tr("Install"),
[=]() { installPythonLanguageServer(python, document); });
info.addCustomButton(Tr::tr("Install"), [=]() {
installPythonLanguageServer(python, document, state.pylsModulePath);
});
infoBar->addInfo(info);
m_infoBarEntries[python] << document;
} else if (state.state == PythonLanguageServerState::AlreadyInstalled) {

View File

@@ -64,7 +64,8 @@ private:
TextEditor::TextDocument *document);
void resetEditorInfoBar(TextEditor::TextDocument *document);
void installPythonLanguageServer(const Utils::FilePath &python,
QPointer<TextEditor::TextDocument> document);
QPointer<TextEditor::TextDocument> document,
const Utils::FilePath &pylsPath);
QHash<Utils::FilePath, QList<TextEditor::TextDocument *>> m_infoBarEntries;
QHash<TextEditor::TextDocument *, QPointer<QFutureWatcher<PythonLanguageServerState>>>

View File

@@ -21,6 +21,8 @@
#include <utils/mimeutils.h>
#include <utils/process.h>
#include <QReadLocker>
using namespace ProjectExplorer;
using namespace Utils;
@@ -208,4 +210,28 @@ bool pipIsUsable(const Utils::FilePath &python)
return process.result() == ProcessResult::FinishedWithSuccess;
}
QString pythonVersion(const FilePath &python)
{
static QReadWriteLock lock;
static QMap<FilePath, QString> versionCache;
{
QReadLocker locker(&lock);
auto it = versionCache.constFind(python);
if (it != versionCache.constEnd())
return *it;
}
Process p;
p.setCommand({python, {"--version"}});
p.runBlocking();
if (p.result() == Utils::ProcessResult::FinishedWithSuccess) {
const QString version = p.readAllStandardOutput().trimmed();
QWriteLocker locker(&lock);
versionCache.insert(python, version);
return version;
}
return QString();
}
} // Python::Internal

View File

@@ -24,4 +24,6 @@ bool isVenvPython(const Utils::FilePath &python);
bool venvIsUsable(const Utils::FilePath &python);
bool pipIsUsable(const Utils::FilePath &python);
QString pythonVersion(const Utils::FilePath &python);
} // Python::Internal