yarn icon indicating copy to clipboard operation
yarn copied to clipboard

feature: Add --force option to `yarn link` to overwrite existing link

Open k80w opened this issue 5 years ago • 10 comments

Currently, trying to yarn link a package which is already registered spits out a warning and does nothing. Manually removing the existing link adds unnecessary tedium. Having a yarn link --force flag which replaces the existing link would be very useful.

Related issue: #7054

k80w avatar Apr 21 '19 12:04 k80w

What is the process to unlink a directory that has been deleted? Do we have to create a whole new one just to delete it? I agree this would be a much better way.

zwhitchcox avatar Dec 19 '19 20:12 zwhitchcox

At least tell us where the folder that is supposedly linked is, so we know where to delete

zwhitchcox avatar Dec 19 '19 20:12 zwhitchcox

Bumped into the same. The case is quite simple: I'm doing some work on project 1, other work on project 2, other work on project 3. And suddenly:

❯ yarn link
yarn link v1.17.3
warning There's already a package called "my-addon" registered. This command has had no effect. If this command was run in another folder with the same name, the other folder is still linked. Please run yarn unlink in the other folder if you want to register this folder.

But ... what is the other folder? Where did it come from? I assume that yarn knows the location of the other folder so it would be more helpful to tell me:

There's already a package called "my-addon" registered in ${my-addon-path}. This command has had no effect. If this command was run in another folder with the same name, the other folder is still linked. Please run cd ${my-addon-path}; yarn unlink before you try to register this folder.

MichalBryxi avatar Jan 04 '20 08:01 MichalBryxi

I created https://www.npmjs.com/package/manage-linked-packages to help with this, whilst Yarn doesn't have a --force option

dominicfallows avatar Feb 12 '20 15:02 dominicfallows

AFAICT yarn unlink will unlink based on the package name in package.json, rather than based on the folder name. Is that correct?

If this command was run in another folder with the same name, the other folder is still linked.

I think the "folder with the same name" part is misleading, given that. In my testing it doesn't matter what the folder is named, nor do you need to find the "other folder" and unlink from there; any folder with the same name in its package.json should work.

phoenixeliot avatar May 07 '20 22:05 phoenixeliot

This feature is desperately needed. Or at least a yarn links command that would list out all the links with their paths. I apparently linked a package in the wrong place and it's hard to find

ChuckJonas avatar Jul 09 '21 14:07 ChuckJonas

A workaround for this issue is to simply run yarn unlink && yarn link inside the directory that you want to link. Yarn looks at the package name for unlink, so it will unlink the package and then link the new directory.

Edit: please note that yarn unlink will return an error when there is no linked package, so in order to use this as a replacement for yarn link --force, one would have to do something like (yarn unlink || true) && yarn link.

bvanreeven avatar Oct 29 '21 09:10 bvanreeven

I think (yarn unlink || true) && yarn link is equivalent to yarn unlink; yarn link

phoenixeliot avatar Nov 15 '21 04:11 phoenixeliot

@phoenixeliot This is true so long as the shell isn't set to exit on a command failure (i.e. when the -e flag is set). It is perhaps good practice to explicitly allow yarn unlink to fail regardless.

Note that the subshell in that command isn't necessary; yarn unlink || true && yarn link or yarn unlink || true; yarn link behave almost identically to that example.

k80w avatar Nov 15 '21 15:11 k80w

What is the status of this?

I am currently manually deleting links I do not need anymore in the ./config/yarn/link folder. This is my clumsy "unlink" process.

Bildschirmfoto 2023-11-23 um 15 34 18

nerdess avatar Nov 23 '23 14:11 nerdess