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

Nx jest resolver might resolve the same file as different modules #20865

Closed
1 of 4 tasks
yjaaidi opened this issue Dec 20, 2023 · 1 comment · Fixed by #23396
Closed
1 of 4 tasks

Nx jest resolver might resolve the same file as different modules #20865

yjaaidi opened this issue Dec 20, 2023 · 1 comment · Fixed by #23396
Assignees
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug

Comments

@yjaaidi
Copy link
Contributor

yjaaidi commented Dec 20, 2023

Current Behavior

Given the following conditions:

  • adding a tsconfig.spec.json at the root of the workspace
  • using tsconfig paths with deep import (e.g. "@demo/stuff/*": ["apps/demo/stuff/*"]) + a barrel export in index.ts

then importing the same symbol from the @demo/stuff and @demo/stuff/something will produce different symbols:

import { Something } from '@demo/stuff';
import { Something as DeepSomething } from '@demo/stuff/something';

Something === DeepSomething; // false

This can break many things (e.g. dependency injection, variables holding global variables / counters etc...)

It seems that the problem comes partially from Nx jest resolver:

compilerSetup = compilerSetup || getCompilerSetup(options.rootDir);

  1. when the files are resolved by jest (e.g. test-setup.ts), the rootDir is undefined
  2. if tsconfig.spec.json does exist in the workspace root, getCompilerSetup() works instead of crashing and resolves the configuration from the root config which resolves TS rootDir to . instead of the absolute path to the library.
  3. from there, all modules are resolved with a relative path starting from the workspace root instead of an absolute path and this seems to cause the issue here.

Suggested solutions

A. While I still don't understand why rootDir is undefined, throwing an error in getCompilerSetup() if rootDir is undefined is a better option than mistakenly using tsconfig.spec.json from the workspace root as it solves this issue.
B. We could improve the heuristics of finding the "right" tsconfig.

Expected Behavior

Modules should be resolved consistently no matter what the configuration is.

GitHub Repo

https://github.com/yjaaidi/experiments/tree/repro/nx-jest-resolver

Steps to Reproduce

  1. clone repro from https://github.com/yjaaidi/experiments/tree/repro/nx-jest-resolver
  2. nx test demo

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 21.4.0
   OS     : darwin-arm64
   pnpm   : 8.12.0
   
   nx                 : v17.2.5
   @nx/js             : v17.2.5
   @nx/jest           : v17.2.5
   @nx/linter         : v17.2.5
   @nx/eslint         : v17.2.5
   @nx/workspace      : v17.2.5
   @nx/angular        : v17.2.5
   @nx/cypress        : v17.2.5
   @nx/devkit         : v17.2.5
   @nx/eslint-plugin  : v17.2.5
   @nx/playwright     : v17.2.5
   @nrwl/tao          : v17.2.5
   @nx/web            : v17.2.5
   @nx/webpack        : v17.2.5
   typescript         : 5.2.2

Failure Logs

No response

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@leosvelperez leosvelperez added the scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx label Dec 21, 2023
leosvelperez added a commit that referenced this issue May 15, 2024
<!-- 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` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

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

Fixes #20865
FrozenPandaz pushed a commit that referenced this issue May 15, 2024
<!-- 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` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

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

Fixes #20865

(cherry picked from commit c434394)
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 Jun 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants