Open Bug 2086992 Opened 24 days ago Updated 2 days ago

Add ESLint rules to check the MOZ_LOG module names of consoles

Categories

(Developer Infrastructure :: Linting and Formatting: JavaScript, task)

task

Tracking

(Not tracked)

ASSIGNED

People

(Reporter: dcarver, Assigned: dcarver)

References

(Depends on 1 open bug, Blocks 3 open bugs)

Details

Attachments

(12 external files, 1 obsolete file)

Bug 2086657 adds a logModule option to console.createInstance to name the MOZ_LOG module of a console, separately from its prefix, which is only printed. Bugs 2084854 and 2086676 fix the modules whose names MOZ_LOG cannot parse. Nothing stops new ones from being added.

ESLint can check this:

  • A rule requiring the logModule to be a string literal made only of the characters MOZ_LOG reads in a module name: ASCII letters, digits, _, - and ., not starting with a digit. A literal also lets every JS module be listed from the source, which the meta bug 2077111 asks for. Only the prefix may have parts known at run time.
  • A rule warning about a console with a prefix and no logModule, as naming the module with the prefix alone is deprecated by bug 2086657. It would only warn, as code outside the tree still relies on it.

A few consoles do not give a string literal as their module name, so they need to change rather than have exceptions to the rule:

  • LoginHelper.createLogger, the SQLiteStoreBase subclasses and GeckoViewUtils.initLoggingConsole create the console in a helper, from a name they are given. Each caller should create its console with a literal logModule instead.
  • AboutHomeStartupCache uses a constant, and the test_console_shouldLog.js and test_logFilePrefReset.js tests use a constant or build the name at run time.
    The names of their modules do not change.

The logging docs (xpcom/docs/logging.md and toolkit/docs/javascript-logging.md) should also say that the logModule names the MOZ_LOG module, and how to find the JS modules. The Searchfox query in xpcom/docs/logging.md that claims to list all modules only finds the C++ ones.

I'm not convinced that an ESLint rule is the best way to resolve this - it would effectively be checking the arguments to a function, which the function itself should be checking. If we add this then we'll be encoding business logic in a linter, which doesn't seem right.

Thanks for your feedback. The minimum requirement is that every MOZ_LOG module created from JS (and elsewhere) can be listed from the source, which bug 2077111 needs. For example, about:logging could then show all the available modules, including those whose code hasn't run yet. That requires logModule to be a string literal at the call site. This is a property of the source code, not of the value passed at run time, so only a tool that reads the source can check it, and ESLint already reads every call site and is what developers already run.

A run-time check could also help, in LogModule::Get (bug 2079145), but it would be stripped from release builds.

Do you see a better way to meet this requirement?

Flags: needinfo?(standard8)

If we want to require that module names be static literals then that is something best checked by the linter and at that point it's reasonable to assert that the literal is of the expected form. I think this is less likely to be missed than a console message at runtime.

Okay, let's go with the linter for now. In some ways I'd like this to be encodable via TypeScript, unfortunately it doesn't look like that is easily possible (and we haven't got TypeScript enabled everywhere yet).

Flags: needinfo?(standard8)

Using a string literal rather than the LOG_NAME property, which nothing
else reads, lets the mozilla/valid-log-module ESLint rule of a following
patch check it. The name of the log module does not change.

Using a string literal rather than a constant lets the mozilla/valid-log-module ESLint rule of a following patch check it.

Using a string literal for each, rather than building it from the MOZ_LOG level of the case, lets the mozilla/valid-log-module ESLint rule of a following patch check it. The test still checks the same modules, with the same maximum log levels.

Depends on: 2087579
Component: Lint and Formatting → Linting and Formatting: JavaScript

Its callers now create their console themselves, so that its logModule
is a string literal the mozilla/valid-log-module ESLint rule of a
following patch can check. Since bug 2087579, createLogger only gave
the console signon.loglevel as its maxLogLevelPref, which each caller
now gives itself. The names of the log modules do not change.

Creating the console in each store, rather than in SQLiteStoreBase from
the logPrefix and logLevelPref getters, makes its logModule a string
literal, that the mozilla/valid-log-module ESLint rule of a following
patch can check. The createLog method replaces the logLevelPref getter.
The names of the log modules do not change.

Creating the console at the call site, rather than in
GeckoViewUtils.initLoggingConsole from the tag it is given, makes its
logModule a string literal, that the mozilla/valid-log-module ESLint
rule of a following patch can check. The name of the
GeckoView.IPProtection log module does not change.

The MOZ_LOG module of a console created with console.createInstance is
named by its logModule. The new mozilla/valid-log-module rule requires
the logModule to be a string literal made only of the characters MOZ_LOG
reads in a module name, so that every module can be configured, and
listed from the source. A prefix may have parts only known at run time.

So that it can read the logModule, the rule also requires the options of
console.createInstance, when given, to be an object literal written in the
call.

The rule also rejects a logModule named like a MOZ_LOG option such as
"sync", which MOZ_LOG reads as the option instead of the module.

Without a logModule, the prefix of a console created with
console.createInstance names its MOZ_LOG module, which is deprecated and
only kept for compatibility, as code outside the tree still relies on
it. The new mozilla/console-prefix-requires-log-module rule warns about
a prefix with no logModule next to it. It is only a warning, so that
code which has not moved to logModule yet still passes the lint. The two
consoles that test this compatibility disable the rule.

The logModule of a console names its MOZ_LOG module and must be a string
literal that MOZ_LOG can parse, while its prefix is only printed. The
docs now say so, and how to find the modules of JavaScript.

MOZ_LOG is now read by the parser of mozglue/misc/LogModulesParser.h. Like
it, the rule rejects a logModule starting with "-", and one named like a
MOZ_LOG option such as "sync" whatever its case, since the parser compares
the options without case.

The test creates its consoles from options that are spread into
console.createInstance, so their logModule is not a string literal in
the call. Write each console.createInstance call out with its
logModule as a string literal, so that the mozilla/valid-log-module
ESLint rule of a following patch can check it.

Blocks: 2091452
Attachment #9674694 - Attachment description: WIP: Bug 2086992 - Add an ESLint rule to check if logModules can be parsed in MOZ_LOG. r?#frontend-codestyle-reviewers → Bug 2086992 - Add an ESLint rule to check if logModules can be parsed in MOZ_LOG. r?#frontend-codestyle-reviewers

Give the console of TaskbarTabsPinBase the TaskbarTabs logModule, as the
other consoles of browser/components/taskbartabs/ have. The
mozilla/console-prefix-requires-log-module rule of the next patch then
has nothing to warn about in this file.

Attachment #9674695 - Attachment description: WIP: Bug 2086992 - Add an ESLint rule to warn about consoles naming their MOZ_LOG module with the prefix alone. r?#frontend-codestyle-reviewers → Bug 2086992 - Add an ESLint rule to warn about consoles naming their MOZ_LOG module with the prefix alone. r?#frontend-codestyle-reviewers
Attachment #9674696 - Attachment description: WIP: Bug 2086992 - Document that console logModules must be MOZ_LOG module names, and how to find them. r?#frontend-codestyle-reviewers → Bug 2086992 - Document that console logModules must be MOZ_LOG module names, and how to find them. r?#frontend-codestyle-reviewers
Attachment #9674864 - Attachment description: WIP: Bug 2086992 - Check logModules against the strict MOZ_LOG parser in the valid-log-module ESLint rule. r?#frontend-codestyle-reviewers → WIP: Bug 2086992 - Check logModules against the new MOZ_LOG parser in the valid-log-module ESLint rule. r?#frontend-codestyle-reviewers
Attachment #9674694 - Attachment description: Bug 2086992 - Add an ESLint rule to check if logModules can be parsed in MOZ_LOG. r?#frontend-codestyle-reviewers → WIP: Bug 2086992 - Add an ESLint rule to check if logModules can be parsed in MOZ_LOG. r?#frontend-codestyle-reviewers
Blocks: 2095093
Attachment #9674695 - Attachment description: Bug 2086992 - Add an ESLint rule to warn about consoles naming their MOZ_LOG module with the prefix alone. r?#frontend-codestyle-reviewers → WIP: Bug 2086992 - Add an ESLint rule to warn about consoles naming their MOZ_LOG module with the prefix alone. r?#frontend-codestyle-reviewers
Attachment #9674695 - Attachment description: WIP: Bug 2086992 - Add an ESLint rule to warn about consoles naming their MOZ_LOG module with the prefix alone. r?#frontend-codestyle-reviewers → Bug 2086992 - Add an ESLint rule to warn about consoles naming their MOZ_LOG module with the prefix alone. r?#frontend-codestyle-reviewers
Attachment #9674694 - Attachment description: WIP: Bug 2086992 - Add an ESLint rule to check if logModules can be parsed in MOZ_LOG. r?#frontend-codestyle-reviewers → Bug 2086992 - Add an ESLint rule to check if logModules can be parsed in MOZ_LOG. r?#frontend-codestyle-reviewers
Attachment #9679709 - Attachment description: WIP: Bug 2086992 - Give the console of TaskbarTabsPinBase a string literal as its logModule. r?#frontend-codestyle-reviewers → Bug 2086992 - Give the console of TaskbarTabsPinBase a string literal as its logModule. r?#frontend-codestyle-reviewers
Attachment #9678916 - Attachment description: WIP: Bug 2086992 - Give the console of NetworkGeolocationCache a string literal as its logModule. r?#frontend-codestyle-reviewers → WIP: Bug 2086992 - Give the console of NetworkGeolocationCache a string literal as its logModule. r?#geoprovider-reviewers
Attachment #9678916 - Attachment description: WIP: Bug 2086992 - Give the console of NetworkGeolocationCache a string literal as its logModule. r?#geoprovider-reviewers → Bug 2086992 - Give the console of NetworkGeolocationCache a string literal as its logModule. r?#geoprovider-reviewers
Attachment #9674666 - Attachment description: WIP: Bug 2086992 - Remove LoginHelper.createLogger. r?#credential-management-reviewers → Bug 2086992 - Remove LoginHelper.createLogger. r?#credential-management-reviewers
Attachment #9671574 - Attachment description: WIP: Bug 2086992 - Give the console of AboutHomeStartupCache a string literal as its logModule. r?#home-newtab-reviewers → Bug 2086992 - Give the console of AboutHomeStartupCache a string literal as its logModule. r?#home-newtab-reviewers
Attachment #9671575 - Attachment description: WIP: Bug 2086992 - Give the console of test_logFilePrefReset.js a string literal as its logModule. r?#xpcom-reviewers → Bug 2086992 - Give the console of test_logFilePrefReset.js a string literal as its logModule. r?#xpcom-reviewers
Attachment #9671576 - Attachment description: WIP: Bug 2086992 - Give the consoles of test_console_shouldLog.js string literals as their logModule. r?#dom-core-reviewers → Bug 2086992 - Give the consoles of test_console_shouldLog.js string literals as their logModule. r?#dom-core-reviewers
Attachment #9674667 - Attachment description: WIP: Bug 2086992 - Make the subclasses of SQLiteStoreBase create their console, with a string literal as its logModule. r?omarg → Bug 2086992 - Make the subclasses of SQLiteStoreBase create their console, with a string literal as its logModule. r?omarg
Attachment #9674693 - Attachment description: WIP: Bug 2086992 - Make GeckoViewUtils.initLoggingConsole take a console instance, created with a string literal as its logModule. r?#geckoview-reviewers → Bug 2086992 - Make GeckoViewUtils.initLoggingConsole take a console instance, created with a string literal as its logModule. r?#geckoview-reviewers
Attachment #9677945 - Attachment description: WIP: Bug 2086992 - Give the consoles of test_console_logModule.js string literals as their logModule. r?#dom-core-reviewers → Bug 2086992 - Give the consoles of test_console_logModule.js string literals as their logModule. r?#dom-core-reviewers

Comment on attachment 9674864 [details]
WIP: Bug 2086992 - Check logModules against the new MOZ_LOG parser in the valid-log-module ESLint rule. r?#frontend-codestyle-reviewers

Revision D339982 was moved to bug 2080769. Setting attachment 9674864 [details] to obsolete.

Attachment #9674864 - Attachment is obsolete: true
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: