yarn install overwrites linked modules with cached version

Created on 19 Oct 2016  Ā·  41Comments  Ā·  Source: yarnpkg/yarn

Do you want to request a _feature_ or report a _bug_?

Bug

What is the current behavior?

Running yarn install for an app that has linked modules results in yarn copying its cached version of each module to the symlinked directory, overwriting the local copy in the process.

If the current behavior is a bug, please provide the steps to reproduce.

I struggled to reproduce this from scratch, it's possible that it's down to a specific combination of factors with my setup. This involves local copies of private/scoped npm modules yarn linked from an application, and referenced in package.json via file:../xxx. The version of the modules yarn has in its cache appears to be from when I first installed and ran it a few days ago (there have been numerous changes to the modules since), so it's possible the problem lies with its caching strategy as much as its installation strategy.

One observation I've made is that the problem occurs even when using yarn install --force to supposedly bypass the cache.

Happy to answer any questions and/or try anything to help pinpoint the problem.

What is the expected behavior?

yarn install should skip linked modules like npm intstall does. Performing the same steps above using npm install does not result in this problem.

Please mention your node.js, yarn and operating system version.

node v6.2.2
yarn v0.16.0
Mac OS X v10.10.5

Most helpful comment

I’d be much happier if Yarn just left linked dependencies alone—if it’s a symlink, either do nothing on the assumption that I want to work with it as a symlink, or if for some reason that can’t be done, issue an error and refuse to do anything. Even deleting the symlinks and replacing them with normal dependencies would be better—still a nuisance, but at least not as bad as the current behaviour.

  1. If I run yarn link, I should never have to worry about Yarn _changing_ my files.
  2. There’s no reason why Yarn should be modifying files outside of node_modules (and its own caches).

IMHO, the preferred behaviour would be to leave symlinked directories alone, unless there’s a version mismatch, in which case error out with a descriptive message.

All 41 comments

This isn't specific to cached packages. I have a published package [email protected]. Locally I link an unpublished package [email protected]. My project has a package.json dependency for [email protected] and I link to that. When I run yarn after linking, it tries to install [email protected] from the repo instead of using the linked version and it fails because [email protected] hasn't been published yet.

really need this

this is breaking

This applies even when you install _different_ packages. E.g.

cd @my/local-dep
yarn link
cd ../../@my/main-project
yarn link @my/local-dep
yarn add lodash

Even though I didn’t even explicitly do anything with @my/local-dep, yarn still goes through the modules and installs anything not locally installed, even if it’s linked.

This happens even if I specify --no-lockfile.

It’s a particularly nasty problem because it overwrites my local _working copy_ of @my/local-dep, so it will overwrite anything you haven’t committed yet, effectively erasing local work.

$ yarn --version
0.16.1

I've noticed this issue as well. In my testing it seems to be limited to scoped modules, but that may not be true for everyone. My use case:

Our team works in a monorepo. To get shared packages locally, the only way to have an efficient development process is to symlink them. Since we can't save symlinked installs to package.json, we use file: specifiers with relative paths, followed by a library called linklocal that goes finds and symlinks local dependencies.

It's kind of a hack, but it works very well. However, npm shrinkwrap is so painful for us that switching to yarn is extremely advantageous, even if we have to work around a few warts due to early adoption.

This issue is very bad for that above workflow though. Anytime yarn tries to install we run the risk of losing work.

What exactly is the advantage of using the cache for local dependencies? Since they would never hit the network anyway, couldn't yarn just treat anything that's a file: reference as a cache miss and either skip copying it entirely (as npm does) or copy it in from the local dir?

I’d be much happier if Yarn just left linked dependencies alone—if it’s a symlink, either do nothing on the assumption that I want to work with it as a symlink, or if for some reason that can’t be done, issue an error and refuse to do anything. Even deleting the symlinks and replacing them with normal dependencies would be better—still a nuisance, but at least not as bad as the current behaviour.

  1. If I run yarn link, I should never have to worry about Yarn _changing_ my files.
  2. There’s no reason why Yarn should be modifying files outside of node_modules (and its own caches).

IMHO, the preferred behaviour would be to leave symlinked directories alone, unless there’s a version mismatch, in which case error out with a descriptive message.

unless there’s a version mismatch, in which case error out with a descriptive message.

What would be the advantage of this? Why not allow linked packages to be any version? I believe that's how NPM currently works.

Having the same issue. It seems like it's replacing the linked module with the published version. As tho it's installing the package into the directory without realising it's a symlinked package. I don't think yarn's cache is the reason.

I was able to get around this issue for time being by using yarn link and then not adding the dependencies to my package.json at all. The script that starts our dev server & installs deps for CI/production builds also handles running yarn link for our shared packages, which ensures it happens everywhere we need to build our code.

This is very confusing behaviour of yarn, a big source of hassle, have you managed fix the logic behind it? I believe yarn should not check for cache when dealing with file: modules (it also refers to other non-versioned sources like remote git repo).

I've described inconsistent behaviour or yarn that leads to the issue: https://github.com/yarnpkg/yarn/issues/1794

Btw in new 0.17.6 it seems that yarn doesn't replace content of simlinked folder (in node_modules) with version from cache, but installs new dependencies (that changed in linked package.json - which is nice)

Can others confirm that this not an issue anymore?
@lukebennett @joefiorini @haggholm

@whitecolor can confirm that with 0.17.6 the problem still persists. simlinked folder is having code overwritten by yarn.

This is happening with a privately scoped npm repo (@rocketrip/name-of-package)

@travisbloom right it seem to be true when you link scoped package, it works ok with not scoped symlinks (doesn't touch it).

@wyze can you look in to it, to close issues with scoped packages?

Just added a fix for this in #1970

Still an issue with 0.17.9 and a huge blocker for most of the scenarios involving npm/yarn link.

+1, This is a big issue that caused us to lose uncommitted work from a linked repo. Would be good to get it fixed.

Can confirm I don't get this issue in the latest version 0.19.1.

Yep, this can probably be closed now

I am still seeing this issue on [email protected]. I'm using 'lerna' so if I have packageA depending on packageB via a symlink and then run yarn upgrade packageB inside the packageA directory It ends up overwriting my non-commited local changes of packageB.

@AlastairTaft can you make a list of simple steps to reproduce?

@whitecolor

Simple reproduction with file: protocol.

  • Install a package through the registry (for example "lodash")
  • Change your package.json so that lodash uses file protocol (eg: "lodash": file:./custom_packages/lodash")
  • yarn install
  • observe node_modules => lodash version from cache is used instead of the one from custom_packages

yarn v0.22.0

@dalmaer and on the latest stable 23.2?

We just landed a fix to file: dependencies on master.
Check out in 0.24 next week

On 27 April 2017 at 15:09, Alex notifications@github.com wrote:

@dalmaer https://github.com/dalmaer and on the latest stable 23.2?

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/yarnpkg/yarn/issues/1214#issuecomment-297724254, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACBdWJx3UiG6uRUHgZeNqqGlsUyGyd2Pks5r0KGIgaJpZM4KaX2W
.

This is still happening. I just opened #3288 to report the same behavior. Happens with 0.23.4. We're not using file protocol. Could possibly be related to scoped/private as that is where we're seeing it.

Is there any workaround for this which I am not aware of? Needless to say, having your working copies constantly overwritten by cached content without you realizing it has a major impact on development.

@oliversalzburg I advice to try this for development with local packages: https://github.com/whitecolor/yalc (it can make your life really easier)

@whitecolor Awesome. Thanks a lot. I'll check it out.

I'm still trying to confirm if this is what happened, but it appears that yarn overwrote local files across multiple files with cached symlinked versions. I'm using yarn 0.23.4

@bestander Can you reopen this issue? It is not solved. I would really appreciate some feedback on this.

@oliversalzburg please open a new one with a scenario how to reproduce it.
Ideally if you could provide a bash script to make it automated.

@bestander I had already opened #3288, but I'm having a really hard time reproducing the issue in isolation. It happens multiple times every day in our main project tree though. It would be really nice to investigate this with someone who is more familiar with the code base and maybe give me some pointers on what to look for.

I'm getting this issue on version 0.24.5.
I have some packages that i developed and they are not published on any server, they are just linked.
My projects uses this linked packages and some published packages like express or bluebird.
when I try to run "yarn" to install all or a specific package it returns the error:

error Couldn't find package "package-name" on the "npm" registry

steps to reproduce:

cd path/to/a-dev-package
yarn link
cd path/to/a/project
yarn link a-dev-package
yarn add bluebird
it returns :
error Couldn't find package "a-dev-package" on the "npm" registry

Thanks, @danilo-drs, I've extracted this into a separate issue https://github.com/yarnpkg/yarn/issues/3645

I believe this issue is not solved in v0.27.5. node here is 6.11 and npm 5.04.

Steps (note: A is a dependency to B in package.json):

  1. yarn link / npm link module A (a symlink goes into the local $PREFIX/lib/node_modules/)
  2. chdir into module B's dir
  3. yarn link A (a symlink to A goes into B/node_modules/).
  4. yarn install => complains about versions from npmjs.com, rather than taking into consideration the local

@stelf yarn takes into account what is in your package.json to keep consistency. So it actually doesn't take into account that you linked the module (there is no any notice about it, except symlink directory in node_modules ). There is link: dependencies in yarn, but it is more for linking local deps in monorepo. For working with external modules I successfully use yalc

This is still happening with yarn v1.2.1:

> $ yarn link mongodb-promise                                                                                                                                         yarn link v1.2.1
success Using linked module for "mongodb-promise".
✨  Done in 0.07s.

> $ ls -lad node_modules/mongodb-promise                                                                                                                              
lrwxr-xr-x  1 fcoury  staff  21 Oct 18 18:52 node_modules/mongodb-promise -> ../../mongodb-promise

fcoury@iMac ~/code/faxing                                                                                                                                              > $ yarn                                                                                                                                                              [±master ā—]
yarn install v1.2.1
[1/4] šŸ”  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] šŸ”—  Linking dependencies...
[4/4] šŸ“ƒ  Building fresh packages...
success Saved lockfile.
✨  Done in 1.99s.

> $ ls -lad node_modules/mongodb-promise                                                                                                                              drwxr-xr-x  9 fcoury  staff  306 Oct 18 18:52 node_modules/mongodb-promise

So this is the expected behavior?

Still outstanding and needs to be fixed.

When I run yarn add lodash (for example) it removes any links I had set up for my project, and I have to manually run yarn link my-package-name again.

Looks like the original issue was fixed but there may be other cases when it is still relevant.
For everyone seeing this please open a new issue with the repro script.
It would be hard to reopen this one as it tracks too many things.

Created new issue #4770 as requested.

Was this page helpful?
0 / 5 - 0 ratings