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

fix: Remove legacy spawnSync feature detection #87

Merged
merged 1 commit into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ const pathRe = isWindows ? /^PATH=/i : /^PATH=/
const colon = isWindows ? ';' : ':'

function wrap (argv, env, workingDir) {
// spawn_sync available since Node v0.11
let spawnSyncBinding, spawnSync
try {
spawnSyncBinding = process.binding('spawn_sync')
} catch (e) {}
const spawnSyncBinding = process.binding('spawn_sync')
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I was out earlier didn't get a chance to review this. process.binding is silently deprecated in current versions of node.js, this change assumes it will never be removed. Is that a good idea?

// @bcoe @demurgos

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remember reading about possible deprecations but haven't heard more about it. Do you know how soon it may be deprecated? Node 13? Node 14?

Generally, I don't like that this library use Node's internals and monkey-patches them because it is fragile and has bad encapsulation. It is possible to patch the spawn function at a higher level: I would prefer to move into this direction. The internal patching could be kept for the moment, but ultimately I'd like to deprecate it. This progression would be similar to graceful-fs: it initially patched Node's internals but then moved to an "external wrapper" design.

Copy link
Member

Choose a reason for hiding this comment

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

process.binding hit 'documentation only' deprecation in 10.9.0. Since 11.12.0 it is a 'pending deprecation', so node --pending-deprecation will show a warning. I don't have any information on when it will be escalated to full deprecation. I raised this module as a potential issue at nodejs/node#27344. I'm not entirely sure the 'external wrapper' way of graceful-fs will really work, the idea is to 'hijack' calls to spawn / spawnSync. Maybe the NODE_OPTIONS environment will work, I haven't had a chance to give that much thought yet.

https://nodejs.org/dist/latest-v11.x/docs/api/deprecations.html#DEP0111

Copy link
Contributor Author

@demurgos demurgos May 10, 2019

Choose a reason for hiding this comment

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

Oh, thank you very much for the info: I wasn't aware that it already started being deprecated.

An external wrapper would be a function that changes the options and then passes them to require("child_process").spawn, without ever changing the internals. But it only solves the issue at the top level. To wrap processes deep in the process tree (and hooking into code you don't control) I still patch the internals.

It's a tough problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opened an issue to discuss this deprecation: #89


// if we're passed in the working dir, then it means that setup
// was already done, so no need.
Expand All @@ -54,23 +50,17 @@ function wrap (argv, env, workingDir) {
workingDir = setup(argv, env)
}
const spawn = ChildProcess.prototype.spawn
if (spawnSyncBinding) {
spawnSync = spawnSyncBinding.spawn
}
const spawnSync = spawnSyncBinding.spawn

function unwrap () {
function unwrap() {
if (doSetup && !doDebug) {
rimraf.sync(workingDir)
}
ChildProcess.prototype.spawn = spawn
if (spawnSyncBinding) {
spawnSyncBinding.spawn = spawnSync
}
spawnSyncBinding.spawn = spawnSync
}

if (spawnSyncBinding) {
spawnSyncBinding.spawn = wrappedSpawnFunction(spawnSync, workingDir)
}
spawnSyncBinding.spawn = wrappedSpawnFunction(spawnSync, workingDir)
ChildProcess.prototype.spawn = wrappedSpawnFunction(spawn, workingDir)

return unwrap
Expand Down
1 change: 0 additions & 1 deletion shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
const pexec = process.execArgv
if (JSON.stringify(pexec) !== JSON.stringify(needExecArgv)) {
debug('need execArgv for this', pexec, '=>', needExecArgv)
const spawn = require('child_process').spawn
const sargs = pexec.concat(needExecArgv).concat(process.argv.slice(1))
foregroundChild(node, sargs)
return
Expand Down