From ce463d50b2327c09a22d487454b55dfecfff90f8 Mon Sep 17 00:00:00 2001 From: Kian Eliasi Date: Wed, 8 Oct 2025 10:23:30 +0330 Subject: [PATCH] Fix: set PWD only if not set Signed-off-by: Kian Eliasi --- cmd/compose/compose.go | 54 +++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index da3c7def2..5a1772080 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -383,32 +383,38 @@ func (o *ProjectOptions) remoteLoaders(dockerCli command.Cli) []loader.ResourceL } func (o *ProjectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.ProjectOptions, error) { - pwd, err := os.Getwd() - if err != nil { - return nil, err + opts := []cli.ProjectOptionsFn{ + cli.WithWorkingDirectory(o.ProjectDir), + // First apply os.Environment, always win + cli.WithOsEnv, } - return cli.NewProjectOptions(o.ConfigPaths, - append(po, - cli.WithWorkingDirectory(o.ProjectDir), - // First apply os.Environment, always win - cli.WithOsEnv, - // set PWD as this variable is not consistently supported on Windows - cli.WithEnv([]string{"PWD=" + pwd}), - // Load PWD/.env if present and no explicit --env-file has been set - cli.WithEnvFiles(o.EnvFiles...), - // read dot env file to populate project environment - cli.WithDotEnv, - // get compose file path set by COMPOSE_FILE - cli.WithConfigFileEnv, - // if none was selected, get default compose.yaml file from current dir or parent folder - cli.WithDefaultConfigPath, - // .. and then, a project directory != PWD maybe has been set so let's load .env file - cli.WithEnvFiles(o.EnvFiles...), - cli.WithDotEnv, - // eventually COMPOSE_PROFILES should have been set - cli.WithDefaultProfiles(o.Profiles...), - cli.WithName(o.ProjectName))...) + if _, present := os.LookupEnv("PWD"); !present { + if pwd, err := os.Getwd(); err != nil { + return nil, err + } else { + opts = append(opts, cli.WithEnv([]string{"PWD=" + pwd})) + } + } + + opts = append(opts, + // Load PWD/.env if present and no explicit --env-file has been set + cli.WithEnvFiles(o.EnvFiles...), + // read dot env file to populate project environment + cli.WithDotEnv, + // get compose file path set by COMPOSE_FILE + cli.WithConfigFileEnv, + // if none was selected, get default compose.yaml file from current dir or parent folder + cli.WithDefaultConfigPath, + // .. and then, a project directory != PWD maybe has been set so let's load .env file + cli.WithEnvFiles(o.EnvFiles...), + cli.WithDotEnv, + // eventually COMPOSE_PROFILES should have been set + cli.WithDefaultProfiles(o.Profiles...), + cli.WithName(o.ProjectName), + ) + + return cli.NewProjectOptions(o.ConfigPaths, append(po, opts...)...) } // PluginName is the name of the plugin