nx/docs/shared/packages/conformance/conformance-plugin.md
Jack Hsu a31226437e
docs(misc): update conformance rule options (#31183)
This PR updates our conformance rule that enforces module boundaries.
See here for more details: https://github.com/nrwl/ocean/pull/7377
2025-05-13 09:59:27 -06:00

8.0 KiB

title description
Overview of the Nx powerpack-conformance Plugin The Nx Powerpack Conformance plugin provides the ability to write and apply rules for your workspace

The @nx/conformance plugin allows Nx Powerpack users to write and apply rules for your entire workspace that help with consistency, maintainability, reliability and security.

The conformance plugin allows you to encode your own organization's standards so that they can be enforced automatically. Conformance rules can also complement linting tools by enforcing that those tools are configured in the recommended way. The rules are written in TypeScript but can be applied to any language in the codebase or focus entirely on configuration files.

The plugin also provides the following pre-written rules:

{% callout title="This plugin requires an active Nx Powerpack license" %} In order to use @nx/conformance, you need to have an active Powerpack license. If you don't have a license or it has expired, the nx conformance command will fail. {% /callout %}

Set Up @nx/conformance

  1. Activate Powerpack if you haven't already

  2. Install the package

    nx add @nx/conformance
    
  3. Configure Conformance Rules

    Configure the @nx/conformance plugin in the nx.json file or in individual project configuration files. Consult the Conformance Configuration Reference section for more details.

  4. Run the nx conformance command in CI

Add nx conformance to the beginning of the CI process.

{% tabs %} {% tab label="Without Nx Cloud" %}

- name: Enforce all conformance rules
  run: npx nx conformance

{% /tab %} {% tab label="Using Nx Cloud" %}

- name: Enforce all conformance rules
  run: npx nx-cloud record -- npx nx conformance

Use npx nx-cloud record -- to capture the logs for nx conformance in the Nx Cloud dashboard.

{% /tab %} {% /tabs %}

Conformance Configuration Reference

{
  "conformance": {
    "rules": [
      {
        /**
         * Relative path to a local rule implementation or node_module path.
         */
        "rule": "@nx/conformance/enforce-project-boundaries",
        /**
         * Rule specific configuration options. (Optional)
         */
        "options": {},
        /**
         * The projects array allows users to opt in or out of violations for specific projects being reported by the current rule.
         * The array can contain any valid matchers for findMatchingProjects(), by default the implied value is ["*"]. (Optional)
         */
        "projects": ["*"]
      }
    ]
  }
}

Provided Conformance Rules

The following rules are provided by Nx along with the @nx/conformance plugin.

Enforce Project Boundaries

This rule is similar to the Nx ESLint Enforce Module Boundaries rule, but enforces the boundaries on every project dependency, not just those created from TypeScript imports or package.json dependencies.

Set the rule property to: @nx/conformance/enforce-project-boundaries

{
  "conformance": {
    "rules": [
      {
        "rule": "@nx/conformance/enforce-project-boundaries",
        "options": {
          // Optional
          // Can be a boolean or an object with an array of buildTargetNames
          "requireBuildableDependenciesForBuildableProjects": {
            // Defaults to ["build"]
            "buildTargetNames": ["build", "compile"]
          },
          // Optional
          "ignoredCircularDependencies": [["projectA", "projectB"]],
          // Optional
          "depConstraints": [
            {
              // Must define either `sourceTag` or `allSourceTags`
              "sourceTag": "string",
              "allSourceTags": ["string"],
              // Optional
              "onlyDependOnProjectsWithTags": [],
              // Optional
              "notDependOnProjectsWithTags": []
            }
          ]
        }
      }
    ]
  }
}

Options

Property Type Default Description
ignoredCircularDependencies Array<[string, string]> [] List of project pairs that should be skipped from Circular dependencies checks, including the self-circular dependency check. E.g. ['feature-project-a', 'myapp']. Project name can be replaced by catch all * for more generic matches.
requireBuildableDependenciesForBuildableProjects boolean false Enable to restrict the buildable projects from importing non-buildable libraries
depConstraints Array [] List of dependency constraints between projects

Dependency constraints

The depConstraints is an array of objects representing the constraints defined between source and target projects. A constraint must include sourceTag or allSourceTags. The constraints are applied with AND logical operation - for a given source project the resulting constraints would be all that match its tags.

Property Type Description
sourceTag string Tag that source project must contain to match the constraint
allSourceTags Array List of tags the source project must contain to match the constraint
onlyDependOnProjectsWithTags Array The source can depend only on projects that contain at least one of these tags
notDependOnProjectsWithTags Array The source can not depend on projects that contain at least one of these tags

Ensure Owners

This rule requires every project to have an owner defined for the @nx/owners plugin

Set the rule property to: @nx/conformance/ensure-owners

{
  "conformance": {
    "rules": [
      {
        "rule": "@nx/conformance/ensure-owners"
      }
    ]
  }
}

Next Steps

For more information about the conformance plugin, consult the following articles: