Skip to content

Add beforeWorkspaceDependency addition/replacement/removal hooks#6994

Open
suhdonghwi wants to merge 8 commits intoyarnpkg:masterfrom
suhdonghwi:feat/before-workspace-dependency-change-2
Open

Add beforeWorkspaceDependency addition/replacement/removal hooks#6994
suhdonghwi wants to merge 8 commits intoyarnpkg:masterfrom
suhdonghwi:feat/before-workspace-dependency-change-2

Conversation

@suhdonghwi
Copy link
Copy Markdown

What's the problem this PR addresses?

Currently, @yarnpkg/plugin-essentials only provides "after" hooks (afterWorkspaceDependencyAddition, afterWorkspaceDependencyReplacement, afterWorkspaceDependencyRemoval) that fire after dependencies are added, updated, or removed via CLI commands.

There is currently no way to intercept or control the behavior before package version suggestions or installations take place. Also "after" hooks do not provide enough information to determine whether the user explicitly specified a version range in CLI commands.

How did you fix it?

Added three new hooks to the Hooks interface in @yarnpkg/plugin-essentials that mirror the existing "after" hooks:

beforeWorkspaceDependencyAddition?: (
  workspace: Workspace,
  target: suggestUtils.Target,
  descriptor: Descriptor,
) => Promise<void>;

beforeWorkspaceDependencyReplacement?: (
  workspace: Workspace,
  target: suggestUtils.Target,
  fromDescriptor: Descriptor,
  toDescriptor: Descriptor,
) => Promise<void>;

beforeWorkspaceDependencyRemoval?: (
  workspace: Workspace,
  target: suggestUtils.Target,
  descriptor: Descriptor,
) => Promise<void>;

Checklist

  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

Copy link
Copy Markdown
Member

@clemyan clemyan left a comment

Choose a reason for hiding this comment

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

I'd prefer if beforeWorkspaceDependencyAddition/Replacement uses reduceHook instead of mutation, but unfortunately that is not possible if we are to match their signature with the corresponding after hooks. Oh well...

declined:
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/cli"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@yarnpkg/cli also needs a release

Comment on lines +200 to +223
for (const plugin of configuration.plugins.values()) {
const hooks = plugin.hooks as Hooks;

if (isReplacement) {
if (!hooks?.beforeWorkspaceDependencyReplacement)
continue;

await hooks.beforeWorkspaceDependencyReplacement(
workspace,
target,
current,
request,
);
} else {
if (!hooks?.beforeWorkspaceDependencyAddition)
continue;

await hooks.beforeWorkspaceDependencyAddition(
workspace,
target,
request,
);
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not use configuration.triggerMultipleHooks here?

@clemyan clemyan requested a review from arcanis March 16, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants