Skip to content

Commit 54dd3a2

Browse files
authored
Merge pull request #443 from zong-zhe/fix-run-bug
fix: fix kcl.yaml missing bug when kcl run
2 parents 1467eaa + 859064b commit 54dd3a2

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

pkg/client/client_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,45 @@ func TestRunLocalWithArgs(t *testing.T) {
18501850
}, []string{
18511851
filepath.Join(pkgPath, "with_args", "run_11", "sub", "kcl.yaml"),
18521852
}, filepath.Join(pkgPath, "with_args", "run_11"), false, "", "The_sub_kcl_program: Hello Sub World!"},
1853+
{
1854+
[]string{filepath.Join(pkgPath, "with_args", "run_0", "main.k")}, []string{}, filepath.Join(pkgPath, "with_args", "run_0"),
1855+
false, "", "The_first_kcl_program: Hello World!"},
1856+
{
1857+
[]string{filepath.Join(pkgPath, "with_args", "run_1", "main.k")}, []string{}, filepath.Join(pkgPath, "with_args", "run_1"),
1858+
false, "", "The_first_kcl_program: Hello World!"},
1859+
{[]string{
1860+
filepath.Join(pkgPath, "with_args", "run_2", "base.k"),
1861+
filepath.Join(pkgPath, "with_args", "run_2", "main.k"),
1862+
}, []string{}, "", false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
1863+
{[]string{
1864+
filepath.Join(pkgPath, "with_args", "run_3", "main.k"),
1865+
}, []string{}, "", false, "", "The_first_kcl_program: Hello World!"},
1866+
{[]string{
1867+
filepath.Join(pkgPath, "with_args", "run_4", "main.k"),
1868+
}, []string{}, "", false, "", "The_first_kcl_program: Hello World!"},
1869+
{[]string{
1870+
filepath.Join(pkgPath, "with_args", "run_5"),
1871+
}, []string{}, "", false, "", "The_first_kcl_program: Hello World!"},
1872+
{[]string{
1873+
filepath.Join(pkgPath, "with_args", "run_6"),
1874+
}, []string{}, "", false, "", "The_first_kcl_program: Hello World!"},
1875+
{[]string{
1876+
filepath.Join(pkgPath, "with_args", "run_7"),
1877+
}, []string{}, "", false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
1878+
{[]string{
1879+
filepath.Join(pkgPath, "with_args", "run_8"),
1880+
}, []string{}, "", false, "", "sub: SUB"},
1881+
{[]string{
1882+
filepath.Join(pkgPath, "with_args", "run_9"),
1883+
}, []string{}, "", false, "", "The_sub_kcl_program: Hello Sub World!"},
1884+
{[]string{}, []string{
1885+
filepath.Join(pkgPath, "with_args", "run_10", "sub", "kcl.yaml"),
1886+
}, "", false, "", "The_sub_kcl_program_1: Hello Sub World 1!"},
1887+
{[]string{
1888+
filepath.Join(pkgPath, "with_args", "run_11", "sub", "sub.k"),
1889+
}, []string{
1890+
filepath.Join(pkgPath, "with_args", "run_11", "sub", "kcl.yaml"),
1891+
}, "", false, "", "The_sub_kcl_program: Hello Sub World!"},
18531892
}
18541893

18551894
for _, test := range tests {

pkg/client/run.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,31 @@ func getCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) *kcl.Option {
378378
func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.KclPkg, workDir string) error {
379379
o.Merge(kcl.WithWorkDir(workDir))
380380

381+
// Check if the source is a local path and is the kcl package.
382+
sourceIsKclPackage := func(pkgSource *downloader.Source) bool {
383+
if !pkgSource.IsLocalPath() {
384+
return false
385+
}
386+
sourcePath := pkgSource.Path
387+
if !filepath.IsAbs(sourcePath) && !utils.IsModRelativePath(sourcePath) {
388+
sourcePath = filepath.Join(workDir, sourcePath)
389+
}
390+
391+
pkgHome := kclPkg.HomePath
392+
393+
if !filepath.IsAbs(pkgHome) && !utils.IsModRelativePath(sourcePath) {
394+
pkgHome = filepath.Join(workDir, pkgHome)
395+
}
396+
397+
return sourcePath == pkgHome
398+
}
399+
381400
// If the sources from cli is not empty, use the sources from cli.
382401
if len(o.Sources) != 0 {
383402
var compiledFiles []string
384403
// All the cli relative path should be transformed to the absolute path by workdir
385404
for _, source := range o.Sources {
386-
if source.IsLocalPath() && source.Path != kclPkg.HomePath {
405+
if source.IsLocalPath() && !sourceIsKclPackage(source) {
387406
sPath := source.Path
388407
if !filepath.IsAbs(sPath) && !utils.IsModRelativePath(sPath) {
389408
sPath = filepath.Join(workDir, sPath)
@@ -402,7 +421,7 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
402421
var yamlOpts *kcl.Option
403422
// For the packaged kcl package, git, oci and tar, settings yaml file should be find from the package path if not set by cli.
404423
// For the local kcl package, settings yaml file should be find from the workdir if not set by cli.
405-
if source.IsPackaged() {
424+
if source.IsPackaged() || sourceIsKclPackage(&source) {
406425
yamlOpts = o.getCompileOptionsFromYaml(kclPkg.HomePath)
407426
} else {
408427
yamlOpts = o.getCompileOptionsFromYaml(workDir)
@@ -431,7 +450,7 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
431450
if len(o.KFilenameList) == 0 {
432451
// 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.
433452
// For the local kcl package, no *.k files are set by cli, kcl.yaml or kcl.mod, compile the workdir.
434-
if source.IsPackaged() {
453+
if source.IsPackaged() || sourceIsKclPackage(&source) {
435454
o.KFilenameList = []string{kclPkg.HomePath}
436455
} else {
437456
o.KFilenameList = []string{workDir}

0 commit comments

Comments
 (0)