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 explicit process.exit(0) from cli.ts (take 2) #8135

Closed

Conversation

rossipedia
Copy link
Contributor

I accidentally deleted my fork, so I'm re-opening #7552. This is a copy/paste of that PR's body.


Calling process.exit(0) in the successful case is redundant, but also causes problems when wanting to use the CLI commands in any kind of automated capacity, if the output is larger than 8192 bytes. This is because the Promise resolves before the stdout buffers have been flushed, and when process.exit(0) is called, the entire process terminates leaving buffered output unsent to the calling process.

This is usually not a problem when running the CLI directly, but can be problematic when invoked via a child process. For instance, @brophdawg11 supplied this sample code in Discord:

// tmp.js
const child_process = require('child_process')
const { matchRoutes } = require("react-router-dom")
const routes = JSON.parse(
  child_process.execSync("npx remix routes --json").toString()
)
const matches = matchRoutes(routes, "/")

However, attempting to run this on a project with a large number of routes, resulting in a JSON object larger than 8192 bytes, results in a deserialization error:

❯ node tmp.js
undefined:160
                "

SyntaxError: Unterminated string in JSON at position 8192

The default case for a node script that exits gracefully is an exit code of 0. So, removing the explicit process.exit(0) leaves the process running until all I/O is completed and STDOUT buffers are flushed.

Testing Strategy:

I tested this using the following patch on an installed version of @remix-run/dev, which resulted in letting the above JS code run successfully and parse the full JSON output (which is larger than 8192 bytes):

diff --git a/dist/cli.js b/dist/cli.js
index a7df897f9b2e3efee0f8d154dae72fc7450ca937..bd3d4462ebdbaa8ed89d1e9ec8ec0ce46cad7328 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -13,9 +13,7 @@
 
 var index = require('./index');
 
-index.cli.run().then(() => {
-  process.exit(0);
-}, error => {
+index.cli.run().catch(error => {
   if (error) console.error(error);
   process.exit(1);
 });

Copy link

changeset-bot bot commented Nov 26, 2023

🦋 Changeset detected

Latest commit: acfa17a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@remix-run/dev Patch
create-remix Patch
remix Patch
@remix-run/architect Patch
@remix-run/cloudflare Patch
@remix-run/cloudflare-pages Patch
@remix-run/cloudflare-workers Patch
@remix-run/css-bundle Patch
@remix-run/deno Patch
@remix-run/eslint-config Patch
@remix-run/express Patch
@remix-run/node Patch
@remix-run/react Patch
@remix-run/serve Patch
@remix-run/server-runtime Patch
@remix-run/testing Patch

Not sure what this means? Click here to learn what changesets are.

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

@rossipedia rossipedia changed the title Fix/remove process exit from cli Remove explicit process.exit(0) from cli.ts (take 2) Nov 26, 2023
@rossipedia rossipedia changed the base branch from main to dev November 26, 2023 00:53
@rossipedia
Copy link
Contributor Author

There's definitely something off here, I tried this patch manually and it caused remix build to completely hang in our CI pipelines.

Feels like there's some kind of orphaned ref that's preventing node from exiting naturally 🤔

@rossipedia rossipedia closed this Feb 5, 2025
@rossipedia rossipedia deleted the fix/remove-process-exit-from-cli branch February 5, 2025 03:45
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.

2 participants