fix(python): do not stop at pyenv fail

adds more logging for #2569
This commit is contained in:
Jan De Dobbeleer 2022-07-26 07:28:54 +02:00 committed by Jan De Dobbeleer
parent 000602f28a
commit b5b35d1456
5 changed files with 33 additions and 19 deletions

View file

@ -416,7 +416,14 @@ func (env *ShellEnvironment) HasFolder(folder string) bool {
}
func (env *ShellEnvironment) ResolveSymlink(path string) (string, error) {
return filepath.EvalSymlinks(path)
defer env.Trace(time.Now(), "ResolveSymlink", path)
link, err := filepath.EvalSymlinks(path)
if err != nil {
env.Log(Error, "ResolveSymlink", err.Error())
return "", err
}
env.Log(Debug, "ResolveSymlink", link)
return link, nil
}
func (env *ShellEnvironment) FileContent(file string) string {
@ -505,18 +512,21 @@ func (env *ShellEnvironment) RunShellCommand(shell, command string) string {
}
func (env *ShellEnvironment) CommandPath(command string) string {
defer env.Trace(time.Now(), "HasCommand", command)
defer env.Trace(time.Now(), "CommandPath", command)
if path, ok := env.cmdCache.get(command); ok {
env.Log(Debug, "CommandPath", path)
return path
}
path, err := exec.LookPath(command)
if err == nil {
env.cmdCache.set(command, path)
env.Log(Debug, "CommandPath", path)
return path
}
path, err = env.LookWinAppPath(command)
if err == nil {
env.cmdCache.set(command, path)
env.Log(Debug, "CommandPath", path)
return path
}
env.Log(Error, "CommandPath", err.Error())
@ -524,6 +534,7 @@ func (env *ShellEnvironment) CommandPath(command string) string {
}
func (env *ShellEnvironment) HasCommand(command string) bool {
defer env.Trace(time.Now(), "HasCommand", command)
if path := env.CommandPath(command); path != "" {
return true
}

View file

@ -53,7 +53,7 @@ func TestGolang(t *testing.T) {
ParseModFile: true,
HasModFileInParentDir: true,
InvalidModfile: true,
ExpectedString: "../go.mod:1: unknown directive: invalid",
ExpectedString: "1.16",
Version: "go version go1.16 darwin/amd64",
},
}

View file

@ -171,7 +171,7 @@ func (l *language) setVersion() error {
} else {
versionStr, err = command.getVersion()
if err != nil {
return err
continue
}
}
if versionStr == "" {

View file

@ -1,6 +1,8 @@
package segments
import (
"errors"
"fmt"
"oh-my-posh/environment"
"oh-my-posh/properties"
"path/filepath"
@ -102,36 +104,36 @@ func (p *Python) pyenvVersion() (string, error) {
// Is our Python executable at $PYENV_ROOT/bin/python ?
// Should p.env expose command paths?
path := p.env.CommandPath("python")
if path == "" {
if len(path) == 0 {
path = p.env.CommandPath("python3")
}
if path == "" {
return "", nil
if len(path) == 0 {
return "", errors.New("no python executable found")
}
pyEnvRoot := p.env.Getenv("PYENV_ROOT")
// TODO: pyenv-win has this at $PYENV_ROOT/pyenv-win/shims
if path != filepath.Join(p.env.Getenv("PYENV_ROOT"), "shims", "python") {
return "", nil
if path != filepath.Join(pyEnvRoot, "shims", "python") {
return "", fmt.Errorf("executable at %s is not a pyenv shim", path)
}
// pyenv version-name will return current version or virtualenv
cmdOutput, err := p.env.RunCommand("pyenv", "version-name")
if err != nil {
// TODO: Improve reporting
return "", nil
return "", err
}
versionString := strings.Split(cmdOutput, ":")[0]
if versionString == "" {
return "", nil
if len(versionString) == 0 {
return "", errors.New("no pyenv version-name found")
}
// $PYENV_ROOT/versions + versionString (symlinks resolved) == $PYENV_ROOT/versions/(version)[/envs/(virtualenv)]
realPath, err := p.env.ResolveSymlink(filepath.Join(p.env.Getenv("PYENV_ROOT"), "versions", versionString))
realPath, err := p.env.ResolveSymlink(filepath.Join(pyEnvRoot, "versions", versionString))
if err != nil {
return "", nil
return "", err
}
// ../versions/(version)[/envs/(virtualenv)]
shortPath, err := filepath.Rel(filepath.Join(p.env.Getenv("PYENV_ROOT"), "versions"), realPath)
shortPath, err := filepath.Rel(filepath.Join(pyEnvRoot, "versions"), realPath)
if err != nil {
return "", nil
return "", err
}
// Unset whatever loadContext thinks Venv should be
p.Venv = ""

View file

@ -8,6 +8,7 @@ import (
"testing"
"github.com/alecthomas/assert"
mock2 "github.com/stretchr/testify/mock"
)
func TestPythonTemplate(t *testing.T) {
@ -70,7 +71,7 @@ func TestPythonTemplate(t *testing.T) {
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("HasCommand", "python").Return(true)
env.On("CommandPath", "mock.Anything").Return(tc.PythonPath)
env.On("CommandPath", mock2.Anything).Return(tc.PythonPath)
env.On("RunCommand", "python", []string{"--version"}).Return("Python 3.8.4", nil)
env.On("RunCommand", "pyenv", []string{"version-name"}).Return(tc.VirtualEnvName, nil)
env.On("HasFiles", "*.py").Return(true)
@ -81,7 +82,7 @@ func TestPythonTemplate(t *testing.T) {
env.On("PathSeparator").Return("")
env.On("Pwd").Return("/usr/home/project")
env.On("Home").Return("/usr/home")
env.On("ResolveSymlink", "mock.Anything").Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err)
env.On("ResolveSymlink", mock2.Anything).Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err)
props := properties.Map{
properties.FetchVersion: tc.FetchVersion,
UsePythonVersionFile: true,