Replies: 10 comments 2 replies
-
Original reply by @verdverm in cuelang/cue#836 (comment) related: cuelang/cue#403 This may be helpful as an alternative way to do this: https://cuelang.org/docs/concepts/packages/#instances |
Beta Was this translation helpful? Give feedback.
-
Original reply by @bergkvist in cuelang/cue#836 (comment) Thanks @verdverm So I have to create a cue-module to import other local cue-files? Ideally I'd want my folder structure to reflect what I'm trying to accomplish, rather than which tools I'm using. It seems like cue modules force a very opinionated folder structure. |
Beta Was this translation helpful? Give feedback.
-
Original reply by @verdverm in cuelang/cue#836 (comment) The above is one possible way to accomplish what you'd like among many. You can organize your directories any way you like with imports. To import across directories, a boundary to the import paths is needed (a.k.a. setting up a module, which is basically just the I wrote an example here that shows some other ways: https://cuetorials.com/first-steps/modules-and-packages/ If you don't want to use packages, you should be able to list all paths to be merged at the command line. |
Beta Was this translation helpful? Give feedback.
-
Original reply by @mpvl in cuelang/cue#836 (comment)
We are thinking about expanding the importing possibilities a bit. However, in order for CUE to remain meaningfully hermetic, we would still require a cue module. The main purpose of the module is to mark some root. There is not much more to it. We are considering allowing relative paths for individual files (non-package CUE, JSON or YAML), but in this case it seems that the standard package mechanism would work. Also, keep in mind that for the relative imports, we would still need a CUE module to deal with versioning. Not just for CUE. YAML, for instance, has a history of introducing backwards-incompatible changes and even allows deviations within a single version. The module allows us to add any meta-interpretation needed to properly interpret YAML. Even JSON can have compatibilities issues. We do intend to keep modules light-weight, though, so that this does not need to be burdensome. |
Beta Was this translation helpful? Give feedback.
-
Original reply by @bergkvist in cuelang/cue#836 (comment) The way it works is not really intuitive to me. I can't figure out how to import another cue file in the same folder - even after wrapping the two files in a module. What I've tried: cue mod init example.com
echo "b: 2" > cue.mod/usr/b.cue
echo 'import "example.com/usr/b"\na: 1' > cue.mod/usr/a.cue
cue eval cue.mod/usr/a.cue
# import failed: cannot find package "example.com/usr/b":
# ./cue.mod/usr/a.cue:2:8 Expected output:
Trying to add .cue extension to the import statement to see if it makes a difference: echo 'import "example.com/usr/b.cue"\na: 1' > cue.mod/usr/a.cue
cue eval cue.mod/usr/a.cue
# import failed: cannot determine package name for "example.com/usr/b.cue" (set explicitly with ':'):
# ./cue.mod/usr/a.cue:2:8 Okay, what is this package-thing? and why do I need it? echo 'package root\nb: 2' > cue.mod/usr/b.cue
echo 'package root\nimport "example.com/usr/b.cue:root"\na: 1' > cue.mod/usr/a.cue
cue eval cue.mod/usr/a.cue
# import failed: cannot find package "example.com/usr/b.cue:root":
# ./cue.mod/usr/a.cue:2:8 import formats I've tried: import "example.com/usr/b" # import failed: cannot find package "example.com/usr/b"
import "example.com/usr/b:root" # import failed: cannot determine package name for "example.com/usr/b.cue" (set explicitly with ':')
import "example.com/usr/b.cue:root" # import failed: cannot find package "example.com/usr/b.cue:root"
import "example.com/usr/b:root" # import failed: cannot find package "example.com/usr/b:root"
import "example.com/b" # import failed: cannot find package "example.com/b"
import "example.com/b.cue" # import failed: cannot determine package name for "example.com/b.cue" (set explicitly with ':')
import "example.com/b.cue:root" # import failed: cannot find package "example.com/b.cue:root"
import "example.com/b:root" # import failed: cannot find package "example.com/b:root" What am I doing wrong here? Some other questions:
I guess my difficulty getting this to work comes from me not being familiar enough with the module system in Golang, which it seems like Cue depends on. |
Beta Was this translation helpful? Give feedback.
-
Original reply by @myitcv in cuelang/cue#836 (comment) @bergkvist have you see the introduction to packages and modules at https://cuelang.org/docs/concepts/packages/? |
Beta Was this translation helpful? Give feedback.
-
Original reply by @bergkvist in cuelang/cue#836 (comment) Okay - I managed to at least get something working! A major misconception from my side here: I should not put my config files inside the cue.mod-folder, but rather next to it! Some more questions in my head right now:
The most minimal setup I've been able to create is:(click to expand) my-project/cue.mod/module.cue
my-project/a.cuepackage x
import b "example.com/b:y"
a: 1
b my-project/b/b.cue
And output:
|
Beta Was this translation helpful? Give feedback.
-
Original reply by @bergkvist in cuelang/cue#836 (comment) Making some mutations to my-project/a.cue that I expected not to make a difference:Note that the command run is always
^ Seems to work the same way still, which is nice
^ Just hangs forever without doing anything
^ import failed: cannot find package "example.com/b/b.cue:y"
^ imported and not used: "example.com/b:y"
^ import failed: build constraints exclude all CUE files in example.com/b (ignored: b/b.cue, a.cue) |
Beta Was this translation helpful? Give feedback.
-
Original reply by @myitcv in cuelang/cue#836 (comment)
Correct. The existence of a
CUE does not support importing of individual CUE files. It might support embedding of individual CUE files, but that's not currently supported and quite a different concept. CUE heavily relies on its order independence for package organisation. Definitions and constraints can be split across files within a package, and even organised across directories. So in that sense a package abstracts away whether a definition/constraint exists in one file, or is split across multiple (in multiple directories). Reasons like this are why CUE has decided to follow the Go model of abstracting this to the concept of a package.
To be able to import the CUE you are defining, yes. Where you have a CUE file(s) that is not imported elsewhere, you don't strictly need it, but generally it's good practice to think in terms of packages. Because that way the Adapting your minimal example slightly:
(note the format of expressing multiple files like this is Within the root of the module you can simply run I'm going to migrate this issue to a discussion so that others can benefit from these great questions. |
Beta Was this translation helpful? Give feedback.
-
Original reply by @eonpatapon in cuelang/cue#836 (comment)
For this you could use build tags see (
When evaluating you choose which file (prod.cue or stg.cue) to include:
|
Beta Was this translation helpful? Give feedback.
-
Originally opened by @bergkvist in cuelang/cue#836
Is your feature request related to a problem? Please describe.
I want to be able to import/include other cue-files directly using a relative path. Example:
Then I could run:
to produce two different outputs that share something in common.
In particular I want to use this pattern with Kubernetes configs to create something like:
staging.cue
production.cue
deployment.cue
Where staging.cue and production.cue both include deployment.cue.
I don't want production.cue and staging.cue to be loaded at the same time.
The include-statement could be implemented like "#include" in the C-preprocessor, simply inserting the contents of the included file directly into where the include-keyword was used.
I think one of the main issues with "#include" in the C-preprocessor was that it is not idempotent (by default anyways), and that order matters. It seems to me like neither of these things are issues in Cue, since definitions are idempotent and order-independent.
Beta Was this translation helpful? Give feedback.
All reactions