Repository navigation
Bad error message for assert.ok in ESM #50593
Copy link
Copy link
Closed
Labels
assertIssues and PRs related to the assert subsystem.Issues and PRs related to the assert subsystem.esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
Description
Version
21.1.0
Platform
No response
Subsystem
No response
What steps will reproduce the bug?
const assert = require("assert");
assert.ok(0 === 2)import assert from "assert"
assert.ok(0 === 2)How often does it reproduce? Is there a required condition?
No response
What is the expected behavior? Why is that the expected behavior?
The CJS code throws this error:
➜ node file.js
node:assert:399
throw err;
^
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert.ok(0 === 2)
at Object.<anonymous> (/Users/nic/Documents/misc/node-assert-test/file.js:3:8)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49 {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
I would expect something similar for ESM
What do you see instead?
ESM throws this error:
➜ node file.mjs
node:internal/process/esm_loader:40
internalBinding('errors').triggerUncaughtException(
^
AssertionError [ERR_ASSERTION]: false == true
at file:///Users/nic/Documents/misc/node-assert-test/file.mjs:3:8
at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:113:12) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
Without showing the assert.ok call.
Additional information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
assertIssues and PRs related to the assert subsystem.Issues and PRs related to the assert subsystem.esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
Activity
txn100 commentedon Nov 8, 2023
I think the variation you're seeing in error outputs between CJS and ESM is due to their different execution contexts—CJS is synchronous, while ESM is inherently asynchronous.
MrJithil commentedon Nov 8, 2023
error code is same, so the message has to be.
Checking the implementation.
MrJithil commentedon Nov 8, 2023
Both cases, the error iso originating from the assert.
In CJS, the error will reach directly to the file. But, for ESM, the
esm_loaderwill catch and re-throw this exception usingtriggerUncaughtExceptioninternal bindings.Also, inside the asserts, we are generating the message using
getErrorMessagefunction. There we are prefixing the messageThe expression evaluated to a falsy value:for errors.an attempt to open the file
fd = openSync(filename, 'r', 0o666);is failing for ESM. So, the further message formatting being skipped.MrJithil commentedon Nov 8, 2023
#49913
MrJithil commentedon Nov 9, 2023
For ESM,
call.getFileName()is returning fileName asfile://<path to file>instead of/<path to file>node/lib/assert.js
Line 299 in adf332b
MrJithil commentedon Nov 9, 2023
Fix raised
MrJithil commentedon Nov 11, 2023
PR landed hence closing the issue. Thanks @nicolo-ribaudo for bringing this issue to notice.