Skip to content

Commit a110766

Browse files
authored
Merge pull request #439 from zong-zhe/fix-run-workdir
fix: fix missing options in kcl run
2 parents 3bd93ec + 63eaf4e commit a110766

File tree

14 files changed

+111
-50
lines changed

14 files changed

+111
-50
lines changed

pkg/client/client_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,48 +1804,58 @@ func TestRunLocalWithArgs(t *testing.T) {
18041804
kpmcli.SetLogWriter(logbuf)
18051805

18061806
tests := []struct {
1807-
inputs []string
1808-
workdir string
1809-
withVendor bool
1810-
diagnostic string
1811-
expected string
1807+
inputs []string
1808+
settingsFiles []string
1809+
workdir string
1810+
withVendor bool
1811+
diagnostic string
1812+
expected string
18121813
}{
18131814
{
1814-
[]string{filepath.Join(pkgPath, "with_args", "run_0", "main.k")}, filepath.Join(pkgPath, "with_args", "run_0"),
1815+
[]string{filepath.Join(pkgPath, "with_args", "run_0", "main.k")}, []string{}, filepath.Join(pkgPath, "with_args", "run_0"),
18151816
false, "", "The_first_kcl_program: Hello World!"},
18161817
{
1817-
[]string{filepath.Join(pkgPath, "with_args", "run_1", "main.k")}, filepath.Join(pkgPath, "with_args", "run_1"),
1818+
[]string{filepath.Join(pkgPath, "with_args", "run_1", "main.k")}, []string{}, filepath.Join(pkgPath, "with_args", "run_1"),
18181819
false, "", "The_first_kcl_program: Hello World!"},
18191820
{[]string{
18201821
filepath.Join(pkgPath, "with_args", "run_2", "base.k"),
18211822
filepath.Join(pkgPath, "with_args", "run_2", "main.k"),
1822-
}, filepath.Join(pkgPath, "with_args", "run_2"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
1823+
}, []string{}, filepath.Join(pkgPath, "with_args", "run_2"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
18231824
{[]string{
18241825
filepath.Join(pkgPath, "with_args", "run_3", "main.k"),
1825-
}, filepath.Join(pkgPath, "with_args", "run_3"), false, "", "The_first_kcl_program: Hello World!"},
1826+
}, []string{}, filepath.Join(pkgPath, "with_args", "run_3"), false, "", "The_first_kcl_program: Hello World!"},
18261827
{[]string{
18271828
filepath.Join(pkgPath, "with_args", "run_4", "main.k"),
1828-
}, filepath.Join(pkgPath, "with_args", "run_4"), false, "", "The_first_kcl_program: Hello World!"},
1829+
}, []string{}, filepath.Join(pkgPath, "with_args", "run_4"), false, "", "The_first_kcl_program: Hello World!"},
18291830
{[]string{
18301831
filepath.Join(pkgPath, "with_args", "run_5"),
1831-
}, filepath.Join(pkgPath, "with_args", "run_5"), false, "", "The_first_kcl_program: Hello World!"},
1832+
}, []string{}, filepath.Join(pkgPath, "with_args", "run_5"), false, "", "The_first_kcl_program: Hello World!"},
18321833
{[]string{
18331834
filepath.Join(pkgPath, "with_args", "run_6"),
1834-
}, filepath.Join(pkgPath, "with_args", "run_6"), false, "", "The_first_kcl_program: Hello World!"},
1835+
}, []string{}, filepath.Join(pkgPath, "with_args", "run_6"), false, "", "The_first_kcl_program: Hello World!"},
18351836
{[]string{
18361837
filepath.Join(pkgPath, "with_args", "run_7"),
1837-
}, filepath.Join(pkgPath, "with_args", "run_7"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
1838+
}, []string{}, filepath.Join(pkgPath, "with_args", "run_7"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
18381839
{[]string{
18391840
filepath.Join(pkgPath, "with_args", "run_8"),
1840-
}, filepath.Join(pkgPath, "with_args", "run_8"), false, "", "The_first_kcl_program: Hello World!"},
1841+
}, []string{}, filepath.Join(pkgPath, "with_args", "run_8"), false, "", "sub: SUB"},
18411842
{[]string{
18421843
filepath.Join(pkgPath, "with_args", "run_9"),
1843-
}, filepath.Join(pkgPath, "with_args", "run_9"), false, "", "The_first_kcl_program: Hello World!"},
1844+
}, []string{}, filepath.Join(pkgPath, "with_args", "run_9"), false, "", "The_sub_kcl_program: Hello Sub World!"},
1845+
{[]string{}, []string{
1846+
filepath.Join(pkgPath, "with_args", "run_10", "sub", "kcl.yaml"),
1847+
}, filepath.Join(pkgPath, "with_args", "run_10"), false, "", "The_sub_kcl_program_1: Hello Sub World 1!"},
1848+
{[]string{
1849+
filepath.Join(pkgPath, "with_args", "run_11", "sub", "sub.k"),
1850+
}, []string{
1851+
filepath.Join(pkgPath, "with_args", "run_11", "sub", "kcl.yaml"),
1852+
}, filepath.Join(pkgPath, "with_args", "run_11"), false, "", "The_sub_kcl_program: Hello Sub World!"},
18441853
}
18451854

18461855
for _, test := range tests {
18471856
res, err := kpmcli.Run(
18481857
WithRunSourceUrls(test.inputs),
1858+
WithSettingFiles(test.settingsFiles),
18491859
WithWorkDir(test.workdir),
18501860
)
18511861

pkg/client/run.go

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -326,52 +326,52 @@ func WithVendor(vendor bool) RunOption {
326326
}
327327

328328
// applyCompileOptionsFromYaml applies the compile options from the kcl.yaml file.
329-
func (o *RunOptions) applyCompileOptionsFromYaml(workdir string) bool {
330-
succeed := false
329+
func (o *RunOptions) getCompileOptionsFromYaml(workdir string) *kcl.Option {
330+
resOpts := kcl.NewOption()
331+
var settingsYamlDir string
331332
// load the kcl.yaml from cli
332333
if len(o.settingYamlFiles) != 0 {
333334
for _, settingYamlFile := range o.settingYamlFiles {
334-
o.Merge(kcl.WithSettings(settingYamlFile))
335-
succeed = true
335+
settingsYamlDir = filepath.Dir(settingYamlFile)
336+
resOpts.Merge(kcl.WithSettings(settingYamlFile))
336337
}
337338
} else {
338339
// load the kcl.yaml from the workdir
339340
// If the workdir is not empty, try to find the settings.yaml file in the workdir.
340-
settingsYamlPath := filepath.Join(workdir, constants.KCL_YAML)
341+
settingsYamlDir = workdir
342+
settingsYamlPath := filepath.Join(settingsYamlDir, constants.KCL_YAML)
341343
if utils.DirExists(settingsYamlPath) {
342-
o.Merge(kcl.WithSettings(settingsYamlPath))
343-
succeed = true
344+
resOpts.Merge(kcl.WithSettings(settingsYamlPath))
344345
}
345346
}
346347

347348
// transform the relative path to the absolute path in kcl.yaml by workdir
348349
var updatedKFilenameList []string
349-
for _, kfile := range o.KFilenameList {
350+
for _, kfile := range resOpts.KFilenameList {
350351
if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) {
351-
kfile = filepath.Join(workdir, kfile)
352+
kfile = filepath.Join(settingsYamlDir, kfile)
352353
}
353354
updatedKFilenameList = append(updatedKFilenameList, kfile)
354355
}
355-
o.KFilenameList = updatedKFilenameList
356+
resOpts.KFilenameList = updatedKFilenameList
356357

357-
return succeed
358+
return resOpts
358359
}
359360

360361
// applyCompileOptionsFromKclMod applies the compile options from the kcl.mod file.
361-
func (o *RunOptions) applyCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) bool {
362-
o.Merge(*kclPkg.GetKclOpts())
363-
362+
func getCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) *kcl.Option {
363+
resOpts := kcl.NewOption()
364+
resOpts.Merge(*kclPkg.GetKclOpts())
364365
var updatedKFilenameList []string
365366
// transform the relative path to the absolute path in kcl.yaml by kcl.mod path
366-
for _, kfile := range o.KFilenameList {
367+
for _, kfile := range resOpts.KFilenameList {
367368
if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) {
368369
kfile = filepath.Join(kclPkg.HomePath, kfile)
369370
}
370371
updatedKFilenameList = append(updatedKFilenameList, kfile)
371372
}
372-
o.KFilenameList = updatedKFilenameList
373-
374-
return len(o.KFilenameList) != 0
373+
resOpts.KFilenameList = updatedKFilenameList
374+
return resOpts
375375
}
376376

377377
// 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
383383
var compiledFiles []string
384384
// All the cli relative path should be transformed to the absolute path by workdir
385385
for _, source := range o.Sources {
386-
if source.IsLocalPath() {
386+
if source.IsLocalPath() && source.Path != kclPkg.HomePath {
387387
sPath := source.Path
388388
if !filepath.IsAbs(sPath) && !utils.IsModRelativePath(sPath) {
389389
sPath = filepath.Join(workDir, sPath)
@@ -392,23 +392,49 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
392392
}
393393
}
394394
o.KFilenameList = compiledFiles
395-
if len(o.KFilenameList) == 0 {
396-
if !o.applyCompileOptionsFromKclMod(kclPkg) {
397-
o.KFilenameList = []string{kclPkg.HomePath}
398-
}
399-
}
395+
}
396+
397+
cliOpts := o.Option
398+
// Get the compile options from kcl.mod
399+
modOpts := getCompileOptionsFromKclMod(kclPkg)
400+
401+
// Get the compile options from kcl.yaml
402+
var yamlOpts *kcl.Option
403+
// For the packaged kcl package, git, oci and tar, settings yaml file should be find from the package path if not set by cli.
404+
// For the local kcl package, settings yaml file should be find from the workdir if not set by cli.
405+
if source.IsPackaged() {
406+
yamlOpts = o.getCompileOptionsFromYaml(kclPkg.HomePath)
400407
} else {
401-
// If the sources from cli is empty, try to apply the compile options from kcl.yaml
402-
if !o.applyCompileOptionsFromYaml(workDir) {
403-
// If the sources from kcl.yaml is empty, try to apply the compile options from kcl.mod
404-
if !o.applyCompileOptionsFromKclMod(kclPkg) {
405-
// If the sources from kcl.mod is empty, compile the current sources.
406-
if source.IsLocalPath() {
407-
o.KFilenameList = []string{workDir}
408-
} else {
409-
o.KFilenameList = []string{kclPkg.HomePath}
410-
}
411-
}
408+
yamlOpts = o.getCompileOptionsFromYaml(workDir)
409+
}
410+
411+
// Merge the compile options from cli, kcl.mod and kcl.yaml
412+
// The options from cli will override the options from kcl.mod and kcl.yaml
413+
// The options from kcl.yaml will override the options from kcl.mod
414+
o.Option = kcl.NewOption()
415+
o.Merge(*modOpts).Merge(*yamlOpts).Merge(*cliOpts)
416+
if len(modOpts.KFilenameList) != 0 {
417+
o.KFilenameList = modOpts.KFilenameList
418+
}
419+
420+
if len(yamlOpts.KFilenameList) != 0 {
421+
o.KFilenameList = yamlOpts.KFilenameList
422+
}
423+
424+
if len(cliOpts.KFilenameList) != 0 {
425+
o.KFilenameList = cliOpts.KFilenameList
426+
}
427+
428+
// There is no compiled files from cli, kcl.mod and kcl.yaml,
429+
// use the workdir as input.
430+
// the workdir is the dictionary which executes `kcl run`
431+
if len(o.KFilenameList) == 0 {
432+
// 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.
433+
// For the local kcl package, no *.k files are set by cli, kcl.yaml or kcl.mod, compile the workdir.
434+
if source.IsPackaged() {
435+
o.KFilenameList = []string{kclPkg.HomePath}
436+
} else {
437+
o.KFilenameList = []string{workDir}
412438
}
413439
}
414440

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "run_10"
3+
edition = "v0.10.0"
4+
version = "0.0.1"
5+
6+

pkg/client/test_data/test_run_options/with_args/run_10/kcl.mod.lock

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kcl_cli_configs:
2+
files:
3+
- ../sub1/sub.k
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_sub_kcl_program = "Hello Sub World!"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_sub_kcl_program_1 = "Hello Sub World 1!"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "run_11"
3+
edition = "v0.10.0"
4+
version = "0.0.1"
5+
6+
[profile]
7+
entries = ["sub/sub.k"]

pkg/client/test_data/test_run_options/with_args/run_11/kcl.mod.lock

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kcl_cli_configs:
2+
files:
3+
- ../sub1/sub.k
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_sub_kcl_program = "Hello Sub World!"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_sub_kcl_program_1 = "Hello Sub World 1!"

0 commit comments

Comments
 (0)