From 0e133ae6dbe998bdcfad4aee0a33caceda23bf7c Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Sun, 15 Mar 2026 21:50:26 +0100 Subject: [PATCH] fix(path): fixing the default compose file check --- internal/generator/converter.go | 19 +++++++++++++++---- internal/parser/main.go | 11 ++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/internal/generator/converter.go b/internal/generator/converter.go index ca823e8..afde74c 100644 --- a/internal/generator/converter.go +++ b/internal/generator/converter.go @@ -109,8 +109,19 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) error { // the current working directory is the directory currentDir, _ := os.Getwd() + // Filter to only existing files before chdir + var existingFiles []string + for _, f := range dockerComposeFile { + if _, err := os.Stat(f); err == nil { + existingFiles = append(existingFiles, f) + } + } + if len(existingFiles) == 0 && len(dockerComposeFile) > 0 { + return fmt.Errorf("no compose file found: %v", dockerComposeFile) + } + // go to the root of the project - if err := os.Chdir(filepath.Dir(dockerComposeFile[0])); err != nil { + if err := os.Chdir(filepath.Dir(existingFiles[0])); err != nil { logger.Failure(err.Error()) return err } @@ -122,12 +133,12 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) error { }() // repove the directory part of the docker-compose files - for i, f := range dockerComposeFile { - dockerComposeFile[i] = filepath.Base(f) + for i, f := range existingFiles { + existingFiles[i] = filepath.Base(f) } // parse the compose files - project, err := parser.Parse(config.Profiles, config.EnvFiles, dockerComposeFile...) + project, err := parser.Parse(config.Profiles, config.EnvFiles, existingFiles...) if err != nil { logger.Failure("Cannot parse compose files", err.Error()) return err diff --git a/internal/parser/main.go b/internal/parser/main.go index 6b9ad8d..c3a8cd7 100644 --- a/internal/parser/main.go +++ b/internal/parser/main.go @@ -41,16 +41,21 @@ func Parse(profiles []string, envFiles []string, dockerComposeFile ...string) (* } } - options, err := cli.NewProjectOptions(dockerComposeFile, + opts := []cli.ProjectOptionsFn{ cli.WithProfiles(profiles), cli.WithInterpolation(true), - cli.WithDefaultConfigPath, cli.WithEnvFiles(envFiles...), cli.WithOsEnv, cli.WithDotEnv, cli.WithNormalization(true), cli.WithResolvedPaths(false), - ) + } + + if len(dockerComposeFile) == 0 { + opts = append(opts, cli.WithDefaultConfigPath) + } + + options, err := cli.NewProjectOptions(dockerComposeFile, opts...) if err != nil { return nil, err }