Skip to content

fix(agent/agentcontainers): generate devcontainer metadata from schema #16881

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

Merged
merged 8 commits into from
Mar 18, 2025

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Mar 11, 2025

Generated using glideapps/quicktype. This is unlikely to change often, but checking it into the repo and adding the ability to update when required.

@johnstcn johnstcn self-assigned this Mar 11, 2025
@johnstcn johnstcn force-pushed the cj/gen-devcontainer-schema branch from 4c2532a to 075d291 Compare March 12, 2025 16:02
@johnstcn johnstcn requested review from mafredri and SasSwart March 12, 2025 16:43
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Is quicktype already a dependency or should we add it to our image/nix/etc? Are you adding a Makefile entry later?

Hmm.. Speaking of Makefile, I wonder if we should commit the spec.json so that we can ensure in CI that there are no changes by gen.

@@ -183,7 +184,7 @@ func devcontainerEnv(ctx context.Context, execer agentexec.Execer, container str
return nil, nil
}

meta := make([]DevContainerMeta, 0)
meta := make([]dcspec.DevContainer, 0)
if err := json.Unmarshal([]byte(rawMeta), &meta); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: We could first attempt a strict unmarshal via dec := json.NewDecoder(), dec.DisallowUnknownFields(), if it catches an issue we can log it and fall-back to non-strict unmarshaling.

Not strictly necessary, but may help provide hints down the line why something isn't working.

@@ -192,7 +193,10 @@ func devcontainerEnv(ctx context.Context, execer agentexec.Execer, container str
env := make([]string, 0)
for _, m := range meta {
for k, v := range m.RemoteEnv {
env = append(env, fmt.Sprintf("%s=%s", k, v))
if v == nil { // *string per spec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I interpret this as env var k should be removed. Does the spec specify more concretely what null means in this context (or do we have to try it out)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, no. I think we'll have to either try it out or read the implementation.

Copy link
Member

@mafredri mafredri Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems @devcontainer/cli has a poor implementation, from:

	"remoteEnv": {
		"FOO": "bar",
		"NODE_OPTIONS": "",
		"GOPRIVATE": null
	},

We get this.

NODE_OPTIONS=
GOPRIVATE=null
FOO=bar

I'm not a fan of the null handling here. But if we want to retain a similar behavior, we shouldn't just skip it.

I'd suggest setting it to an empty string when "none", does that sound reasonable? Or do we want to mimic @devcontainer/cli exactly here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a null sneak in is suprising behaviour to me. I'd honestly prefer an empty string, but we can revisit this if it causes compat issues.

@johnstcn
Copy link
Member Author

johnstcn commented Mar 12, 2025

Awesome! Is quicktype already a dependency or should we add it to our image/nix/etc? Are you adding a Makefile entry later?

I was hoping to avoid adding a new dependency, but I can add it.

Hmm.. Speaking of Makefile, I wonder if we should commit the spec.json so that we can ensure in CI that there are no changes by gen.

I can do that! Depends on how much spec stuff we want lying around in agent/agentcontainers.

@mafredri
Copy link
Member

I was hoping to avoid adding a new dependency, but I can add it.

True. It's just that without locking in the version, it might at some point be hard to reproduce a working copy if the output ever diverges.

I can do that! Depends on how much spec stuff we want lying around in agent/agentcontainers.

My main concern is having code that's never or rarely run and ends up being incompatible by the time its needed.

I suppose we have two options. 1) Keep it download-only and only run it (manually) when we know there's an update, or 2) Commit the spec json and run generate every time.

For 1) a CI cron to check if the output/spec changed would be good. And for 2) we may still want to do CI cron, but in general it will ensure our quicktype dependency remains compatible for when it's needed the next time.

@johnstcn johnstcn force-pushed the cj/gen-devcontainer-schema branch from 23209fc to 2a565b3 Compare March 13, 2025 21:29
@johnstcn
Copy link
Member Author

  1. Commit the spec json and run generate every time.

I went ahead with option 2!

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor nit (as well as ? re: null handling), but this is looking great! Thanks for improving the codegen.

Approving, as I don't need to re-review.

mv -v "${TMPDIR}/${DEST_FILENAME}" "${DEST_PATH}"

# Add a header so that Go recognizes this as a generated file.
sed -i '1s/^/\/\/ Code generated by dcspec\/gen.sh. DO NOT EDIT.\n/' "${DEST_PATH}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This transformation could be done in temp still, before the move.

But unfortunately, this is not cross-platform 😔.

If you want a quick fix and not use another tool, see: a1f5468

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice callout, thanks!

@johnstcn johnstcn merged commit 13a3ddd into main Mar 18, 2025
31 of 33 checks passed
@johnstcn johnstcn deleted the cj/gen-devcontainer-schema branch March 18, 2025 13:00
@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants