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

Add resolvePeerDependenciesFrom option. #4

Merged
merged 2 commits into from
Oct 20, 2020

Conversation

rwjblue
Copy link
Owner

@rwjblue rwjblue commented Oct 20, 2020

Pass this option if you know the base directory (the dir containing the package.json) that should be used as the starting point of peer dependency resolution.

For example, given the following dependencies:

  • Package parent depends on child and sibling
  • Package child has a peer dependency on sibling package
  • Package child uses validate-peer-dependencies to confirm that sibling is provided

Most of the time in the Node ecosystem you can not actually know the path to parent (it could be hoisted / deduplicated to any number of possible locations), but in some (some what special) circumstances you can. For example, in the ember-cli addon ecosystem an addon is instantiated with access to the root path of the package that included it (parent in the example above).

The main benefit of specifying resolvePeerDependenciesFrom is that while locally developing child you might npm link/yarn link it into parent manually. In that case the default behavior (using the directory that contains child's package.json) is not correct! When linking (and not specifying resolvePeerDependenciesFrom) the invocation to validatePeerDependencies would always find the peer dependencies (even if the parent didn't have them installed) because the locally linked copy of child would have specified them in its devDependencies and therefore the peer dependency would be resolvable from child's on disk location.

Here is an example of what usage by an ember-cli addon would look like:

'use strict';

const validatePeerDependencies = require('validate-peer-dependencies');

module.exports = {
  // ...snip...
  init() {
    this._super.init.apply(this, arguments);

    validatePeerDependencies(__dirname, {
      resolvePeerDependenciesFrom: this.parent.root,
    });
  }
};

@rwjblue rwjblue added the enhancement New feature or request label Oct 20, 2020
@rwjblue
Copy link
Owner Author

rwjblue commented Oct 20, 2020

/cc @hjdivad

Pass this option if you **know** the base directory (the dir containing the
`package.json`) that should be used as the starting point of peer dependency
resolution.

For example, given the following dependencies:

* Package `parent` depends on `child` and `sibling`
* Package `child` has a peer dependency on `sibling` package
* Package `child` uses `validate-peer-dependencies` to confirm that `sibling` is
  provided

_Most_ of the time in the Node ecosystem you can not actually know the path to
`parent` (it could be hoisted / deduplicated to any number of possible
locations), but in some (some what special) circumstances you can. For example,
in the `ember-cli` addon ecosystem an addon is instantiated with access to the
root path of the package that included it (`parent` in the example above).

The main benefit of specifying `resolvePeerDependenciesFrom` is that while
locally developing `child` you might `npm link`/`yarn link` it into `parent`
manually. In that case the default behavior (using the directory that contains
`child`'s `package.json`) is not correct! When linking (and not specifying
`resolvePeerDependenciesFrom`) the invocation to `validatePeerDependencies`
would **always** find the peer dependencies (even if the `parent` didn't have
them installed) because the locally linked copy of `child` would have specified
them in its `devDependencies` and therefore the peer dependency would be
resolvable from `child`'s on disk location.

Here is an example of what usage by an ember-cli addon would look like:

```js
'use strict';

const validatePeerDependencies = require('validate-peer-dependencies');

module.exports = {
  // ...snip...
  init() {
    this._super.init.apply(this, arguments);

    validatePeerDependencies(__dirname, {
      resolvePeerDependenciesFrom: this.parent.root,
    });
  }
};
```
@rwjblue rwjblue force-pushed the add-resolve-peer-dependencies-from branch from 4a630ce to e3c86c8 Compare October 20, 2020 16:28
@hjdivad
Copy link
Collaborator

hjdivad commented Oct 20, 2020

@rwjblue this looks great. As discussed OOB I would emphasize that failure to pass resolvePeerDependenciesFrom will in some cases result in incorrect behaviour (not just the linking case) but

  1. you will never have false negatives (i.e. we will not throw incorrectly)
  2. you can have false positives (i.e. we will sometimes fail to throw when we should)

Really just to highlight that if you know the parent dir you should absolutely pass it to catch all error cases, and if you don't we will still catch most error cases, but there are some known ones we cannot detect.

@rwjblue rwjblue merged commit ee24274 into main Oct 20, 2020
@rwjblue rwjblue deleted the add-resolve-peer-dependencies-from branch October 20, 2020 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants