Skip to content

Commit e43c1ed

Browse files
fix(dev): default log gating to DEBUG until settings resolve
- Use DEBUG threshold when _logLevel is unavailable (async DataStore.settings) - Document plugin vs HTML/React WebView sync/async behavior in file header Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a8cfc5e commit e43c1ed

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

‎helpers/dev.js‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
// @flow
2-
// Development-related helper functions
3-
// Note: none of these rely on DataStore.* functions etc., _except_ for the logging functions. However, the DataStore.settings object _is_ available in React windows/components, through @DBW's wizardry.
2+
/**
3+
* Development-related helper functions.
4+
*
5+
* Logging vs sync/async `DataStore.settings` (plugin JS vs HTML/React WebView):
6+
* - In the main plugin context, `DataStore.settings` is typically a plain object, so reading `_logLevel` for
7+
* `shouldOutputForLogLevel` is synchronous and immediate.
8+
* - In HTML/React windows, `DataStore.settings` may be thenable (Promise-like). Synchronous code cannot read
9+
* `_logLevel` until it resolves. This file uses an in-memory cache filled by a one-shot background `await`
10+
* (`getPluginSettingsForLogging`, `primePluginSettingsCacheViaAwait`). Until the cache is populated,
11+
* log gating uses the default threshold (DEBUG) so early lines are not silenced. After settings resolve,
12+
* `_logLevel` from the real object applies. If the first logs look noisier than afterward or the level
13+
* seems to "kick in" shortly after load, that is this bootstrap window - not necessarily a wrong user setting.
14+
*
15+
* Aside from logging helpers, most functions here intentionally avoid `DataStore.*`.
16+
*/
417

518
import isEqual from 'lodash-es/isEqual'
619
import isObject from 'lodash-es/isObject'
@@ -611,8 +624,7 @@ let cachedPluginSettingsForLog: any = null
611624
let pluginSettingsAwaitPrimeStartedForLog: boolean = false
612625

613626
/** Trace settings resolution via console.log. Enable with env NP_INSTRUMENT_PLUGIN_SETTINGS=1 (Node/Jest only). */
614-
const ENABLE_GET_PLUGIN_SETTINGS_INSTRUMENTATION: boolean =
615-
typeof process !== 'undefined' && process.env && process.env.NP_INSTRUMENT_PLUGIN_SETTINGS === '1'
627+
const ENABLE_GET_PLUGIN_SETTINGS_INSTRUMENTATION: boolean = typeof process !== 'undefined' && process.env && process.env.NP_INSTRUMENT_PLUGIN_SETTINGS === '1'
616628
let getPluginSettingsInstrumentationResolutionLogged: boolean = false
617629

618630
/**
@@ -670,7 +682,7 @@ function primePluginSettingsCacheViaAwait(): void {
670682
/**
671683
* Return plugin settings for log-level checks.
672684
* - If `DataStore.settings` is already a plain object (not a thenable), use it synchronously.
673-
* - Otherwise schedule one background await and return null until cache fills (same default log level as “no settings”).
685+
* - Otherwise schedule one background await and return null until cache fills (shouldOutputForLogLevel then defaults to DEBUG until settings apply).
674686
*
675687
* @returns {?Object}
676688
*/
@@ -707,7 +719,8 @@ function getPluginSettingsForLogging(): any {
707719
* @returns {boolean}
708720
*/
709721
export const shouldOutputForLogLevel = (logType: string): boolean => {
710-
let userLogLevel = 1
722+
// Default DEBUG so early logs are not dropped while DataStore.settings is still unresolved or _logLevel is unset.
723+
let userLogLevel = 0
711724
const thisMessageLevel = LOG_LEVELS.indexOf(logType.toUpperCase())
712725
const pluginSettings = getPluginSettingsForLogging()
713726
// Note: Performing a null change against a value that is `undefined` will be true

0 commit comments

Comments
 (0)