diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index d30063e2..3d053469 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1757,6 +1757,7 @@ func TestRunLocalWithoutArgs(t *testing.T) { {"run_4", true, "", "a: A package in vendor path"}, {"run_5", true, "", "kcl_6: KCL 6\na: sub6\nkcl_7: KCL 7\nb: sub7"}, {filepath.Join("run_6", "main"), true, "", "The_sub_kcl_program: Hello Sub World!\nThe_first_kcl_program: Hello World!"}, + {"run_7", true, "", "hello: Hello World!\nThe_first_kcl_program: Hello World!"}, } for _, test := range tests { diff --git a/pkg/client/run.go b/pkg/client/run.go index ffab514c..31250c82 100644 --- a/pkg/client/run.go +++ b/pkg/client/run.go @@ -337,7 +337,7 @@ func (o *RunOptions) applyCompileOptionsFromYaml(workdir string) bool { // transform the relative path to the absolute path in kcl.yaml by workdir var updatedKFilenameList []string for _, kfile := range o.KFilenameList { - if !filepath.IsAbs(kfile) { + if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) { kfile = filepath.Join(workdir, kfile) } updatedKFilenameList = append(updatedKFilenameList, kfile) @@ -354,7 +354,7 @@ func (o *RunOptions) applyCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) bool { var updatedKFilenameList []string // transform the relative path to the absolute path in kcl.yaml by kcl.mod path for _, kfile := range o.KFilenameList { - if !filepath.IsAbs(kfile) { + if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) { kfile = filepath.Join(kclPkg.HomePath, kfile) } updatedKFilenameList = append(updatedKFilenameList, kfile) @@ -374,11 +374,11 @@ func (o *RunOptions) applyCompileOptions(kclPkg *pkg.KclPkg, workDir string) err // All the cli relative path should be transformed to the absolute path by workdir for _, source := range o.Sources { if source.IsLocalPath() { - if filepath.IsAbs(source.Path) { - compiledFiles = append(compiledFiles, source.Path) - } else { - compiledFiles = append(compiledFiles, filepath.Join(workDir, source.Path)) + sPath := source.Path + if !filepath.IsAbs(sPath) && !utils.IsModRelativePath(sPath) { + sPath = filepath.Join(workDir, sPath) } + compiledFiles = append(compiledFiles, sPath) } } o.KFilenameList = compiledFiles diff --git a/pkg/client/test_data/test_run_options/no_args/run_7/kcl.mod b/pkg/client/test_data/test_run_options/no_args/run_7/kcl.mod new file mode 100644 index 00000000..b1ce16cf --- /dev/null +++ b/pkg/client/test_data/test_run_options/no_args/run_7/kcl.mod @@ -0,0 +1,10 @@ +[package] +name = "run_7" +edition = "v0.9.0" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.0" + +[profile] +entries = ["main.k", "${helloworld:KCL_MOD}/main.k"] diff --git a/pkg/client/test_data/test_run_options/no_args/run_7/kcl.mod.lock b/pkg/client/test_data/test_run_options/no_args/run_7/kcl.mod.lock new file mode 100644 index 00000000..7b80eba3 --- /dev/null +++ b/pkg/client/test_data/test_run_options/no_args/run_7/kcl.mod.lock @@ -0,0 +1,5 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.0" + version = "0.1.0" diff --git a/pkg/client/test_data/test_run_options/no_args/run_7/main.k b/pkg/client/test_data/test_run_options/no_args/run_7/main.k new file mode 100644 index 00000000..9d8d829a --- /dev/null +++ b/pkg/client/test_data/test_run_options/no_args/run_7/main.k @@ -0,0 +1 @@ +hello = 'Hello World!' \ No newline at end of file diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 20e75809..5dd9380d 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -306,6 +306,14 @@ func DirExists(path string) bool { return err == nil } +const ModRelativePathPattern = `^\$\{[^}]+:KCL_MOD\}/main\.k$` + +// If the path preffix is `${KCL_MOD}` or `${KCL_MOD:xxx}` +func IsModRelativePath(s string) bool { + re := regexp.MustCompile(ModRelativePathPattern) + return re.MatchString(s) +} + // MoveFile will move the file from 'src' to 'dest'. // On windows, it will copy the file from 'src' to 'dest', and then delete the file under 'src'. // On unix-like systems, it will rename the file from 'src' to 'dest'. @@ -590,4 +598,4 @@ func AbsTarPath(tarPath string) (string, error) { } return absTarPath, nil -} \ No newline at end of file +}