-
Notifications
You must be signed in to change notification settings - Fork 118
feat: plugin for dynamically overriding dependencies #4263
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # @rnx-kit/yarn-plugin-dynamic-overrides | ||
|
|
||
| [](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml) | ||
| [](https://www.npmjs.com/package/@rnx-kit/yarn-plugin-dynamic-overrides) | ||
|
|
||
| 🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 | ||
|
|
||
| ### THIS TOOL IS EXPERIMENTAL — USE WITH CAUTION | ||
|
|
||
| 🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 | ||
|
|
||
| This is a Yarn plugin that lets you override a package's dependencies to allow | ||
| greater version ranges than the package declares. | ||
|
|
||
| ## Motivation | ||
|
|
||
| A number of packages use explicit version numbers instead of version ranges, | ||
| forcing package managers to install multiple copies of a dependency even though | ||
| a more recent version is still compatible. | ||
|
|
||
| This plugin attempts to address to by replacing the explicit version with a | ||
| version range. This is useful for deduping and allows for updating dependencies | ||
| to a more recent version. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| yarn plugin import https://raw.githubusercontent.com/microsoft/rnx-kit/main/incubator/yarn-plugin-dynamic-overrides/index.js | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| List the packages that you want to override in `.yarnrc.yml`. For example, to | ||
| override dependencies of `@appium/base-driver`: | ||
|
|
||
| ```yml | ||
| dynamicPackageOverrides: | ||
| - id: "@appium/base-driver" | ||
| ``` | ||
|
|
||
| Next time you run `yarn install`, its dependencies will be overridden: | ||
|
|
||
| ``` | ||
| "@appium/support": "npm:7.2.5" | ||
| "@appium/types": "npm:1.5.1" | ||
| async-lock: "npm:1.4.1" -> "npm:^1.4.1" | ||
| asyncbox: "npm:6.3.0" -> "npm:^6.3.0" | ||
| axios: "npm:1.18.0" -> "npm:^1.18.0" | ||
| ``` | ||
|
|
||
| Note that `@appium` packages are untouched. The plugin will try to detect | ||
| inter-dependencies and leave them alone. | ||
|
|
||
| If you want to use a different specifier than `^`, you can configure it like | ||
| below: | ||
|
|
||
| ```yml | ||
| dynamicPackageOverrides: | ||
| - id: "@appium/base-driver" | ||
| specifier: "~" | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // @ts-check | ||
|
|
||
| /** | ||
| * @import { Descriptor, Hooks, Ident, Plugin } from "@yarnpkg/core"; | ||
| * | ||
| * @typedef {Ident & { specifier: string; }} ExtendedIdent | ||
| */ | ||
|
|
||
| const DEFAULT_SPECIFIER = "^"; | ||
| const DYNAMIC_OVERRIDES_KEY = "dynamicPackageOverrides"; | ||
|
|
||
| /** | ||
| * @param {Descriptor} dependency | ||
| * @param {Ident=} locator | ||
| * @returns {locator is Ident} | ||
| */ | ||
| function matches(dependency, locator) { | ||
| if (!locator) { | ||
| return false; | ||
| } | ||
|
|
||
| // appium -> @appium/base-plugin | ||
| if (locator.name === dependency.scope) { | ||
| return false; | ||
| } | ||
|
|
||
| // @appium/base-plugin -> @appium/base-driver | ||
| if (locator.scope && locator.scope === dependency.scope) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // This module *must* be CommonJS because `actions/setup-node` (and probably | ||
| // other GitHub actions) does not support ESM. Yarn itself does. | ||
| exports.name = "@rnx-kit/yarn-plugin-dynamic-overrides"; | ||
|
|
||
| /** @type {(require: NodeJS.Require) => Plugin<Hooks>} */ | ||
| exports.factory = (require) => { | ||
| const { SettingsType, structUtils } = require("@yarnpkg/core"); | ||
|
|
||
| /** | ||
| * @param {Map<string, string>} entry | ||
| * @returns {[string, ExtendedIdent]} | ||
| */ | ||
| function toKeyValuePair(entry) { | ||
| const id = entry.get("id"); | ||
| if (!id) { | ||
| throw new Error(`"${DYNAMIC_OVERRIDES_KEY}" expects an id`); | ||
| } | ||
| const ident = structUtils.parseIdent(id); | ||
| return [ | ||
| ident.identHash, | ||
| { | ||
| ...ident, | ||
| specifier: entry.get("specifier") ?? DEFAULT_SPECIFIER, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| /** @type {Plugin<Hooks>["configuration"] & Record<string, unknown>} */ | ||
| const configuration = {}; | ||
| configuration[DYNAMIC_OVERRIDES_KEY] = { | ||
| description: "Packages whose dependencies are overridden", | ||
| type: SettingsType.SHAPE, | ||
| isArray: true, | ||
| properties: { | ||
| id: { | ||
| type: SettingsType.STRING, | ||
| }, | ||
| specifier: { | ||
| type: SettingsType.STRING, | ||
| default: DEFAULT_SPECIFIER, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| /** @type {Map<string, ExtendedIdent>} */ | ||
| let overrides; | ||
|
|
||
| return { | ||
| configuration, | ||
| hooks: { | ||
| reduceDependency: async ( | ||
| dependency, | ||
| project, | ||
| locator, | ||
| _initialDependency, | ||
| _extra | ||
| ) => { | ||
| if (!overrides) { | ||
| const config = project.configuration.get(DYNAMIC_OVERRIDES_KEY); | ||
| if (!Array.isArray(config)) { | ||
| throw new Error( | ||
| `Expected "${DYNAMIC_OVERRIDES_KEY}" to be an array` | ||
| ); | ||
| } | ||
| overrides = new Map(config.map(toKeyValuePair)); | ||
| } | ||
|
|
||
| const ident = overrides.get(locator.identHash); | ||
| if (matches(dependency, ident)) { | ||
| const { protocol, selector } = structUtils.parseRange( | ||
| dependency.range | ||
| ); | ||
| if (protocol === "npm:" && /^\d/.test(selector)) { | ||
| return structUtils.makeDescriptor( | ||
| dependency, | ||
| `${protocol}${ident.specifier}${selector}` | ||
| ); | ||
| } | ||
| } | ||
| return dependency; | ||
| }, | ||
| }, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "name": "@rnx-kit/yarn-plugin-dynamic-overrides", | ||
| "version": "0.0.1", | ||
| "private": true, | ||
| "description": "EXPERIMENTAL - USE WITH CAUTION - yarn-plugin-dynamic-overrides", | ||
| "homepage": "https://github.com/microsoft/rnx-kit/tree/main/incubator/yarn-plugin-dynamic-overrides#readme", | ||
| "license": "MIT", | ||
| "author": { | ||
| "name": "Microsoft Open Source", | ||
| "email": "microsoftopensource@users.noreply.github.com" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/microsoft/rnx-kit", | ||
| "directory": "incubator/yarn-plugin-dynamic-overrides" | ||
| }, | ||
| "files": [ | ||
| "index.js" | ||
| ], | ||
| "main": "index.js", | ||
| "exports": { | ||
| ".": "./index.js" | ||
| }, | ||
| "scripts": { | ||
| "build": "rnx-kit-scripts build", | ||
| "format": "rnx-kit-scripts format", | ||
| "lint": "rnx-kit-scripts lint", | ||
| "test": "rnx-kit-scripts test" | ||
| }, | ||
| "devDependencies": { | ||
| "@rnx-kit/scripts": "*", | ||
| "@rnx-kit/tsconfig": "*", | ||
| "@yarnpkg/core": "^4.0.0" | ||
| }, | ||
| "engines": { | ||
| "node": ">=20.18", | ||
| "yarn": ">=4.0" | ||
| }, | ||
| "experimental": true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "extends": "@rnx-kit/tsconfig/tsconfig.nodenext.json", | ||
| "compilerOptions": { | ||
| "noEmit": true | ||
| }, | ||
| "include": ["index.js"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.