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

The node:execute builder immediately exits after build when --watch=false. #7031

Closed
llwt opened this issue Sep 16, 2021 · 10 comments
Closed
Assignees
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug

Comments

@llwt
Copy link
Contributor

llwt commented Sep 16, 2021

Current Behavior

The node:execute builder immediately exits after build when --watch=false.

Expected Behavior

The node:execute builder wait for the subprocess to exit, or SIGTERM to be received, before exiting. This is how things worked previously.

Steps to Reproduce

npx create-nx-workspace --preset=express
# set app-name to node-test

npm start # works as expected, stays around until you kill it

npm start -- --watch=false # immediately exits after app is started

Failure Logs

Screen Shot 2021-09-16 at 11 24 07

Environment

NX Report complete - copy this into the issue template

Node : 16.6.2
OS : darwin x64
pnpm : 6.13.0

nx : Not Found
@nrwl/angular : Not Found
@nrwl/cli : 12.9.0
@nrwl/cypress : Not Found
@nrwl/devkit : Not Found
@nrwl/eslint-plugin-nx : 12.9.0
@nrwl/express : 12.9.0
@nrwl/jest : 12.9.0
@nrwl/linter : 12.9.0
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : 12.9.0
@nrwl/nx-cloud : Not Found
@nrwl/react : Not Found
@nrwl/schematics : Not Found
@nrwl/tao : 12.9.0
@nrwl/web : Not Found
@nrwl/workspace : 12.9.0
@nrwl/storybook : Not Found
@nrwl/gatsby : Not Found
typescript : 4.3.5

@llwt llwt added the type: bug label Sep 16, 2021
@llwt
Copy link
Contributor Author

llwt commented Sep 16, 2021

for await (const event of startBuild(options, context)) {
if (!event.success) {
logger.error('There was an error with the build. See above.');
logger.info(`${event.outfile} was not restarted.`);
}
await handleBuildEvent(event, options);
yield event;
}

Something like this after these lines seems like it would probably do the trick:

if (!options.watch) {
  await waitForSubProcessToExit(event);
}

Or maybe better, modifying handleBuildEvent to wait in cases where options.watch === false 🤔

async function runProcess(
  event: NodeBuildEvent,
  options: NodeExecuteBuilderOptions
) {
  if (subProcess || !event.success) {
    return;
  }

  subProcess = fork(event.outfile, options.args, {
    execArgv: getExecArgv(options),
  });

  if (!options.watch) {
    await new Promise((resolve, reject) => {
      subProcess.once('exit', resolve);
      subProcess.once('error', reject);
    });
  }
}

One further thing that's tripping me up a bit here is we'd like to also add support for using process.send to send the server URL up to the builder and trigger an iteration/emission (specifically for getting the started port in our e2e tests when we start a server on port: 0). I don't see an obvious way to do it without merging async iterables but maybe y'all see something obvious?

@FrozenPandaz FrozenPandaz added the scope: node Issues related to Node, Express, NestJS support for Nx label Sep 24, 2021
@AgentEnder AgentEnder self-assigned this Dec 14, 2021
@AgentEnder
Copy link
Member

@dpchamps @llwt Hey! Sorry for letting this linger so long, lets discuss this a bit and see what we think would be the best path forward.

For some additional context, we are in the middle of moving a ton of logic out of @nrwl/node and @nrwl/workspace and into @nrwl/js. Part of this move will involve the executors from @nrwl/node being moved into @nrwl/js, and the build logic being rethought to introduce potential support for other tooling like SWC.

I agree with @FrozenPandaz's thoughts in the PR, that the solution there seems overly complex for the description of the issue. The second code suggestion provided by @llwt seems much more lightweight, though it doesn't emit all subprocess events I'd argue that the executor doesn't need to do so.... If you want that much control its probably best to write a minimal custom executor. I do agree that the current executor shouldn't exit until the subprocess concludes, and that's a much simpler problem to tackle.

Thoughts? I'm about to be on leave for the next 6 weeks, so I'm going to pull @nartc in on this discussion too, more perspectives shouldn't hurt 😄.

@hubertgrzeskowiak
Copy link

Hey, is there a known workaround for this?

@jlndk
Copy link

jlndk commented Feb 23, 2022

I took the liberty of making a PR based on the fix in #7031 (comment), with some additional error handling.

@nartc nartc closed this as completed May 16, 2022
@dpchamps
Copy link

Hi 👋

Is there a change log / commit to reference where this was fixed? Or was it closed as a wont do?

@nartc
Copy link
Contributor

nartc commented May 17, 2022

Hi @dpchamps, sorry for closing without any information.

Here's the PR that fixed the issue #9180
Here's my testing:

  • npx create-nx-workspace happyorg --preset=express
  • npx nx serve
  • npx nx serve --watch=false

--watch=false won't exit immediately anymore

@dpchamps
Copy link

@nartc Thanks so much!

I've asked elsewhere, but it seems that @llwt 's follow up request in this issue has largely been overlooked:

... we'd like to also add support for using process.send to send the server URL up to the builder and trigger an iteration/emission

What is the best way to move forward with that request? Are there no plans to support it? It won't be possible without introducing additional complexity, but it is a desired feature that we've essentially hacked in to our build tools. It'd be great to have some support.

@nartc
Copy link
Contributor

nartc commented May 17, 2022

@dpchamps I think it's complex enough to warrant a separate issue with a clear use-case and explanation. I personally don't truly understand the use-case just yet so it's hard for mecto respond responsibly (I'll follow up with Jack internally to see if he has any idea). That said, please feel free to open a new issue (and feel free to reference as many issues/PRs as you can in the new issue) and we'll track that as a feature request. Sounds good?

@dpchamps
Copy link

Sounds great, thanks :D

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug
Projects
None yet
8 participants