Skip to content

[BUG] import projects generator incorrectly checks for new projects  #685

@Take-Limeship

Description

@Take-Limeship

Current Behavior

Currently, the import-projects generator checks if it is a new project by using the method

if ( !existingProjectJsonDirectories.some((x) => projectFile.startsWith(x)) )

However, because the filtering uses startsWith(), new projects with a path beginning the same way as an already imported project but differentiating at the end of the name are also counted as an existing project.

eg.:
We have a csproj already containing a project.json in the directory 'apps/example-app'
We create a new C# project in the directory 'apps/example-app-tests'

According to this logic:
https://github.com/nx-dotnet/nx-dotnet/blob/master/packages/core/src/generators/import-projects/generator.ts#L47
running the import-projects generator, 'apps/example-app-tests' would match the existingProjectJsonDirectories array as it starts the same as 'apps/example-app'

The same logic is also used to check if a library is new.

Expected Behavior

It is expected that the generator checks for the exact path to determine if it is an already existing project or not.
Our current solution is adding a forward slash to the end of the startsWith parameter:

if (!existingProjectJsonDirectories.some((x) => projectFile.startsWith(x + '/')))

Github Repo

No response

Steps to Reproduce

  1. import a new project in the directory 'apps/example-app'
  2. run the import-projects generator so that 'app/example-app' contains a project.json
  3. import another project in the directory 'apps/example-app-test'
  4. run the import-projects generator
  5. the 'apps/example-app-test' project has no project.json

Nx Report

Node : 16.0.0
   OS   : win32 x64
   npm  : 9.6.0

   nx                      : 15.9.2
   @nrwl/js                : 15.9.2
   @nrwl/nx-plugin         : 15.9.2
   @nrwl/tao               : 15.9.2
   @nrwl/webpack           : 15.9.2
   typescript              : 4.9.5
   ---------------------------------------
   Community plugins:
   @nx-dotnet/core        : 1.22.0
   @nx-tools/nx-container : 4.0.3
   ---------------------------------------
   Local workspace plugins:
         @asw/tools

nx.json

{
  "$schema": "./node_modules/nx/schemas/nx-schema.json",
  "npmScope": "asw",
  "affected": {
    "defaultBase": "main"
  },
  "tasksRunnerOptions": {
    "default": {
      "runner": "nx/tasks-runners/default",
      "options": {
        "cacheableOperations": [
          "build",
          "lint",
          "test",
          "e2e"
        ]
      }
    }
  },
  "targetDefaults": {
    "build": {
      "dependsOn": [
        "^build",
        "generate-dotnet-sources",
        "generate-angular-sources"
      ],
      "inputs": [
        "production",
        "^production"
      ],
      "outputs": [
        "{workspaceRoot}/dist/{projectRoot}"
      ]
    },
    "test": {
      "inputs": [
        "default",
        "^production",
        "{workspaceRoot}/jest.preset.js"
      ]
    },
    "unit-test": {
      "dependsOn": [
        "build"
      ]
    },
    "docker": {
      "dependsOn": [
        "build"
      ]
    },
    "pack": {
      "dependsOn": [
        "build"
      ]
    },
    "run": {
      "dependsOn": [
        "build"
      ]
    },
    "e2e": {
      "inputs": [
        "default",
        "^production"
      ]
    },
    "lint": {
      "inputs": [
        "default",
        "{workspaceRoot}/.eslintrc.json"
      ]
    },
    "generate-dotnet-sources": {
      "executor": "@asw/tools:dotnet-api-to-dotnet-sources",
      "outputs": [
        "{projectRoot}/**/*"
      ],
      "dependsOn": [
        "^build"
      ]
    },
    "generate-angular-sources": {
      "executor": "@asw/tools:dotnet-api-to-angular-sources",
      "outputs": [
        "{projectRoot}/src/lib/*"
      ],
      "dependsOn": [
        "^build"
      ]
    }
  },
  "namedInputs": {
    "default": [
      "{projectRoot}/**/*",
      "sharedGlobals"
    ],
    "production": [
      "default",
      "!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)",
      "!{projectRoot}/tsconfig.spec.json",
      "!{projectRoot}/jest.config.[jt]s",
      "!{projectRoot}/.eslintrc.json"
    ],
    "sharedGlobals": []
  },
  "workspaceLayout": {
    "appsDir": "apps",
    "libsDir": "libs"
  },
  "cli": {
    "packageManager": "npm"
  },
  "generators": {
    "@nrwl/angular": {
      "application": {
        "linter": "eslint"
      },
      "library": {
        "linter": "eslint"
      },
      "storybook-configuration": {
        "linter": "eslint"
      }
    },
    "@nrwl/angular:application": {
      "style": "scss",
      "linter": "eslint",
      "unitTestRunner": "jest",
      "e2eTestRunner": "cypress"
    },
    "@nrwl/angular:library": {
      "linter": "eslint",
      "unitTestRunner": "jest"
    },
    "@nrwl/angular:component": {
      "style": "scss"
    }
  },
  "plugins": [
    "@nx-dotnet/core",
    "@asw/tools"
  ],
  "defaultProject": "cicd-runtime-ui"
}

Failure Logs

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions