vite icon indicating copy to clipboard operation
vite copied to clipboard

feat: add `debugDepth` option

Open btea opened this issue 1 year ago • 3 comments

Description

When I set the debug parameters and prepare to view relevant information, I expect to be able to see some object information with a deeper nesting level more intuitively.

before after
image image

btea avatar Oct 05 '24 14:10 btea

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Could it be set for the config loading specifically only? I'm not sure if Vite should change the default depth as that might expand other logs.

bluwy avatar Oct 05 '24 14:10 bluwy

That makes sense, it seems that adding a new configuration alone will have the least impact.

btea avatar Oct 05 '24 14:10 btea

Maybe we can add a debugDepth option like the cli option debug.

btea avatar Oct 17 '24 14:10 btea

Sorry for not responding before. I was thinking the depth could be more of an option for createDebugger() options, and we can set the value on log.inspectOpts.depth so that the depth is scoped to the logging directly.

Great find with log.inspectOpts.depth though. I didn't know and thought we'd hit a brick wall. So we could do a change like this:

export function createDebugger(
  namespace: ViteDebugScope,
  options: DebuggerOptions = {},
): debug.Debugger['log'] | undefined {
  const log = debug(namespace)
  if (options.depth && log.inspectOpts.depth == null) {
    log.inspectOpts.depth = options.depth
  }

What do you think?

bluwy avatar Oct 25 '24 13:10 bluwy

This looks good. However, I have a question. createDebugger is a global public function. We can't specify whether it should be applied to a certain scope through parameter configuration.

btea avatar Oct 26 '24 02:10 btea

I'm not quite sure what you mean. Doesn't my code example only applies the custom depth to an instance of debug()? So only calling the returned log() will have the special depth behaviour, but other debug log() will not have, so it's not polluted elsewhere.

bluwy avatar Oct 28 '24 03:10 bluwy

What I mean is that all debug logs that are output now are created by createDebugger. If I pass a configuration parameter in like I currently do, it seems that I can't determine which call createDebugger to pass in the second parameter.

btea avatar Oct 28 '24 03:10 btea

Oh, what I was thinking is that we don't need to have a new debugDepth config in Vite. If the user wants to configure that, I think it's enough to use DEBUG_DEPTH=100 before running Vite.

I think it should be enough for Vite to pass an opinionated:

const debug = createDebugger('vite:config', { depth: 100 })

So that it gets a nice logging by default, only for the vite:config namespace.

bluwy avatar Oct 28 '24 03:10 bluwy

I think it's enough to use DEBUG_DEPTH=100 before running Vite.

Directly configure the environment variable DEBUG_DEPTH=100? Or is it to configure a cli option? If we configure the environment variable directly, then debug will handle it directly, and the following manual passing of the second parameter should be unnecessary.

btea avatar Oct 28 '24 04:10 btea

Directly configure the environment variable. The issue though is that because it defaults to 2 (via nodes formatWithOptions), it's not the best default to use for config logging. We could change the default for the specific logging instead to eg 10 so that it logs better without any config. But if the user explicitly set DEBUG_DEPTH we can respect that instead.

bluwy avatar Oct 28 '24 04:10 bluwy

Oh, I see. However, changing this default value will cause the default output information structure to change. Will it affect some users?

btea avatar Oct 28 '24 05:10 btea

I think since it's only debug information, it should probably be safe. They should also get more logging of the config by default which I think is better.

bluwy avatar Oct 28 '24 05:10 bluwy

OK, I have updated it.

btea avatar Oct 28 '24 05:10 btea