-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Remove uncommon functions from common module #17781
Conversation
let tty_fd = 0; | ||
if (!tty.isatty(tty_fd)) tty_fd++; | ||
else if (!tty.isatty(tty_fd)) tty_fd++; | ||
else if (!tty.isatty(tty_fd)) tty_fd++; |
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 someone explain to me how this works?
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 write the code but I assume:
The default file descriptors for I/O are stdin, stdout and stderr (0, 1 and 2 respectively). It's not always guaranteed to be the case, so we try the first 3 and then try /dev/tty
which usually works (it maps for the controlling terminal of a process for each process) but it's slower to call than isatty it if it's 0.
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 was a cut-and-paste, I didn't write it, but yeah, it's not just hard to follow, but also buggy. If the first if
fails, on line 253, it increments tty_fd
but doesn't then check the condition on line 254 because that's an else
so it skips that line. Ugh.
If 0 and 1 are not TTYs but 2 is, we won't get 2 (unless that's what's returned by the try/catch
block subsequently).
I think something more like this is what we want:
let tty_fd = [0, 1, 2].find(tty.isatty);
if (tty_fd === undefined) {
try {
tty_fd = fs.openSync('/dev/tty');
} catch (e) {
// There aren't any tty fd's available to use.
return -1;
}
}
return tty_fd;
(And while I'm in there fixing this function so it hopefully works correctly, I should probably refactor tty_fd
to have a name that doesn't include _
.)
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.
Added a commit here to fix this. PTAL.
common.projectDir is used in one test, so it's not so common. Remove from common module to the one test file that needs it.
common.getTTYfd() is used in one test only. Move it's definition to that test and out of the common module.
common.firstInvalidFD() is used in only one test. Move it out of the common module and into the one test that uses it.
Some code refactored and bug-fixed. |
if (tty_fd >= 0) { | ||
function getTTYfd() { | ||
const tty = require('tty'); | ||
let ttyFd = [0, 1, 2].find(tty.isatty); |
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.
Now that I read it - I'm surprised isatty is synchronous :D Change LGTM
const tty = require('tty'); | ||
let tty_fd = 0; | ||
if (!tty.isatty(tty_fd)) tty_fd++; | ||
else if (!tty.isatty(tty_fd)) tty_fd++; |
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.
Was this branch ever reached?
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.
Nvm, 8be1259 is the answer.
(CI failures are unrelated.) |
common.projectDir is used in one test, so it's not so common. Remove from common module to the one test file that needs it. PR-URL: nodejs#17781 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
common.getTTYfd() is used in one test only. Move it's definition to that test and out of the common module. PR-URL: nodejs#17781 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
common.firstInvalidFD() is used in only one test. Move it out of the common module and into the one test that uses it. PR-URL: nodejs#17781 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: nodejs#17781 Ref: nodejs#17781 (comment) Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
This does not cleanly land on v9.x, would someone be willing to manually backport? |
@MylesBorins This will land cleanly once #17401 is backported. |
common.projectDir is used in one test, so it's not so common. Remove from common module to the one test file that needs it. PR-URL: #17781 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
common.getTTYfd() is used in one test only. Move it's definition to that test and out of the common module. PR-URL: #17781 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
common.firstInvalidFD() is used in only one test. Move it out of the common module and into the one test that uses it. PR-URL: #17781 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #17781 Ref: #17781 (comment) Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
In an effort to make the
common
module slightly less of a monolith and junk drawer, and to reduce the needed parsing/loading of some function from around 2000 times per test run to just 1, this PR removes functions that are only used once.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
test