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

--search-parent-directories doesn't appear to respect $XDG_CONFIG_HOME #260

Closed
timbedard opened this issue Sep 8, 2021 · 4 comments · Fixed by #266
Closed

--search-parent-directories doesn't appear to respect $XDG_CONFIG_HOME #260

timbedard opened this issue Sep 8, 2021 · 4 comments · Fixed by #266
Labels
bug Something isn't working

Comments

@timbedard
Copy link
Contributor

--search-parent-directories doesn't appear to respect the $XDG_CONFIG_HOME environment variable. This is based on the output when using -v, as I can't actually test due to #259.

  • Version: 0.10.1
  • Platform: macOS Big Sur 11.5.2
  • $XDG_CONFIG_HOME:/Users/tim/.dotfiles/config

Config at /Users/tim/.dotfiles/config/stylua.toml:

column_width = 88

Command and output:

stylua -sv example.lua

config: starting config search from /Users/tim - recurisvely searching parents: true
config: no configuration file found
config: looking in $HOME/.config/stylua
config: looking in $HOME/.config
config: falling back to default config
config: Config {
    column_width: 120,
    line_endings: Unix,
    indent_type: Tabs,
    indent_width: 4,
    quote_style: AutoPreferDouble,
    no_call_parentheses: false,
}
creating a pool with 8 threads
formatted example.lua in 969.815µs

Note that the searched locations in the output have not changed even though $XDG_CONFIG_HOME is set to /Users/tim/.dotfiles/config

@timbedard
Copy link
Contributor Author

timbedard commented Sep 8, 2021

Looks like this and #259 are due to the directories crate. It doesn't actually use XDG_CONFIG_HOME at all on macOS. 😱

Is there a better lib option for this purpose that we could use? That one doesn't appear to cut the mustard.

We'll also want to update the verbose logging to say where it's actually looking.

@JohnnyMorganz
Copy link
Owner

Thanks for the report and digging into it further. It is a shame that the directories crate doesn't look at $XDG_CONFIG_HOME at all on macOS, whilst it looks correctly on Linux - from a quick glance there doesn't seem to be any way to force it to use the Linux variant on macOS in either directories or it's lower level sister crate dirs.

A quick google leads to alternatively the xdg crate, which handles looking at both $HOME/.config and $XDG_CONFIG_HOME, which seems to be useful. Looks quite old/unmaintained though, and nothing specific for Windows support (but I've never really seen any of the Windows paths linked in directories actually be used...)

I wonder if it's worthwhile to just handle it ourselves rather than sending it off to another crate. It seems to just be looking at $XDG_CONFIG_HOME, $XDG_CONFIG_HOME/stylua, $HOME/.config and $HOME/.config/stylua. (And the Windows variants if we want to use them too)

@timbedard
Copy link
Contributor Author

I'm not much of a rustacean, but I can't imagine it would be particularly difficult to handle it ourselves.

I'm not sure what folks typically do for Windows, but for *nix the logic would be (in Lua):

-- default to "~/.config" if unset
local config_dir = os.getenv("XDG_CONFIG_HOME") or os.getenv("HOME") .. "/.config"
local xdg_root_file = config_dir .. "/stylua.toml"
local xdg_subdir_file = config_dir .. "/stylua/stylua.toml"

if io.open(xdg_root_file) then
  config_file = xdg_root_file
elseif io.open(xdg_subdir_file) then
  config_file = xdg_subdir_file
end

@timbedard
Copy link
Contributor Author

Seems to be working great. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants