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

Question on using .instances() #1880

Open
steeling opened this issue Mar 6, 2025 · 6 comments
Open

Question on using .instances() #1880

steeling opened this issue Mar 6, 2025 · 6 comments

Comments

@steeling
Copy link

steeling commented Mar 6, 2025

We have a setup where we have many specs.Containers objects spread across many files.

I'd like to add a script, where I can run kcl render.k, which consumes all the containers, and outputs a single object (either to stdout, or to a file). My issue is that to get this to work, I'm using globbing all kcl files such that the script calls:

kcl file1.k file2.k file3.k ... fileN.k render.k

This seems to have some issues:

  1. It's very slow, and I have suspicion it's evaluating some things twice.
  2. File scoped variables become global scoped, ie: 2 separate files in 2 separate directories with a top level myField variable, will now result in a conflict
  3. specs.Containers.instances() works within render.k, but if I have another file that is not part of the glob, but maybe indirectly imported that also attempts to find all containers, it doesn't. Which leads me to believe there are some further scoping oddities.

I might be abusing the intent of running kcl, is there a better way to do what I want to accomplish? Let me know if I can provide more info on this :)

@spastorclovr
Copy link

+1 on instances reporting the double amount of instances actually created.
This is what I am experimenting.

@Peefy
Copy link
Contributor

Peefy commented Mar 7, 2025

cc @zong-zhe @He1pa

@He1pa
Copy link
Contributor

He1pa commented Mar 10, 2025

For question 2, according to the design of kcl, all files in kcl file1.k file2.k file3.k ... fileN.k render.k are regarded as the entry of compilation. So their variables are all glob,

For question 3, can you provide more details? like example code

@steeling
Copy link
Author

Gotcha, I'll try to get an example out tomorrow.

In the meantime, is there a better way to get all instances in a set of directories without globbing everything together, and without explicitly importing each file? I'd like a way where someone can add in a new specs.Container, and we automatically consume it

@He1pa
Copy link
Contributor

He1pa commented Mar 10, 2025

Gotcha, I'll try to get an example out tomorrow.

In the meantime, is there a better way to get all instances in a set of directories without globbing everything together, and without explicitly importing each file? I'd like a way where someone can add in a new specs.Container, and we automatically consume it

put the .k files into a directory and import it in main.k?

@steeling
Copy link
Author

looked into this a bit more. Let's say I have 100+ files. It's not very desirable to put this into a single directory, as then I need to coordinate a bunch of name conflicts, and I've noticed rendering slows down a lot.

Another option is to autogenerate an imports file, like

import configs.specs
import configs.package1
import configs.package2
import configs.package3
import configs.package4
...

containers = specs.Containers.instances()

However instances only searches the local package. It seems there's no mechanism to auto retrieve/glob instances of an object type without putting them all into one directory, which becomes somewhat of an anti pattern at a larger scale.

Any recommendations on how to proceed here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants