yarn icon indicating copy to clipboard operation
yarn copied to clipboard

XDG compliance

Open Conaclos opened this issue 8 years ago • 20 comments

Hi :)

I noted that Yarn doesn't fit the XDG basedir specification. It could be nice to support it.

What is the current behavior?

Currently Yarn creates its own data / cache / config directories:

  • $HOME/.yarn
  • $HOME/.yarn-cache
  • $HOME/.yarn-config

What is the expected behavior?

Next the desired replacement (or fallback) of each directory.

  • $HOME/.yarn: ${XDG_DATA_HOME:-$HOME/.local/share}/yarn
  • $HOME/.yarn-cache: ${XDG_CACHE_HOME:-$HOME/.cache}/yarn
  • $HOME/.yarn-config: ${XDG_CONFIG_HOME:-$HOME/.config}/yarn

OS: Linux

Conaclos avatar Dec 23 '16 11:12 Conaclos

@kevinburke @bestander So there was an attempt at fixing this back in April, but it got abandoned.

Any chance we could get a proper plan of what needs to be done to fix this?

Qt has an excellent cross-platform reference implementation of "standard paths" in QStandardPaths: https://doc.qt.io/qt-5/qstandardpaths.html - I don't know if this exists for node, but I know it's come up in other places as well (eg. https://github.com/serverless/serverless/issues/4602)

jleclanche avatar Dec 23 '17 02:12 jleclanche

If someone opens a new PR based on https://github.com/yarnpkg/yarn/pull/3674 ping me I'll merge it

bestander avatar Jan 08 '18 08:01 bestander

This seems to be partially fixed by #5336, at least on Yarn 1.9.4. However there's still $HOME/.yarn (which appears to only contain the bin folder) and $HOME/.yarnrc, despite there also being $XDG_CONFIG_HOME/yarn (with the contents debatably being configuration), $XDG_DATA_HOME/yarn (which seems to overlap with $XDG_CONFIG_HOME/yarn...?), and $XDG_CACHE_HOME/yarn (which seems to be the only one that is properly and correctly used). This is even worse than not supporting it at all, as it's now just inconsistent and confusing.

It would be very nice if everything would be moved to follow the XDG spec. Ideally .yarnrc would be $XDG_CONFIG_HOME/yarn/config. As for $HOME/.yarn/bin, that is a bit more complicated, as the spec doesn't define a location for user-installed binaries, although $HOME/.local/bin is commonly used, which lines up with the default for $XDG_DATA_HOME ($HOME/.local/share). Hopefully one day the spec will be amended to include something like $XDG_BIN_HOME. However I believe the best solution for now is to place executables in $XDG_DATA_HOME/yarn/bin and provide an environment variable to customise that location (for example, YARN_BIN_DIR).

I may be able to work on this provided I can find my way around the codebase. Making sure my home directory is completely clean and not littered with a thousand random dotfiles is the dream after all.

severen avatar Aug 29 '18 06:08 severen

Is this free to pick up? I'm thinking of fixing this, I've already done this for other projects like electron-download, js-v8flags, greenkeeper and node-gyp. But I worry about having to be backward compatible, looking at the current logic it can get pretty messy.

Is it possible to make this a major change? Then I would be more than happy to fix this, using env-paths it will be pretty straightforward. @bestander

Siilwyn avatar Oct 09 '18 08:10 Siilwyn

Dropping support for the old config locations seems fine to me, as long as the migration path is painless.

This wouldn't be the only change waiting for a major version bump - see the next-major label on open PRs.

There is an open PR relating to this though - #5343. There was talk in the PR about waiting for a major version bump. I'm not sure how close that PR brings us to full XDG compliance.

Gudahtt avatar Oct 09 '18 13:10 Gudahtt

@Gudahtt thanks for your reply & good to hear. If we do migration it is not a major version bump, what do you envision as migration path?

Moving the old location files to the new locations is risky since conflicts can happen and it can break other tools using the old paths. Instead I would suggest to check the old locations but use the new locations when nothing exists on the old. But again, both solutions do not require a major version bump.

Also seeing https://github.com/yarnpkg/yarn/pull/5343#issuecomment-367730189 this looks like what I just described with an added warning. Still this PR does all the implementation itself, using a module feels more appropriate.

Siilwyn avatar Oct 09 '18 13:10 Siilwyn

Sorry, I didn't explain myself very well. I meant to say that dropping support seems fine, as long as the migration is painless and it coincides with a major version bump. A major version bump is likely in the near future, so that isn't a huge problem.

Even if we make it a breaking change, we should provide a way to migrate users from the old to the new locations. The approach discussed in that PR seems reasonable to me.

Would you mind reviewing that PR? I'd be interested to know how it lines up with what you had in mind. I would prefer that we accept that PR, but perhaps you could take over if they don't have time to finish the work.

Gudahtt avatar Oct 09 '18 15:10 Gudahtt

Most applications that add XDG support don't change the config file location, they support both. So they check if ~/.yarnrc exists, if not use the XDG dirs. This means new installs use XDG and don't clutter home, while old installs see no breaking change.

RX14 avatar Oct 10 '18 15:10 RX14

Haha, nice coincidence @RX14! Check my comment on the PR: https://github.com/yarnpkg/yarn/pull/5343#issuecomment-428239895

Siilwyn avatar Oct 10 '18 16:10 Siilwyn

@arcanis with no activity at the PR or in this issue can I pick this up by creating a new PR? If so: do you agree with my idea at https://github.com/yarnpkg/yarn/pull/5343#issuecomment-428239895?

Siilwyn avatar Jan 16 '19 20:01 Siilwyn

any progress?

soredake avatar Mar 08 '20 14:03 soredake

Make it read environment variables at least. So it can be manually configured ,work on XDG compliance can continue meanwhile

charlie39 avatar Mar 28 '20 22:03 charlie39

bump

felixsanz avatar Jun 08 '20 23:06 felixsanz

👀

gaving avatar Sep 13 '20 08:09 gaving

It seems 1.22 supports XDG? https://github.com/yarnpkg/yarn/commit/2d454b552d447a0f79a04e4e451e926e1c0a29e7

jedahan avatar Dec 10 '20 15:12 jedahan

I'm still finding that .yarn and .yarnrc files are being created in my home directory.

Screenshot 2020-12-16 at 21 21 20

adamroyjones avatar Dec 16 '20 21:12 adamroyjones

I have the same behavior as shown in @adamroyjones screenshot with yarn version 1.22.10

melvio avatar Apr 01 '21 08:04 melvio

seems yarn is creating ~/.yarnrc every time when you run yarn install:

https://github.com/yarnpkg/yarn/blob/3119382885ea373d3c13d6a846de743eca8c914b/src/cli/commands/install.js#L1105-L1107

what saveHomeConfig does is:

https://github.com/yarnpkg/yarn/blob/3119382885ea373d3c13d6a846de743eca8c914b/src/registries/yarn-registry.js#L139

where homeConfigLoc is hardcoded:

https://github.com/yarnpkg/yarn/blob/3119382885ea373d3c13d6a846de743eca8c914b/src/registries/yarn-registry.js#L56

it should find possible config dir like https://github.com/yarnpkg/yarn/pull/5336 , or at least try more locations like npm-registry does.

https://github.com/yarnpkg/yarn/blob/3119382885ea373d3c13d6a846de743eca8c914b/src/registries/npm-registry.js#L253-L293

rocka avatar May 06 '21 16:05 rocka

Side note: currently it seems like it's not possible to set a custom path for .yarnrc folder inside a container that runs with the nobody user (no home dir).

In the php world, composer solves this issue with a special environment variable (COMPOSER_HOME).

guvra avatar Sep 26 '22 08:09 guvra

It would be nice to be implemented, at least for configuration Any news on this one?

eirnym avatar Jan 24 '25 15:01 eirnym

Also worth noting that the Arch Wiki suggests aliasing yarn to force the --use-yarnrc flag: https://wiki.archlinux.org/title/XDG_Base_Directory

But this is not a great workaround for actual XDG support because that causes yarn to fail when the yarnrc file doesn't exist.

$ alias yarn
alias yarn='yarn --use-yarnrc "${XDG_CONFIG_HOME:?}/yarnrc"'
$ yarn -h
Error: ENOENT: no such file or directory, open '/Users/jasonkarns/.config/yarnrc'

jasonkarns avatar Nov 20 '25 20:11 jasonkarns

Yeah, but since you add alias to shell config, you can also add creation of the parent directories and the file with content if it's not present.

yarn = ''yarn --use-yarnrc "''${XDG_CONFIG_HOME:-$HOME/.config}/yarn/config"'';

Or for Nix users, you can configure stuff so that it automatically creates the file on config switch.

NixOS (home-manager)
        with config.xdg; rec {
          home.sessionVariables = {
            YARN_CONFIG = "${configHome}/yarn/config";
          };
          home.file."${home.sessionVariables.YARN_CONFIG}".text = ''
            prefix "${dataHome}/yarn"
          '';
        })

Andrew15-5 avatar Nov 20 '25 20:11 Andrew15-5