Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: supports the mod relative path in api #381

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pkg/client/test_data/test_run_options/no_args/run_7/kcl.mod
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.0"
version = "0.1.0"
1 change: 1 addition & 0 deletions pkg/client/test_data/test_run_options/no_args/run_7/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello = 'Hello World!'
10 changes: 9 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -590,4 +598,4 @@ func AbsTarPath(tarPath string) (string, error) {
}

return absTarPath, nil
}
}
Loading