Yarn: Running Yarn for the first time as `sudo` causes subsequent runs of yarn to crash

Created on 25 Oct 2016  路  11Comments  路  Source: yarnpkg/yarn

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

What is the current behavior?

If you run yarn as root for its first run: for example: sudo yarn --version
then yarn creates ~/.yarn and ~/.yarn/.roadrunner.json with the permissions and ownership like so:

drwxr-xr-x   2 root    root      4096 Oct 20 13:25 .yarn
and: 
-rw-r--r--    1 root    root    112183 Oct 24 18:05 .roadrunner.json

Subsequent runs of yarn as a normal user fail because they cannot read .roadrunner.json:

$ yarn --version
0.16.1
/usr/lib/node_modules/yarn/bin/yarn.js:47
      throw err;
      ^

Error: EACCES: permission denied, open '/home/vagrant/.yarn/.roadrunner.json'
    at Error (native)
    at Object.fs.openSync (fs.js:549:18)
    at Object.fs.writeFileSync (fs.js:1156:15)
    at /usr/lib/node_modules/yarn/node_modules/roadrunner/index.js:25:6
    at /usr/lib/node_modules/yarn/node_modules/roadrunner/index.js:12:12
    at emitOne (events.js:82:20)
    at process.emit (events.js:169:7)
    at processEmit [as emit] (/usr/lib/node_modules/yarn/node_modules/loud-rejection/node_modules/signal-exit/index.js:140:35)
    at process.exit (node.js:750:17)
    at Command.<anonymous> (/usr/lib/node_modules/yarn/node_modules/commander/index.js:825:13)

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

sudo npm install -g yarn
sudo yarn --version
yarn --version

What is the expected behavior?
I would have expected to be able to run yarn after running it as sudo once.

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

Yarn version:
0.16.1

Node version:
4.6.1

Platform:
linux x64

cat-bug

Most helpful comment

yarn should warn if called with sudo or by root, like bower, file permissions are out of control by package manager, in my opinion.

All 11 comments

Hmm this is tricky... We probably need to chown the directory to the real user ID if it's different from the effective user ID.

Same with 0.17.6

yarn should warn if called with sudo or by root, like bower, file permissions are out of control by package manager, in my opinion.

What does npm do in this case?

I feel like chowning ~/.yarn to the real user ID is fine, and would solve this problem.

I cannot reproduce this issue following the exact same steps on (after deleting all temporary yarn directories):

  • Fedora 25
  • node 6.9.1 (from NodeSource)
  • yarn 0.17.8 (from official RPM package)

Also tried to install yarn through npm install -g yarn, which got version 0.17.9, and still cannot reproduce.

$ sudo find / -name '.roadrunner.json' -ls                                                             
  1973661     44 -rw-r--r--   1  root     root        42635 Nov 29 08:19 /usr/local/share/.cache/yarn/.roadrunner.json              
  6556939     44 -rw-rw-r--   1  gtirloni gtirloni    43499 Nov 29 08:19 /home/gtirloni/.cache/yarn/.roadrunner.json

Got the same when I updated Yarn to 0.17.9

same error with 0.17.10
Error: EACCES: permission denied, open '/Users/RiCoTeRoX/Library/Caches/Yarn/.roadrunner.json

My solution was to change the owner.

So how did you change the owner? @RiCoTeRoX ... I have tried what I know, I'll be glad if you could be of assistance.

chown -R youruser:yourgroup *

For those who might need a bit more solution detail (or stop-gap solution, if you will):

# Show all yarn binaries in the path
$ which -a yarn | xargs ls -lA
lrwxrwxrwx 1 root root 36 Dec  8 16:12 /usr/bin/yarn -> ../lib/node_modules/yarn/bin/yarn.js
lrwxrwxrwx 1 root root 58 Dec  8 15:32 /usr/local/bin/yarn -> ../share/.config/yarn/global/node_modules/yarn/bin/yarn.js

# Show all yarn binaries in the path, but follow the symlinks
$ which -a yarn | xargs ls -lAH
-rw-r--r-- 1 root root 873 Dec  8 16:12 /usr/bin/yarn
#  ^  ^  ^ Here is the issue, no run permission (x) for anyone!
-rwxr-xr-x 1 root root 873 Dec  8 15:32 /usr/local/bin/yarn

# Set the run permission for everyone on the problem binary.
$ xargs sudo chmod a+x /usr/bin/yarn

Edit: Clarity of code and typos

Unfortunately, I am also encountering this issue. :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jiripospisil picture jiripospisil  路  128Comments

donaldpipowitch picture donaldpipowitch  路  75Comments

donovan-graham picture donovan-graham  路  115Comments

carlosduclos picture carlosduclos  路  86Comments

DannyBrown picture DannyBrown  路  89Comments