diff --git a/internal/virtualenvironment/virtualenvironment.go b/internal/virtualenvironment/virtualenvironment.go index 2edc976b4e..055bc890fc 100644 --- a/internal/virtualenvironment/virtualenvironment.go +++ b/internal/virtualenvironment/virtualenvironment.go @@ -42,14 +42,12 @@ func (v *VirtualEnvironment) GetEnv(inherit bool, useExecutors bool, projectDir, } if projectDir != "" { - configFile := filepath.Join(projectDir, constants.ConfigFileName) - envMap[constants.ActivatedStateEnvVarName] = projectDir - envMap[constants.ProjectEnvVarName] = configFile envMap[constants.ActivatedStateIDEnvVarName] = v.activationID envMap[constants.ActivatedStateNamespaceEnvVarName] = namespace // Get project from explicitly defined configuration file + configFile := filepath.Join(projectDir, constants.ConfigFileName) pj, err := project.Parse(configFile) if err != nil { return envMap, locale.WrapError(err, "err_parse_project", "", configFile) diff --git a/pkg/projectfile/projectfile.go b/pkg/projectfile/projectfile.go index 977835feb9..cf2e749387 100644 --- a/pkg/projectfile/projectfile.go +++ b/pkg/projectfile/projectfile.go @@ -752,6 +752,7 @@ func GetProjectFilePath() (string, error) { lookup := []func() (string, error){ getProjectFilePathFromEnv, getProjectFilePathFromWd, + getProjectFilePathFromShell, getProjectFilePathFromDefault, } for _, getProjectFilePath := range lookup { @@ -792,6 +793,18 @@ func getProjectFilePathFromWd() (string, error) { return path, nil } +func getProjectFilePathFromShell() (string, error) { + projectFilePath := os.Getenv(constants.ActivatedStateEnvVarName) + if projectFilePath != "" { + configFile := filepath.Join(projectFilePath, constants.ConfigFileName) + if fileutils.FileExists(configFile) { + return configFile, nil + } + return "", &ErrorNoProject{locale.NewInputError("err_shell_no_projectfile", "The project for the shell you are in no longer exists.")} + } + return "", nil +} + func getProjectFilePathFromDefault() (_ string, rerr error) { cfg, err := config.New() if err != nil { diff --git a/pkg/projectfile/projectfile_test.go b/pkg/projectfile/projectfile_test.go index cf1fd8f2d0..1b596023cc 100644 --- a/pkg/projectfile/projectfile_test.go +++ b/pkg/projectfile/projectfile_test.go @@ -236,6 +236,14 @@ func TestGetProjectFilePath(t *testing.T) { assert.Equal(t, expectedPath, configPath, "Project path is properly detected using the ProjectEnvVarName") os.Unsetenv(constants.ProjectEnvVarName) + + os.Setenv(constants.ActivatedStateEnvVarName, "/some/path") + defer os.Unsetenv(constants.ActivatedStateEnvVarName) + configPath, err = GetProjectFilePath() + require.Nil(t, err) + assert.Equal(t, expectedPath, configPath, "Project path prioritizes working directory project over activated project") + os.Unsetenv(constants.ActivatedStateEnvVarName) + cfg, err := config.New() require.NoError(t, err) defer func() { require.NoError(t, cfg.Close()) }() @@ -246,6 +254,11 @@ func TestGetProjectFilePath(t *testing.T) { os.Chdir(tmpDir) _, err = GetProjectFilePath() assert.Error(t, err, "GetProjectFilePath should fail") + os.Setenv(constants.ActivatedStateEnvVarName, filepath.Dir(expectedPath)) + configPath, err = GetProjectFilePath() + require.Nil(t, err) + assert.Equal(t, expectedPath, configPath, "Project path falls back on activated project path") + os.Unsetenv(constants.ActivatedStateEnvVarName) cfg.Set(constants.GlobalDefaultPrefname, expectedPath) configPath, err = GetProjectFilePath() assert.NoError(t, err, "GetProjectFilePath should succeed")