From de5e036ab965ab8ce546679a20cc32d6830450bd Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 8 Aug 2024 17:40:22 +0800 Subject: [PATCH] fix: fix missing options in kcl run Signed-off-by: zongz --- pkg/client/client_test.go | 40 +++++--- pkg/client/run.go | 96 ++++++++++++------- .../test_run_options/with_args/run_10/kcl.mod | 6 ++ .../with_args/run_10/kcl.mod.lock | 0 .../test_run_options/with_args/run_10/main.k | 1 + .../with_args/run_10/sub/kcl.yaml | 3 + .../with_args/run_10/sub/sub.k | 1 + .../with_args/run_10/sub1/sub.k | 1 + .../test_run_options/with_args/run_11/kcl.mod | 7 ++ .../with_args/run_11/kcl.mod.lock | 0 .../test_run_options/with_args/run_11/main.k | 1 + .../with_args/run_11/sub/kcl.yaml | 3 + .../with_args/run_11/sub/sub.k | 1 + .../with_args/run_11/sub1/sub.k | 1 + 14 files changed, 111 insertions(+), 50 deletions(-) create mode 100644 pkg/client/test_data/test_run_options/with_args/run_10/kcl.mod create mode 100644 pkg/client/test_data/test_run_options/with_args/run_10/kcl.mod.lock create mode 100644 pkg/client/test_data/test_run_options/with_args/run_10/main.k create mode 100644 pkg/client/test_data/test_run_options/with_args/run_10/sub/kcl.yaml create mode 100644 pkg/client/test_data/test_run_options/with_args/run_10/sub/sub.k create mode 100644 pkg/client/test_data/test_run_options/with_args/run_10/sub1/sub.k create mode 100644 pkg/client/test_data/test_run_options/with_args/run_11/kcl.mod create mode 100644 pkg/client/test_data/test_run_options/with_args/run_11/kcl.mod.lock create mode 100644 pkg/client/test_data/test_run_options/with_args/run_11/main.k create mode 100644 pkg/client/test_data/test_run_options/with_args/run_11/sub/kcl.yaml create mode 100644 pkg/client/test_data/test_run_options/with_args/run_11/sub/sub.k create mode 100644 pkg/client/test_data/test_run_options/with_args/run_11/sub1/sub.k diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index ddeb2425..e8ce6670 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1804,48 +1804,58 @@ func TestRunLocalWithArgs(t *testing.T) { kpmcli.SetLogWriter(logbuf) tests := []struct { - inputs []string - workdir string - withVendor bool - diagnostic string - expected string + inputs []string + settingsFiles []string + workdir string + withVendor bool + diagnostic string + expected string }{ { - []string{filepath.Join(pkgPath, "with_args", "run_0", "main.k")}, filepath.Join(pkgPath, "with_args", "run_0"), + []string{filepath.Join(pkgPath, "with_args", "run_0", "main.k")}, []string{}, filepath.Join(pkgPath, "with_args", "run_0"), false, "", "The_first_kcl_program: Hello World!"}, { - []string{filepath.Join(pkgPath, "with_args", "run_1", "main.k")}, filepath.Join(pkgPath, "with_args", "run_1"), + []string{filepath.Join(pkgPath, "with_args", "run_1", "main.k")}, []string{}, filepath.Join(pkgPath, "with_args", "run_1"), false, "", "The_first_kcl_program: Hello World!"}, {[]string{ filepath.Join(pkgPath, "with_args", "run_2", "base.k"), filepath.Join(pkgPath, "with_args", "run_2", "main.k"), - }, filepath.Join(pkgPath, "with_args", "run_2"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"}, + }, []string{}, filepath.Join(pkgPath, "with_args", "run_2"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"}, {[]string{ filepath.Join(pkgPath, "with_args", "run_3", "main.k"), - }, filepath.Join(pkgPath, "with_args", "run_3"), false, "", "The_first_kcl_program: Hello World!"}, + }, []string{}, filepath.Join(pkgPath, "with_args", "run_3"), false, "", "The_first_kcl_program: Hello World!"}, {[]string{ filepath.Join(pkgPath, "with_args", "run_4", "main.k"), - }, filepath.Join(pkgPath, "with_args", "run_4"), false, "", "The_first_kcl_program: Hello World!"}, + }, []string{}, filepath.Join(pkgPath, "with_args", "run_4"), false, "", "The_first_kcl_program: Hello World!"}, {[]string{ filepath.Join(pkgPath, "with_args", "run_5"), - }, filepath.Join(pkgPath, "with_args", "run_5"), false, "", "The_first_kcl_program: Hello World!"}, + }, []string{}, filepath.Join(pkgPath, "with_args", "run_5"), false, "", "The_first_kcl_program: Hello World!"}, {[]string{ filepath.Join(pkgPath, "with_args", "run_6"), - }, filepath.Join(pkgPath, "with_args", "run_6"), false, "", "The_first_kcl_program: Hello World!"}, + }, []string{}, filepath.Join(pkgPath, "with_args", "run_6"), false, "", "The_first_kcl_program: Hello World!"}, {[]string{ filepath.Join(pkgPath, "with_args", "run_7"), - }, filepath.Join(pkgPath, "with_args", "run_7"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"}, + }, []string{}, filepath.Join(pkgPath, "with_args", "run_7"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"}, {[]string{ filepath.Join(pkgPath, "with_args", "run_8"), - }, filepath.Join(pkgPath, "with_args", "run_8"), false, "", "The_first_kcl_program: Hello World!"}, + }, []string{}, filepath.Join(pkgPath, "with_args", "run_8"), false, "", "sub: SUB"}, {[]string{ filepath.Join(pkgPath, "with_args", "run_9"), - }, filepath.Join(pkgPath, "with_args", "run_9"), false, "", "The_first_kcl_program: Hello World!"}, + }, []string{}, filepath.Join(pkgPath, "with_args", "run_9"), false, "", "The_sub_kcl_program: Hello Sub World!"}, + {[]string{}, []string{ + filepath.Join(pkgPath, "with_args", "run_10", "sub", "kcl.yaml"), + }, filepath.Join(pkgPath, "with_args", "run_10"), false, "", "The_sub_kcl_program_1: Hello Sub World 1!"}, + {[]string{ + filepath.Join(pkgPath, "with_args", "run_11", "sub", "sub.k"), + }, []string{ + filepath.Join(pkgPath, "with_args", "run_11", "sub", "kcl.yaml"), + }, filepath.Join(pkgPath, "with_args", "run_11"), false, "", "The_sub_kcl_program: Hello Sub World!"}, } for _, test := range tests { res, err := kpmcli.Run( WithRunSourceUrls(test.inputs), + WithSettingFiles(test.settingsFiles), WithWorkDir(test.workdir), ) diff --git a/pkg/client/run.go b/pkg/client/run.go index dcddbd44..e52be324 100644 --- a/pkg/client/run.go +++ b/pkg/client/run.go @@ -326,52 +326,52 @@ func WithVendor(vendor bool) RunOption { } // applyCompileOptionsFromYaml applies the compile options from the kcl.yaml file. -func (o *RunOptions) applyCompileOptionsFromYaml(workdir string) bool { - succeed := false +func (o *RunOptions) getCompileOptionsFromYaml(workdir string) *kcl.Option { + resOpts := kcl.NewOption() + var settingsYamlDir string // load the kcl.yaml from cli if len(o.settingYamlFiles) != 0 { for _, settingYamlFile := range o.settingYamlFiles { - o.Merge(kcl.WithSettings(settingYamlFile)) - succeed = true + settingsYamlDir = filepath.Dir(settingYamlFile) + resOpts.Merge(kcl.WithSettings(settingYamlFile)) } } else { // load the kcl.yaml from the workdir // If the workdir is not empty, try to find the settings.yaml file in the workdir. - settingsYamlPath := filepath.Join(workdir, constants.KCL_YAML) + settingsYamlDir = workdir + settingsYamlPath := filepath.Join(settingsYamlDir, constants.KCL_YAML) if utils.DirExists(settingsYamlPath) { - o.Merge(kcl.WithSettings(settingsYamlPath)) - succeed = true + resOpts.Merge(kcl.WithSettings(settingsYamlPath)) } } // transform the relative path to the absolute path in kcl.yaml by workdir var updatedKFilenameList []string - for _, kfile := range o.KFilenameList { + for _, kfile := range resOpts.KFilenameList { if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) { - kfile = filepath.Join(workdir, kfile) + kfile = filepath.Join(settingsYamlDir, kfile) } updatedKFilenameList = append(updatedKFilenameList, kfile) } - o.KFilenameList = updatedKFilenameList + resOpts.KFilenameList = updatedKFilenameList - return succeed + return resOpts } // applyCompileOptionsFromKclMod applies the compile options from the kcl.mod file. -func (o *RunOptions) applyCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) bool { - o.Merge(*kclPkg.GetKclOpts()) - +func getCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) *kcl.Option { + resOpts := kcl.NewOption() + resOpts.Merge(*kclPkg.GetKclOpts()) var updatedKFilenameList []string // transform the relative path to the absolute path in kcl.yaml by kcl.mod path - for _, kfile := range o.KFilenameList { + for _, kfile := range resOpts.KFilenameList { if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) { kfile = filepath.Join(kclPkg.HomePath, kfile) } updatedKFilenameList = append(updatedKFilenameList, kfile) } - o.KFilenameList = updatedKFilenameList - - return len(o.KFilenameList) != 0 + resOpts.KFilenameList = updatedKFilenameList + return resOpts } // applyCompileOptions applies the compile options from cli, kcl.yaml and kcl.mod. @@ -383,7 +383,7 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K var compiledFiles []string // All the cli relative path should be transformed to the absolute path by workdir for _, source := range o.Sources { - if source.IsLocalPath() { + if source.IsLocalPath() && source.Path != kclPkg.HomePath { sPath := source.Path if !filepath.IsAbs(sPath) && !utils.IsModRelativePath(sPath) { sPath = filepath.Join(workDir, sPath) @@ -392,23 +392,49 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K } } o.KFilenameList = compiledFiles - if len(o.KFilenameList) == 0 { - if !o.applyCompileOptionsFromKclMod(kclPkg) { - o.KFilenameList = []string{kclPkg.HomePath} - } - } + } + + cliOpts := o.Option + // Get the compile options from kcl.mod + modOpts := getCompileOptionsFromKclMod(kclPkg) + + // Get the compile options from kcl.yaml + var yamlOpts *kcl.Option + // For the packaged kcl package, git, oci and tar, settings yaml file should be find from the package path if not set by cli. + // For the local kcl package, settings yaml file should be find from the workdir if not set by cli. + if source.IsPackaged() { + yamlOpts = o.getCompileOptionsFromYaml(kclPkg.HomePath) } else { - // If the sources from cli is empty, try to apply the compile options from kcl.yaml - if !o.applyCompileOptionsFromYaml(workDir) { - // If the sources from kcl.yaml is empty, try to apply the compile options from kcl.mod - if !o.applyCompileOptionsFromKclMod(kclPkg) { - // If the sources from kcl.mod is empty, compile the current sources. - if source.IsLocalPath() { - o.KFilenameList = []string{workDir} - } else { - o.KFilenameList = []string{kclPkg.HomePath} - } - } + yamlOpts = o.getCompileOptionsFromYaml(workDir) + } + + // Merge the compile options from cli, kcl.mod and kcl.yaml + // The options from cli will override the options from kcl.mod and kcl.yaml + // The options from kcl.yaml will override the options from kcl.mod + o.Option = kcl.NewOption() + o.Merge(*modOpts).Merge(*yamlOpts).Merge(*cliOpts) + if len(modOpts.KFilenameList) != 0 { + o.KFilenameList = modOpts.KFilenameList + } + + if len(yamlOpts.KFilenameList) != 0 { + o.KFilenameList = yamlOpts.KFilenameList + } + + if len(cliOpts.KFilenameList) != 0 { + o.KFilenameList = cliOpts.KFilenameList + } + + // There is no compiled files from cli, kcl.mod and kcl.yaml, + // use the workdir as input. + // the workdir is the dictionary which executes `kcl run` + if len(o.KFilenameList) == 0 { + // For the packaged kcl package, git, oci and tar, no *.k files are set by cli, kcl.yaml or kcl.mod, compile the package path. + // For the local kcl package, no *.k files are set by cli, kcl.yaml or kcl.mod, compile the workdir. + if source.IsPackaged() { + o.KFilenameList = []string{kclPkg.HomePath} + } else { + o.KFilenameList = []string{workDir} } } diff --git a/pkg/client/test_data/test_run_options/with_args/run_10/kcl.mod b/pkg/client/test_data/test_run_options/with_args/run_10/kcl.mod new file mode 100644 index 00000000..dfd18a21 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_10/kcl.mod @@ -0,0 +1,6 @@ +[package] +name = "run_10" +edition = "v0.10.0" +version = "0.0.1" + + diff --git a/pkg/client/test_data/test_run_options/with_args/run_10/kcl.mod.lock b/pkg/client/test_data/test_run_options/with_args/run_10/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_run_options/with_args/run_10/main.k b/pkg/client/test_data/test_run_options/with_args/run_10/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_10/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/with_args/run_10/sub/kcl.yaml b/pkg/client/test_data/test_run_options/with_args/run_10/sub/kcl.yaml new file mode 100644 index 00000000..08271813 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_10/sub/kcl.yaml @@ -0,0 +1,3 @@ +kcl_cli_configs: + files: + - ../sub1/sub.k \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/with_args/run_10/sub/sub.k b/pkg/client/test_data/test_run_options/with_args/run_10/sub/sub.k new file mode 100644 index 00000000..d4971505 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_10/sub/sub.k @@ -0,0 +1 @@ +The_sub_kcl_program = "Hello Sub World!" \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/with_args/run_10/sub1/sub.k b/pkg/client/test_data/test_run_options/with_args/run_10/sub1/sub.k new file mode 100644 index 00000000..e0455a47 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_10/sub1/sub.k @@ -0,0 +1 @@ +The_sub_kcl_program_1 = "Hello Sub World 1!" \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/with_args/run_11/kcl.mod b/pkg/client/test_data/test_run_options/with_args/run_11/kcl.mod new file mode 100644 index 00000000..70da47f3 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_11/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "run_11" +edition = "v0.10.0" +version = "0.0.1" + +[profile] +entries = ["sub/sub.k"] \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/with_args/run_11/kcl.mod.lock b/pkg/client/test_data/test_run_options/with_args/run_11/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_run_options/with_args/run_11/main.k b/pkg/client/test_data/test_run_options/with_args/run_11/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_11/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/with_args/run_11/sub/kcl.yaml b/pkg/client/test_data/test_run_options/with_args/run_11/sub/kcl.yaml new file mode 100644 index 00000000..08271813 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_11/sub/kcl.yaml @@ -0,0 +1,3 @@ +kcl_cli_configs: + files: + - ../sub1/sub.k \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/with_args/run_11/sub/sub.k b/pkg/client/test_data/test_run_options/with_args/run_11/sub/sub.k new file mode 100644 index 00000000..d4971505 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_11/sub/sub.k @@ -0,0 +1 @@ +The_sub_kcl_program = "Hello Sub World!" \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/with_args/run_11/sub1/sub.k b/pkg/client/test_data/test_run_options/with_args/run_11/sub1/sub.k new file mode 100644 index 00000000..e0455a47 --- /dev/null +++ b/pkg/client/test_data/test_run_options/with_args/run_11/sub1/sub.k @@ -0,0 +1 @@ +The_sub_kcl_program_1 = "Hello Sub World 1!" \ No newline at end of file