Sitelet https://github.com/nodejs/node/issues/50593
Skip to content

Bad error message for assert.ok in ESM #50593

Description

@nicolo-ribaudo

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

Activity

  1. added
    assertIssues and PRs related to the assert subsystem.
    esmIssues and PRs related to the ECMAScript Modules implementation.
    on Nov 7, 2023
  2. txn100 commented on Nov 8, 2023

    @txn100

    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.

  3. MrJithil commented on Nov 8, 2023

    @MrJithil
    Member

    error code is same, so the message has to be.

    Checking the implementation.

  4. MrJithil commented on Nov 8, 2023

    @MrJithil
    Member

    Both cases, the error iso originating from the assert.
    In CJS, the error will reach directly to the file. But, for ESM, the esm_loader will catch and re-throw this exception using triggerUncaughtException internal bindings.

    Also, inside the asserts, we are generating the message using getErrorMessage function. There we are prefixing the message The 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.

    static void TriggerUncaughtException(const FunctionCallbackInfo<Value>& args) {
    
  5. MrJithil commented on Nov 8, 2023

    @MrJithil
    Member
  6. MrJithil commented on Nov 9, 2023

    @MrJithil
    Member

    For ESM, call.getFileName() is returning fileName as file://<path to file> instead of /<path to file>

    const filename = call.getFileName();

  7. MrJithil commented on Nov 9, 2023

    @MrJithil
    Member

    Fix raised

  8. MrJithil commented on Nov 11, 2023

    @MrJithil
    Member

    PR landed hence closing the issue. Thanks @nicolo-ribaudo for bringing this issue to notice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions