Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmllsmain.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qmllsmain_p.h"
6
7#include <private/qhttpmessagestreamparser_p.h>
8#include <private/qqmlglobal_p.h>
9#include <private/qqmljscompiler_p.h>
10#include <private/qqmljsimporter_p.h>
11#include <private/qqmljslogger_p.h>
12#include <private/qqmljsresourcefilemapper_p.h>
13#include <private/qqmljsscope_p.h>
14#include <private/qqmllanguageserver_p.h>
15#include <private/qqmltoolingsettings_p.h>
16#include <private/qqmltoolingutils_p.h>
17
18#include <QtCore/qdebug.h>
19#include <QtCore/qfile.h>
20#include <QtCore/qdir.h>
21#include <QtCore/qfileinfo.h>
22#include <QtCore/qcoreapplication.h>
23#include <QtCore/qdiriterator.h>
24#include <QtCore/qjsonobject.h>
25#include <QtCore/qjsonarray.h>
26#include <QtCore/qjsondocument.h>
27#include <QtCore/qmutex.h>
28#include <QtCore/QMutexLocker>
29#include <QtCore/qscopedpointer.h>
30#include <QtCore/qrunnable.h>
31#include <QtCore/qthreadpool.h>
32#include <QtCore/qtimer.h>
33
34#if QT_CONFIG(commandlineparser)
35# include <QtCore/qcommandlineparser.h>
36#endif
37
38#ifndef QT_BOOTSTRAPPED
39# include <QtCore/qlibraryinfo.h>
40#endif
41
42#include <iostream>
43#ifdef Q_OS_WIN32
44# include <fcntl.h>
45# include <io.h>
46#endif
47
48QT_BEGIN_NAMESPACE
49
50namespace QmlLsp {
51
52QFile *logFile = nullptr;
54
55class AbstractReader : public QObject
56{
58public:
59 virtual ~AbstractReader() { }
60
61protected:
62 virtual void readNextMessageImpl() = 0;
65 void eof();
66public slots:
68};
69
70class StdinReader final : public AbstractReader
71{
72 Q_OBJECT
73public:
74 StdinReader()
75 : m_streamReader(
76 [](const QByteArray &, const QByteArray &) { /* just a header, do nothing */ },
77 [this](const QByteArray &) {
78 // stop reading until we are sure that the server is not shutting down
79 m_isReading = false;
80
81 // message body
82 m_shouldSendData = true;
83 },
84 [this](QtMsgType, const QString &) {
85 // there was an error
86 m_shouldSendData = true;
87 },
88 QHttpMessageStreamParser::UNBUFFERED)
89 {
90 }
91
92 void sendData()
93 {
94 const bool isEndOfMessage = !m_isReading && !m_hasEof;
95 const qsizetype toSend = m_bytesInBuf;
96 m_bytesInBuf = 0;
97 const QByteArray dataToSend(m_buffer, toSend);
98 emit receivedData(dataToSend, isEndOfMessage);
99 }
100
101private:
102 const static constexpr qsizetype s_bufSize = 1024;
103 qsizetype m_bytesInBuf = 0;
104 char m_buffer[2 * s_bufSize] = {};
105 QHttpMessageStreamParser m_streamReader;
106 /*!
107 \internal
108 Indicates if the current message is not read out entirely.
109 */
110 bool m_isReading = true;
111 /*!
112 \internal
113 Indicates if an EOF was encountered. No more data can be read after an EOF.
114 */
115 bool m_hasEof = false;
116 /*!
117 \internal
118 Indicates whether sendData() should be called or not.
119 */
120 bool m_shouldSendData = false;
121
122protected:
124 {
125 if (m_hasEof)
126 return;
127 m_isReading = true;
128 // Try to fill up the buffer as much as possible before calling the queued signal:
129 // each loop iteration might read only one character from std::in in the worstcase, this
130 // happens for example on macos.
131 while (m_isReading) {
132 // block while waiting for some data
133 if (!std::cin.get(m_buffer[m_bytesInBuf])) {
134 qInfo() << "Received EOF, stopping...";
135 m_hasEof = true;
136 emit eof();
137 return;
138 }
139 // see if more data is available and fill the buffer with it
140 qsizetype readNow = std::cin.readsome(m_buffer + m_bytesInBuf + 1, s_bufSize) + 1;
141 QByteArray toAdd(m_buffer + m_bytesInBuf, readNow);
142 m_bytesInBuf += readNow;
143 m_streamReader.receiveData(std::move(toAdd));
144
145 m_shouldSendData |= m_bytesInBuf >= s_bufSize;
146 if (std::exchange(m_shouldSendData, false))
147 sendData();
148 }
149 }
150};
151
152/*!
153 \internal
154 FileReader allows to read JsonRPC commands from a file. This is useful when the JsonRPC
155 commands can't be read from stdin, for example when running qmlls in the macos profiler.
156 */
157class FileReader final : public AbstractReader
158{
159 Q_OBJECT
160public:
161 FileReader(const QString &fileName) : m_file(fileName)
162 {
163 bool isOpen = m_file.open(QFile::ReadOnly);
164 Q_ASSERT(isOpen);
165 }
166
167private:
168 QFile m_file;
169
170protected:
172 {
173 if (!m_file.isOpen())
174 return;
175 emit receivedData(m_file.readAll(), false);
176 // emit eof will shutdown qmlls before it manages to complete the preceding requests, so
177 // don't emit eof when reading from a file.
178 // also, just read the content of the file once, so close it now
179 m_file.close();
180 }
181};
182
183static QStringList collectImportPaths(const QCommandLineParser &parser,
184 const QCommandLineOption &qmlImportPathOption,
185 const QCommandLineOption &environmentOption,
186 const QCommandLineOption &qmlImportNoDefault)
187{
188 QStringList importPaths;
189 if (parser.isSet(qmlImportPathOption)) {
190 const QStringList pathsFromOption =
191 QQmlToolingUtils::getAndWarnForInvalidDirsFromOption(parser, qmlImportPathOption);
192 qInfo().nospace().noquote() << "Using import directories passed by -I: \""
193 << pathsFromOption.join(u"\", \""_s) << "\".";
194 importPaths << pathsFromOption;
195 }
196 if (parser.isSet(environmentOption)) {
197 if (const QStringList dirsFromEnv =
198 QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(u"QML_IMPORT_PATH"_s);
199 !dirsFromEnv.isEmpty()) {
200 qInfo().nospace().noquote()
201 << "Using import directories passed from environment variable "
202 "\"QML_IMPORT_PATH\": \""
203 << dirsFromEnv.join(u"\", \""_s) << "\".";
204 importPaths << dirsFromEnv;
205 }
206
207 if (const QStringList dirsFromEnv2 =
208 QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(u"QML2_IMPORT_PATH"_s);
209 !dirsFromEnv2.isEmpty()) {
210 qInfo().nospace().noquote()
211 << "Using import directories passed from the deprecated environment variable "
212 "\"QML2_IMPORT_PATH\": \""
213 << dirsFromEnv2.join(u"\", \""_s) << "\".";
214 importPaths << dirsFromEnv2;
215 }
216 }
217
218 // add as default fallback at the end
219 if (!parser.isSet(qmlImportNoDefault))
220 importPaths << QLibraryInfo::paths(QLibraryInfo::QmlImportsPath);
221 return importPaths;
222}
223
224static int cmakeJobs(const QCommandLineParser &parser, const QCommandLineOption &cmakeJobsOption)
225{
226 auto parseAndWarn = [](const QString &valueString, const QString &infoMessage,
227 const QString &warningMessage) {
228 bool ok = false;
229 if (valueString == QQmlCodeModel::s_maxCMakeJobs) {
230 const int value = QThread::idealThreadCount();
231 qInfo().noquote() << infoMessage.arg("max (%1)"_L1.arg(QString::number(value)));
232 return value;
233 }
234
235 const int value = valueString.toInt(&ok);
236 if (!ok || value < 1) {
237 qInfo().noquote() << warningMessage.arg(valueString);
238 return QQmlCodeModel::s_defaultCMakeJobs;
239 }
240 qInfo().noquote() << infoMessage.arg(QString::number(value));
241 return value;
242 };
243
244 if (parser.isSet(cmakeJobsOption)) {
245 return parseAndWarn(
246 parser.value(cmakeJobsOption), "Using %1 jobs for CMake, set via --cmake-jobs."_L1,
247 "Value \"%1\" passed to --cmake-jobs is not a number greater than 0 and not "
248 "\"max\", using default value of 1 instead."_L1);
249 }
250
251 if (!qEnvironmentVariableIsSet("QMLLS_CMAKE_JOBS")) {
252 qInfo() << "Using 1 job for CMake, you can increase that value with the QMLLS_CMAKE_JOBS "
253 "environment variable or the --cmake-jobs option.";
254 return QQmlCodeModel::s_defaultCMakeJobs;
255 }
256 return parseAndWarn(
257 qEnvironmentVariable("QMLLS_CMAKE_JOBS"),
258 "Using %1 jobs for CMake, set via QMLLS_CMAKE_JOBS environment variable."_L1,
259 "Value \"%1\" passed to QMLLS_CMAKE_JOBS is not a number greater than 0 and not "
260 "\"max\", using default value of 1 instead."_L1);
261}
262
263static bool prepareModuleExplorationFeature(const QCommandLineParser &parser,
264 const QCommandLineOption &noModuleExplorationOption)
265{
266 if (qmlGetConfigOption<bool, qmlConvertBoolConfigOption>("QMLLS_NO_MODULE_EXPLORATION")) {
267 qWarning() << "Disabling module exploration via QMLLS_NO_MODULE_EXPLORATION environment "
268 "variable.";
269 return false;
270 }
271 if (parser.isSet(noModuleExplorationOption)) {
272 qWarning() << "Disabling module exploration via command line switch.";
273 return false;
274 }
275 return true;
276}
277
278static bool prepareCMakeFeature(const QCommandLineParser &parser,
279 const QCommandLineOption &noCMakeCallsOption)
280{
281 if (qmlGetConfigOption<bool, qmlConvertBoolConfigOption>("QMLLS_NO_CMAKE_CALLS")) {
282 qWarning() << "Disabling CMake calls via QMLLS_NO_CMAKE_CALLS environment variable.";
283 return false;
284 }
285 if (parser.isSet(noCMakeCallsOption)) {
286 qWarning() << "Disabling CMake calls via command line switch.";
287 return false;
288 }
289 return true;
290}
291
292// To debug:
293//
294// * simple logging can be redirected to a file
295// passing -l <file> to the qmlls command
296//
297// * more complex debugging can use named pipes:
298//
299// mkfifo qmllsIn
300// mkfifo qmllsOut
301//
302// this together with a qmllsEcho script that can be defined as
303//
304// #!/bin/sh
305// cat -u < ~/qmllsOut &
306// cat -u > ~/qmllsIn
307//
308// allows to use qmllsEcho as lsp server, and still easily start
309// it in a terminal
310//
311// qmlls < ~/qmllsIn > ~/qmllsOut
312//
313// * statup can be slowed down to have the time to attach via the
314// -w <nSeconds> flag.
315
316int qmllsMain(int argv, char *argc[])
317{
318#ifdef Q_OS_WIN32
319 // windows does not open stdin/stdout in binary mode by default
320 int err = _setmode(_fileno(stdout), _O_BINARY);
321 if (err == -1)
322 perror("Cannot set mode for stdout");
323 err = _setmode(_fileno(stdin), _O_BINARY);
324 if (err == -1)
325 perror("Cannot set mode for stdin");
326#endif
327
328 QHashSeed::setDeterministicGlobalSeed();
329 QCoreApplication app(argv, argc);
330
331 QCommandLineParser parser;
332 QQmlToolingSharedSettings settings("qmlls"_L1);
333 parser.setApplicationDescription("QML languageserver"_L1);
334
335 parser.addHelpOption();
336 QCommandLineOption waitOption(QStringList() << "w"_L1
337 << "wait"_L1,
338 "Waits the given number of seconds before startup"_L1,
339 "waitSeconds"_L1);
340 parser.addOption(waitOption);
341
342 QCommandLineOption verboseOption(
343 QStringList() << "v"_L1
344 << "verbose"_L1,
345 "Outputs extra information on the operations being performed"_L1);
346 parser.addOption(verboseOption);
347
348 QCommandLineOption logFileOption(QStringList() << "l"_L1
349 << "log-file"_L1,
350 "Writes logging to the given file"_L1, "logFile"_L1);
351 parser.addOption(logFileOption);
352
353 QString buildDir = QStringLiteral(u"buildDir");
354 QCommandLineOption buildDirOption(QStringList() << "b"_L1
355 << "build-dir"_L1,
356 "Adds a build dir to look up for qml information"_L1,
357 buildDir);
358 parser.addOption(buildDirOption);
359 settings.addOption(buildDir);
360
361 QString qmlImportPath = QStringLiteral(u"importPaths");
362 QCommandLineOption qmlImportPathOption(QStringList() << "I"_L1,
363 "Look for QML modules in the specified directory"_L1,
364 qmlImportPath);
365 parser.addOption(qmlImportPathOption);
366 settings.addOption(qmlImportPath);
367
368 QCommandLineOption environmentOption(
369 QStringList() << "E"_L1,
370 "Use the QML_IMPORT_PATH environment variable to look for QML Modules"_L1);
371 parser.addOption(environmentOption);
372
373 QCommandLineOption writeDefaultsOption(
374 QStringList() << "write-defaults"_L1,
375 "Writes defaults settings to .qmlls.ini and exits (Warning: This "
376 "will overwrite any existing settings and comments!)"_L1);
377 parser.addOption(writeDefaultsOption);
378
379 QCommandLineOption ignoreSettings(QStringList() << "ignore-settings"_L1,
380 "Ignores all settings files and only takes "
381 "command line options into consideration"_L1);
382 parser.addOption(ignoreSettings);
383
384 QCommandLineOption noCMakeCallsOption(
385 QStringList() << "no-cmake-calls"_L1,
386 "Disables automatic CMake rebuilds and C++ file watching."_L1);
387 parser.addOption(noCMakeCallsOption);
388 settings.addOption("no-cmake-calls"_L1, "false"_L1);
389
390 QCommandLineOption noModuleExplorationOption(
391 QStringList() << "no-module-exploration"_L1,
392 "Disables the background search for importable QML modules in the import paths."_L1);
393 parser.addOption(noModuleExplorationOption);
394 settings.addOption("no-module-exploration"_L1, "false"_L1);
395
396 QCommandLineOption cmakeJobsOption(QStringList() << "cmake-jobs"_L1 << "j"_L1,
397 "Number of CMake jobs for automatic CMake rebuilds. "_L1,
398 "jobs"_L1, "1"_L1);
399 parser.addOption(cmakeJobsOption);
400 settings.addOption("CMakeJobs"_L1, "1"_L1);
401
402 QCommandLineOption docDir({ { "d"_L1, "p"_L1, "doc-dir"_L1 },
403 "Documentation path to use for the documentation hints feature"_L1,
404 "path"_L1,
405 QString() });
406 parser.addOption(docDir);
407 settings.addOption("docDir"_L1);
408
409 QCommandLineOption qmlImportNoDefault(QStringList() << "bare"_L1,
410 "Do not include default import directories. "
411 "This may be used to run qmlls on a Boot2Qt project."_L1);
412 parser.addOption(qmlImportNoDefault);
413 const QString qmlImportNoDefaultSetting = "DisableDefaultImports"_L1;
414 settings.addOption(qmlImportNoDefaultSetting, false);
415
416 QCommandLineOption inputFile(QStringList() << "inputFile"_L1,
417 "Read from file instead of stdin"_L1, "fileName"_L1);
418 inputFile.setFlags(QCommandLineOption::HiddenFromHelp);
419 parser.addOption(inputFile);
420
421 // we can't use parser.addVersionOption() because we already have one '-v' option for verbose...
422 QCommandLineOption versionOption("version"_L1, "Displays version information."_L1);
423 parser.addOption(versionOption);
424
425 parser.process(app);
426
427 if (parser.isSet(versionOption)) {
428 parser.showVersion();
429 return EXIT_SUCCESS;
430 }
431
432 if (parser.isSet(writeDefaultsOption)) {
433 return settings.writeDefaults() ? 0 : 1;
434 }
435
436 if (parser.isSet(logFileOption)) {
437 QString fileName = parser.value(logFileOption);
438 qInfo() << "will log to" << fileName;
439 logFile = new QFile(fileName);
440 logFileLock = new QMutex;
441 if (!logFile->open(QFile::WriteOnly | QFile::Truncate | QFile::Text)) {
442 qWarning("Failed to open file %s: %s", qPrintable(logFile->fileName()),
443 qPrintable(logFile->errorString()));
444 }
445 qInstallMessageHandler([](QtMsgType t, const QMessageLogContext &, const QString &msg) {
446 QMutexLocker l(logFileLock);
447 logFile->write(QString::number(int(t)).toUtf8());
448 logFile->write(" ");
449 logFile->write(msg.toUtf8());
450 logFile->write("\n");
451 logFile->flush();
452 });
453 }
454 if (parser.isSet(waitOption)) {
455 int waitSeconds = parser.value(waitOption).toInt();
456 if (waitSeconds > 0)
457 qDebug() << "waiting";
458 QThread::sleep(waitSeconds);
459 qDebug() << "starting";
460 }
461 QMutex writeMutex;
462 QQmlLanguageServer qmlServer(
463 [&writeMutex](const QByteArray &data) {
464 QMutexLocker l(&writeMutex);
465 std::cout.write(data.constData(), data.size());
466 std::cout.flush();
467 },
468 (parser.isSet(ignoreSettings) ? nullptr : &settings));
469
470 if (parser.isSet(verboseOption)) {
471 QLoggingCategory::setFilterRules("qt.languageserver*.debug=true\n"_L1);
472 qmlServer.codeModelManager()->setVerbose(true);
473 }
474 if (parser.isSet(docDir))
475 qmlServer.codeModelManager()->setDocumentationRootPath(
476 QString::fromUtf8(parser.value(docDir).toUtf8()));
477
478 if (parser.isSet(buildDirOption)) {
479 const QStringList dirs =
480 QQmlToolingUtils::getAndWarnForInvalidDirsFromOption(parser, buildDirOption);
481
482 qInfo().nospace().noquote()
483 << "Using build directories passed by -b: \"" << dirs.join(u"\", \""_s) << "\".";
484
485 qmlServer.codeModelManager()->setBuildPathsForRootUrl(QByteArray(), dirs);
486 } else if (QStringList dirsFromEnv =
487 QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(u"QMLLS_BUILD_DIRS"_s);
488 !dirsFromEnv.isEmpty()) {
489
490 // warn now at qmlls startup that those directories will be used later in qqmlcodemodel when
491 // searching for build folders.
492 qInfo().nospace().noquote() << "Using build directories passed from environment variable "
493 "\"QMLLS_BUILD_DIRS\": \""
494 << dirsFromEnv.join(u"\", \""_s) << "\".";
495 qmlServer.codeModelManager()->setBuildPathsForRootUrl(QByteArray(), dirsFromEnv);
496 } else {
497 qInfo() << "Using the build directories found in the .qmlls.ini file. Your build folder "
498 "might not be found if no .qmlls.ini files are present in the root source "
499 "folder.";
500 }
501
502 if (!prepareModuleExplorationFeature(parser, noModuleExplorationOption))
503 qmlServer.codeModelManager()->disableModuleExploration();
504
505 qmlServer.codeModelManager()->setImportPaths(
506 collectImportPaths(parser, qmlImportPathOption, environmentOption, qmlImportNoDefault));
507
508 if (prepareCMakeFeature(parser, noCMakeCallsOption)) {
509 qmlServer.codeModelManager()->setCMakeJobs(cmakeJobs(parser, cmakeJobsOption));
510 qmlServer.codeModelManager()->tryEnableCMakeCalls();
511 } else {
512 qmlServer.codeModelManager()->disableCMakeCalls();
513 }
514
515 auto r = parser.isSet(inputFile) && parser.value(inputFile) != "-"_L1
516 ? std::unique_ptr<AbstractReader>(std::make_unique<FileReader>(parser.value(inputFile)))
517 : std::unique_ptr<AbstractReader>(std::make_unique<StdinReader>());
518 QThread workerThread;
519 r->moveToThread(&workerThread);
520 QObject::connect(r.get(), &AbstractReader::receivedData, &qmlServer,
521 &QLanguageServer::receiveData);
522 QObject::connect(&qmlServer, &QLanguageServer::readNextMessage, r.get(),
523 &AbstractReader::readNextMessage);
524 auto exit = [&app, &workerThread]() {
525 workerThread.quit();
526 workerThread.wait();
527 QTimer::singleShot(100, &app, []() {
528 QCoreApplication::processEvents();
529 QCoreApplication::exit();
530 });
531 };
532 QObject::connect(r.get(), &StdinReader::eof, &app, exit);
533 QObject::connect(&qmlServer, &QLanguageServer::exit, &workerThread, exit);
534
535 emit r->readNextMessage();
536 workerThread.start();
537 app.exec();
538 workerThread.quit();
539 workerThread.wait();
540 return qmlServer.returnValue();
541}
542
543} // namespace QmlLsp
544
545QT_END_NAMESPACE
546
547#include <qmllsmain.moc>
void readNextMessageImpl() override
void readNextMessageImpl() override
static bool prepareModuleExplorationFeature(const QCommandLineParser &parser, const QCommandLineOption &noModuleExplorationOption)
QBasicMutex * logFileLock
Definition qmllsmain.cpp:53
static int cmakeJobs(const QCommandLineParser &parser, const QCommandLineOption &cmakeJobsOption)
int qmllsMain(int argv, char *argc[])
static bool prepareCMakeFeature(const QCommandLineParser &parser, const QCommandLineOption &noCMakeCallsOption)
QFile * logFile
Definition qmllsmain.cpp:52
static QStringList collectImportPaths(const QCommandLineParser &parser, const QCommandLineOption &qmlImportPathOption, const QCommandLineOption &environmentOption, const QCommandLineOption &qmlImportNoDefault)