Skip to content

Commit

Permalink
Merge pull request #445 from zong-zhe/fix-run-bug-09
Browse files Browse the repository at this point in the history
fix: fix kcl.yaml missing bug when kcl run
  • Loading branch information
Peefy authored Aug 12, 2024
2 parents 683d248 + dec6f4e commit 3aed38f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
39 changes: 39 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,45 @@ func TestRunLocalWithArgs(t *testing.T) {
}, []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!"},
{
[]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")}, []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"),
}, []string{}, "", false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_3", "main.k"),
}, []string{}, "", false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_4", "main.k"),
}, []string{}, "", false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_5"),
}, []string{}, "", false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_6"),
}, []string{}, "", false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_7"),
}, []string{}, "", false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_8"),
}, []string{}, "", false, "", "sub: SUB"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_9"),
}, []string{}, "", false, "", "The_sub_kcl_program: Hello Sub World!"},
{[]string{}, []string{
filepath.Join(pkgPath, "with_args", "run_10", "sub", "kcl.yaml"),
}, "", 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"),
}, "", false, "", "The_sub_kcl_program: Hello Sub World!"},
}

for _, test := range tests {
Expand Down
25 changes: 22 additions & 3 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,31 @@ func getCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) *kcl.Option {
func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.KclPkg, workDir string) error {
o.Merge(kcl.WithWorkDir(workDir))

// Check if the source is a local path and is the kcl package.
sourceIsKclPackage := func(pkgSource *downloader.Source) bool {
if !pkgSource.IsLocalPath() {
return false
}
sourcePath := pkgSource.Path
if !filepath.IsAbs(sourcePath) && !utils.IsModRelativePath(sourcePath) {
sourcePath = filepath.Join(workDir, sourcePath)
}

pkgHome := kclPkg.HomePath

if !filepath.IsAbs(pkgHome) && !utils.IsModRelativePath(sourcePath) {
pkgHome = filepath.Join(workDir, pkgHome)
}

return sourcePath == pkgHome
}

// If the sources from cli is not empty, use the sources from cli.
if len(o.Sources) != 0 {
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() && source.Path != kclPkg.HomePath {
if source.IsLocalPath() && !sourceIsKclPackage(source) {
sPath := source.Path
if !filepath.IsAbs(sPath) && !utils.IsModRelativePath(sPath) {
sPath = filepath.Join(workDir, sPath)
Expand All @@ -402,7 +421,7 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
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() {
if source.IsPackaged() || sourceIsKclPackage(&source) {
yamlOpts = o.getCompileOptionsFromYaml(kclPkg.HomePath)
} else {
yamlOpts = o.getCompileOptionsFromYaml(workDir)
Expand Down Expand Up @@ -431,7 +450,7 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
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() {
if source.IsPackaged() || sourceIsKclPackage(&source) {
o.KFilenameList = []string{kclPkg.HomePath}
} else {
o.KFilenameList = []string{workDir}
Expand Down

0 comments on commit 3aed38f

Please sign in to comment.