Skip to content

Commit 6708818

Browse files
authored
fix(client/v2): use (PREFIX)_HOME instead of NODE_HOME (#20964)
1 parent 86ea861 commit 6708818

5 files changed

Lines changed: 24 additions & 6 deletions

File tree

client/v2/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
4747

4848
* [#17709](https://github.com/cosmos/cosmos-sdk/pull/17709) Address codecs have been removed from `autocli.AppOptions` and `flag.Builder`. Instead client/v2 uses the address codecs present in the context (introduced in [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503)).
4949

50+
### Bug Fixes
51+
52+
* [#20964](https://github.com/cosmos/cosmos-sdk/pull/20964) Fix `GetNodeHomeDirectory` helper in `client/v2/helpers` to respect the `(PREFIX)_HOME` environment variable.
53+
5054
## [v2.0.0-beta.3] - 2024-07-15
5155

5256
### Features

client/v2/helpers/home.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import (
66
"strings"
77
)
88

9+
// EnvPrefix is the prefix for environment variables that are used by the CLI.
10+
// It should match the one used for viper in the CLI.
11+
var EnvPrefix = ""
12+
913
// GetNodeHomeDirectory gets the home directory of the node (where the config is located).
10-
// It parses the home flag if set if the `NODE_HOME` environment variable if set (and ignores name).
14+
// It parses the home flag if set if the `(PREFIX)_HOME` environment variable if set (and ignores name).
15+
// When no prefix is set, it reads the `NODE_HOME` environment variable.
1116
// Otherwise, it returns the default home directory given its name.
1217
func GetNodeHomeDirectory(name string) (string, error) {
1318
// get the home directory from the flag
@@ -21,12 +26,19 @@ func GetNodeHomeDirectory(name string) (string, error) {
2126
}
2227

2328
// get the home directory from the environment variable
24-
homeDir := os.Getenv("NODE_HOME")
29+
// to not clash with the $HOME system variable, when no prefix is set
30+
// we check the NODE_HOME environment variable
31+
homeDir, envHome := "", "HOME"
32+
if len(EnvPrefix) > 0 {
33+
homeDir = os.Getenv(EnvPrefix + "_" + envHome)
34+
} else {
35+
homeDir = os.Getenv("NODE_" + envHome)
36+
}
2537
if homeDir != "" {
2638
return filepath.Clean(homeDir), nil
2739
}
2840

29-
// return the default home directory
41+
// get user home directory
3042
userHomeDir, err := os.UserHomeDir()
3143
if err != nil {
3244
return "", err

docs/learn/advanced/07-cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ this will be more convenient:
182182

183183
```shell
184184
# define env variables in .env, .envrc etc
185-
NODE_HOME=<path to home>
185+
GAIA_HOME=<path to home>
186186
GAIA_NODE=<node address>
187187
GAIA_CHAIN_ID="testchain-1"
188188
GAIA_KEYRING_BACKEND="test"

simapp/simd/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
clientv2helpers "cosmossdk.io/client/v2/helpers"
78
"cosmossdk.io/simapp"
89
"cosmossdk.io/simapp/simd/cmd"
910

@@ -12,7 +13,7 @@ import (
1213

1314
func main() {
1415
rootCmd := cmd.NewRootCmd()
15-
if err := svrcmd.Execute(rootCmd, "", simapp.DefaultNodeHome); err != nil {
16+
if err := svrcmd.Execute(rootCmd, clientv2helpers.EnvPrefix, simapp.DefaultNodeHome); err != nil {
1617
fmt.Fprintln(rootCmd.OutOrStderr(), err)
1718
os.Exit(1)
1819
}

simapp/v2/simdv2/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
clientv2helpers "cosmossdk.io/client/v2/helpers"
78
"cosmossdk.io/core/transaction"
89
serverv2 "cosmossdk.io/server/v2"
910
"cosmossdk.io/simapp/v2"
@@ -12,7 +13,7 @@ import (
1213

1314
func main() {
1415
rootCmd := cmd.NewRootCmd[transaction.Tx]()
15-
if err := serverv2.Execute(rootCmd, "", simapp.DefaultNodeHome); err != nil {
16+
if err := serverv2.Execute(rootCmd, clientv2helpers.EnvPrefix, simapp.DefaultNodeHome); err != nil {
1617
fmt.Fprintln(rootCmd.OutOrStderr(), err)
1718
os.Exit(1)
1819
}

0 commit comments

Comments
 (0)