11 ms·
Fear and Loathing in YAML
- pjbk 6y agoLooking back to the last decade, I cannot remember a single project I worked in that relied on YAML that didn't have at least one incident from its users or developers dealing with the files and messing up. Just some months ago a team I was working with lost almost a day on a misbehaving software module. After serious detective work it all came down to whitespace in a YAML configuration file. I remember being pretty excited when I discovered YAML and even pushed its use in some projects trying to avoid the verbosity of XML. These days I will never trade accuracy for conciseness, in particular if its form relies on whitespace and there is not enough redundancy in the specs for consistency checks. Yes, syntax matters.
- deleted 6y ago[deleted]
- ars 6y agoThe biggest problem I find with YML is that whitespace matters, but merging tools usually are set to ignore whitespace. So git merge will say "no merge conflicts", but actually there were whitespace changes.
- Anka33 6y agoXML master race!
- brown9-2 6y agoI’m curious, do you recall what the specifics of the whitespace change were?
- pjbk 6y agoYes. It was an indentation issue. It turned that the parent list accepted an item with the same id, so it was a valid statement and the full thing seemed OK. However the data ended up missing from where it was supposed to be.
- andrenth 6y agoBeing a superset of JSON, YAML allows you to use braces for maps and square brackets for arrays instead of relying on indentation. You can still omit quotes around strings, use comments and add trailing commas after the last element of collections. A configuration file written in that style is actually pretty nice. I’ve never understood why people don’t use this.
- smitty1e 6y agoBut you can't put comments in JSON. But you can write Python (or pretty much any other tool) and have it emitted as JSON.
- andrenth 6y agoI think you misunderstood. What I meant is that you can put comments in YAML but use JSON-like braces instead of relying on indentation.
- balfirevic 6y agoExcept for omitting quotes around strings, that sounds very similar to json5 [0]. [0] - https://json5.org/ https://json5.org/
- predakanga 6y agoHave you considered validating your YAML against a schema? JSON Schema[0] can do the trick, and has decent editor support[1]. It's not a panacea, but it can help avoid those long debugging sessions. [0]: https://json-schema-everywhere.github.io/yaml https://json-schema-everywhere.github.io/yaml [1]: https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml https://marketplace.visualstudio.com/items?itemName=redhat.v...
- pjbk 6y agoThanks for the suggestion. Last time I had to use YAML I used StrictYAML instead which gave me more peace of mind. I knew that there were schema tools around, however I just put more checks in the validator to avoid adding extra dependencies. Next time I will definitely look into those.
- zdw 6y agoHow much of this can be solved by using a linter like yamllint, or a a mode that validates the data via a schema or dry-run mode? I'd think it would knock out most of the low hanging fruit.
- mixmastamyk 6y agohttps://hitchdev.com/strictyaml/ https://hitchdev.com/strictyaml/ Also most editors have indentation guides, which help a lot with indentation issues.
- ben509 6y agoTheir sections on "Why StrictYAML" and "design justifications" are required reading for anyone working on a text-based serialization format. You may not agree with all their conclusions, you may have another use case, but you ought to make sure you're addressing all those points.
- samatman 6y agoTOML remains severely underrated, for some reason. It's easy to read, easy to write, trivial to map onto JSON, and refreshingly clear of footguns. There are comments! If you get to pick your format for a configuration language, start by trying to talk yourself out of TOML. You'll probably fail; I'm interested to hear if folks out there have examples of something TOML is bad at, because I didn't come up with any when I was doing this evaluation for my own project.
- baq 6y agoIt’s a bit too strict for configuration files and too verbose for an interchange format. It’s an improvement over JSON and arguably yaml but it won’t beat actual syntax.
- ben509 6y agoAnother comment brought up StrictYAML, and they have a good dissection of the problems with TOML[1]. Personally, after doing a few pyproject.toml files, I still find it confusing and awkward as soon as you get past simple headers and key-values. TOML is like YAML, it translates to "jsonic" types, but JSON's virtue is that simple 1:1 mapping between syntax and data. TOML and YAML both have non-obvious syntax and there's a layer of translation you have to do to understand what you're actually generating. But I don't really want to generate a big blob of objects, arrays and such, I want to configure something. I think that's why none of these languages work very well. [1]: https://hitchdev.com/strictyaml/why-not/toml/ https://hitchdev.com/strictyaml/why-not/toml/
- saghm 6y agoIMHO the arguments in your link aren't particularly convincing. The three main points seem to be verbosity (defined in terms of total number of characters for a given file, which is not nearly as important to me as readability), not having indentation be significant (which might be downside to a Python programmer but feels like a win to me), and having explicit syntax to distinguish between types (e.g. strings being quoted, which is preferable to me, since it makes them stand out as values compared to the keys). If these are the strongest arguments against TOML, I find myself fairly convinced that TOML is probably the format I'd like to use most even though I was I different towards it before clicking the link.
- baq 6y agoI’d say yaml itself is the smaller part of the problem. The real fun starts when its obvious shortcomings (for use cases it’s being attempted to fulfill) are being papered over with jinja2, code in nondescript languages embedded in string values and css-like composition rules for multiple files.
- durandal1 6y agoAn interesting candidate in the other end of the spectrum is Dhall, https://dhall-lang.org https://dhall-lang.org. If I was working in one of the languages with current implementations I would no doubt try it out. Edit: I will also add that dhall will export to YAML/JSON etc, however I'm not a fan on intermediary config file processing.
- ghuntley 6y agoicymi https://noyaml.com https://noyaml.com
- bryanrasmussen 6y agoWe were editing a 2000 node Ant Config file by hand when the drugs began to take hold. I remember saying something like "I feel a bit lightheaded; maybe all configuration should be done in YAML from now on..."
- brundolf 6y agoI wonder why IDE-enforced markup-schemas haven't caught on. Like a static type system for your markup (i.e. Kubernetes could declare your "types" and your editor could ensure the config fits them). VSCode has something along these lines for JSON - at least on its own settings configuration - but somehow the approach hasn't become prevalent. Are we lacking a standard? Motivation?
- FridgeSeal 6y agoThere's a Kubernetes plugin/package for Dhall [0] that can do this [1], it's very neat. I've played with it briefly, and whilst a tiny bit verbose (mostly as a function of the K8s specs themselves I suspect), the ability to have it validate everything before you even touched K8s was worth it imo. [0] https://dhall-lang.org/ https://dhall-lang.org/ [1] https://github.com/dhall-lang/dhall-kubernetes https://github.com/dhall-lang/dhall-kubernetes
- EdwardDiego 6y agoIDEA's K8s plugin validates its YAML. It's stricter than K8S itself though. K8s will accept a cpu value of 0.1, but the plugin marks it as an error, it wants the millicpu version 100m instead.
- ratorx 6y agoI had this exact problem a while ago when trying to have static types for blog entries. I ended up using a strategy where I would write the config in the language that I would eventually use it in (for my blog, my entries became typescript files). I think perhaps protobufs (specifically text format protos) are a potentially really nice solution, since you get to leverage the protobuf type system. Unfortunately the support for them is a bit lacking.... (afaik, no formal spec - there’s a canonical implementation in C++; no editor support for things like linting and completions).
- djhaskin987 6y ago> there was something off-putting about YAML. It was a markup language claiming not to be a markup language. The acronym literally stands for "Yet Another Markup Language". Edit: To comment something less pedantic, the article doesn't really go into anything specific it doesn't like about it. It's sort of like when my brother comments about how awful the vim home web page looks. It's still a good web page. I don't know why people are commenting about aesthetics in YAML if they can't pinpoint a particular functionality problem. That said, I'm not blind to the many warts and weird edge cases and dozens of different ways you can specify a string in YAML. I hope something more elegant comes along that's still not as draconian as JSON (simple, elegant, hard to write strings, no comments).
- tofflos 6y agoYAML (a recursive acronym for "YAML Ain't Markup Language") Source https://en.wikipedia.org/wiki/YAML https://en.wikipedia.org/wiki/YAML. Also https://yaml.org/spec/1.2/spec.html https://yaml.org/spec/1.2/spec.html.
- djhaskin987 6y agoIt didn't used to be: https://yaml.org/spec/history/2001-08-01.html https://yaml.org/spec/history/2001-08-01.html However I must admit some surprise at learning that even before 2010 they changed to the recursive acronym. I was still calling it yet another markup language even back then. I thought anything back then was yet another if it started with ya, like YaST from SuSE (yet another setup tool).
- mrgalaxy 6y agoWow I always thought it was "Yet Another Markup Language". It's crazy that they would change to be literally opposite of the original name. From the wikipedia article: > but it was then repurposed as YAML Ain't Markup Language, a recursive acronym, to distinguish its purpose as data-oriented, rather than document markup. I mean couldn't it be both data-oriented and a markup language?
- slifin 6y agoIn a world where there's only a few primitive types no one can share uuids or dates across the wire without both sides knowing which fields are dates or uuids ahead of time JSON is too simple we need more primitive types and an extensible data notation to formalise data sharing of unknown types
- viraptor 6y agoThat's one way to go, but there are other options. You want inline annotation of types. On the other side you can have an external jsonschema which provides the same information. Realistically, you can't do "data sharing of unknown types" - at that point you're just sharing binary blobs.
- goatinaboat 6y agoRealistically, you can't do "data sharing of unknown types" - at that point you're just sharing binary blobs. ASN.1 and CORBA both let you share types that are unknown ahead of receiving.
- viraptor 6y agoI don't know much corba, but in asn.1 it's basically "a field tag and a binary blob". It could be also a standard structure tagged with an unknown id, but either way I don't really see it as much more than json `{"uid": "blob-data"}` in practice. If it's unknown data, you can do almost nothing with it apart from passing it on. If I recall correctly, x509 doesn't even want to deal with any complicated tagging and embeds each extension value as a serialised DER itself.
- kstenerud 6y agoI had the same complaints, thus https://concise-encoding.org/ https://concise-encoding.org/
- p1necone 6y agoThe thing I don't like about YAML is the strict meaningful indentation. It's fine once you install an extension that won't let you do anything other than two spaces, but it's pretty horrific if you try to edit a yaml file in a virgin text editor when you aren't super familiar with the format, especially if you try to copy paste things.
- TheSwordsman 6y agoI think I would genuinely be pretty happy with a well-supported extension of the JSON spec that allows comments. Even if it required an explicit start and end of the comment (/* ... */). I also can't say I want or need anchors/references from YAML, so I think that helps me be quite OK with using JSON. I appreciate the challenges of writing JSON, but also think it can be easier to spot issues if you have an editor that's doing magic indentation based on the JSON. Give me comments to help the humans, and I'm sold.
- davidpires 6y agoIf you are using JavaScript you can use jsonc [0] it's exactly that just JSON with comments. VSCode and typescript config files already use it by default https://github.com/microsoft/node-jsonc-parser https://github.com/microsoft/node-jsonc-parser
- lioeters 6y agoI like Human JSON: https://hjson.github.io/ https://hjson.github.io/ It has comments and accepts trailing commas.
- outworlder 6y agosigh I would like to mumble something about s-expressions and how this has been solved long ago but I lack the stamina after doing it so many times. Let me go back to my job and my task of modifying a Helm chart which decided that string manipulation of yaml files is truly the way to go.
- tofflos 6y agoAm I the only one who finds YAML much harder to write than JSON or XML? It's not just the fact that I get the syntax for lists and maps confused all the damn time. The worst part is that I'm unable to use the keyboard shortcut for automatically formatting my document! I love that keyboard shortcut! I hate being without it! I'm certain it represents more than half of my key presses in other languages! I type a few characters and then I press the code formatting shortcut. That's what I do! That's my workflow! I don't understand how people who work in whitespace-significant languages can stand being without it! ;-) (I'm only half-joking - I do find it very frustrating)
- andybak 6y agoMy keyboard shortcut for automatically formatting my documents usually understand the document format or programming language I'm using. If it doesn't then I'm using the wrong editor.
- colejohnson66 6y agoThe problem is that in YAML, white space is significant. How is a formatted supposed to know: key1: value1 key2: value2 ...is supposed to be: key1: value1 key2: value2 ? How is it supposed to know if the first is actually what you meant to write? In grammars that aren’t white space dependent (such as JSON), it’s clear what this means: { "key1": "value1", "key2": "value2" }
- dragonwriter 6y ago{ "key1": values, “key2”: value2 } is valid YAML with the same sematics as it has in JSON; if you have a YAML-aware editor (or editor mode) it will probably format that just as easily as in JSON.
- cageface 6y agoSignificant whitespace seems like a good idea and looks great in small examples but it has some very serious downsides in real use. Being able to reliably reformat hundreds of lines of code with a single keypress is far more valuable than a few saved braces.
- jjtheblunt 6y agoAmazon's Ion format is decent, albeit not so long in the public domain, i believe
- dnautics 6y agowow, thanks for that. Ion is really pretty. But I still can't shake it that curly brace delimiters are inappropriate for a configuration format.
- jjtheblunt 6y agoI think that a subset of Ion is exactly JSON, so it gets that curly brace notation as a previous choice from the Javascript world.
- dragonwriter 6y agoA subset of YAML is exactly JSON, but you very visibly don't have to use curly brace notation with it.
- lioeters 6y agoThat is quite nice! http://amzn.github.io/ion-docs http://amzn.github.io/ion-docs
- andybak 6y ago> YAML is better than XML but, it sucks compared to something like HTML or Markdown (which I can teach to execs and children alike). I'm still regularly surprised by Markdown. I have to rely on previews and occasionally still need to check the docs. My bulleted lists often break and I can never remember the syntax for links. It's ok to like WYSIWYG even if you're a hacker.
- skywhopper 6y agoI hate this sort of article. Lots of griping about the problems with YAML, but no solutions. What does the author think is better? XML doesn't map to the same data structures as YAML, JSON, and TOML, so it's not really a comparable beast. The author mentions a desire to be able to teach a data format to people in the way someone can comprehend HTML or Markdown easily. But the formats have different goals. The goal of HTML and Markdown are to provide a way to encode document formatting in a simple text syntax. There's not one single correct way to format a document intended for presentation to and consumption by humans. Pretty much anything goes, HTML and Markdown just provide some decoration. But the purpose of YAML (and JSON and TOML) is to provide a way to represent strictly defined data structures for consumption by a computer. The reason YAML is frustrating is because you can't fudge the result; you can't be sloppy or inexact; YAML operates at the boundary between the human and the computer program, and just like any HCI, it's going to be frustrating and error-prone. Now, would it be possible to come up with a better format than YAML or JSON or TOML that does the same thing? Maybe. But the facts that tools exist to search and manipulate these formats and that we'll probably still be using them in 2030 isn't evidence that they are bad. The fact that yq can exist and is useful is a point in favor of YAML, imo. If simple tools can be written to do useful things with a data format, that makes the format more useful.
- t-writescode 6y agoHow do you mean? They're all interchangeable. If you treat XML attributes as @-prefixed variables in the set of json nodes and visa-versa, they're fully compatible.
- l0b0 6y agoAfter working with YAML and JSON (and XML and INI and CSV and so on), it seems like a) JSON is equivalent and simpler for basic structures, b) JSON + jq or a language library is better than YAML's complex syntax (like not being able to merge lists[1]), and c) YAML has too many ways to express the same thing. Writing YAML is basically trial and error. [1] https://stackoverflow.com/q/24090177/96588 https://stackoverflow.com/q/24090177/96588
- jeremyjh 6y agoConfig files should support comments. JSON is being wedged into use cases it simply wasn't designed for.
- rswail 6y agoI used to like Python, but the whitespace stuff irritates me more and more. Sure, it's easy to read, but having those braces also makes code easier to read. Maybe not opening braces, they're sorta redundant, but closing braces allow me to "pop stack" when I read them. Yaml combines the worst of whitespace with the use of whitespace without keywords, which makes things even harder. JSON is ok, particularly if your editor auto-quotes things, I wish it had comments, but having something that strips comments is a single sed pre-processing line. Neither are great for human generation or consumption, but at least JSON is only trying to be a serialize/deserialize syntax and doesn't claim to be more. YAML truly lives up to the "YA" part. It's trying to be the COBOL of configuration, able to be read and written by anyone. But no one needs that.
- HelloNurse 6y agoToday I edited a Docker Compose YAML configuration file. The network configuration has "ports" sections, such as ports: - "8080:80" Which (assuming some other network configurations) means that whatever answers to port 80 in the container can be reached at port 8080 of the host running Docker. Simple syntax and straightforward semantics, but port numbers are integers: why strings? Because, as the Docker manual warns, YAML is clever. > When mapping ports in the HOST:CONTAINER format, you may experience erroneous results when using a container port lower than 60, because YAML parses numbers in the format xx:yy as a base-60 value. For this reason, we recommend always explicitly specifying your port mappings as strings.
- tieske 6y agoLike perl is a "write-only" language, YAML is a "read-only" format. Humans should not be manually editing YAML, there are way too many caveats. (my favorite being the country code for Norway "NO" being a boolean-false, if not quoted)
- bionhoward 6y agoWe should all just write our config in JavaScript or TypeScript because then we can standardize around a common interface language. It’s simple, widely known, has comments, not dependent on white space, JSON has the right types we need program (and can be validated, with JSON Schema). Critically, to generate configs, we can write loops, conditions, ternaries, functional programs with Ramda, classes/OOP, and even fancy GUIs. Simple English JavaScript is powerful and readable. .js is the move here... especially once node supports immutable data structures This is why stuff like AWS CDK and Pulumi are legit. Configure your stuff with actual code. All we gotta do is fill the gap between JS and Python in terms of scientific computing, and we’re set. Typescript->WASM could be interesting. I’m sure there are some details that make this difficult but, what do you guys think about standardizing around JS/TS?