nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
22.62k stars 2.26k forks source link

enforce-module-boundaries plugin can't detect error in packages #26646

Open MrErHu opened 1 week ago

MrErHu commented 1 week ago

Current Behavior

In my project, I use the enforce-module-boundaries plugin to standardize the code。 The node version is 18.20.3, npm version is 10.7.0。 Other third-party package versions are as follows:

{
    "@nx/eslint-plugin": "19.1.2",
    "@typescript-eslint/parser": "7.13.1",
    "eslint": "8.56.0",
    "nx": "19.1.2",
    "typescript": "5.1.6"
 }

project directory structure is as follows:

nx-plugin-ts-demo
├── packages
│   ├── a
│   │   ├── src
│   │   │   └── index.ts
│   │   ├── package.json
│   │   └── tsconfig.json
│   └── b
│       ├── src
│       │   └── index.ts
│       ├── package.json
│       └── tsconfig.json
├── .eslintrc.js
├── nx.json
├── package.json
└── tsconfig.json

package.json(package/a)

{
  "name": "@packages/a",
  "version": "1.0.0",
  "main": "./src/index.ts",
  "scripts": {
    "lint": "eslint \"src/**/*.ts\" --fix --quiet"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "nx": {
    "tags": ["packages:a"]
  }
}

package.json(package/b)

{
  "name": "@packages/b",
  "version": "1.0.0",
  "main": "./src/index.ts",
  "scripts": {
    "lint": "eslint \"src/**/*.ts\" --fix --quiet"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "nx": {
    "tags": ["packages:b"]
  }
}

.eslintrc.js

module.exports = {
    parser: '@typescript-eslint/parser',
    plugins: ['@nx'],
    rules: {
        "@nx/enforce-module-boundaries": [
            "error",
            {
                "allow": [],
                "depConstraints": [
                    {
                        "sourceTag": "packages:a",
                        "onlyDependOnLibsWithTags": ["packages:a"]
                    },
                    {
                        "sourceTag": "packages:b",
                        "onlyDependOnLibsWithTags": ["packages:b"]
                    }
                ]
            }
        ]
    }
};

project root package.json

{
  "name": "nx-plugin-ts-demo",
  "version": "1.0.0",
  "main": "index.js",
  "workspaces": [
    "packages/*"
  ],
  "scripts": {
    "lint": "npx nx run-many -t lint",
    "lint:global": "eslint \"packages/**/*.ts\" --fix --quiet"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "@nx/eslint-plugin": "19.1.2",
    "@typescript-eslint/parser": "7.13.1",
    "eslint": "8.56.0",
    "nx": "19.1.2",
    "typescript": "5.1.6"
  },
  "engine": {
    "node": ">=18.20.3 <19.0",
    "npm": ">=10.7.0"
  },
  "volta": {
    "node": "18.20.3",
    "npm": "10.7.0"
  }
}

packages/b/src/index.ts

import { add } from '@packages/a'

console.log('add:', add(1, 2))

Under normal circumstances, running command npm run lint and npm run lint:global should have the same effect.

However, running npm run lint:global can detect errors: image

running npm run lint can‘t detect errors:

image

This is my project url: https://github.com/MrErHu/nx-plugin-ts-demo

Expected Behavior

The plugin can detect errors normally

GitHub Repo

nx-plugin-ts-demo

Steps to Reproduce

  1. git clone repo url : https://github.com/MrErHu/nx-plugin-ts-demo
  2. npm run lint
  3. npm run lint:global

Nx Report

Noting

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

No response

MrErHu commented 1 week ago

The problem may occur in

      targetProject =
        targetProject ||
        findProjectUsingImport(
          projectGraph,
          targetProjectLocator,
          sourceFilePath,
          imp
        );

targetProject is undefined。