pulumi/changelog/pending
bors[bot] 7a173bedac
Merge #11456
11456: Implement YAML roundtripping, comment preserving edits r=AaronFriel a=AaronFriel

By caching a copy of the raw bytes of the original loaded document - project, policy project, or stack - on save we:

* Unmarshal the original bytes to a YAML AST
* Recursively edit nodes of the document to match the values of the value we're saving, preserving trivia
* Marshal the modified AST to []byte

Closes #423, as we don't support comments in JSON presently (not using "JSON5")

Closes #5235

Simplifies work done in #10797 and #10437 which used the previous `yamlutil` package to edit AST nodes directly.

# Automation API

Integration with automation API requires that operations either perform a "read-modify-write" in a single operation, or use a side channel to allow subsequent "read" (select stack?) and "write" operations to keep state of the original file.

However I don't expect this to be needed. We don't have a reason to believe automation API users maintain comments in particular stack files, and doing so is already unsupported in automation scenarios. The Pulumi Service does not persist raw config files, thus `pulumi config refresh` and `CreateOrSelectStack` create config files without comments.

n.b.: If you test this, due to the new behavior of this PR, `pulumi config refresh` will persist comments if there is an existing config file. I would expect automation API using local file workspaces to behave similarly. When using these tools to create a config file from the last deployment's definition, the file is created restored without comments.

# Tradeoffs

I believe that this closes #423 in spirit, but doesn't strictly implement round-tripping. There are a couple reasons for this.

## Marshaller interface and round-tripping simplification decisions

We support unmarshaling "simplified" YAML, including allowing:

```yaml
runtime: yaml # this unmarshals to a struct, not a scalar value
config:
  someKey: "someScalar" # likewise
```

These simplifications make it much more difficult, the `Marshal` operation would _also_ need context of decisions made when unmarshaling to ensure a bijection. Otherwise we may save one of these as the other:

```yaml
runtime:
  name: yaml
```

```yaml
runtime: yaml
```

This could be considered a feature, as it enforces a style guide - the decisions implemented in our custom `Marshaller` implementations become the style enforced on save.

That said, I believe the new `Edit` API while not round-trippable, does reach a fixed point after one save through the new API. Once saved with this API, subsequent loads & saves without changes are idempotent.

## Indentation

The YAML library uses a global indentation when marshaling values to YAML, as opposed to indentation being a property of AST nodes. This means we may lose other indentation.

Co-authored-by: Aaron Friel <mayreply@aaronfriel.com>
2022-12-13 20:37:38 +00:00
..
20221116--cli--adds-a-flag-that-allows-user-to-set-the-node-label-as-the-resource-name-instead-of-the-full-urn-in-the-stack-graph.yaml Changes to stack_graph.go file and envrc file 2022-12-12 18:37:34 -08:00
20221117--cli--delete-stack-config-file-on-pulumi-remove.yaml chore: Add changelog 2022-12-13 02:34:54 +00:00
20221123--cli-config-new-package--preserve-comments.yaml chore: changelog 2022-12-10 12:29:33 -08:00
20221125--sdkgen-nodejs--fix-nodejs-sdk-when-a-component-is-using-another-component-from-the-same-schema-as-a-property.yaml Remove .NET type imports and add a test for #11467 2022-12-09 14:03:20 +01:00
20221129--sdk-nodejs--emit-closure-requires-in-global-scope-for-improved-cold-start-on-lambda.yaml docs: add changelog entry 2022-11-29 07:29:17 +00:00
20221206--sdk-dotnet--add-output-jsonserialize-using-system-text-json.yaml Add Output.JsonSerialize to dotnet sdk 2022-12-09 11:41:22 +00:00
20221206--sdk-nodejs--remove-function-serialization-code-for-out-of-suppport-nodejs-versions.yaml Remove support for old NodeJS versions from function serializer 2022-12-11 21:20:06 +00:00
20221208--sdk-python--fix-a-deadlock-on-provider-side-error-with-automation-api.yaml docs: Add changelog entry 2022-12-09 23:13:14 +01:00
20221208--sdkgen-dotnet-go-nodejs-python--do-not-generate-result-types-for-functions-with-empty-outputs.yaml Do not generate Result types for functions with empty outputs 2022-12-09 14:36:44 +01:00
20221209--sdk-go--add-jsonmarshal-to-go-sdk.yaml Add JSONMarshal to go sdk 2022-12-09 16:34:53 +00:00
20221209--sdk-nodejs--add-output-jsonstringify-using-json-stringify.yaml Add jsonStringify to nodejs sdk 2022-12-09 11:37:18 +00:00
20221212--cli-display--fixes-negative-durations-on-update-display.yaml fix negative durations in update display 2022-12-12 12:22:03 -08:00
20221212--programgen-go--Check-for-optional-Ptr-types-within-Union-types.yaml Check for optional/ Ptr types within Unions [go/programgen] 2022-12-12 15:49:20 -08:00