Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use xdg_config_home on unix instead of hard-coded path #167

Closed
wants to merge 3 commits into from

Conversation

noiioiu
Copy link

@noiioiu noiioiu commented Nov 17, 2024

This should fix a bug where XDG_CONFIG_HOME is ignored by beet.

@jackwilsdon
Copy link
Member

Is this not already being handled here?

confuse/confuse/util.py

Lines 133 to 145 in d1fa1ad

def xdg_config_dirs():
"""Returns a list of paths taken from the XDG_CONFIG_DIRS
and XDG_CONFIG_HOME environment varibables if they exist
"""
paths = []
if 'XDG_CONFIG_HOME' in os.environ:
paths.append(os.environ['XDG_CONFIG_HOME'])
if 'XDG_CONFIG_DIRS' in os.environ:
paths.extend(os.environ['XDG_CONFIG_DIRS'].split(':'))
else:
paths.append('/etc/xdg')
paths.append('/etc')
return paths

confuse/confuse/util.py

Lines 168 to 171 in d1fa1ad

else:
# Assume Unix.
paths.append(UNIX_DIR_FALLBACK)
paths.extend(xdg_config_dirs())

@jackwilsdon
Copy link
Member

jackwilsdon commented Nov 17, 2024

Doing some more digging, config_dirs() does correctly return XDG_CONFIG_HOME:

$ XDG_CONFIG_HOME=/tmp/test python3 -c "import confuse; print(confuse.config_dirs())"
['/home/jack/.config', '/tmp/test', '/etc']

The issue is that confuse is picking the first item from the list when no configuration exists in any of the paths returned by config_dirs:

confuse/confuse/core.py

Lines 563 to 571 in 5001e18

# Search platform-specific locations. If no config file is
# found, fall back to the first directory in the list.
configdirs = util.config_dirs()
for confdir in configdirs:
appdir = os.path.join(confdir, self.appname)
if os.path.isfile(os.path.join(appdir, CONFIG_FILENAME)):
break
else:
appdir = os.path.join(configdirs[0], self.appname)

The docstring for config_dirs seems to suggest the last item should be the fallback to use when creating a new configuration, but that'd be /etc on my system which sounds wrong.

confuse/confuse/util.py

Lines 148 to 155 in d1fa1ad

def config_dirs():
"""Return a platform-specific list of candidates for user
configuration directories on the system.
The candidates are in order of priority, from highest to lowest. The
last element is the "fallback" location to be used when no
higher-priority config file exists.
"""

It feels like we should maybe re-order config_dirs to:

  • XDG_CONFIG_HOME (if set)
  • ~/.config
  • XDG_CONFIG_DIRS (if set)

And continue using the first item as the default.

From the XDG specification, it sounds like we shouldn't create configs in XDG_CONFIG_DIRS, but that's a separate issue I guess.

@noiioiu
Copy link
Author

noiioiu commented Nov 17, 2024

Alright, I'll write a new patch that just changes the order.

@noiioiu noiioiu closed this Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants