kcl-lang / kcl-lang.io

KCL Website and Documentation Repo: https://kcl-lang.io
https://kcl-lang.github.io
Apache License 2.0
13 stars 35 forks source link

[FAQ] Error when trying to create and use a plugin: Need some clarifications please #432

Closed soubinan closed 1 month ago

soubinan commented 1 month ago

The issue encountered

Hello, I was not sure if this one should go to the BUG category or no. Could someone confirm the procedure to create and use a plugin, please ? For python (for golang if python is not possible). I checked the doc (from v0.4 to v0.9) but it seems something is missing...

I followed both the golang and the python examples, but none of them worked for me, and always receiving the same error message as shared below:

Error: error[E2F04]: CannotFindModule
 --> /test.k:1:1
  |
1 | import kcl_plugin.my_plugin
  | ^ the plugin package `kcl_plugin.my_plugin` is not found, please confirm if plugin mode is enabled
  |

In some document version we are supposed to use kcl_plugin (but not explained how to install it) In some other I found it is an alias of the kclvm.tools.plugin python package In some (the more recent), there is no mention of these steps None of all versions I checked is mentioning what is the plugin mode and how to enable it (but it is mentioned it was disabled by default for python, then not sure if it has been re-enabled or no)

But all share the same thing, none of those procedures works for me. It is a bit confusing and lead me to many questions:

Additional info

I am currently using the latest kcl version:

$ kclvm_cli version
Version: 0.9.2-c020ab3eb4b9179219d6837a57f5d323
Platform: x86_64-unknown-linux-gnu
GitCommit: VERGEN_IDEMPOTENT_OUTPUT

$ kcl version
0.9.4-linux-amd64

Thanks !

Peefy commented 1 month ago

Hello @soubinan

https://www.kcl-lang.io/docs/reference/plugin/overview

According to the latest documentation of KCL v0.9, it is currently supported to use Go, Python, and Java to write plugins for KCL, but they need to be used in the corresponding Go SDK, Python SDK, or Java SDK, such as for the Go SDK.

package main

import (
    "fmt"

    "kcl-lang.io/kcl-go/pkg/kcl"
    "kcl-lang.io/kcl-go/pkg/native"                // Import the native API
    _ "kcl-lang.io/kcl-go/pkg/plugin/hello_plugin" // Import the hello plugin
)

func main() {
    // Note we use `native.MustRun` here instead of `kcl.MustRun`, because it needs the cgo feature.
    yaml := native.MustRun("main.k", kcl.WithCode(code)).GetRawYamlResult()
    fmt.Println(yaml)
}

const code = `
import kcl_plugin.hello

name = "kcl"
three = hello.add(1,2)  # hello.add is written by Go
`

The plugin location is here. https://github.com/kcl-lang/kcl-plugin/tree/main/hello

There are some other examples:

soubinan commented 1 month ago

Hello @Peefy, thank you for your feedback I checked the examples your shared and tried my best to have a working example, still at the same point with questions neither the exemples or the documentation answer.

Peefy commented 1 month ago

Thank you for your detailed reply. Currently, plugins are only supported in the SDK for various languages such as Python, Go and Java. If you want to use plugins in the CLI, the plugin author would need to release a new version of the KCL plugin that includes the plugin for CLI usage. The KCL does not yet support automated management and registration of plugins, but this may be supported in a future version.

Just as an example, I will show you how to use Go and Python to create and publish KCL CLI with plugins.

https://github.com/kcl-lang/cli/pull/120

Just like Go, you can write Python code to call KCL Python API for extension.

Peefy commented 1 month ago

In addition, with package management in languages such as Go and Python, you can store these packages anywhere you like by registering them before calling the KCL API.

soubinan commented 1 month ago

Thank you @Peefy I finally got it! Very grateful for your help!