Comments (10)
Yes, this is not documented yet. At least not properly, partly because it is very new. The cue tool (not language) assumes files are layed out in a directory tree. You can mark the root with an empty cue.mod file, which will ultimately hold more configuration. In that same directory, there can be a pkg directory.
import "path" looks for the directory rooted at pkg and loads all files in that directory as a single package, with the package name as the identifier (conventionally the base of the dir). If you know Go, this is pretty much exactly how Go works. And like Go, the first path component is a simple identifier for core packages, whereas for non-core packages it starts with a domain name.
The pkg directory is somewhat like a vendor directory. It is also the destination for code generation, though. For instance
cue get go k8s.io/api/extensions/v1beta1 github.com/prometheus/prometheus/pkg/rulefmt
will get down the respective go packages transitively, analyses how types would be marshalled with encoding/json (or yaml) and then generates CUE templates from that.
Generated files end with _gen.cue. Users may add other cue files in the same directories to augment the definitions, although ideally that should be done elsewhere.
from cue.
If somebody like me also stumbles about this issue and needs a quick fix. I hope the following change to the documentation helps https://cue-review.googlesource.com/c/cue/+/2601.
from cue.
I actually can't figure out how to perform imports the way I expect... Is it possible to have two files in some directory, foo.cue
and bar.cue
, and import foo.cue
from bar.cue
? When I try this naively, I get an error:
// foo.cue
a: 7
// bar.cue
import "foo"
b: foo.a
// Command line
$ cue eval bar.cue
package "foo" not found:
./bar.cue:1:8
terminating because of errors
I know I can remove the import statement, put e.g. package blah
on the first line of both files, and then run cue eval bar.cue foo.cue
, which works. But that's not the type of modularity I'm used to... I prefer explicit linkages between files...
from cue.
@joshburkart could you provide a more detailed view of your directory layout. Since as far as I understand it cue packages need to reside in the pkg
directory beside your cue.mod
file (this file can be empty). Something like the following:
$ tree .
.
├── bar.cue
├── cue.mod
└── pkg
└── foo
└── foo.cue
2 directories, 3 files
And if I'm not mistaken your foo.cue file needs the following package directive package foo
at the top. Maybe the following change does help https://cue.googlesource.com/cue/+/refs/changes/01/2601/6/doc/tutorial/basics/instances.md#modules
from cue.
@joshburkart: CUE uses Go-like importing, which only happens at the package level, not file level. So typically you don't import files from within the same directory. Instead, files marked with the same package directory can see each other's top-level symbols.
The works extra well in CUE: as order doesn't matter, you can just lump definitions together.
But yes, this is not well documented. Like with Go, package organization is not part of the language spec, so you can't find how this works in the spec. But documentation for this is in the works and coming soon. See @xinau 's CL for now.
from cue.
@mpvl @xinau Thanks so much for the responses! I'll try to wrap my head around Go-style imports. :)
from cue.
@xinau I tried to follow your demo (thanks!), but unfortunately couldn't get it to work... Any help would be appreciated...
Here's my directory layout:
$ tree
.
├── channels_demo.cue
├── cue.mod
└── pkg
└── schemas
└── channel
└── channel.cue
3 directories, 3 files
File contents:
# cue.mod (empty)
# channel.cue:
package channel
Channel: {
name: string
}
# channels_demo.cue:
package demo
import "schemas/channel"
channels: [...channel.Channel] & [
channel.Channel & {name: "blah"},
]
Invocation attempt:
$ cue eval channels_demo.cue
package "schemas/channel" not found:
./channels_demo.cue:3:8
terminating because of errors
from cue.
Like with Go, the first path component of a package path must be a domain name (and thus have a "." in the name). This gives it a globally unique name.
Like Go, CUE also uses this to distinguish between builtin packages and external ones.
A first version of the website is up. A concepts section with a special section on packages and modules is high on the list of things to write next.
from cue.
Thanks; I'll give that a try.
New website looks awesome!
from cue.
This issue has been migrated to cue-lang/cue#41.
For more details about CUE's migration to a new home, please see cue-lang/cue#1078.
from cue.
Related Issues (20)
- cmd/cue: import/export (go|rust|...) HOT 3
- cmd/cue: import should ignore null documents HOT 4
- cmd/cue: fmt converts tabs to spaces in comments HOT 2
- cue eval panic instead of erroing with circular definition HOT 2
- Intermittent #FileInfo errors when parsing cue HOT 3
- cue vet hangs with template, list and default value HOT 1
- cmd/cue: simplify ["k"]: v to "k": v HOT 1
- Exporting binary & text protos via protoreflect HOT 1
- cuelang.org: document cue export for all conversions HOT 1
- panic in `cue get go github.com/argoproj/argo-cd/v2/...` HOT 2
- import cycle causes infinite loop HOT 2
- [regression] cue def openapi fails to handle disjunctions HOT 2
- cue segfault HOT 2
- exec.Run runner of kind "XXX" not found with k8s resources HOT 1
- cue trim bug: conjunctions and definitions HOT 1
- Parse error on default constraints (...type) HOT 5
- Tags across packages HOT 3
- YAML exports with strings in certain formats are interpreted as datetimes HOT 9
- `command "dump" is not defined` when running `cue dump ./...` HOT 2
- CUE has moved to a new home! HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cue.