Skip to content

fix: Make sure env-type settings are populated before start, don't override .ddev/.gitignore file, for #7236#7286

Merged
rfay merged 5 commits into
ddev:mainfrom
rfay:20250513_rfay_create_settings_earlier
May 19, 2025
Merged

fix: Make sure env-type settings are populated before start, don't override .ddev/.gitignore file, for #7236#7286
rfay merged 5 commits into
ddev:mainfrom
rfay:20250513_rfay_create_settings_earlier

Conversation

@rfay

@rfay rfay commented May 13, 2025

Copy link
Copy Markdown
Member

The Issue

In #7236 (comment) @stasadev pointed out that now that we're using .ddev/.env* for settings (instead of using php application-defined settings load techniques) the settings/env values may be loaded too late (after containers are up) in some situations.

Before #7236 I believe that all explicit use of settings, including project types that used project-level .env (loaded by PHP dotenv for example) were application-loaded, which meant that it didn't matter that we wrote the .env late (post-start). But now with #7236 we're using DDEV's .ddev/.env.* so those only get loaded during docker-compose up, and need to be written before docker-compose up.


The CraftCMS config overrides .ddev/.gitignore completely (which is wrong), and our logic for settings files creates .gitignore file in the directory of the settings file. Since CraftCMS uses .ddev/.env.web, its parent is .ddev, so it used .ddev/.gitignore.

How This PR Solves The Issue

Move the call to app.CreateSettingsFile() in the start sequence to before the docker-compose up

Don't override .ddev/.gitignore for CraftCMS.

Manual Testing Instructions

Mostly automated tests should tell us about this;

A good check of Craft CMS in light of #7236 (comment) is a good idea

Automated Testing Overview

No changes.

Release/Deployment Notes

@github-actions

github-actions Bot commented May 13, 2025

Copy link
Copy Markdown

@stasadev stasadev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you, I tested the Craft CMS quickstart using this PR, and now it updates .ddev/.env.web as expected.

@rfay

rfay commented May 13, 2025

Copy link
Copy Markdown
Member Author

Open to suggestions about whether we should get this into this release. I don't foresee problems with doing the settings early, but perhaps I'm being naive. I didn't see any of the settings creators that tried to use a running container either.

@stasadev

stasadev commented May 13, 2025

Copy link
Copy Markdown
Member

To be safe, it can be merged early in the next release.

The workaround is to leave .ddev/.env.web untouched and run ddev restart.

I don't see any issues with this fix, but we can test it for a longer time in DDEV HEAD.

@rfay rfay marked this pull request as ready for review May 15, 2025 16:33
@rfay rfay requested a review from a team as a code owner May 15, 2025 16:33
@rfay

rfay commented May 15, 2025

Copy link
Copy Markdown
Member Author

Rebased

@stasadev stasadev force-pushed the 20250513_rfay_create_settings_earlier branch from 642dd08 to a2dc75f Compare May 15, 2025 19:06
@stasadev stasadev changed the title fix: Make sure env-type settings are populated before start, for #7236 fix: Make sure env-type settings are populated before start, don't override .ddev/.gitignore file, for #7236 May 15, 2025
@stasadev

stasadev commented May 15, 2025

Copy link
Copy Markdown
Member

The first post is updated with .ddev/.gitignore issue, which I fixed here.

We may want to think about how to properly gitignore .ddev/.env.* files (but they were not intended to be gitignored, when I added them for add-ons).

This is probably a task for:

Comment thread pkg/ddevapp/ddevapp.go
Comment on lines -1761 to -1771
if _, err = app.CreateSettingsFile(); err != nil {
return fmt.Errorf("failed to write settings file %s: %v", app.SiteDdevSettingsFile, err)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should have app.CreateSettingsFile() in both places, on pre-start and post-start.

I suggest to bring back this removed condition.

This code runs only when site is running:

ddev/pkg/ddevapp/drupal.go

Lines 565 to 582 in 7b0cb14

// It is possible that a Drupal install has been done, setting sites/default and sites/default/settings.php
// to read-only, which breaks mutagen's access to them so mutagen then
// can't sync. See https://github.com/ddev/ddev/issues/6491
// So we chmod +w the two files that a Drupal install may set read-only
// *inside* the container, allowing mutagen access to it again
if app.IsMutagenEnabled() && status == SiteRunning {
settingsFiles := []string{
path.Join(app.GetAbsDocroot(true), `sites/default`),
path.Join(app.GetAbsDocroot(true), `sites/default/settings.php`),
}
stdout, stderr, err := app.Exec(&ExecOpts{
Cmd: fmt.Sprintf(`chmod u+w %s 2>&1`, strings.Join(settingsFiles, " ")),
})
if err != nil {
util.Warning("Unable to set permissions inside container on settings files; err='%v', stdout='%s', stderr='%s'", err, stdout, stderr)
}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

IMO it's pretty safe to do that, but here it was inside mutagenEnabled right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, inside mutagenEnabled.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added it back where it was before (but that was late in Start, not in mutagen section)

Comment thread pkg/ddevapp/apptypes.go
if path.Dir(app.SiteSettingsPath) != app.AppRoot {
// Don't override the existing .ddev/.gitignore as well
// TODO: we may want to append to .ddev/.gitignore if needed.
if path.Dir(app.SiteSettingsPath) != app.AppRoot && path.Dir(app.SiteSettingsPath) != app.GetConfigPath("") && app.SiteDdevSettingsFile != "" {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fascinating. I'll take a bit of a look at this in the light of its history. The reason this was like this was that the drupal sites/default had to have its own gitignore IIRC.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is great; now I understand the whole history and the stumble here.

@rfay rfay force-pushed the 20250513_rfay_create_settings_earlier branch from a2dc75f to cec8720 Compare May 16, 2025 16:37
@rfay

rfay commented May 16, 2025

Copy link
Copy Markdown
Member Author

I made minor changes and re-added the previous near-post-start settings update. I don't understand exactly why it would be needed, but don't see it doing any harm.

@rfay rfay requested a review from stasadev May 16, 2025 16:56

@stasadev stasadev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

Adding code without deleting it seems safer to me.

Tested it again, the .ddev/.env.web file is updated on ddev start (I edited it during the start, and it was updated again)

And .ddev/.gitignore is not broken.

@rfay rfay mentioned this pull request May 19, 2025
1 task
@rfay rfay merged commit 4eac605 into ddev:main May 19, 2025
14 checks passed
@rfay rfay deleted the 20250513_rfay_create_settings_earlier branch May 19, 2025 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants