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

fix(core): support subpath exports when constructing the project graph #29577

Merged
merged 2 commits into from
Jan 10, 2025

Conversation

robbtraister
Copy link
Contributor

Sibling dependencies that rely exclusively on subpath exports are excluded from the dependency graph because there is no exact match.

This adds a fallback to look for subpath exports if the exact match is not found.

This also adds logic to respect conditional exports independent from subpath exports.

Current Behavior

Importing a workspace dependency via subpath export fails to match the package name, and so is not included in the dependency graph.

Example

apps/api/package.json

  "name": "@my-org/api",
  "dependencies": {
    "@my-org/services": "workspace:*"
  }

libs/services/package.json

  "name": "@my-org/services",
  "exports": {
    "./email": "./dist/email.js"
  }

The @my-org/api app should be able to import the email service with import { EmailService } from "@my-org/services/email".

However, the getPackageEntryPointsToProjectMap implementation results in an object with a key of @my-org/services/email, but not @my-org/services. This is not specifically a problem, except that findDependencyInWorkspaceProjects only considers exact matches within those object keys.

Expected Behavior

Importing a workspace dependency via subpath export should be included in the dependency graph.

I also addressed a related issue where the following resulted in keys of @my-org/services/default and @my-org/services/types, which is incorrect according to the subpath/conditional export rules.

  "exports": {
    "default": "./dist/index.js",
    "types": "./dist/index.d.ts"
  }

Related Issue(s)

Fixes #29486

@robbtraister robbtraister requested a review from a team as a code owner January 9, 2025 22:06
Copy link

vercel bot commented Jan 9, 2025

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

1 Skipped Deployment
Name Status Preview Updated (UTC)
nx-dev ⬜️ Ignored (Inspect) Visit Preview Jan 10, 2025 1:33pm

@leosvelperez leosvelperez self-assigned this Jan 10, 2025
robbtraister and others added 2 commits January 10, 2025 14:30
Sibling dependencies that rely exclusively on subpath exports are
excluded from the dependency graph because there is no exact match.

This adds a fallback to look for subpath exports if the exact match is
not found.

This also adds logic to respect conditional exports independent from
subpath exports.
@leosvelperez leosvelperez force-pushed the fix/support-subpath-exports-in-graph branch from ced1a12 to 872b514 Compare January 10, 2025 13:30
Copy link

nx-cloud bot commented Jan 10, 2025

View your CI Pipeline Execution ↗ for commit 872b514.

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 34m 58s View ↗
nx run-many -t check-imports check-commit check... ✅ Succeeded 57s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 1s View ↗
nx-cloud record -- nx format:check --base=dd9b0... ✅ Succeeded 23s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 22s View ↗
nx documentation --no-dte ✅ Succeeded 1m View ↗

☁️ Nx Cloud last updated this comment at 2025-01-10 14:22:23 UTC

Copy link
Member

@leosvelperez leosvelperez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

I updated it to include some extra changes to cover more scenarios and added test coverage to ensure we don't regress.

@jaysoo jaysoo merged commit d08ad75 into nrwl:master Jan 10, 2025
6 checks passed
jaysoo pushed a commit that referenced this pull request Jan 10, 2025
#29577)

Sibling dependencies that rely exclusively on subpath exports are
excluded from the dependency graph because there is no exact match.

This adds a fallback to look for subpath exports if the exact match is
not found.

This also adds logic to respect conditional exports independent from
subpath exports.

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior

Importing a workspace dependency via subpath export fails to match the
package name, and so is not included in the dependency graph.

### Example

```apps/api/package.json```
```json
  "name": "@my-org/api",
  "dependencies": {
    "@my-org/services": "workspace:*"
  }
```

```libs/services/package.json```
```json
  "name": "@my-org/services",
  "exports": {
    "./email": "./dist/email.js"
  }
```

The `@my-org/api` app should be able to import the email service with
`import { EmailService } from "@my-org/services/email"`.

However, the `getPackageEntryPointsToProjectMap` implementation results
in an object with a key of `@my-org/services/email`, but not
`@my-org/services`. This is not specifically a problem, except that
`findDependencyInWorkspaceProjects` only considers exact matches within
those object keys.

## Expected Behavior

Importing a workspace dependency via subpath export should be included
in the dependency graph.

I also addressed a related issue where the following resulted in keys of
`@my-org/services/default` and `@my-org/services/types`, which is
incorrect according to the subpath/conditional export rules.
```json
  "exports": {
    "default": "./dist/index.js",
    "types": "./dist/index.d.ts"
  }
```

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #29486

---------

Co-authored-by: Leosvel Pérez Espinosa <[email protected]>
FrozenPandaz pushed a commit that referenced this pull request Jan 15, 2025
#29577)

Sibling dependencies that rely exclusively on subpath exports are
excluded from the dependency graph because there is no exact match.

This adds a fallback to look for subpath exports if the exact match is
not found.

This also adds logic to respect conditional exports independent from
subpath exports.

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior

Importing a workspace dependency via subpath export fails to match the
package name, and so is not included in the dependency graph.

### Example

```apps/api/package.json```
```json
  "name": "@my-org/api",
  "dependencies": {
    "@my-org/services": "workspace:*"
  }
```

```libs/services/package.json```
```json
  "name": "@my-org/services",
  "exports": {
    "./email": "./dist/email.js"
  }
```

The `@my-org/api` app should be able to import the email service with
`import { EmailService } from "@my-org/services/email"`.

However, the `getPackageEntryPointsToProjectMap` implementation results
in an object with a key of `@my-org/services/email`, but not
`@my-org/services`. This is not specifically a problem, except that
`findDependencyInWorkspaceProjects` only considers exact matches within
those object keys.

## Expected Behavior

Importing a workspace dependency via subpath export should be included
in the dependency graph.

I also addressed a related issue where the following resulted in keys of
`@my-org/services/default` and `@my-org/services/types`, which is
incorrect according to the subpath/conditional export rules.
```json
  "exports": {
    "default": "./dist/index.js",
    "types": "./dist/index.d.ts"
  }
```

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #29486

---------

Co-authored-by: Leosvel Pérez Espinosa <[email protected]>
(cherry picked from commit d08ad75)
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 Jan 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NX Packages without default export are excluded from dependencies
3 participants