Skip to content

Incorrectly cleans file:// paths #60

Open
@jaydenseric

Description

@jaydenseric

file:// URL paths can only be absolute:

file: URLs are always absolute paths.
https://nodejs.org/api/fs.html#fs_file_url_paths

Yet the clean method strips out the CWD from the file: URLs, leaving the file:// at the start, making them invalid.

This is a pressing issue, because the error stack from errors thrown in an ESM file starts with the file: URL of the ESM. The ecosystem is currently migrating to vanilla ESM (in .mjs files, or .js files with package "type": "module") and more people will start to notice this bug.

In test.mjs:

import StackUtils from 'stack-utils';

const stackUtils = new StackUtils();

try {
  throw new Error('Message.');
} catch (error) {
  console.log(error.stack);
  console.log(stackUtils.clean(error.stack));
}

Then, run node test.mjs and the console log is ([CWD] substituting the absolute CWD path):

Error: Message.
    at file://[CWD]/test.mjs:6:9
    at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
file://test.mjs:6:9

Note the invalid file://test.mjs:6:9, it should be test.mjs:6:9.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions