-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: COMPOSE_REMOVE_ORPHANS resolves identically for every command carrying the flag #14139
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
Open
ndeloof
wants to merge
1
commit into
docker:main
Choose a base branch
from
ndeloof:d1-remove-orphans-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| Copyright 2020 Docker Compose CLI authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package compose | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/compose-spec/compose-go/v2/loader" | ||
| "github.com/spf13/pflag" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func removeOrphansFlagSet(t *testing.T, args ...string) *pflag.FlagSet { | ||
| t.Helper() | ||
| flags := pflag.NewFlagSet("test", pflag.ContinueOnError) | ||
| var v bool | ||
| flags.BoolVar(&v, "remove-orphans", false, "") | ||
| assert.NilError(t, flags.Parse(args)) | ||
| return flags | ||
| } | ||
|
|
||
| // An explicit --remove-orphans flag always wins; without it the value comes | ||
| // from COMPOSE_REMOVE_ORPHANS in the process environment — which the root | ||
| // PersistentPreRunE completed with the project's local .env beforehand. | ||
| func TestRemoveOrphansFromEnv(t *testing.T) { | ||
| t.Run("explicit flag wins over the environment", func(t *testing.T) { | ||
| t.Setenv(ComposeRemoveOrphans, "true") | ||
| flags := removeOrphansFlagSet(t, "--remove-orphans=false") | ||
| assert.Equal(t, removeOrphansFromEnv(flags, false), false) | ||
| }) | ||
|
|
||
| t.Run("environment applies when the flag is not passed", func(t *testing.T) { | ||
| t.Setenv(ComposeRemoveOrphans, "true") | ||
| flags := removeOrphansFlagSet(t) | ||
| assert.Equal(t, removeOrphansFromEnv(flags, false), true) | ||
| }) | ||
|
|
||
| t.Run("defaults to false when neither is set", func(t *testing.T) { | ||
| t.Setenv(ComposeRemoveOrphans, "") | ||
| assert.NilError(t, os.Unsetenv(ComposeRemoveOrphans)) | ||
| flags := removeOrphansFlagSet(t) | ||
| assert.Equal(t, removeOrphansFromEnv(flags, false), false) | ||
| }) | ||
| } | ||
|
|
||
| func writeProjectWithDotEnv(t *testing.T, dotEnv string) ProjectOptions { | ||
| t.Helper() | ||
| dir := t.TempDir() | ||
| composePath := filepath.Join(dir, "compose.yaml") | ||
| assert.NilError(t, os.WriteFile(composePath, []byte("services: {}\n"), 0o600)) | ||
| assert.NilError(t, os.WriteFile(filepath.Join(dir, ".env"), []byte(dotEnv), 0o600)) | ||
| return ProjectOptions{ | ||
| ConfigPaths: []string{composePath}, | ||
| ProjectDir: dir, | ||
| } | ||
| } | ||
|
|
||
| // setEnvWithDotEnv turns the COMPOSE_* keys of the project's local .env into | ||
| // per-project defaults by completing the process environment — process | ||
| // values win, non-COMPOSE_ keys are left alone, and remote configs are | ||
| // deliberately excluded (COMPOSE_* variables are the local user's choice; a | ||
| // remote model must not steer the CLI consuming it). | ||
| func TestSetEnvWithDotEnv(t *testing.T) { | ||
| unset := func(t *testing.T, keys ...string) { | ||
| t.Helper() | ||
| for _, k := range keys { | ||
| t.Setenv(k, "") // registers restoration | ||
| assert.NilError(t, os.Unsetenv(k)) | ||
| } | ||
| } | ||
|
|
||
| t.Run("COMPOSE_ keys of the local .env are injected", func(t *testing.T) { | ||
| unset(t, ComposeRemoveOrphans, "NOT_COMPOSE_VAR") | ||
| opts := writeProjectWithDotEnv(t, "COMPOSE_REMOVE_ORPHANS=true\nNOT_COMPOSE_VAR=x\n") | ||
|
|
||
| assert.NilError(t, setEnvWithDotEnv(opts, nil)) | ||
|
|
||
| assert.Equal(t, os.Getenv(ComposeRemoveOrphans), "true") | ||
| _, injected := os.LookupEnv("NOT_COMPOSE_VAR") | ||
| assert.Check(t, !injected, "non-COMPOSE_ keys must not leak into the process environment") | ||
| }) | ||
|
|
||
| t.Run("process environment wins over the .env", func(t *testing.T) { | ||
| t.Setenv(ComposeRemoveOrphans, "false") | ||
| opts := writeProjectWithDotEnv(t, "COMPOSE_REMOVE_ORPHANS=true\n") | ||
|
|
||
| assert.NilError(t, setEnvWithDotEnv(opts, nil)) | ||
|
|
||
| assert.Equal(t, os.Getenv(ComposeRemoveOrphans), "false") | ||
| }) | ||
|
|
||
| t.Run("remote configs are excluded", func(t *testing.T) { | ||
| unset(t, ComposeRemoveOrphans) | ||
| opts := writeProjectWithDotEnv(t, "COMPOSE_REMOVE_ORPHANS=true\n") | ||
| opts.ConfigPaths = []string{"test://remote/compose.yaml"} | ||
| opts.remoteLoadersOverride = []loader.ResourceLoader{testRemoteLoader{}} | ||
|
|
||
| assert.NilError(t, setEnvWithDotEnv(opts, nil)) | ||
|
|
||
| _, injected := os.LookupEnv(ComposeRemoveOrphans) | ||
| assert.Check(t, !injected, "a remote model must not inject COMPOSE_* variables") | ||
| }) | ||
| } | ||
|
|
||
| // G.3 of epic #14074: the full resolution order, .env → process env → flag. | ||
| func TestRemoveOrphansResolutionOrder(t *testing.T) { | ||
| t.Setenv(ComposeRemoveOrphans, "") | ||
| assert.NilError(t, os.Unsetenv(ComposeRemoveOrphans)) | ||
| opts := writeProjectWithDotEnv(t, "COMPOSE_REMOVE_ORPHANS=true\n") | ||
|
|
||
| // .env applies when nothing else is set | ||
| assert.NilError(t, setEnvWithDotEnv(opts, nil)) | ||
| assert.Equal(t, removeOrphansFromEnv(removeOrphansFlagSet(t), false), true) | ||
|
|
||
| // an explicit flag beats both | ||
| assert.Equal(t, removeOrphansFromEnv(removeOrphansFlagSet(t, "--remove-orphans=false"), false), false) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[medium] Missing
ignoreOrphansvsremoveOrphansconflict guard inrunsubcommandThis PR correctly adds
removeOrphansFromEnvtorun'sPreRunEsoCOMPOSE_REMOVE_ORPHANSis now resolved forrun— but the corresponding conflict guard that bothupandcreatehave was not added torun'sRunE.In
up.go(line 129) andcreate.go(line 103):In
run.go'sRunE, afteroptions.ignoreOrphans = utils.StringToBool(project.Environment[ComposeIgnoreOrphans])(line 217), there is no equivalent check. Now thatremoveOrphansis properly resolved forrun, a user with bothCOMPOSE_IGNORE_ORPHANS=truein their project.envandCOMPOSE_REMOVE_ORPHANS=true(or--remove-orphans) will get conflicting options silently accepted forrun, whileupandcreatewould return an error.Fix: Add the same guard in
run.go'sRunEafteroptions.ignoreOrphansis assigned:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed and confirmed: run is the only command missing this guard; the fix above resolves it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for confirming! Adding the conflict guard to
runwill ensure consistent validation across all commands that support orphan handling.