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

Remove usages of require.main #9771

Closed
wants to merge 3 commits into from
Closed

Conversation

nwalters512
Copy link
Contributor

There is no direct replacement for require.main in ESM modules. There's always https://www.npmjs.com/package/es-main... but I'd rather not drag in another dependency when IMO we never actually really need this check.

Comment on lines -128 to -131
if (require.main !== module) {
throw new Error('This script is designed to be run as the main process');
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was meant as a safely check, but I think it's safe enough to get rid of it 🙃

Copy link
Member

Choose a reason for hiding this comment

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

Agreed

Comment on lines -81 to -87
// If there is only one argument and `server.js` is being executed directly,
// legacy it into the config option.
if (require.main === module && argv['_'].length === 1) {
argv['config'] = argv['_'][0];
argv['_'] = [];
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hopefully it's safe to break this legacy feature now? We certainly don't rely on it for production deployments, and it's been over 6 years since we introduced --config.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think it's fine to break this. Just to make sure I understand, if someone is in fact using the old syntax then it will break with an error message, right? It won't just silently break?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it will in fact silently break; yargs-parser doesn't do any validation.

Copy link
Contributor

github-actions bot commented Apr 24, 2024

All images

Image Platform Old Size New Size Change
prairielearn/executor:8c4883df11a3e13fd4a2b4fce89a22bae58d438b null 1630.71 MB 1630.71 MB -0.00%
prairielearn/prairielearn:8c4883df11a3e13fd4a2b4fce89a22bae58d438b linux/amd64 1630.71 MB 1630.71 MB -0.00%

@@ -2165,7 +2158,7 @@ export async function insertDevUser() {
await sqldb.queryAsync(adminSql, { user_id });
}

if (require.main === module && config.startServer) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out this is required for our test suite 🙃 I'll add it back for now and figure out something else on the ESM conversion branch.

Copy link
Member

Choose a reason for hiding this comment

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

Dang! So many hacks 😅

@nwalters512
Copy link
Contributor Author

This was ultimately rolled into the ESM conversion.

@nwalters512 nwalters512 deleted the remove-require-main branch May 11, 2024 00:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants