From 3775ab6decc2f8f013142e1282934c12fbd1881e Mon Sep 17 00:00:00 2001 From: Igor Klopov Date: Wed, 1 May 2019 14:37:43 +0300 Subject: [PATCH] path.resolve argv[1] for non-default entrypoint. fixes #671 --- prelude/bootstrap.js | 5 +++++ test/test-50-spawn/test-spawn-a-2.js | 11 ++++++++--- test/test-50-spawn/test-spawn-a-3.js | 10 +++------- test/test-50-spawn/test-spawn-a-4.js | 6 ++---- test/test-50-spawn/test-spawn-a-5.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 test/test-50-spawn/test-spawn-a-5.js diff --git a/prelude/bootstrap.js b/prelude/bootstrap.js index 11d086d99..aa89854a7 100644 --- a/prelude/bootstrap.js +++ b/prelude/bootstrap.js @@ -53,6 +53,11 @@ if (process.argv[1] !== 'PKG_DUMMY_ENTRYPOINT') { if (process.env.PKG_EXECPATH === EXECPATH) { process.argv.splice(1, 1); + + if (process.argv[1] && process.argv[1] !== '-') { + // https://github.com/nodejs/node/blob/1a96d83a223ff9f05f7d942fb84440d323f7b596/lib/internal/bootstrap/node.js#L269 + process.argv[1] = require('path').resolve(process.argv[1]); + } } else { process.argv[1] = DEFAULT_ENTRYPOINT; } diff --git a/test/test-50-spawn/test-spawn-a-2.js b/test/test-50-spawn/test-spawn-a-2.js index 991755a00..e5d8e2bc4 100644 --- a/test/test-50-spawn/test-spawn-a-2.js +++ b/test/test-50-spawn/test-spawn-a-2.js @@ -2,12 +2,17 @@ 'use strict'; +var path = require('path'); var spawn = require('child_process').spawn; +if (process.send) { + require('./test-spawn-a-child.js'); + return; +} + var child = spawn( - process.execPath, [ - require.resolve('./test-spawn-a-child.js'), 'argvx', '--argvy' - ], { stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ] } + process.execPath, [ path.basename(__filename), 'argvx', '--argvy' ], + { stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ] } ); child.on('message', function (value) { diff --git a/test/test-50-spawn/test-spawn-a-3.js b/test/test-50-spawn/test-spawn-a-3.js index 6b4dd4e9b..991755a00 100644 --- a/test/test-50-spawn/test-spawn-a-3.js +++ b/test/test-50-spawn/test-spawn-a-3.js @@ -4,14 +4,10 @@ var spawn = require('child_process').spawn; -if (process.send) { - require('./test-spawn-a-child.js'); - return; -} - var child = spawn( - process.execPath, [ process.argv[1], 'argvx', '--argvy' ], - { stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ] } + process.execPath, [ + require.resolve('./test-spawn-a-child.js'), 'argvx', '--argvy' + ], { stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ] } ); child.on('message', function (value) { diff --git a/test/test-50-spawn/test-spawn-a-4.js b/test/test-50-spawn/test-spawn-a-4.js index cb105b7b6..6b4dd4e9b 100644 --- a/test/test-50-spawn/test-spawn-a-4.js +++ b/test/test-50-spawn/test-spawn-a-4.js @@ -2,7 +2,6 @@ 'use strict'; -var path = require('path'); var spawn = require('child_process').spawn; if (process.send) { @@ -11,9 +10,8 @@ if (process.send) { } var child = spawn( - process.argv[0], [ - path.join(process.cwd(), path.basename(__filename)), 'argvx', '--argvy' - ], { stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ] } + process.execPath, [ process.argv[1], 'argvx', '--argvy' ], + { stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ] } ); child.on('message', function (value) { diff --git a/test/test-50-spawn/test-spawn-a-5.js b/test/test-50-spawn/test-spawn-a-5.js new file mode 100644 index 000000000..cb105b7b6 --- /dev/null +++ b/test/test-50-spawn/test-spawn-a-5.js @@ -0,0 +1,28 @@ +#!/usr/bin/env node + +'use strict'; + +var path = require('path'); +var spawn = require('child_process').spawn; + +if (process.send) { + require('./test-spawn-a-child.js'); + return; +} + +var child = spawn( + process.argv[0], [ + path.join(process.cwd(), path.basename(__filename)), 'argvx', '--argvy' + ], { stdio: [ 'inherit', 'inherit', 'inherit', 'ipc' ] } +); + +child.on('message', function (value) { + console.log(value.toString()); + child.send(value); +}); + +child.send(2); + +child.on('exit', function (code) { + console.log('Child exited with code', code); +});