microsoft / go-sqlcmd

The new sqlcmd, CLI for SQL Server and Azure SQL (winget install sqlcmd / sqlcmd create mssql / sqlcmd open ads)
https://learn.microsoft.com/sql/tools/sqlcmd/go-sqlcmd-utility
MIT License
334 stars 58 forks source link

panic on no home dir #279

Closed karataliu closed 1 year ago

karataliu commented 1 year ago
30:58[W] panic: $HOME is not defined
30:58[W]
30:58[W] goroutine 1 [running]:
30:58[W] github.com/microsoft/go-sqlcmd/internal/config.init.0.func1({0xdc2740?, 0xc0003b7910?})
30:58[W] /home/vsts/work/1/s/internal/config/initialize.go:21 +0x3a
30:58[W] github.com/microsoft/go-sqlcmd/internal/config.checkErr(...)
30:58[W] /home/vsts/work/1/s/internal/config/error.go:9
30:58[W] github.com/microsoft/go-sqlcmd/internal/config.DefaultFileName()

This is a breaking change in recent versions, previous version doesn't have a requirement on HOME dir.

apoorvdeshmukh commented 1 year ago

Can you describe what command was run to reproduce this? If you do not have a $HOME defined then you can specify alternate location for the sqlcmd config file via --sqlconfig flag at the runtime as a workaround

stuartpa commented 1 year ago

Is this related to the change to use sqlconfig when sqlcmd is run without any params?

stuartpa commented 1 year ago

This is failing in the golang runtime function:

os.UserHomeDir()

// UserHomeDir returns the current user's home directory.
//
// On Unix, including macOS, it returns the $HOME environment variable.
// On Windows, it returns %USERPROFILE%.
// On Plan 9, it returns the $home environment variable.
func UserHomeDir() (string, error) {
    env, enverr := "HOME", "$HOME"
    switch runtime.GOOS {
    case "windows":
        env, enverr = "USERPROFILE", "%userprofile%"
    case "plan9":
        env, enverr = "home", "$home"
    }
    if v := Getenv(env); v != "" {
        return v, nil
    }
    // On some geese the home directory is not always defined.
    switch runtime.GOOS {
    case "android":
        return "/sdcard", nil
    case "ios":
        return "/", nil
    }
    return "", errors.New(enverr + " is not defined")
}

I am curious, how is it possible to not have a $HOME dir? Does the shell in this case have any envvar that is the equiv of $HOME, if so, we could fix the golang function.

But I agree, looks like we need to handle the situation where there is no $HOME (or Window equiv.).

We are calling os.UserHomeDir() to generate the Flag help (because we set --sqlconfig to default in this dir).

karataliu commented 1 year ago

Thanks for the fix.

In the env of a non-interactivate shell, is possible that HOME is not present. I found this issue when using Azure VM custom script extension.