Skip to content

modules: enforce extensions and dissalow directories - #21572

Closed
MylesBorins wants to merge 3 commits into
nodejs:masterfrom
MylesBorins:enforce-ext
Closed

modules: enforce extensions and dissalow directories#21572
MylesBorins wants to merge 3 commits into
nodejs:masterfrom
MylesBorins:enforce-ext

Conversation

@MylesBorins

Copy link
Copy Markdown
Contributor

this build on top of the limitations of the package name map proposal. It enforces the use of file extensions and doesn't support index.js or package.json resolution for the default ESM loader implementation.

The current approach is naive and has lots of room for improvement but this can serve as a base for discussion

Refs: https://github.com/domenic/package-name-maps

this build on top of the limitations of the package name map proposal.
It enforces the use of file extensions and doesn't support index.js
or package.json resolution for the default ESM loader implementation.
The current approach is naive and has lots of room for improvement
but this can serve as a base for discussion
Refs: https://github.com/domenic/package-name-maps
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-botnodejs-github-bot added the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Jun 28, 2018
Comment threaddoc/api/errors.md
An attempt was made to use features that require [ICU][], but Node.js was not
compiled with ICU support.

<a id="ERR_NON_EXPLICIT_SPECIFIER"></a>

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, sorry, N is before _. But then it should go before ERR_NO_CRYPTO, right?

Comment threaddoc/api/errors.md Outdated
<a id="ERR_NON_EXPLICIT_SPECIFIER"></a>
### ERR_NON_EXPLICIT_SPECIFIER

Modules that are imported must use explicit file extensions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing period.

Comment threaddoc/api/esm.md Outdated

### Mandatory file extensions

You must provide a file extension when using the `import` keyword.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We usually avoid you in API docs, as per style guide.

Comment threadlib/internal/errors.js Outdated
E('ERR_NAPI_INVALID_TYPEDARRAY_LENGTH',
'Invalid typed array length', RangeError);
E('ERR_NON_EXPLICIT_SPECIFIER',
'The file %s does not include an extensionimported modules must declare' +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

extensionimported -> extension. Imported?

@MylesBorinsMylesBorins added esm Issues and PRs related to the ECMAScript Modules implementation. modules-agenda Issues and PRs to discuss during the meetings of the Modules team. labels Jun 28, 2018
throw new ERR_MISSING_MODULE(target);
}
if ((target[0] === '.' || target[0] === '/') &&
(extname(target) <= 1)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It may be a little clearer to have an explicit .length check here instead of relying on JavaScript magic.

// We cannot search without a base.
throw new ERR_MISSING_MODULE(target);
}
if ((target[0] === '.' || target[0] === '/') &&

@guybedfordguybedfordJun 28, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This check should be target[0] === '.' && (target[1] === '/' || target[1] === '.' && target[2] === '/') || target[0] === '/' to ensure support for filenames starting with .. Alternatively just the regex /^\.{0,2}\//.test(target).

I understand this is an initial implementation, but note that this implementation still supports "main"s and URLs getting default extensions. Happy to do a deeper review in due course.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

maybe String.prototype.startsWith can be used?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we do use a method, i'd prefer the non-taintable approaches used elsewhere for robustness

@bmeck

Copy link
Copy Markdown
Member

We should discuss migration strategies here in depth and the impact of this, in particular this seems at odds with the findings of nodejs/modules#139 about migration strategies that support a smooth ability to migrate. It would be good if the overall reasoning about why matching package-name-maps is seen as superior to the migration path findings in that issue.

@bmeckbmeck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this should not land without heavy discussion and documentation of a migration strategy existing somewhere

@MylesBorins

Copy link
Copy Markdown
ContributorAuthor

Closing in lieu of #21729

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

Labels

errorsIssues and PRs related to JavaScript errors originated in Node.js core.esmIssues and PRs related to the ECMAScript Modules implementation.modules-agendaIssues and PRs to discuss during the meetings of the Modules team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@MylesBorins@nodejs-github-bot@bmeck@guybedford@YurySolovyov@vsemozhetbyt