Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log error stack traces to the console #1940

Closed

Conversation

thecrypticace
Copy link

What kind of change does this PR introduce?
Show stack traces for errors logged by webpack-cli.

Did you add tests for your changes?
No.

If relevant, did you update the documentation?
No.

Summary
An error occurring in a plugin (for example calling missing APIs or just throwing an error during apply()) results in a displayed error but no stack trace. This in turn makes it nearly impossible to track down the source of the problem and address it.

You may see an example of what I'm trying to address here: https://github.com/thecrypticace/webpack-cli-stack-traces

Does this PR introduce a breaking change?
No.

Other information
n/a

@thecrypticace thecrypticace requested a review from a team as a code owner October 13, 2020 23:20
@jsf-clabot
Copy link

jsf-clabot commented Oct 13, 2020

CLA assistant check
All committers have signed the CLA.

val = val.stack;
}

console.error(`[webpack-cli] ${red(val)}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? error.toString() always show the stacktrace

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Node 14 error.toString() only shows the error message.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto in Node 12.x and 13.x as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -55,7 +55,8 @@ const addonGenerator = (
try {
mkdirp.sync(pathToProjectDir);
} catch (err) {
logger.error('Failed to create directory', err);
logger.error('Failed to create directory');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.error(Failed to create directory: ${err});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above about error.toString()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No:

error.js

const error = new Error('test');

console.log(error);

and run node: error.js

Error: test
    at Object.<anonymous> (/home/path/to/test.js:1:77)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is special handling by the console functions.

The code doesn't do that because logger.error does this:

console.log(`[webpack-cli] ${error}`)

Which at runtime is the same as:

console.log('[webpack-cli] ' + error.toString())

Which does not print the stack trace. Did you check out the test repo which showcases the behavior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more direct test would be to log the error using the logger. You can create a test.js file in the root of the repo with this code:

const logger = require("webpack-cli/lib/utils/logger");
logger.error(new Error("test"));

And see it not have the expected behavior when you run it with node test.js

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need improve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants