Skip to content

Commit f92beea

Browse files
committed
fix(sync): honor remote preference during initial downloads
Force remote-preferred archive extraction only during initial sync, restoring the normal newer-local-file guard afterward. Cover all initial-sync strategies and subsequent-download protection with a real downstream archive regression test, and clarify the public documentation. Fixes #3351
1 parent 8ff6260 commit f92beea

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

‎docs/pages/configuration/dev/connections/file-sync.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ The `initialSync` option expects a string with an initial sync strategy. The fol
294294

295295
`disabled` disabled the initial sync completely
296296

297+
:::caution Remote-preferred conflicts
298+
When resolving an initial-sync conflict, `mirrorRemote` and `preferRemote` replace the local file even if its last-modified timestamp is newer than the container file's timestamp. Save any local changes you want to keep before starting the sync. Use `preferNewest` to resolve conflicts by modification time instead.
299+
300+
This remote preference applies only to initial sync. Subsequent downloads retain the normal protection against overwriting newer local edits.
301+
:::
302+
297303
```yaml
298304
deployments:
299305
app-backend:

‎pkg/devspace/sync/initial_test.go‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package sync
2+
3+
import (
4+
"context"
5+
"net"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
"time"
10+
11+
"github.com/loft-sh/devspace/helper/remote"
12+
"github.com/loft-sh/devspace/helper/server"
13+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
14+
"github.com/loft-sh/devspace/pkg/util/log"
15+
)
16+
17+
func TestInitialSyncRemotePreference(t *testing.T) {
18+
for _, testCase := range []struct {
19+
strategy latest.InitialSyncStrategy
20+
want string
21+
}{
22+
{latest.InitialSyncStrategyMirrorRemote, "remote"},
23+
{latest.InitialSyncStrategyPreferRemote, "remote"},
24+
{latest.InitialSyncStrategyPreferNewest, "local"},
25+
{latest.InitialSyncStrategyMirrorLocal, "local"},
26+
{latest.InitialSyncStrategyPreferLocal, "local"},
27+
{latest.InitialSyncStrategyKeepAll, "local"},
28+
{latest.InitialSyncStrategyDisabled, "local"},
29+
} {
30+
t.Run(string(testCase.strategy), func(t *testing.T) {
31+
local, remotePath := t.TempDir(), t.TempDir()
32+
localFile := filepath.Join(local, "conflict.txt")
33+
remoteFile := filepath.Join(remotePath, "conflict.txt")
34+
writeFile := func(filename, content string, modified time.Time) {
35+
t.Helper()
36+
if err := os.WriteFile(filename, []byte(content), 0644); err != nil {
37+
t.Fatal(err)
38+
}
39+
if err := os.Chtimes(filename, modified, modified); err != nil {
40+
t.Fatal(err)
41+
}
42+
}
43+
writeFile(localFile, "local", time.Unix(2000000000, 0))
44+
writeFile(remoteFile, "remote", time.Unix(1700000000, 0))
45+
46+
client, err := NewSync(context.Background(), local, Options{
47+
InitialSync: testCase.strategy,
48+
UpstreamDisabled: true,
49+
Log: log.Discard,
50+
})
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
t.Cleanup(func() { client.Stop(nil) })
55+
// Download-only sync still marks the upstream initial-sync state complete.
56+
client.upstream = &upstream{}
57+
58+
clientConn, serverConn := net.Pipe()
59+
t.Cleanup(func() {
60+
_ = clientConn.Close()
61+
_ = serverConn.Close()
62+
})
63+
go func() {
64+
if err := server.StartDownstreamServer(serverConn, serverConn, &server.DownstreamOptions{
65+
RemotePath: remotePath,
66+
Polling: true,
67+
}); err != nil {
68+
panic(err)
69+
}
70+
}()
71+
if err := client.InitDownstream(clientConn, clientConn); err != nil {
72+
t.Fatal(err)
73+
}
74+
uploadDone := make(chan struct{})
75+
if err := client.initialSync(uploadDone, nil); err != nil {
76+
t.Fatal(err)
77+
}
78+
<-uploadDone
79+
content, err := os.ReadFile(localFile)
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
if string(content) != testCase.want {
84+
t.Errorf("initial sync: got %q, want %q", content, testCase.want)
85+
}
86+
87+
// Later downloads must continue to protect a newer, pending local edit.
88+
writeFile(localFile, "pending local edit", time.Unix(2000000000, 0))
89+
writeFile(remoteFile, "later remote edit", time.Unix(1700000001, 0))
90+
if err := client.downstream.applyChanges([]*remote.Change{{
91+
ChangeType: remote.ChangeType_CHANGE,
92+
Path: "/conflict.txt",
93+
}}, false); err != nil {
94+
t.Fatal(err)
95+
}
96+
content, err = os.ReadFile(localFile)
97+
if err != nil {
98+
t.Fatal(err)
99+
}
100+
if string(content) != "pending local edit" {
101+
t.Errorf("normal sync overwrote a newer local edit: %q", content)
102+
}
103+
})
104+
}
105+
}

‎pkg/devspace/sync/sync.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ func (s *Sync) startDownstream() {
275275
}
276276

277277
func (s *Sync) initialSync(onInitUploadDone chan struct{}, onInitDownloadDone chan struct{}) error {
278+
// Remote preference overrides mtimes only during initial sync, not while watching.
279+
s.downstream.unarchiver.forceOverride = s.Options.InitialSync == latest.InitialSyncStrategyMirrorRemote || s.Options.InitialSync == latest.InitialSyncStrategyPreferRemote
280+
defer func() {
281+
s.downstream.unarchiver.forceOverride = false
282+
}()
283+
278284
initialSync := newInitialSyncer(&initialSyncOptions{
279285
LocalPath: s.LocalPath,
280286
Strategy: s.Options.InitialSync,

0 commit comments

Comments
 (0)