Skip to content

Commit

Permalink
Merge pull request #468 from zong-zhe/fix-run-bug
Browse files Browse the repository at this point in the history
fix: fix run bug when compile single k files without kcl.mod
  • Loading branch information
Peefy committed Aug 28, 2024
2 parents 655022e + b8b2343 commit 51b9862
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
10 changes: 7 additions & 3 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,11 +898,11 @@ func TestTestResolveMetadataInJsonStrWithPackage(t *testing.T) {
assert.Equal(t, err, nil)

assert.Equal(t, res, string(expectedDepStr))

defer func() {
err = os.RemoveAll(vendorDir)
assert.Equal(t, err, nil)
} ()
}()
}

func TestPkgWithInVendorMode(t *testing.T) {
Expand Down Expand Up @@ -1212,7 +1212,7 @@ func TestRunWithGitPackage(t *testing.T) {
assert.Equal(t, err, nil)
expectedCompileResult := `{"apiVersion": "v1", "kind": "Pod", "metadata": {"name": "web-app"}, "spec": {"containers": [{"image": "nginx", "name": "main-container", "ports": [{"containerPort": 80}]}]}}`
assert.Equal(t, expectedCompileResult, compileResult.GetRawJsonResult())

assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)

defer func() {
Expand Down Expand Up @@ -2130,6 +2130,10 @@ func TestRunLocalWithArgs(t *testing.T) {
}, []string{
filepath.Join(pkgPath, "with_args", "run_11", "sub", "kcl.yaml"),
}, "", false, "", "The_sub_kcl_program: Hello Sub World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_12", "sub1", "main.k"),
filepath.Join(pkgPath, "with_args", "run_12", "sub2", "main.k"),
}, []string{}, "", false, "", "sub1: 1\nsub2: 2"},
}

for _, test := range tests {
Expand Down
3 changes: 2 additions & 1 deletion pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ func (o *RunOptions) getPkgSource() (*downloader.Source, error) {
)
}

if !pkgSource.IsPackaged() && !source.IsPackaged() {
if !pkgSource.IsPackaged() && !source.IsPackaged() &&
pkgSource.IsLocalPkg() && source.IsLocalPkg() {
tmpRootPath, err := source.FindRootPath()
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub1 = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub2 = 2
8 changes: 8 additions & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func (source *Source) IsPackaged() bool {
return source.IsLocalTarPath() || source.Git != nil || source.Oci != nil || source.Registry != nil
}

// If the source is a local path, check if it is a real local package(a directory with kcl.mod file).
func (source *Source) IsLocalPkg() bool {
if source.IsLocalPath() {
return utils.DirExists(filepath.Join(source.Local.Path, constants.KCL_MOD))
}
return false
}

func (source *Source) FindRootPath() (string, error) {
if source == nil {
return "", fmt.Errorf("source is nil")
Expand Down

0 comments on commit 51b9862

Please sign in to comment.