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

fix(client/v2): use (PREFIX)_HOME instead of NODE_HOME #20964

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/v2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* [#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)).

### Bug Fixes

* [#20964](https://github.com/cosmos/cosmos-sdk/pull/20964) Fix `GetNodeHomeDirectory` helper in `client/v2/helpers` to respect the `(PREFIX)_HOME` environment variable.

## [v2.0.0-beta.3] - 2024-07-15

### Features
Expand Down
18 changes: 15 additions & 3 deletions client/v2/helpers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import (
"strings"
)

// EnvPrefix is the prefix for environment variables that are used by the CLI.
// It should match the one used for viper in the CLI.
var EnvPrefix = ""

// GetNodeHomeDirectory gets the home directory of the node (where the config is located).
// It parses the home flag if set if the `NODE_HOME` environment variable if set (and ignores name).
// It parses the home flag if set if the `(PREFIX)_HOME` environment variable if set (and ignores name).
// When no prefix is set, it reads the `NODE_HOME` environment variable.
// Otherwise, it returns the default home directory given its name.
func GetNodeHomeDirectory(name string) (string, error) {
// get the home directory from the flag
Expand All @@ -21,12 +26,19 @@ func GetNodeHomeDirectory(name string) (string, error) {
}

// get the home directory from the environment variable
homeDir := os.Getenv("NODE_HOME")
// to not clash with the $HOME system variable, when no prefix is set
// we check the NODE_HOME environment variable
homeDir, envHome := "", "HOME"
if len(EnvPrefix) > 0 {
homeDir = os.Getenv(EnvPrefix + "_" + envHome)
} else {
homeDir = os.Getenv("NODE_" + envHome)
Copy link
Member Author

@julienrbrt julienrbrt Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solves the bug that effectively if you want to set another home using $HOME, you always get the directory called .simapp. This only happens when you do not use a prefix, and the command home clashes with system home (on linux and macos)

HOME=~/download/.simapp simd config home 
/home/user/download/.simapp/.simapp

If we make the env check on home, earlier then we would have had

simd config home
/home/user

So the fix is using node home if you want to specify another folder than simapp when you have no prefix

NODE_HOME=~/download/simappv2 simd config home
/home/user/download/simappv2

and

simd config home
/home/user/.simapp

}
if homeDir != "" {
return filepath.Clean(homeDir), nil
}

// return the default home directory
// get user home directory
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion docs/learn/advanced/07-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ this will be more convenient:

```shell
# define env variables in .env, .envrc etc
NODE_HOME=<path to home>
GAIA_HOME=<path to home>
GAIA_NODE=<node address>
GAIA_CHAIN_ID="testchain-1"
GAIA_KEYRING_BACKEND="test"
Expand Down
3 changes: 2 additions & 1 deletion simapp/simd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

clientv2helpers "cosmossdk.io/client/v2/helpers"
"cosmossdk.io/simapp"
"cosmossdk.io/simapp/simd/cmd"

Expand All @@ -12,7 +13,7 @@ import (

func main() {
rootCmd := cmd.NewRootCmd()
if err := svrcmd.Execute(rootCmd, "", simapp.DefaultNodeHome); err != nil {
if err := svrcmd.Execute(rootCmd, clientv2helpers.EnvPrefix, simapp.DefaultNodeHome); err != nil {
fmt.Fprintln(rootCmd.OutOrStderr(), err)
os.Exit(1)
}
Expand Down
3 changes: 2 additions & 1 deletion simapp/v2/simdv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

clientv2helpers "cosmossdk.io/client/v2/helpers"
"cosmossdk.io/core/transaction"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/simapp/v2"
Expand All @@ -12,7 +13,7 @@ import (

func main() {
rootCmd := cmd.NewRootCmd[serverv2.AppI[transaction.Tx], transaction.Tx]()
if err := serverv2.Execute(rootCmd, "", simapp.DefaultNodeHome); err != nil {
if err := serverv2.Execute(rootCmd, clientv2helpers.EnvPrefix, simapp.DefaultNodeHome); err != nil {
fmt.Fprintln(rootCmd.OutOrStderr(), err)
os.Exit(1)
}
Expand Down
Loading