From 71785d67da221e543a8d98c7f56f241c543c2cf2 Mon Sep 17 00:00:00 2001 From: mitchell Date: Thu, 7 Dec 2023 16:39:54 -0500 Subject: [PATCH] When determining the current project, prefer the activated project before anything else. --- pkg/projectfile/projectfile.go | 10 +++++++++- pkg/projectfile/projectfile_test.go | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/projectfile/projectfile.go b/pkg/projectfile/projectfile.go index 223d2af4bb..59fbe68735 100644 --- a/pkg/projectfile/projectfile.go +++ b/pkg/projectfile/projectfile.go @@ -779,13 +779,21 @@ func GetProjectFilePath() (string, error) { } func getProjectFilePathFromEnv() (string, error) { - projectFilePath := os.Getenv(constants.ProjectEnvVarName) + var projectFilePath string + + if activatedProjectDirPath := os.Getenv(constants.ActivatedStateEnvVarName); activatedProjectDirPath != "" { + projectFilePath = filepath.Join(activatedProjectDirPath, constants.ConfigFileName) + } else { + projectFilePath = os.Getenv(constants.ProjectEnvVarName) + } + if projectFilePath != "" { if fileutils.FileExists(projectFilePath) { return projectFilePath, nil } return "", &ErrorNoProjectFromEnv{locale.NewInputError("err_project_env_file_not_exist", "", projectFilePath)} } + return "", nil } diff --git a/pkg/projectfile/projectfile_test.go b/pkg/projectfile/projectfile_test.go index 03ede03f45..6f2e497e07 100644 --- a/pkg/projectfile/projectfile_test.go +++ b/pkg/projectfile/projectfile_test.go @@ -222,6 +222,7 @@ func TestGetProjectFilePath(t *testing.T) { require.Nil(t, err) expectedPath := filepath.Join(root, "pkg", "projectfile", "testdata", constants.ConfigFileName) assert.Equal(t, expectedPath, configPath, "Project path is properly detected") + os.Chdir(cwd) // restore defer os.Unsetenv(constants.ProjectEnvVarName) @@ -251,6 +252,14 @@ func TestGetProjectFilePath(t *testing.T) { configPath, err = GetProjectFilePath() assert.NoError(t, err, "GetProjectFilePath should succeed") assert.Equal(t, expectedPath, configPath, "Project path is properly detected using default path from config") + + // The activestate.yaml for an activated project should be used no matter what. + defer os.Unsetenv(constants.ActivatedStateEnvVarName) + os.Setenv(constants.ActivatedStateEnvVarName, filepath.Dir(expectedPath)) + configPath, err = GetProjectFilePath() + require.Nil(t, err) + assert.Equal(t, expectedPath, configPath, "Project path is properly detected using the ActivatedStateEnvVarName") + os.Unsetenv(constants.ActivatedStateEnvVarName) } // TestGet the config