nats-io / natscli

The NATS Command Line Interface
Apache License 2.0
497 stars 99 forks source link

Incorrect local JetStream store directory when $XDG_DATA_HOME is defined #962

Closed erhhung closed 9 months ago

erhhung commented 10 months ago

Observed behavior

When running nats server run --jetstream, the Store Directory shown in the JetStream banner output repeats the ".local/share/" path components:

[76897] [INF] Starting nats-server
[76897] [INF]   Version:  2.10.0
[76897] [INF]   Git:      [not set]
[76897] [INF]   Name:     nats_development
[76897] [INF]   Node:     HhEiCaYr
[76897] [INF]   ID:       NAIC2EHHLBZSS6H7KZWZRP5U6UVHDB4FNORZT2S733PJMLLGXNIHN6KW
[76897] [INF] Using configuration file: /var/folders/yt/lwq8py0n4qxbkr2mg_nwg1y00000gn/T/nats-server-run-3707093925.cfg
[76897] [INF] Starting JetStream
[76897] [INF]     _ ___ _____ ___ _____ ___ ___   _   __  __
[76897] [INF]  _ | | __|_   _/ __|_   _| _ \ __| /_\ |  \/  |
[76897] [INF] | || | _|  | | \__ \ | | |   / _| / _ \| |\/| |
[76897] [INF]  \__/|___| |_| |___/ |_| |_|_\___/_/ \_\_|  |_|
[76897] [INF]
[76897] [INF]          https://docs.nats.io/jetstream
[76897] [INF]
[76897] [INF] ---------------- JETSTREAM ----------------
[76897] [INF]   Max Memory:      48.00 GB
[76897] [INF]   Max Storage:     1.20 TB
[76897] [INF]   Store Directory: "/Users/erhhung/.local/share/.local/share/nats/nats_development/jetstream"
[76897] [INF] -------------------------------------------
[76897] [INF] Listening for client connections on 0.0.0.0:58137
[76897] [INF] Server is ready

Apparently, if the XDG_DATA_HOME environment variable isn't set, the default store directory is correct:

$ echo $XDG_DATA_HOME
/Users/erhhung/.local/share
$ unset XDG_DATA_HOME
[76864] [INF] Starting nats-server
[76864] [INF]   Version:  2.10.0
[76864] [INF]   Git:      [not set]
[76864] [INF]   Name:     nats_development
[76864] [INF]   Node:     HhEiCaYr
[76864] [INF]   ID:       NB5IF7UJZDNZKE4IRDCNVO4AHU64EJXFEJV5L7VRAPRF4BV7ZRIWQVD3
[76864] [INF] Using configuration file: /var/folders/yt/lwq8py0n4qxbkr2mg_nwg1y00000gn/T/nats-server-run-3427777350.cfg
[76864] [INF] Starting JetStream
[76864] [INF]     _ ___ _____ ___ _____ ___ ___   _   __  __
[76864] [INF]  _ | | __|_   _/ __|_   _| _ \ __| /_\ |  \/  |
[76864] [INF] | || | _|  | | \__ \ | | |   / _| / _ \| |\/| |
[76864] [INF]  \__/|___| |_| |___/ |_| |_|_\___/_/ \_\_|  |_|
[76864] [INF]
[76864] [INF]          https://docs.nats.io/jetstream
[76864] [INF]
[76864] [INF] ---------------- JETSTREAM ----------------
[76864] [INF]   Max Memory:      48.00 GB
[76864] [INF]   Max Storage:     1.20 TB
[76864] [INF]   Store Directory: "/Users/erhhung/.local/share/nats/nats_development/jetstream"
[76864] [INF] -------------------------------------------
[76864] [INF] Listening for client connections on 0.0.0.0:58137
[76864] [INF] Server is ready

Expected behavior

Per the XDG Base Directory Specification, the XDG_DATA_HOME environment variable is optional: _if $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used_.

That implies that if the variable is defined in the shell, apps that use it should treat it as if it were the default value—in this case, it was explicitly set by in my "~/.bash_profile" by export XDG_DATA_HOME="$HOME/.local/share"—and not repeat the ".local/share/" path components.

Server and client version

$ nats --version
0.1.1

Host environment

macOS Sonoma 14.2.1

Steps to reproduce

No response

erhhung commented 10 months ago

I see two places where the $XDG_DATA_HOME environment variable is used differently:

  1. cli/util.go: func xdgShareHome() (string, error)
  2. cli/server_run_command.go: func (c *SrvRunCmd) dataParentDir() (string, error)

with the latter function adding the superfluous path components.