-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
console: timeEnd() doesn't throw an error if no matching label #5901
Conversation
if (!stdout || typeof stdout.write !== 'function') { | ||
throw new TypeError('Console expects a writable stream instance'); | ||
} | ||
if (!(this instanceof Console)) return new Console(stdout, stderr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert these style changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cjihrig What style guides do you using? Should I revert all the style changes or only this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an ESLint setup. But, in general, you should change the bare minimum in your PRs. Otherwise, it makes tools like git blame harder to use.
@cjihrig done |
@@ -8,7 +8,8 @@ assert.ok(process.stderr.writable); | |||
assert.equal('number', typeof process.stdout.fd); | |||
assert.equal('number', typeof process.stderr.fd); | |||
|
|||
assert.throws(function() { | |||
assert.doesNotThrow(function() { | |||
console.time('some label'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unnecessary.
@cjihrig got it, so we can ignore non-existing label. |
throw new Error('No such label: ' + label); | ||
} | ||
const time = this._times.get(label); | ||
if (!time) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you just replace the old throw
with return;
. It just needs to be a one line change.
I think so (see #3514 (comment)). What you had before was wrong. It would print something but it had the incorrect times. |
@@ -8,10 +8,6 @@ assert.ok(process.stderr.writable); | |||
assert.equal('number', typeof process.stdout.fd); | |||
assert.equal('number', typeof process.stderr.fd); | |||
|
|||
assert.throws(function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't mean to delete the whole test. Just the one line that I commented on was unnecessary.
@cjihrig here is my code: var console = require('console');
console.time('test');
console.timeEnd('no such label');
console.timeEnd('test'); Prints only for ./node test.js
test: 0.652ms The following code: var console = require('console');
console.timeEnd('no such label'); prints nothing. |
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this newly added line.
LGTM with one comment. |
@cjihrig I see now. I must change only lines where it actually needs. Updated. |
Thanks! LGTM. CI: https://ci.nodejs.org/job/node-test-pull-request/2067/ |
LGTM |
We are doing this so that we would be on par with browsers? It feels like what we are doing is correct. If there is a typo in the name or if the usage is wrong we explicitly throw and let the user know what is wrong. |
How about a compromise? Don't throw but emit a warning using process.emitWarning. |
In case of wrong label you get nothing in the console. That way you will know that you forgot to close |
@thefourtheye ... do you still object to this or ...? |
Yes, I am still not convinced that this change is better. To think of it, emitting a warning, as you suggested, would be the right fit here. This is not a serious error, I agree, but there should be some feedback to the user. |
if (!time) { | ||
throw new Error('No such label: ' + label); | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ghaiklor ... would it be possible for you to add a ...
process.emitWarning(`No such label: ${label}`);
...before returning here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include console.timeEnd
somewhere in the message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't hurt... No such label '${label}' for console.timeEnd()
perhaps? Keep in mind, also, that the warning includes a stack trace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, then the message is just fine.
@jasnell @thefourtheye yeah, I will update branch in few hours. |
Warning doesn't include a stack trace. So I updated message with @jasnell approach. > console.timeEnd('no label');
> (node:51305) Warning: No such label 'no label' for console.timeEnd() |
@ghaiklor. It does, it's just suppressed in the default output. If you use the |
one additional suggestion: in the test case, you can add a check to make sure the warning is issued appropriately... something like... process.on('warning', common.mustCall((warning) => {
assert(/No such label 'no label' for console.timeEnd\(\)/.test(warning.message));
})); |
LGTM with a suggestion. |
I've added check for |
LGTM |
@@ -1,5 +1,5 @@ | |||
'use strict'; | |||
require('../common'); | |||
var common = require('../common'); | |||
var assert = require('assert'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: these requires should use const
couple more nits after adding the warning check |
assert.throws(function() { | ||
assert.doesNotThrow(function() { | ||
const message = /No such label 'no such label' for console\.timeEnd\(\)/; | ||
const onWarning = common.mustCall(warning => assert(message.test(warning.message))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this pass linting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't. We added a rule where parens are explicitly required I though
Yea, still LGTM once the comments are addressed. |
@cjihrig updated and fixed conflicts with master branch. |
It would probably be simpler to use |
@cjihrig yes, I can, but I'm not sure if I can push them with force flag. As I understand, I need to do: git rebase -i master Squash all commits in one and then git push --force origin fix/3514 |
It's fine to force push your own branch. No one else is using it. |
@cjihrig done |
const message = /No such label 'no such label' for console\.timeEnd\(\)/; | ||
const onWarning = (warning) => assert(message.test(warning.message)); | ||
process.once('warning', common.mustCall(onWarning)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could likely be rewritten and simplified to just:
process.once('warning', common.mustCall((warning) => {
assert(/no such label/.test(warning.message);
});
There's likely very little need to test the entire warning message. You just want to verify that the warning is being issued.
Just one last nit while I was looking it back over :-) Looking good! |
When timeEnd() provided with label that doesn't exists it emits warning in the console, so developer get know about it.
@jasnell done. BTW, why do I need squash commits by myself if it can be done when merging into master (after rebase)? |
@ghaiklor ... while it's possible for the person landing the PR to squash everything, it's often easier for those of us who do the landing to have the author of the PR squash them down (typically because it saves time). I personally don't mind squashing commits when I land things. |
New CI: https://ci.nodejs.org/job/node-test-pull-request/2359/ |
@jasnell I see, thanks for explanation 😃 |
Awesome! Thank you for the contribution and for the patience! @cjihrig ... does this LGTY? |
(yes, LGTM) |
Awesome. Given that this is a semver-major, let's give time for one more round of last call comments from @nodejs/ctc. If there are no objections by Monday I'll get this landed. |
When timeEnd() provided with label that doesn't exists it emits warning in the console, so developer get know about it. PR-URL: #5901 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Landed in 1c84579 |
When timeEnd() provided with label that doesn't exists it emits warning in the console, so developer get know about it. PR-URL: nodejs#5901 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
When timeEnd() provided with label that doesn't exists it emits warning in the console, so developer get know about it. PR-URL: #5901 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Pull Request check-list
make -j8 test
(UNIX) orvcbuild test nosign
(Windows) pass withthis change (including linting)?
test (or a benchmark) included?
existing APIs, or introduces new ones)?
Affected core subsystem(s)
console
Description of change
Based on #3514 discussion, I've removed throw an error in timeEnd() method, if there is no matching label.