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

Unable to run load nextjs jest config outside of nx run #22450

Closed
karlismelderis-mckinsey opened this issue Mar 22, 2024 · 7 comments · Fixed by #27523
Closed

Unable to run load nextjs jest config outside of nx run #22450

karlismelderis-mckinsey opened this issue Mar 22, 2024 · 7 comments · Fixed by #27523
Assignees
Labels
community This is a good first issue for contributing outdated scope: nextjs Issues related to NextJS support for Nx type: bug

Comments

@karlismelderis-mckinsey

Current Behavior

seems that this PR broke simple script:

pnpx knip --config ./knip.js

with latest changes code fails here - https://github.com/nrwl/nx/blob/master/packages/next/plugins/with-nx.ts#L144
because none of these ENV variables are there

Also running jest tests outside of nx (e.g. IntelliJ) is not possible now

everything fails with same error:

  TypeError: Cannot read properties of undefined (reading 'data')
    at readConfigFileAndSetRootDir (/.../node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:116:13)

maybe with-nx.ts need to add 'phase-test' === phase here but I'm not familiar enough with NextJS plugin

Expected Behavior

I don't need to run every single script within nx run ...

GitHub Repo

No response

Steps to Reproduce

try to run knip of jest outside of nx

Nx Report

TypeError: Cannot read properties of undefined (reading 'data')
    at readConfigFileAndSetRootDir (/.../node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:116:13)


### Failure Logs

_No response_

### Package Manager Version

_No response_

### Operating System

- [X] macOS
- [ ] Linux
- [ ] Windows
- [ ] Other (Please specify)

### Additional Information

_No response_
@karlismelderis-mckinsey
Copy link
Author

@jaysoo would you be able to check this bug?

@FrozenPandaz FrozenPandaz added the scope: nextjs Issues related to NextJS support for Nx label Mar 27, 2024
@karlismelderis-mckinsey
Copy link
Author

@FrozenPandaz and @ndcunningham how soon will you be able to pick this up?

if you're overloaded I can try to investigate internals of nx and come up with a proposal PR

@ndcunningham
Copy link
Contributor

@karlismelderis-mckinsey feel free to pick this up if you can

@ndcunningham ndcunningham added the community This is a good first issue for contributing label May 16, 2024
@johndalvik
Copy link

@karlismelderis-mckinsey @ndcunningham

Any update on this?

I have to provide additional code to fix the behaviour, which is not not very clean and straightforward.

process.env['NX_TASK_TARGET_PROJECT'] = 'app';
process.env['NX_TASK_TARGET_TARGET'] = 'test';
process.env['NX_TASK_TARGET_CONFIGURATION'] = 'development';

@mircoservices
Copy link

Was able to fix the issue with the following patch

diff --git a/node_modules/@nx/next/plugins/with-nx.js b/node_modules/@nx/next/plugins/with-nx.js
index 5ba13ff..4e00bde 100644
--- a/node_modules/@nx/next/plugins/with-nx.js
+++ b/node_modules/@nx/next/plugins/with-nx.js
@@ -67,7 +67,7 @@ function withNx(_nextConfig = {}, context = getWithNxContext()) {
         // 2. During graph creation (i.e. create nodes), we won't have a graph to read, and it is not needed anyway since it's a build-time concern.
         //
         // NOTE: Avoid any `require(...)` or `import(...)` statements here. Development dependencies are not available at production runtime.
-        if (PHASE_PRODUCTION_SERVER === phase || global.NX_GRAPH_CREATION) {
+        if (PHASE_PRODUCTION_SERVER === phase || global.NX_GRAPH_CREATION || !process.env.NX_TASK_TARGET_TARGET) {
             const { nx, ...validNextConfig } = _nextConfig;
             return {
                 distDir: '.next',

@ndcunningham
Copy link
Contributor

I don't think I see an issue with including TASK_TARGET_TARGET env var as we did before.
We probably should've just added the graph creation instead of replacing it.

FrozenPandaz pushed a commit that referenced this issue Aug 21, 2024
fixes: #22450

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Currently, if you run any next target outside of nx the build will fail
because we only fallback for graph creation of if running prod server.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Targets should work if they are run outside of the Nx cli

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

(cherry picked from commit 31c5745)
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 Sep 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community This is a good first issue for contributing outdated scope: nextjs Issues related to NextJS support for Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants