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

feat(config-nx-scopes): support inferred targets #4113

Open
4 tasks
RobbyRabbitman opened this issue Aug 6, 2024 · 4 comments
Open
4 tasks

feat(config-nx-scopes): support inferred targets #4113

RobbyRabbitman opened this issue Aug 6, 2024 · 4 comments

Comments

@RobbyRabbitman
Copy link

Expected Behavior

Respect inferred tasks: https://nx.dev/concepts/inferred-tasks

Current Behavior

In a nx repo, I have a project which has inferred tasks only: There is no targets object in the json, the project itself has targets inferred by nx plugins.

{
  "name": "tools-eslint",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "tags": ["scope:shared", "type:tool"],
}

commitlint output:

⧗   input: docs(tools-eslint): explain eslint set up
✖   scope must be one of [libs-e2e-version-matrix, web-dev-server, web-test-runner, tools-commitlint, tools-local-registry, tools-renovate-bot, tools-tsc, tools-vite] [scope-enum]

✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

If there is an empty targets object, it works as expected:

{
  "name": "tools-eslint",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "tags": ["scope:shared", "type:tool"],
  "targets": {}
}
⧗   input: docs(tools-eslint): explain eslint set up
✔   found 0 problems, 0 warnings

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

Nx has a public api for getting the projects of a workspace in @nx/devkit, maybe readCachedProjectGraph() is a good fit. This could even improve performance, because right now a filesystem is created and then projects are parsed:

const projects = getNXProjects(new FsTree(cwd, false));

Idk exactly how FsTree works but if it just represents the file system, then the current behavior makes sense because the inferred targets are not written to the file system at any time: Therefore the filter() predicate checks not the presence of the actual targets of a project => inferred targets are missing.

.filter((project) => project.targets)

idk if these imports are public api: If they are not, using @nx/devkit would be a good refactor anyways.

import {getProjects as getNXProjects} from 'nx/src/generators/utils/project-configuration.js';
import {FsTree} from 'nx/src/generators/tree.js';

Context

No response

@escapedcat
Copy link
Member

Thanks! Maybe @fguitton has some feedback to this

@RobbyRabbitman
Copy link
Author

In general, I wonder whats the purpose of filtering projects without targets - why are you not allowed to use a scope e.g docs(my-project): foo where my-project is a nx project without any tasks at all, independent of what i have written above.

@fguitton
Copy link
Contributor

fguitton commented Aug 7, 2024

In general, I wonder whats the purpose of filtering projects without targets - why are you not allowed to use a scope e.g docs(my-project): foo where my-project is a nx project without any tasks at all, independent of what i have written above.

Hello @RobbyRabbitman, the answer to your question is rather simple, the implementation of @commitlint/config-nx-scopes has been relying on the @nrwl/tao (then @nx/devkit) API as of v13. Much work has been done in Nx since then, as you correctly pointed out, to enable everything from no-boilerplate configuration to extensive filtering. We simply never migrated away from the old filesystem-based API because they still work and the need didn't rise.

Perhaps it is time for an overhaul to help bring more flexible configurations inspired by the current Nx v19 filtering capabilities. I can probably have a look in the coming weeks and see if I can push something for the next version bump.

@RobbyRabbitman
Copy link
Author

If you don't want to have empty targets in your project.json files, you can copy pasta this config and adjust it to your needs until an official release.

import { RuleConfigSeverity, type UserConfig } from '@commitlint/types';
import { readCachedProjectGraph } from '@nx/devkit';

const getNxProjects = () => Object.keys(readCachedProjectGraph().nodes);

export default {
  // extends: ['@commitlint/config-conventional', '@commitlint/config-nx-scopes'],
  extends: ['@commitlint/config-conventional'],
  rules: {
    'scope-enum': () => [RuleConfigSeverity.Error, 'always', getNxProjects()],
  },
} satisfies UserConfig;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants