Repository: docker/compose
File: cmd/compose/compose.go (Go)

Code before:
	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...), //nolint:gocritic // intentionally applying cli.WithEnvFiles twice.
		cli.WithDotEnv,                  //nolint:gocritic // intentionally applying cli.WithDotEnv twice.
		// 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
const PluginName = "compose"

// RunningAsStandalone detects when running as a standalone program
func RunningAsStandalone() bool {
	return len(os.Args) < 2 || os.Args[1] != metadata.MetadataSubcommandName && os.Args[1] != PluginName
}

type BackendOptions struct {
	Options []compose.Option
}

func (o *BackendOptions) Add(option compose.Option) {
	o.Options = append(o.Options, option)
}

// RootCommand returns the compose command with its child commands
func RootCommand(dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command { //nolint:gocyclo
	// filter out useless commandConn.CloseWrite warning message that can occur
	// when using a remote context that is unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
	// https://github.com/docker/cli/blob/e1f24d3c93df6752d3c27c8d61d18260f141310c/cli/connhelper/commandconn/commandconn.go#L203-L215
	logrus.AddHook(logutil.NewFilter([]logrus.Level{
		logrus.WarnLevel,

Review comment (suggestion):
From the comments, I _THINK_ it's intentional to apply these options multiple times, but if not, I can update and remove these

Code after:
	opts := []cli.ProjectOptionsFn{
		cli.WithWorkingDirectory(o.ProjectDir),
		// First apply os.Environment, always win
		cli.WithOsEnv,
	}

	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...), //nolint:gocritic // intentionally applying cli.WithEnvFiles twice.
		cli.WithDotEnv,                  //nolint:gocritic // intentionally applying cli.WithDotEnv twice.
		// 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
const PluginName = "compose"

// RunningAsStandalone detects when running as a standalone program
func RunningAsStandalone() bool {
	return len(os.Args) < 2 || os.Args[1] != metadata.MetadataSubcommandName && os.Args[1] != PluginName
}

type BackendOptions struct {
	Options []compose.Option
}

func (o *BackendOptions) Add(option compose.Option) {
	o.Options = append(o.Options, option)
}

// RootCommand returns the compose command with its child commands