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, 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 m_hasEof = true;
135 emit eof();
136 return;
137 }
138 // see if more data is available and fill the buffer with it
139 qsizetype readNow = std::cin.readsome(m_buffer + m_bytesInBuf + 1, s_bufSize) + 1;
140 QByteArray toAdd(m_buffer + m_bytesInBuf, readNow);
141 m_bytesInBuf += readNow;
142 m_streamReader.receiveData(std::move(toAdd));
143
144 m_shouldSendData |= m_bytesInBuf >= s_bufSize;
145 if (std::exchange(m_shouldSendData, false))
146 sendData();
147 }
148 }
149};
150
151/*!
152 \internal
153 FileReader allows to read JsonRPC commands from a file. This is useful when the JsonRPC
154 commands can't be read from stdin, for example when running qmlls in the macos profiler.
155 */
156class FileReader final : public AbstractReader
157{
158 Q_OBJECT
159public:
160 FileReader(const QString &fileName) : m_file(fileName)
161 {
162 bool isOpen = m_file.open(QFile::ReadOnly);
163 Q_ASSERT(isOpen);
164 }
165
166private:
167 QFile m_file;
168
169protected:
171 {
172 if (!m_file.isOpen())
173 return;
174 emit receivedData(m_file.readAll(), false);
175 // emit eof will shutdown qmlls before it manages to complete the preceding requests, so
176 // don't emit eof when reading from a file.
177 // also, just read the content of the file once, so close it now
178 m_file.close();
179 }
180};
181
182static QStringList collectImportPaths(const QCommandLineParser &parser,
183 const QCommandLineOption &qmlImportPathOption,
184 const QCommandLineOption &environmentOption,
185 const QCommandLineOption &qmlImportNoDefault)
186{
187 QStringList importPaths;
188 if (parser.isSet(qmlImportPathOption)) {
189 const QStringList pathsFromOption =
190 QQmlToolingUtils::getAndWarnForInvalidDirsFromOption(parser, qmlImportPathOption);
191 qInfo().nospace().noquote() << "Using import directories passed by -I: \""
192 << pathsFromOption.join(u"\", \""_s) << "\".";
193 importPaths << pathsFromOption;
194 }
195 if (parser.isSet(environmentOption)) {
196 if (const QStringList dirsFromEnv =
197 QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(u"QML_IMPORT_PATH"_s);
198 !dirsFromEnv.isEmpty()) {
199 qInfo().nospace().noquote()
200 << "Using import directories passed from environment variable "
201 "\"QML_IMPORT_PATH\": \""
202 << dirsFromEnv.join(u"\", \""_s) << "\".";
203 importPaths << dirsFromEnv;
204 }
205
206 if (const QStringList dirsFromEnv2 =
207 QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(u"QML2_IMPORT_PATH"_s);
208 !dirsFromEnv2.isEmpty()) {
209 qInfo().nospace().noquote()
210 << "Using import directories passed from the deprecated environment variable "
211 "\"QML2_IMPORT_PATH\": \""
212 << dirsFromEnv2.join(u"\", \""_s) << "\".";
213 importPaths << dirsFromEnv2;
214 }
215 }
216
217 // add as default fallback at the end
218 if (!parser.isSet(qmlImportNoDefault))
219 importPaths << QLibraryInfo::path(QLibraryInfo::QmlImportsPath);
220 return importPaths;
221}
222
223// To debug:
224//
225// * simple logging can be redirected to a file
226// passing -l <file> to the qmlls command
227//
228// * more complex debugging can use named pipes:
229//
230// mkfifo qmllsIn
231// mkfifo qmllsOut
232//
233// this together with a qmllsEcho script that can be defined as
234//
235// #!/bin/sh
236// cat -u < ~/qmllsOut &
237// cat -u > ~/qmllsIn
238//
239// allows to use qmllsEcho as lsp server, and still easily start
240// it in a terminal
241//
242// qmlls < ~/qmllsIn > ~/qmllsOut
243//
244// * statup can be slowed down to have the time to attach via the
245// -w <nSeconds> flag.
246
247int qmllsMain(int argv, char *argc[])
248{
249#ifdef Q_OS_WIN32
250 // windows does not open stdin/stdout in binary mode by default
251 int err = _setmode(_fileno(stdout), _O_BINARY);
252 if (err == -1)
253 perror("Cannot set mode for stdout");
254 err = _setmode(_fileno(stdin), _O_BINARY);
255 if (err == -1)
256 perror("Cannot set mode for stdin");
257#endif
258
259 QHashSeed::setDeterministicGlobalSeed();
260 QCoreApplication app(argv, argc);
261
262 QCommandLineParser parser;
263 QQmlToolingSharedSettings settings("qmlls"_L1);
264 parser.setApplicationDescription("QML languageserver"_L1);
265
266 parser.addHelpOption();
267 QCommandLineOption waitOption(QStringList() << "w"_L1
268 << "wait"_L1,
269 "Waits the given number of seconds before startup"_L1,
270 "waitSeconds"_L1);
271 parser.addOption(waitOption);
272
273 QCommandLineOption verboseOption(
274 QStringList() << "v"_L1
275 << "verbose"_L1,
276 "Outputs extra information on the operations being performed"_L1);
277 parser.addOption(verboseOption);
278
279 QCommandLineOption logFileOption(QStringList() << "l"_L1
280 << "log-file"_L1,
281 "Writes logging to the given file"_L1, "logFile"_L1);
282 parser.addOption(logFileOption);
283
284 QString buildDir = QStringLiteral(u"buildDir");
285 QCommandLineOption buildDirOption(QStringList() << "b"_L1
286 << "build-dir"_L1,
287 "Adds a build dir to look up for qml information"_L1,
288 buildDir);
289 parser.addOption(buildDirOption);
290 settings.addOption(buildDir);
291
292 QString qmlImportPath = QStringLiteral(u"importPaths");
293 QCommandLineOption qmlImportPathOption(QStringList() << "I"_L1,
294 "Look for QML modules in the specified directory"_L1,
295 qmlImportPath);
296 parser.addOption(qmlImportPathOption);
297 settings.addOption(qmlImportPath);
298
299 QCommandLineOption environmentOption(
300 QStringList() << "E"_L1,
301 "Use the QML_IMPORT_PATH environment variable to look for QML Modules"_L1);
302 parser.addOption(environmentOption);
303
304 QCommandLineOption writeDefaultsOption(
305 QStringList() << "write-defaults"_L1,
306 "Writes defaults settings to .qmlls.ini and exits (Warning: This "
307 "will overwrite any existing settings and comments!)"_L1);
308 parser.addOption(writeDefaultsOption);
309
310 QCommandLineOption ignoreSettings(QStringList() << "ignore-settings"_L1,
311 "Ignores all settings files and only takes "
312 "command line options into consideration"_L1);
313 parser.addOption(ignoreSettings);
314
315 QCommandLineOption noCMakeCallsOption(
316 QStringList() << "no-cmake-calls"_L1,
317 "Disables automatic CMake rebuilds and C++ file watching."_L1);
318 parser.addOption(noCMakeCallsOption);
319 settings.addOption("no-cmake-calls"_L1, "false"_L1);
320
321 QCommandLineOption docDir({ { "d"_L1, "p"_L1, "doc-dir"_L1 },
322 "Documentation path to use for the documentation hints feature"_L1,
323 "path"_L1,
324 QString() });
325 parser.addOption(docDir);
326 settings.addOption("docDir"_L1);
327
328 QCommandLineOption qmlImportNoDefault(QStringList() << "bare"_L1,
329 "Do not include default import directories. "
330 "This may be used to run qmlls on a Boot2Qt project."_L1);
331 parser.addOption(qmlImportNoDefault);
332 const QString qmlImportNoDefaultSetting = "DisableDefaultImports"_L1;
333 settings.addOption(qmlImportNoDefaultSetting, false);
334
335 QCommandLineOption inputFile(QStringList() << "inputFile"_L1,
336 "Read from file instead of stdin"_L1, "fileName"_L1);
337 inputFile.setFlags(QCommandLineOption::HiddenFromHelp);
338 parser.addOption(inputFile);
339
340 // we can't use parser.addVersionOption() because we already have one '-v' option for verbose...
341 QCommandLineOption versionOption("version"_L1, "Displays version information."_L1);
342 parser.addOption(versionOption);
343
344 parser.process(app);
345
346 if (parser.isSet(versionOption)) {
347 parser.showVersion();
348 return EXIT_SUCCESS;
349 }
350
351 if (parser.isSet(writeDefaultsOption)) {
352 return settings.writeDefaults() ? 0 : 1;
353 }
354
355 if (parser.isSet(logFileOption)) {
356 QString fileName = parser.value(logFileOption);
357 qInfo() << "will log to" << fileName;
358 logFile = new QFile(fileName);
359 logFileLock = new QMutex;
360 if (!logFile->open(QFile::WriteOnly | QFile::Truncate | QFile::Text)) {
361 qWarning("Failed to open file %s: %s", qPrintable(logFile->fileName()),
362 qPrintable(logFile->errorString()));
363 }
364 qInstallMessageHandler([](QtMsgType t, const QMessageLogContext &, const QString &msg) {
365 QMutexLocker l(logFileLock);
366 logFile->write(QString::number(int(t)).toUtf8());
367 logFile->write(" ");
368 logFile->write(msg.toUtf8());
369 logFile->write("\n");
370 logFile->flush();
371 });
372 }
373 if (parser.isSet(waitOption)) {
374 int waitSeconds = parser.value(waitOption).toInt();
375 if (waitSeconds > 0)
376 qDebug() << "waiting";
377 QThread::sleep(waitSeconds);
378 qDebug() << "starting";
379 }
380 QMutex writeMutex;
381 QQmlLanguageServer qmlServer(
382 [&writeMutex](const QByteArray &data) {
383 QMutexLocker l(&writeMutex);
384 std::cout.write(data.constData(), data.size());
385 std::cout.flush();
386 },
387 (parser.isSet(ignoreSettings) ? nullptr : &settings));
388
389 if (parser.isSet(verboseOption)) {
390 QLoggingCategory::setFilterRules("qt.languageserver*.debug=true\n"_L1);
391 qmlServer.codeModelManager()->setVerbose(true);
392 }
393 if (parser.isSet(docDir))
394 qmlServer.codeModelManager()->setDocumentationRootPath(
395 QString::fromUtf8(parser.value(docDir).toUtf8()));
396
397 if (parser.isSet(buildDirOption)) {
398 const QStringList dirs =
399 QQmlToolingUtils::getAndWarnForInvalidDirsFromOption(parser, buildDirOption);
400
401 qInfo().nospace().noquote()
402 << "Using build directories passed by -b: \"" << dirs.join(u"\", \""_s) << "\".";
403
404 qmlServer.codeModelManager()->setBuildPathsForRootUrl(QByteArray(), dirs);
405 } else if (QStringList dirsFromEnv =
406 QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(u"QMLLS_BUILD_DIRS"_s);
407 !dirsFromEnv.isEmpty()) {
408
409 // warn now at qmlls startup that those directories will be used later in qqmlcodemodel when
410 // searching for build folders.
411 qInfo().nospace().noquote() << "Using build directories passed from environment variable "
412 "\"QMLLS_BUILD_DIRS\": \""
413 << dirsFromEnv.join(u"\", \""_s) << "\".";
414 qmlServer.codeModelManager()->setBuildPathsForRootUrl(QByteArray(), dirsFromEnv);
415 } else {
416 qInfo() << "Using the build directories found in the .qmlls.ini file. Your build folder "
417 "might not be found if no .qmlls.ini files are present in the root source "
418 "folder.";
419 }
420
421 qmlServer.codeModelManager()->setImportPaths(
422 collectImportPaths(parser, qmlImportPathOption, environmentOption, qmlImportNoDefault));
423
424 const bool disableCMakeCallsViaEnvironment =
425 qmlGetConfigOption<bool, qmlConvertBoolConfigOption>("QMLLS_NO_CMAKE_CALLS");
426
427 if (disableCMakeCallsViaEnvironment || parser.isSet(noCMakeCallsOption)) {
428 if (disableCMakeCallsViaEnvironment) {
429 qWarning() << "Disabling CMake calls via QMLLS_NO_CMAKE_CALLS environment variable.";
430 } else {
431 qWarning() << "Disabling CMake calls via command line switch.";
432 }
433
434 qmlServer.codeModelManager()->disableCMakeCalls();
435 } else {
436 qmlServer.codeModelManager()->tryEnableCMakeCalls();
437 }
438
439 auto r = parser.isSet(inputFile) && parser.value(inputFile) != "-"_L1
440 ? std::unique_ptr<AbstractReader>(std::make_unique<FileReader>(parser.value(inputFile)))
441 : std::unique_ptr<AbstractReader>(std::make_unique<StdinReader>());
442 QThread workerThread;
443 r->moveToThread(&workerThread);
444 QObject::connect(r.get(), &AbstractReader::receivedData, qmlServer.server(),
445 &QLanguageServer::receiveData);
446 QObject::connect(qmlServer.server(), &QLanguageServer::readNextMessage, r.get(),
447 &AbstractReader::readNextMessage);
448 auto exit = [&app, &workerThread]() {
449 workerThread.quit();
450 workerThread.wait();
451 QTimer::singleShot(100, &app, []() {
452 QCoreApplication::processEvents();
453 QCoreApplication::exit();
454 });
455 };
456 QObject::connect(r.get(), &StdinReader::eof, &app, exit);
457 QObject::connect(qmlServer.server(), &QLanguageServer::exit, exit);
458
459 emit r->readNextMessage();
460 workerThread.start();
461 app.exec();
462 workerThread.quit();
463 workerThread.wait();
464 return qmlServer.returnValue();
465}
466
467} // namespace QmlLsp
468
469QT_END_NAMESPACE
470
471#include <qmllsmain.moc>
void readNextMessageImpl() override
void readNextMessageImpl() override
QBasicMutex * logFileLock
Definition qmllsmain.cpp:53
int qmllsMain(int argv, char *argc[])
QFile * logFile
Definition qmllsmain.cpp:52
static QStringList collectImportPaths(const QCommandLineParser &parser, const QCommandLineOption &qmlImportPathOption, const QCommandLineOption &environmentOption, const QCommandLineOption &qmlImportNoDefault)