yarnpkg / yarn

The 1.x line is frozen - features and bugfixes now happen on https://github.com/yarnpkg/berry
https://classic.yarnpkg.com
Other
41.39k stars 2.72k forks source link

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

Open Taytay opened 7 years ago

Taytay commented 7 years ago

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

Daniel15 commented 7 years ago

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.

mattecapu commented 7 years ago

Same with 0.17.6

S-YOU commented 7 years ago

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

Daniel15 commented 7 years ago

What does npm do in this case?

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

gtirloni commented 7 years ago

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

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
ashishg-qburst commented 7 years ago

Got the same when I updated Yarn to 0.17.9

RiCoTeRoX commented 7 years ago

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.

ayoola-moore commented 7 years ago

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

ltworf commented 6 years ago

chown -R youruser:yourgroup *

servel333 commented 6 years ago

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

dmpe commented 4 years ago

Unfortunately, I am also encountering this issue. :(