Skip to content

Commit 7645bfd

Browse files
committed
chore: new DEBUG_WAITS build flag, improve DBEUG_LOOKUPS build flag logs, other dev env fixes
1 parent e9fedc8 commit 7645bfd

File tree

11 files changed

+149
-31
lines changed

11 files changed

+149
-31
lines changed

lib/modules/src/finders/_internal.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export const FilterResultFlags = {
5656
export type FilterResultFlag =
5757
(typeof FilterResultFlags)[keyof typeof FilterResultFlags]
5858

59+
export const FilterResultFlagToHumanReadable: Record<FilterResultFlag, string> =
60+
{
61+
[FilterResultFlags.Default]: '\u001b[94mdefault\u001b[0m',
62+
[FilterResultFlags.Namespace]: '\u001b[35mnamespace\u001b[0m',
63+
[FilterResultFlags.Found]: '\u001b[96mexportsless\u001b[0m',
64+
}
65+
5966
// The reason this returns a flag is because flags are never falsy, while exports may be falsy when using ID-only filters (eg. `byDependencies`).
6067

6168
// Currently, we only have options that are relevant for checking exports
@@ -84,8 +91,7 @@ export function runFilter(
8491
if (options?.initialize ?? true) {
8592
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__) {
8693
const flag = runFilter(filter, id, __r(id), options)
87-
if (!flag)
88-
warnDeveloperAboutPartialFilterMatch(id, filter.key)
94+
if (!flag) DEBUG_warnPartialFilterMatch(id, filter.key)
8995
return flag
9096
} else return runFilter(filter, id, __r(id), options)
9197
}
@@ -119,9 +125,8 @@ export function exportsFromFilterResultFlag(
119125
/**
120126
* Warns the developer that the filter matched during exportsless comparison, but not during the full comparison.
121127
* This will cause modules to be initialized unnecessarily, and may cause issues.
122-
* @internal
123128
*/
124-
function warnDeveloperAboutPartialFilterMatch(id: Metro.ModuleID, key: string) {
129+
function DEBUG_warnPartialFilterMatch(id: Metro.ModuleID, key: string) {
125130
nativeLoggingHook(
126131
`\u001b[33m${key} matched module ${id} partially.\n${getCurrentStack()}\u001b[0m`,
127132
2,

lib/modules/src/finders/lookup.ts

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ import {
1111
getInitializedModuleExports,
1212
isModuleInitialized,
1313
} from '../metro/utils'
14-
import { exportsFromFilterResultFlag, runFilter } from './_internal'
14+
import {
15+
exportsFromFilterResultFlag,
16+
FilterResultFlagToHumanReadable,
17+
runFilter,
18+
} from './_internal'
1519
import type { If, Nullish } from '@revenge-mod/utils/types'
1620
import type { MaybeDefaultExportMatched, Metro } from '../types'
17-
import type { RunFilterReturnExportsOptions } from './_internal'
21+
import type {
22+
FilterResultFlag,
23+
RunFilterReturnExportsOptions,
24+
} from './_internal'
1825
import type { Filter, FilterResult } from './filters'
1926

2027
type LookupModulesOptionsWithAll<A extends boolean> = If<
@@ -177,6 +184,9 @@ export function* lookupModules(filter: Filter, options?: LookupModulesOptions) {
177184

178185
cached.add(id)
179186

187+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
188+
DEBUG_logLookupMatched(filter.key, flag, id, true)
189+
180190
yield [exportsFromFilterResultFlag(flag, exports, options), id]
181191
}
182192
}
@@ -191,6 +201,10 @@ export function* lookupModules(filter: Filter, options?: LookupModulesOptions) {
191201
const flag = runFilter(filter, id, exports, options)
192202
if (flag) {
193203
notFound = false
204+
205+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
206+
DEBUG_logLookupMatched(filter.key, flag, id)
207+
194208
yield [exportsFromFilterResultFlag(flag, exports, options), id]
195209
}
196210
}
@@ -207,6 +221,10 @@ export function* lookupModules(filter: Filter, options?: LookupModulesOptions) {
207221
const flag = runFilter(filter, id, exports, options)
208222
if (flag) {
209223
notFound = false
224+
225+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
226+
DEBUG_logLookupMatched(filter.key, flag, id)
227+
210228
yield [
211229
exportsFromFilterResultFlag(flag, exports, options),
212230
id,
@@ -222,6 +240,9 @@ export function* lookupModules(filter: Filter, options?: LookupModulesOptions) {
222240
if (flag) {
223241
notFound = false
224242

243+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
244+
DEBUG_logLookupMatched(filter.key, flag, id)
245+
225246
yield [
226247
exportsFromFilterResultFlag(
227248
flag,
@@ -235,7 +256,7 @@ export function* lookupModules(filter: Filter, options?: LookupModulesOptions) {
235256
}
236257

237258
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
238-
if (notFound) warnDeveloperAboutNoFilterMatch(filter)
259+
if (notFound) DEBUG_warnLookupNoMatch(filter.key)
239260
}
240261

241262
/**
@@ -288,6 +309,9 @@ export function lookupModule(filter: Filter, options?: LookupModulesOptions) {
288309
exports = __r(id)
289310
}
290311

312+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
313+
DEBUG_logLookupMatched(filter.key, flag, id, true)
314+
291315
return [exportsFromFilterResultFlag(flag, exports, options), id]
292316
}
293317
}
@@ -297,12 +321,16 @@ export function lookupModule(filter: Filter, options?: LookupModulesOptions) {
297321
for (let id = 0; id < mDeps.length; id++) {
298322
const exports = getInitializedModuleExports(id)
299323
const flag = runFilter(filter, id, exports, options)
300-
if (flag)
324+
if (flag) {
325+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
326+
DEBUG_logLookupMatched(filter.key, flag, id)
327+
301328
return [exportsFromFilterResultFlag(flag, exports, options), id]
329+
}
302330
}
303331

304332
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
305-
warnDeveloperAboutNoFilterMatch(filter)
333+
DEBUG_warnLookupNoMatch(filter.key)
306334

307335
cache[filter.key] = null // Full lookup, and still not found!
308336

@@ -314,17 +342,24 @@ export function lookupModule(filter: Filter, options?: LookupModulesOptions) {
314342
for (const id of mInitialized) {
315343
const exports = getInitializedModuleExports(id)
316344
const flag = runFilter(filter, id, exports, options)
317-
if (flag)
345+
if (flag) {
346+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
347+
DEBUG_logLookupMatched(filter.key, flag, id)
348+
318349
return [
319350
exportsFromFilterResultFlag(flag, exports, options),
320351
id,
321352
]
353+
}
322354
}
323355

324356
if (options?.uninitialized)
325357
for (const id of mUninitialized) {
326358
const flag = runFilter(filter, id, undefined, options)
327-
if (flag)
359+
if (flag) {
360+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
361+
DEBUG_logLookupMatched(filter.key, flag, id)
362+
328363
return [
329364
exportsFromFilterResultFlag(
330365
flag,
@@ -333,11 +368,11 @@ export function lookupModule(filter: Filter, options?: LookupModulesOptions) {
333368
),
334369
id,
335370
]
371+
}
336372
}
337373
}
338374

339-
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__)
340-
warnDeveloperAboutNoFilterMatch(filter)
375+
if (__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__) DEBUG_warnLookupNoMatch(filter.key)
341376

342377
return NotFoundResult
343378
}
@@ -376,17 +411,29 @@ const __DEBUG_TRACER_IGNORE_LIST__ = __BUILD_FLAG_DEBUG_MODULE_LOOKUPS__
376411
)
377412
: []
378413

414+
/**
415+
* Logs to the developer that a module was found, how it is found, and whether it was a cached result.
416+
*/
417+
function DEBUG_logLookupMatched(
418+
key: string,
419+
flag: FilterResultFlag,
420+
id: Metro.ModuleID,
421+
cached?: boolean,
422+
) {
423+
nativeLoggingHook(
424+
`\u001b[32mSuccessful lookup: \u001b[33m${key}\u001b[0m (matched ${id}, ${FilterResultFlagToHumanReadable[flag]}${cached ? ', \u001b[92mcached\u001b[0m' : ''})`,
425+
1,
426+
)
427+
}
428+
379429
/**
380430
* Warns the developer that no module was found for the given filter.
381431
* This is useful for debugging purposes, especially when using filters that are expected to match a module.
382432
*/
383-
function warnDeveloperAboutNoFilterMatch(filter: Filter) {
433+
function DEBUG_warnLookupNoMatch(key: string) {
384434
const stack = getCurrentStack()
385435
for (const func of __DEBUG_TRACER_IGNORE_LIST__)
386436
if (stack.includes(func.name)) return
387437

388-
nativeLoggingHook(
389-
`\u001b[31mNo module found for filter: ${filter.key}\n${stack}\u001b[0m`,
390-
2,
391-
)
438+
nativeLoggingHook(`\u001b[31mFailed lookup: ${key}\n${stack}\u001b[0m`, 2)
392439
}

lib/modules/src/finders/wait.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ import {
44
onModuleFinishedImporting,
55
} from '../metro/subscriptions'
66
import { getInitializedModuleExports } from '../metro/utils'
7-
import { exportsFromFilterResultFlag, runFilter } from './_internal'
7+
import {
8+
exportsFromFilterResultFlag,
9+
FilterResultFlagToHumanReadable,
10+
runFilter,
11+
} from './_internal'
812
import type { MaybeDefaultExportMatched, Metro } from '../types'
9-
import type { RunFilterReturnExportsOptions } from './_internal'
13+
import type {
14+
FilterResultFlag,
15+
RunFilterReturnExportsOptions,
16+
} from './_internal'
1017
import type { Filter, FilterResult } from './filters'
1118

1219
export interface BaseWaitForModulesOptions<All extends boolean = boolean> {
@@ -76,20 +83,33 @@ export function waitForModules(
7683
callback: WaitForModulesCallback<any>,
7784
options?: WaitForModulesOptions,
7885
) {
86+
if (__BUILD_FLAG_DEBUG_MODULE_WAITS__)
87+
nativeLoggingHook(
88+
`\u001b[94mWaiting for module matching: \u001b[93m${filter.key}\u001b[0m`,
89+
1,
90+
)
91+
7992
return onAnyModuleInitialized(
8093
options?.all
8194
? (id, exports) => {
8295
const flag = runFilter(filter, id, exports, options)
83-
if (flag)
96+
if (flag) {
97+
if (__BUILD_FLAG_DEBUG_MODULE_WAITS__)
98+
DEBUG_logWaitMatched(filter.key, id, flag)
99+
84100
callback(
85101
exportsFromFilterResultFlag(flag, exports, options),
86102
id,
87103
)
104+
}
88105
}
89106
: (id, exports) => {
90107
if (mInitialized.has(id)) {
91108
const flag = runFilter(filter, id, exports, options)
92-
if (flag)
109+
if (flag) {
110+
if (__BUILD_FLAG_DEBUG_MODULE_WAITS__)
111+
DEBUG_logWaitMatched(filter.key, id, flag)
112+
93113
callback(
94114
exportsFromFilterResultFlag(
95115
flag,
@@ -98,6 +118,7 @@ export function waitForModules(
98118
),
99119
id,
100120
)
121+
}
101122
}
102123
},
103124
)
@@ -148,3 +169,17 @@ export function waitForModuleByImportedPath<T = any>(
148169

149170
return unsub
150171
}
172+
173+
/**
174+
* Logs to the developer that a module wait has matched.
175+
*/
176+
function DEBUG_logWaitMatched(
177+
key: string,
178+
id: Metro.ModuleID,
179+
flag: FilterResultFlag,
180+
) {
181+
nativeLoggingHook(
182+
`\u001b[32mWait matched: \u001b[33m${key}\u001b[0m (matched ${id}, ${FilterResultFlagToHumanReadable[flag]})`,
183+
1,
184+
)
185+
}

lib/modules/src/metro/_internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export let mList: Metro.ModuleList
2222
* Here's how Metro sets itself up:
2323
* 1. `__METRO_GLOBAL_PREFIX__ = ""`
2424
* 2. `${__METRO_GLOBAL_PREFIX__}__d = function define(...) {}`
25-
* - Why don't we patch it here? Because we need to know the value of __METRO_GLOBAL_PREFIX__ first
25+
* - Why don't we patch it here? Because we need to know the value of `__METRO_GLOBAL_PREFIX__` first
2626
* - And since Revenge runs before everything else, we need to patch in the next steps:
2727
* - We chose to patch it in 4. because we can access all the global functions at that point.
2828
* 3. `__r = function metroRequire(...) {}`

lib/utils/src/proxy.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export function proxify<T>(signal: () => T, options?: ProxifyOptions): T {
147147

148148
if (__BUILD_FLAG_DEBUG_PROXIFIED_VALUES__) {
149149
const v = unproxifyFromHint(hint)
150-
if (v == null) warnDeveloperAboutNullishProxifiedValue()
150+
if (v == null) DEBUG_warnNullishProxifiedValue()
151151
}
152152

153153
return new Proxy(hint, _handler)
@@ -242,7 +242,10 @@ export function destructure<T extends object>(
242242
})
243243
}
244244

245-
function warnDeveloperAboutNullishProxifiedValue() {
245+
/**
246+
* Warns the developer that the proxified value is nullish.
247+
*/
248+
function DEBUG_warnNullishProxifiedValue() {
246249
nativeLoggingHook(
247250
`\u001b[33mProxified value is nullish! The signal is may be invalid.\n${getCurrentStack()}\u001b[0m`,
248251
3,

scripts/build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default async function build(dev = Dev, log = true) {
5959

6060
// See types.d.ts for what these flags do
6161
__BUILD_FLAG_DEBUG_MODULE_LOOKUPS__: String(dev),
62+
__BUILD_FLAG_DEBUG_MODULE_WAITS__: String(dev),
6263
__BUILD_FLAG_DEBUG_PROXIFIED_VALUES__: 'false',
6364
__BUILD_FLAG_LOG_PROMISE_REJECTIONS__: String(dev),
6465
},

shims/shims.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare global {
2+
// CommonJS modules, we don't want to depend on @types/node
3+
const module: {
4+
exports: any
5+
}
6+
}
7+
8+
export {}

shims/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": ["../tsconfig.json"],
3+
"compilerOptions": {
4+
"types": ["./shims.d.ts"]
5+
},
6+
"include": ["./**/*.ts"]
7+
}

src/preinit.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ onModuleFirstRequired(IndexModuleId, function onIndexRequired() {
1414
if (__BUILD_FLAG_LOG_PROMISE_REJECTIONS__)
1515
require('./patches/log-promise-rejections')
1616

17+
if (__BUILD_ENV__ === 'development')
18+
nativeLoggingHook(`\u001b[31m--- PREINIT STAGE ---\u001b[0m`, 1)
19+
1720
// Initialize preinit libraries
1821
require('@revenge-mod/modules/preinit')
1922
require('@revenge-mod/react/preinit')
@@ -27,6 +30,12 @@ onModuleFirstRequired(IndexModuleId, function onIndexRequired() {
2730

2831
onModuleInitialized(IndexModuleId, function onIndexInitialized() {
2932
try {
33+
if (__BUILD_ENV__ === 'development')
34+
nativeLoggingHook(
35+
`\u001b[31m--- INIT STAGE ---\u001b[0m`,
36+
1,
37+
)
38+
3039
// Initialize init libraries
3140
require('@revenge-mod/storage/init')
3241
require('@revenge-mod/externals/init')

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
"~assets/*": ["./dist/assets/generated/*"]
1515
}
1616
},
17-
"include": ["lib", "src", "shims"]
17+
"include": ["lib", "src"]
1818
}

0 commit comments

Comments
 (0)