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 from ceef985 to d3a0f7a Compare July 7, 2023 22:33
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from d3a0f7a to d883850 Compare July 14, 2023 22:41
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from d883850 to b2cabe8 Compare July 17, 2023 19:30
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from b2cabe8 to 7f09b04 Compare July 17, 2023 20:57
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from 7f09b04 to 77c430e Compare July 20, 2023 16:56
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from 77c430e to ba93987 Compare July 20, 2023 22:06
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from ba93987 to 2cec50a Compare July 21, 2023 16:39
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from 2cec50a to ec632e3 Compare July 21, 2023 17:09
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from ec632e3 to 43f4f47 Compare July 21, 2023 19:19
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from 43f4f47 to 56fe9ee Compare July 21, 2023 20:03
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from cd0d320 to cda23af Compare July 24, 2023 16:35
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch 2 times, most recently from f2c8de0 to a71a12c Compare July 24, 2023 17:34
@AgentEnder AgentEnder force-pushed the feat/plugin-api-v2 branch from a71a12c to 0945b64 Compare July 25, 2023 14:40
@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.

6 participants