Skip to content
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

Fixes #7951: prevent remix.init errors when init script is an ES6 module, update docs to clarify init script requirements #8100

Conversation

leothorp
Copy link
Contributor

@leothorp leothorp commented Nov 22, 2023

Closes: #7951

This addresses the two issues below, which are underlying #7951.

  1. Due to the use of require to import the init script, remix.init/index.js currently cannot be defined as an ESM module without throwing Error [ERR_REQUIRE_ESM]: require() of ES Module. This file will be considered an ESM module by default if using npx create-remix@latest, creating a situation where defining a commonjs package.json in remix.init was required for it to not throw- despite the docs indicating that that the package.json should be optional.
  • fixed by switching to a dynamic import for ESM (which still falls back to a require for CJS modules) and adding a test covering ESM remix.init scripts.
  1. The docs don't indicate any required exports for the file, but it only works if exporting an init function. Updated docs and error messages to make the required contract more explicit, and added a test for the case of an init script without an export.
  • Docs
  • Tests

Testing Strategy:

  • added new unit tests for both cases above; see comments below for the related code.
  • Verified existing tests still pass.
  • manual testing of all scenarios before/after the changes (es6 module, cjs module, default-exported function, no export).

Copy link

changeset-bot bot commented Nov 22, 2023

⚠️ No Changeset found

Latest commit: 87534a4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Nov 22, 2023

Hi @leothorp,

Welcome, and thank you for contributing to Remix!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected].

Thanks!

- The Remix team

@leothorp leothorp changed the title Fixes #7951: allow remix.init script to not have an export and/or to be an es6 module Fixes #7951: allow remix.init script to function without an accompanying package.json or a default export Nov 22, 2023
@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Nov 22, 2023

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

if (typeof initFn !== "function" && initFn.default) {
initFn = initFn.default;
}
try {
await initFn({ packageManager, rootDirectory: projectDir });
if (typeof initFn === "function") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Covered by the "succeeds for a remix.init script that doesn't default export a function" test.

@@ -44,12 +44,15 @@ export async function init(
});
}

let initFn = require(initScript);
let initFn = await import(initScript);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Covered by the "succeeds for remix.init defined as an ES6 module" test.

@@ -31,6 +32,31 @@ jest.mock("child_process", () => {
};
});

// Copy over the current build of @remix-run/dev and execute remix init from the node_modules dir there.
// Certain bugs only manifest when using this approach.
const execRemixInitInProject = (projectDir: string) => {
Copy link
Contributor Author

@leothorp leothorp Nov 22, 2023

Choose a reason for hiding this comment

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

This stuff with copying over the newly-built version of @remix-run/dev and running it as a node module in the temp project is clunky, but was the only way I could reproduce the failing behavior of an es6 module init script throwing an error in the unit tests. With run(["init"]) the test always succeeds with the old code, even though remix init errors in actual use.

It seems like some aspect of the run(["init"]) calls used by other tests here doesn't fully replicate the real-world behavior of running remix init as a project dependency, so this was the only workaround I found initially .

@leothorp leothorp marked this pull request as ready for review November 22, 2023 12:25
@leothorp leothorp changed the title Fixes #7951: allow remix.init script to function without an accompanying package.json or a default export Fixes #7951: prevent remix.init errors when init script is an ES6 module or does not export a function Nov 22, 2023
@leothorp leothorp marked this pull request as draft December 7, 2023 21:32
@leothorp leothorp marked this pull request as ready for review December 7, 2023 21:32
@leothorp leothorp closed this Jan 1, 2024
@markdalgleish
Copy link
Member

@leothorp Sorry for the delay. I'm looking to get this in but wanted to first check whether there was a reason you closed it?

@leothorp
Copy link
Contributor Author

leothorp commented Jan 4, 2024

@leothorp Sorry for the delay. I'm looking to get this in but wanted to first check whether there was a reason you closed it?

@markdalgleish ah, sorry about the confusion- was just cleaning up my open PRs that I wasn't sure were still relevant, but happy to reopen.

@leothorp leothorp reopened this Jan 4, 2024
@leothorp
Copy link
Contributor Author

@markdalgleish anything else I can do to help get this over the line? no rush of course- and my apologies again for any confusion from closing it earlier.

@leothorp
Copy link
Contributor Author

@markdalgleish is this still relevant?

@leothorp leothorp closed this Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants