Skip to content
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

fix(dev): fix reload error when using templates #5329

Merged
merged 2 commits into from
Dec 1, 2023
Merged

Conversation

thsig
Copy link
Collaborator

@thsig thsig commented Nov 1, 2023

What this PR does / why we need it:

Before this fix, changing a Garden config during a dev command session and running two subsequent commands would result in the first command succeeding but the second failing with an error like:

"Build foo references template which cannot be found. Vailable templates: "

This was because the config templates scanned by the first subcommand after the config was changed weren't propagated to the parent Garden instance, which was then cloned and passed to the second command (which then had action configs, but no templates).

This was fixed by making the relevant instance variables on the Garden class readonly (since reassignments in a child instance prevent newly scanned configs from propagating to the parent via the shared data structure) and emptying the maps before rescanning instead.

Also added a unit test case that checks this whole flow.

Which issue(s) this PR fixes:

Fixes #5290

Special notes for your reviewer:

This fix is rather hacky. We really shouldn't be relying on shared references to data structures in the instance variables of parent & child Garden instances. This happens to mostly work, but could easily lead to hard-to-diagnose problems in the future.

I thought I'd open the PR to start a discussion on how to proceed properly here.

Would it be better if we stored the action configs and config templates in an instance of a singleton class, to make sure there's ever only one version of the truth (the most up-to-date)?

Or do we explicitly want different child Garden instances to have their own copy of the configs & templates?

I think the choice boils down to either of these two:

  1. A single ConfigRegistry that's shared by all Garden instances. This would be populated/updated whenever configs are scanned, and there would always be exactly one instance of it in existence at any given time.
  2. Deep-cloning the action configs & config templates of the parent Garden when creating a child Garden, and carefully propagating any new configs & templates that are scanned back to the parent.

The former sounds like a better approach to me, but I'm all ears.

CC @eysi09 @edvald

@thsig
Copy link
Collaborator Author

thsig commented Nov 1, 2023

Note: I never did quite repro the error Eythor originally reported (the errors I got were different), but it looks like it stems from the same cause (the templates not existing on the Garden instance in question).

@thsig thsig force-pushed the dev-mode-templates-fix branch from 0022910 to 8a529fb Compare November 1, 2023 13:54
@thsig thsig marked this pull request as draft November 1, 2023 14:01
@eysi09
Copy link
Collaborator

eysi09 commented Nov 1, 2023

My first reaction would be to go for option 1 here unless there are use cases for 2 that I'm missing.

Isn't that something that could live on the InstanceManager class?

I'm wondering if we should still merge the hack though since this frequently crashes running Garden processes.

@thsig thsig marked this pull request as ready for review November 2, 2023 13:38
@thsig thsig marked this pull request as draft November 2, 2023 14:13
@thsig thsig force-pushed the dev-mode-templates-fix branch from 8a529fb to 2df761b Compare November 2, 2023 20:43
@thsig thsig marked this pull request as ready for review November 2, 2023 20:43
@thsig
Copy link
Collaborator Author

thsig commented Nov 2, 2023

With the unit test to cover the behaviour, I think this is OK for now.

I didn't feel that a larger surgery on the state management around the instance manager was worth the effort and potential breakages at this point, given the lack of testing around this subsystem in general (also, the config scanning piece may be the main piece of shared state we'll have to deal with in this context).

So I hope this is a practical, simple solution to the problem.

eysi09
eysi09 previously approved these changes Nov 2, 2023
Copy link
Collaborator

@eysi09 eysi09 left a comment

Choose a reason for hiding this comment

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

Had a couple of non-blocking questions but otherwise this looks good to me. So I'll go ahead and approve.

@@ -287,7 +287,6 @@ export abstract class Command<
return withSessionContext({ sessionId, parentSessionId }, () =>
wrapActiveSpan(this.getFullName(), async () => {
const commandStartTime = new Date()
const server = this.server
Copy link
Collaborator

Choose a reason for hiding this comment

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

We really need a lint rule for "no-unused".

* key errors when adding the newly scanned ones (and those generated from newly scanned config templates).
*/
private clearConfigs() {
for (const kind of Object.getOwnPropertyNames(this.actionConfigs)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Pardon me ignorance but why use getOwnPropertyNames instead of keys?

* Essentially a vanilla object analog of `map.clear()` for ES5 Maps.
*/
export function clearObject<T extends object>(obj: T) {
for (const key of Object.getOwnPropertyNames(obj)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question as above.

@thsig thsig force-pushed the dev-mode-templates-fix branch 2 times, most recently from 2b67422 to 4c7e2b9 Compare November 2, 2023 21:35
eysi09
eysi09 previously approved these changes Nov 21, 2023
@eysi09
Copy link
Collaborator

eysi09 commented Nov 21, 2023

Approving but we need to resolve conflicts.

@eysi09
Copy link
Collaborator

eysi09 commented Nov 29, 2023

Bumping this one @thsig :) Could you look into resolving the conflicts so we can get this merged?

@thsig
Copy link
Collaborator Author

thsig commented Nov 30, 2023

Ah yes, will do!

@thsig thsig force-pushed the dev-mode-templates-fix branch 4 times, most recently from d4f43e6 to 6f4b9b6 Compare November 30, 2023 18:58
Before this fix, changing a Garden config during a `dev` command session
and running two subsequent commands would result in the first command
succeeding but the second failing with an error like:

"Build foo references template <some-template> which cannot be found.
Vailable templates: <None>"

This was because the config templates scanned by the first subcommand
after the config was changed weren't propagated to the parent Garden
instance, which was then cloned and passed to the second command (which
then had action configs, but no templates).

This was fixed by making the relevant instance variables on the Garden
class readonly (since reassignments in a child instance prevent newly
scanned configs from propagating to the parent via the shared data
structure) and emptying the maps before rescanning instead.

Also added a unit test case that checks this whole flow.
The call to `this.clearConfig()` in `Garden#scanAndAddConfigs` had
started breaking certain test cases that manually set action configs
before calling `garden.getConfigGraph` (which would scan and add
configs, thus discarding the manually set configs).

While it's not great that we're adding more tweaks to the `TestGarden`
class, we're almost never interested in explicitly testing the post-edit
config reloading (outside of tests that test interactive / dev command
funcftionality).
@thsig thsig force-pushed the dev-mode-templates-fix branch from 6f4b9b6 to cbb7acf Compare November 30, 2023 19:22
@eysi09 eysi09 added this pull request to the merge queue Dec 1, 2023
Merged via the queue into main with commit 9752310 Dec 1, 2023
3 checks passed
@eysi09 eysi09 deleted the dev-mode-templates-fix branch December 1, 2023 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0.13: [Bug]: Using config templates crashes garden dev
2 participants