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(core): add api for v2 of project project graph plugins #18032

Merged
merged 18 commits into from
Aug 10, 2023

Conversation

AgentEnder
Copy link
Member

@AgentEnder AgentEnder commented Jul 7, 2023

Current Behavior

There are 2 methods to interact with the project graph:

  • Project Inference
    • registerProjectTargets
    • projectFilePatterns
  • Project graph plugins
    • processProjectGraph

These 2 methods create some pain for us as we try to work on the internal code base, and there are some issues with the current inference APIs.

  • Current API requires 1:1 association between project files and projects. This doesn't work for cases like:
    • angular.json
    • someCsProject.sln
    • gradle files
  • processProjectGraph allows adding nodes to the graph, which seems like it would work as a project inference method outside of the official inference APIs. Adding nodes like this results in the project file map not being correctly calculated though, since they are added after we calculate the map.
  • We can't really optimize things for knowing when dependencies and nodes are added, since they are potentially done in parallel rather than ensuring all nodes are present first.

Expected Behavior

Plugins have 2 APIs for updating the graph:

  • createNodes
  • createDependencies

This allows us to proceed while knowing that the graph is updated with all nodes, and afterwards we can create dependencies between them.

CreateNodes

Provides a glob pattern and factory to construct nodes from files that match that pattern. As an example, consider the implementation of the project.json node creator:

const PROJECT_JSON_PLUGIN = {
    name: 'nx-core-build-project-json-nodes',
    createNodes: [
      '{project.json,**/project.json}',
      (file) => {
        const json = readJsonFile<ProjectConfiguration>(join(root, file));
        const project = buildProjectFromProjectJson(json, file);
        return {
          projects: {
            [project.name]: project,
          },
        };
      },
    ],
  };

The createNodes property is a tuple. The first element is the glob pattern, which says that we want all project.json files, including the one at the root if it is present. The second element is a function that takes the path to an individual file that was identified as matching the provided glob pattern, and returns all projects and external dependencies which are read from that file.

In the case of project.json this is a single project. A similar plugin to support workspace.json would look like this:

WARNING
This plugin isn't included, since we don't support workspace.json any more. It is strictly an example.

const WORKSPACE_JSON_PLUGIN = {
    name: 'nx-core-build-workspace-json-nodes',
    createNodes: [
      'workspace.json',
      (file) => {
        const json = readJsonFile(file);
        return {
          projects: json.projects
        };
      },
    ],
  };

CreateDependencies

createDependencies returns an array of dependencies which should be added to the graph. It receives as arguments a context argument similar to the context currently provided to processProjectGraph

Known Limitations

The new APIs don't include methods to remove nodes or dependencies. We can't really find a valid use case for doing so, and it would result in complicating the return types of each of the methods.

Related Issue(s)

Fixes #

@vercel
Copy link

vercel bot commented Jul 7, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 10, 2023 5:27pm

@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch 2 times, most recently from f2c8de0 to a71a12c Compare July 24, 2023 17:34
@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
scope: core core nx functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants