Skip to content

esm: add limited support for --print #52105

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1231,12 +1231,6 @@ provided.
Encoding provided to `TextDecoder()` API was not one of the
[WHATWG Supported Encodings][].

<a id="ERR_EVAL_ESM_CANNOT_PRINT"></a>

### `ERR_EVAL_ESM_CANNOT_PRINT`

`--print` cannot be used with ESM input.

<a id="ERR_EVENT_RECURSION"></a>

### `ERR_EVENT_RECURSION`
Expand Down Expand Up @@ -3696,6 +3690,17 @@ removed: v15.0.0

The native call from `process.cpuUsage` could not be processed.

<a id="ERR_EVAL_ESM_CANNOT_PRINT"></a>

### `ERR_EVAL_ESM_CANNOT_PRINT`

<!--
added: v14.3.0
removed: REPLACEME
-->

`--print` cannot be used with ESM input.

[ES Module]: esm.md
[ICU]: intl.md#internationalization-support
[JSON Web Key Elliptic Curve Registry]: https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
Expand Down
1 change: 0 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,6 @@ E('ERR_ENCODING_INVALID_ENCODED_DATA', function(encoding, ret) {
}, TypeError);
E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported',
RangeError);
E('ERR_EVAL_ESM_CANNOT_PRINT', '--print cannot be used with ESM input', Error);
E('ERR_EVENT_RECURSION', 'The event "%s" is already being dispatched', Error);
E('ERR_FALSY_VALUE_REJECTION', function(reason) {
this.reason = reason;
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET,
ERR_EVAL_ESM_CANNOT_PRINT,
},
} = require('internal/errors');
const { pathToFileURL } = require('internal/url');
Expand Down Expand Up @@ -61,7 +60,7 @@ function getEvalModuleUrl() {
*/
function evalModuleEntryPoint(source, print) {
if (print) {
throw new ERR_EVAL_ESM_CANNOT_PRINT();
source = `console.log((${source}))`;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
source = `console.log((${source}))`;
source = `console.log(await (async(() => {${source}})()))`;

will wrapping source with an async iife make multiple expressions and missing await work?

Copy link
Contributor Author

@aduh95 aduh95 Mar 17, 2024

Choose a reason for hiding this comment

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

It would force the user to write return, which would be weird (let’s not forget that using return at the top-level of a module is not valid syntax)

}
RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs.
return require('internal/modules/run_main').runEntryPointWithESMLoader(
Expand Down
42 changes: 22 additions & 20 deletions test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const assert = require('assert');
const child = require('child_process');
const path = require('path');
const fixtures = require('../common/fixtures');
const { spawnSyncAndAssert } = require('../common/child_process');
const nodejs = `"${process.execPath}"`;

if (process.argv.length > 2) {
Expand Down Expand Up @@ -224,30 +225,31 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`,


// Assert that "42\n" is written to stdout on module eval.
const execOptions = '--input-type module';
child.exec(
`${nodejs} ${execOptions} --eval "console.log(42)"`,
common.mustSucceed((stdout) => {
assert.strictEqual(stdout, '42\n');
}));
const execOptions = '--input-type=module';
spawnSyncAndAssert(process.execPath, [execOptions, '--eval', 'console.log(42)'], {
stdout: '42\n',
stderr: '',
});

// Assert that "42\n" is written to stdout with print option.
child.exec(
`${nodejs} ${execOptions} --print --eval "42"`,
common.mustCall((err, stdout, stderr) => {
assert.ok(err);
assert.strictEqual(stdout, '');
assert.ok(stderr.includes('--print cannot be used with ESM input'));
}));
spawnSyncAndAssert(process.execPath, [execOptions, '--print', '--eval', 42], {
stdout: '42\n',
stderr: '',
});

// Assert that error is written to stderr on invalid input.
child.exec(
`${nodejs} ${execOptions} --eval "!!!!"`,
common.mustCall((err, stdout, stderr) => {
assert.ok(err);
assert.strictEqual(stdout, '');
assert.ok(stderr.indexOf('SyntaxError: Unexpected end of input') > 0);
}));
spawnSyncAndAssert(process.execPath, [execOptions, '--print', '--eval', '!!!!'], {
status: 1,
stdout: '',
stderr: /SyntaxError: Unexpected token '\)'\n/,
});

// Assert that error is written to stderr on unsupported input.
spawnSyncAndAssert(process.execPath, [execOptions, '-p', 'import "node:os"'], {
status: 1,
stdout: '',
stderr: /SyntaxError: Unexpected string\n/,
});

// Assert that require is undefined in ESM support
child.exec(
Expand Down