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

Show full example of getting XDG user directory #2536

Merged
merged 3 commits into from
Dec 22, 2024

Conversation

laniakea64
Copy link
Contributor

Per XDG base directory specification, getting an XDG user directory involves a bit more than reading an environment variable:

All paths set in these environment variables must be absolute. If an implementation encounters a relative path in any of these variables it should consider the path invalid and ignore it.
...
$XDG_CONFIG_HOME defines the base directory relative to which user-specific configuration files should be stored. If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.

This PR expands the example to fully show how to get an XDG user directory in a justfile.

@casey
Copy link
Owner

casey commented Dec 22, 2024

Interesting!

This does at least slightly make the case that getting the XDG dirs correctly is painful, and should be better supported by just.

I wonder if this is something that users should actually care about. I.e., if the XDG_CONFIG_HOME isn't absolute, the user should really fix that, and the justfile shouldn't try to compensate, even if it is acting according to the spec.

Because if so you could do:

xdg_config_dir := env('XDG_CONFIG_HOME', home_directory() / '.config')

Which is much nicer.

@laniakea64
Copy link
Contributor Author

I wonder if this is something that users should actually care about. I.e., if the XDG_CONFIG_HOME isn't absolute, the user should really fix that, and the justfile shouldn't try to compensate, even if it is acting according to the spec.

The justfile is likely seeking to match other XDG-directory-using programs.

Testing a few applications in Xubuntu 22.04, they don't seem to care about relative paths. However, they do care about the env vars being set to empty values: most apps ignore the empty env var and use the default, a few apps proceed as if there is no user directory available.

So even when a justfile wants to be relaxed about the XDG specification, getting an XDG user directory still involves more than a single env() call for the most likely case:

xdg_config_dir := if env('XDG_CONFIG_HOME', '') != '' {
  env('XDG_CONFIG_HOME')
} else {
  home_directory() / '.config'
}

@casey
Copy link
Owner

casey commented Dec 22, 2024

Gotcha. And that isn't much shorter than the fully correct version which uses =~ '^/', so we might as well do that.

@casey casey enabled auto-merge (squash) December 22, 2024 03:19
@casey casey merged commit 82415e5 into casey:master Dec 22, 2024
5 checks passed
@laniakea64 laniakea64 deleted the xdg-example branch December 22, 2024 04:09
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