Skip to content

Modern Docker Management, Designed for Everyone

fix: keep Compose formatting when applying tag updates (#4172)

Kyle Mendellb9d64a9f116f
diff --git a/backend/internal/project/project_update.go b/backend/internal/project/project_update.go
index 98e785e60..89f3c80fb 100644
--- a/backend/internal/project/project_update.go
+++ b/backend/internal/project/project_update.go
@@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"
"time"
+ "unicode/utf8"
"bytes"
"fmt"
@@ -1093,6 +1094,8 @@ func prepareProjectServiceImagesInternal(source []byte, effective *composetypes.
return nil, nil, errors.New("Compose source has no services mapping")
}
serviceNames := make([]string, 0, len(changes))
+ // Rewrite image scalars in place; re-encoding the node tree drops blank lines and operator formatting.
+ edits := make([]composeImageEditInternal, 0, len(changes))
for name, change := range changes {
service, ok := effective.Services[name]
if !ok {
@@ -1116,20 +1119,21 @@ func prepareProjectServiceImagesInternal(source []byte, effective *composetypes.
if !strings.Contains(imageNode.Value, "$") && refs.NormalizeImageUpdateRef(imageNode.Value) != expected && refs.NormalizeImageUpdateRef(imageNode.Value) != target {
return nil, nil, fmt.Errorf("service %s source image changed since Compose was loaded", name)
}
- imageNode.Value = change.TargetRef
+ edit, err := composeImageSourceEditInternal(source, imageNode, change.TargetRef)
+ if err != nil {
+ return nil, nil, fmt.Errorf("service %s: %w", name, err)
+ }
+ edits = append(edits, edit)
serviceNames = append(serviceNames, name)
}
slices.Sort(serviceNames)
- var output bytes.Buffer
- encoder := yaml.NewEncoder(&output)
- encoder.SetIndent(2)
- if err := encoder.Encode(&document); err != nil {
- return nil, nil, fmt.Errorf("encode updated Compose source: %w", err)
+ // Splice from the end so earlier spans keep their offsets; distinct scalars never overlap.
+ slices.SortFunc(edits, func(a, b composeImageEditInternal) int { return b.start - a.start })
+ updated := slices.Clone(source)
+ for _, edit := range edits {
+ updated = slices.Concat(updated[:edit.start], edit.text, updated[edit.end:])
}
- if err := encoder.Close(); err != nil {
- return nil, nil, fmt.Errorf("close updated Compose encoder: %w", err)
- }
- return output.Bytes(), serviceNames, nil
+ return updated, serviceNames, nil
}
func validateImageUpdateSourceInternal(node *yaml.Node) error {
@@ -1163,6 +1167,52 @@ func composeImageFieldInternal(node *yaml.Node, key string) *yaml.Node {
return nil
}
+// composeImageEditInternal is an image scalar's source byte span and its replacement text.
+type composeImageEditInternal struct {
+ start, end int
+ text []byte
+}
+
+// composeImageSourceEditInternal finds an image scalar's single-line source span using YAML's line-break rules
+// and renders the target in the scalar's original style.
+func composeImageSourceEditInternal(source []byte, node *yaml.Node, target string) (composeImageEditInternal, error) {
+ const lineBreaks = "\r\n\u0085\u2028\u2029"
+ edit := composeImageEditInternal{start: len(source) - len(bytes.TrimPrefix(source, []byte("\xEF\xBB\xBF")))}
+ for line, column := 1, 1; line < node.Line || (line == node.Line && column < node.Column); column++ {
+ if edit.start >= len(source) {
+ return edit, errors.New("image scalar position is outside the Compose source")
+ }
+ r, size := utf8.DecodeRune(source[edit.start:])
+ edit.start += size
+ if r == '\r' && bytes.HasPrefix(source[edit.start:], []byte("\n")) {
+ edit.start++
+ }
+ if strings.ContainsRune(lineBreaks, r) {
+ line, column = line+1, 0
+ }
+ }
+ // The shortest same-line span that decodes to the parsed value is the scalar's source text, including tags and escapes.
+ found := false
+ for edit.end = edit.start; !found && edit.end < len(source); {
+ r, size := utf8.DecodeRune(source[edit.end:])
+ if strings.ContainsRune(lineBreaks, r) {
+ break
+ }
+ edit.end += size
+ var decoded string
+ found = yaml.Unmarshal(source[edit.start:edit.end], &decoded) == nil && decoded == node.Value
+ }
+ if !found {
+ return edit, errors.New("tag updates require a single-line image scalar")
+ }
+ text, err := yaml.Marshal(&yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Style: node.Style, Value: target})
+ edit.text = bytes.TrimSuffix(text, []byte("\n"))
+ if err != nil || bytes.ContainsAny(edit.text, "\r\n") {
+ return edit, fmt.Errorf("image %s cannot be written as a single-line scalar", target)
+ }
+ return edit, nil
+}
+
func persistProjectServiceImagesInternal(ctx context.Context, projectPath, logical string, original, updated []byte) error {
entry, err := acfs.Stat(ctx, projectPath, logical, false)
if err != nil {
diff --git a/backend/internal/project/service_test.go b/backend/internal/project/service_test.go
index 77e609257..980e9310c 100644
--- a/backend/internal/project/service_test.go
+++ b/backend/internal/project/service_test.go
@@ -7270,16 +7270,19 @@ func TestProjectPathMapperUsesCurrentSettingsInternal(t *testing.T) {
}
func TestPrepareProjectServiceImages(t *testing.T) {
- source := []byte("# operator configuration\nservices:\n web:\n image: \"app:${VERSION}\" # keep this comment\n environment:\n VERSION: ${VERSION}\n worker:\n image: app:${VERSION}\n")
- effective := &composetypes.Project{Services: composetypes.Services{"web": {Image: "app:1.2.0"}, "worker": {Image: "app:1.2.0"}}}
- updated, names, err := prepareProjectServiceImagesInternal(source, effective, map[string]updatertypes.ServiceImageChange{"web": {ExpectedRef: "docker.io/library/app:1.2.0", TargetRef: "app:1.3.0"}})
- require.NoError(t, err)
- require.Equal(t, []string{"web"}, names)
- require.Contains(t, string(updated), "# operator configuration")
- require.Contains(t, string(updated), "# keep this comment")
- require.Contains(t, string(updated), "image: \"app:1.3.0\"")
- require.Contains(t, string(updated), "image: app:${VERSION}")
- require.Contains(t, string(updated), "VERSION: ${VERSION}")
+ source := []byte("\xEF\xBB\xBF# operator configuration\r\n\r\nservices:\r\n web:\r\n image: \"app:${VERSION}\" # keep this comment\r\n environment:\r\n VERSION: ${VERSION}\r\n\r\n worker:\r\n image: 'app:1.2.0' \r\n tagged:\r\n image: !!str app:1.2.0\r\n escaped:\r\n image: \"app\\u003a1.2.0\"\r\n untouched:\r\n image: app:1.2.0 # stays\r\n\r\r plain:\r\n image: app:1.2.0")
+ expected := "\xEF\xBB\xBF# operator configuration\r\n\r\nservices:\r\n web:\r\n image: \"app:1.3.0\" # keep this comment\r\n environment:\r\n VERSION: ${VERSION}\r\n\r\n worker:\r\n image: 'docker.io/library/app:1.3.0' \r\n tagged:\r\n image: !!str app:1.3.0\r\n escaped:\r\n image: \"app:1.3.0\"\r\n untouched:\r\n image: app:1.2.0 # stays\r\n\r\r plain:\r\n image: app:1.3.0"
+ effective := &composetypes.Project{Services: composetypes.Services{"web": {Image: "app:1.2.0"}, "worker": {Image: "app:1.2.0"}, "tagged": {Image: "app:1.2.0"}, "escaped": {Image: "app:1.2.0"}, "untouched": {Image: "app:1.2.0"}, "plain": {Image: "app:1.2.0"}}}
+ updated, names, err := prepareProjectServiceImagesInternal(source, effective, map[string]updatertypes.ServiceImageChange{
+ "web": {ExpectedRef: "docker.io/library/app:1.2.0", TargetRef: "app:1.3.0"},
+ "worker": {ExpectedRef: "app:1.2.0", TargetRef: "docker.io/library/app:1.3.0"},
+ "tagged": {ExpectedRef: "app:1.2.0", TargetRef: "app:1.3.0"},
+ "escaped": {ExpectedRef: "app:1.2.0", TargetRef: "app:1.3.0"},
+ "plain": {ExpectedRef: "app:1.2.0", TargetRef: "app:1.3.0"},
+ })
+ require.NoError(t, err)
+ require.Equal(t, []string{"escaped", "plain", "tagged", "web", "worker"}, names)
+ require.Equal(t, expected, string(updated))
require.Equal(t, "app:1.2.0", effective.Services["web"].Image)
}
@@ -7292,6 +7295,8 @@ func TestPrepareProjectServiceImagesRejectsUnsupportedSource(t *testing.T) {
{"image alias", "x-image: &image app:1.2.0\nservices:\n web:\n image: *image\n", "app:1.2.0"},
{"missing image", "services:\n web:\n build: .\n", "app:1.2.0"},
{"multiple documents", "services:\n web:\n image: app:1.2.0\n---\nservices: {}\n", "app:1.2.0"},
+ {"literal block image", "services:\n web:\n image: |\n app:1.2.0\n", "app:1.2.0"},
+ {"multi-line plain image", "services:\n web:\n image: app:${VERSION}\n tail\n", "app:1.2.0"},
} {
t.Run(tt.name, func(t *testing.T) {
effective := &composetypes.Project{Services: composetypes.Services{"web": {Image: "app:1.2.0"}}}
@@ -7379,7 +7384,7 @@ func TestUpdateProjectServiceImagesPersistsBeforeDeploymentAndRetries(t *testing
require.NoError(t, err)
require.NoError(t, settingsService.SetStringSetting(ctx, "projectsDirectory", directory))
projectPath := createComposeProjectDir(t, directory, "image-tags")
- source := "# keep\nservices:\n app:\n image: app:${VERSION}\n worker:\n image: app:${VERSION}\n"
+ source := "# keep\n\nservices:\n app:\n image: app:${VERSION}\n\n worker:\n image: app:${VERSION}\n"
require.NoError(t, os.WriteFile(filepath.Join(projectPath, "compose.yaml"), []byte(source), 0o600))
require.NoError(t, os.Chmod(filepath.Join(projectPath, "compose.yaml"), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(projectPath, ".env"), []byte("VERSION=1.2.0\n"), 0o600))
@@ -7394,8 +7399,7 @@ func TestUpdateProjectServiceImagesPersistsBeforeDeploymentAndRetries(t *testing
require.ErrorIs(t, err, deploymentError)
content, err := os.ReadFile(filepath.Join(projectPath, "compose.yaml"))
require.NoError(t, err)
- require.Contains(t, string(content), "image: app:1.3.0")
- require.Contains(t, string(content), "image: app:${VERSION}")
+ require.Equal(t, "# keep\n\nservices:\n app:\n image: app:1.3.0\n\n worker:\n image: app:${VERSION}\n", string(content))
mode, err := os.Stat(filepath.Join(projectPath, "compose.yaml"))
require.NoError(t, err)
require.Equal(t, os.FileMode(0o600), mode.Mode().Perm())
@@ -7585,7 +7589,7 @@ func TestProjectServiceManualUpdateDiscoversTags(t *testing.T) {
content, err := os.ReadFile(filepath.Join(path, "compose.yaml"))
require.NoError(t, err)
if tt.wantChanged {
- require.Contains(t, string(content), tt.wantRef)
+ require.Equal(t, strings.Replace(source, "image: app:1.2.0", "image: "+tt.wantRef, 1), string(content))
} else {
require.Equal(t, source, string(content))
}