Repository navigation
Description
What happened?
Feature description
When the scanner finds a file whose name is NFD-normalized and a file with the same name in NFC form already exists in the same directory, it reports:
normalizing path: item has UTF8 encoding conflict with another item
The file then stays in "Failed items" on every scan until someone fixes it by hand.
I'd like Syncthing to handle the one case where this can be done without any risk of data loss: both items are regular files, they have the same size, and their contents are byte-for-byte identical. In that case the non-normalized copy carries no information of its own, so Syncthing could remove it, log what it did, and scan the normalized copy as usual.
Every other case would stay exactly as it is today (directories, symlinks, files whose contents differ): nothing is touched on disk and the existing error is reported.
Problem or use case
Syncthing on Windows, folder on a local NTFS drive. The affected files were most likely transferred from an iPhone (scans made with the iOS document scanner, plus PDFs received on the phone).
9 items failed with the error above. Names below are anonymized, but the accents are the same as in the real files:
<folder>\old\person-A\Attestation chômage.pdf
<folder>\old\person-A\Avis impôt.pdf
<folder>\old\person-A\Impôt.pdf
<folder>\old\person-B\Dossier locataire Maëlys.pdf
<folder>\old\person-C\scans\Document numérisé 13.pdf
<folder>\old\person-C\scans\Document numérisé 22.pdf
<folder>\old\person-C\scans\Document numérisé 24.pdf
<folder>\old\person-C\scans\Document numérisé 25.pdf
<folder>\old\person-C\scans\Document numérisé 26.pdf
Each of these directories actually contained two files with the same visible name, one in NFC and one in NFD, with the same size and the same modification time. In Windows Explorer they just look like the same file listed twice, and the GUI gives no hint that duplicates are involved. As a regular user, the error message alone did not tell me what to do. I only understood the cause after searching for a long time.
From what I found, this comes up regularly (for example #10314, #8128, #6929). The answer there is that Syncthing can't resolve this automatically without risking data loss. That's true in general, but not when both copies are identical, which is probably the most common case: the same file arriving twice, once from an Apple device and once from somewhere else.
This is unrelated to #9539, which is about reserved characters ("*:<>?|) on NTFS/FAT, not Unicode normalization.
Alternatives or workarounds
I fixed it with a PowerShell script that strips accents from file names. Because the second file of each pair could not take the same name, the script renamed it with a -doublon suffix. Sync resumed right after. I then compared each pair with Get-FileHash, and once I had confirmed they were identical I deleted the duplicates.
This works, but it requires knowing about Unicode normalization, which most users don't. It also renames files that didn't need it (the script stripped accents from every file in the folder, not only the conflicting ones).
Another option would be to only improve the error message, for example: "another file with the same name but a different Unicode normalization exists in this directory", with both names shown. That would already help a lot, even without automatic cleanup.
Possible implementation
I tried this in a fork. The check runs in applyNormalization in lib/scanner/walk.go, after the existing SameFile check. So on normalization-insensitive filesystems (ZFS, APFS), where both names point to the same file, the existing temp-rename path still applies and nothing is removed.
Diff (lib/scanner/walk.go)
@@ imports
import (
+ "bytes"
"context"
"errors"
"fmt"
+ "io"
"log/slog"
@@ func (w *walker) walkAndHashFiles(...)
path, err = w.applyNormalization(nonNormPath, path, info)
if err != nil {
handleError(ctx, "normalizing path", nonNormPath, err, finishedChan)
return skip
}
+ if path == "" {
+ // The item was a redundant duplicate of the normalized one
+ // and has been removed. The normalized item is handled on
+ // its own when the walk gets to it.
+ return skip
+ }
}
@@ func (w *walker) applyNormalization(path, normPath string, info fs.FileInfo) (string, error)
// applyNormalization fixes the normalization of the file on disk, i.e. ensures
// the file at path ends up named normPath. It shouldn't but may happen that the
// file ends up with a different name, in which case that one should be scanned.
+// An empty name and no error is returned when the file at path was a redundant
+// duplicate of the one at normPath and was removed, in which case there is
+// nothing to scan.
...
- // There is something already in the way at the normalized
- // file name.
+ // There is something already in the way at the normalized file name.
+ // This typically happens when the same file was copied both from a
+ // system using NFD (macOS, iOS) and one using NFC. If both are regular
+ // files with the exact same contents, the non-normalized one carries
+ // no information of its own and can be removed without data loss.
+ if info.IsRegular() && normInfo.IsRegular() && info.Size() == normInfo.Size() {
+ same, err := sameContents(w.Filesystem, path, normPath)
+ if err != nil {
+ return "", err
+ }
+ if same {
+ if err := w.Filesystem.Remove(path); err != nil {
+ return "", err
+ }
+ slog.Info("Removed file with non-normalized UTF8 encoding, identical to the normalized one", slogutil.FilePath(path))
+ return "", nil
+ }
+ }
return "", errUTF8Conflict
}
+// sameContents returns true if the two files have exactly the same contents.
+func sameContents(filesystem fs.Filesystem, a, b string) (bool, error) {
+ fa, err := filesystem.Open(a)
+ if err != nil {
+ return false, err
+ }
+ defer fa.Close()
+ fb, err := filesystem.Open(b)
+ if err != nil {
+ return false, err
+ }
+ defer fb.Close()
+
+ const bufSize = 128 << 10
+ bufA := make([]byte, bufSize)
+ bufB := make([]byte, bufSize)
+ for {
+ na, errA := io.ReadFull(fa, bufA)
+ nb, errB := io.ReadFull(fb, bufB)
+ if !bytes.Equal(bufA[:na], bufB[:nb]) {
+ return false, nil
+ }
+ eofA := errors.Is(errA, io.EOF) || errors.Is(errA, io.ErrUnexpectedEOF)
+ eofB := errors.Is(errB, io.EOF) || errors.Is(errB, io.ErrUnexpectedEOF)
+ switch {
+ case errA != nil && !eofA:
+ return false, errA
+ case errB != nil && !eofB:
+ return false, errB
+ case eofA || eofB:
+ return eofA && eofB, nil
+ }
+ }
+}Test (lib/scanner/walk_test.go)
func TestNormalizationConflictIdenticalContents(t *testing.T) {
if build.IsDarwin {
t.Skip("Normalization test not possible on darwin")
return
}
testFs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(16)+"?content=true&nostfolder=true")
const (
inNFC = "\xC3\x84"
inNFD = "\x41\xCC\x88"
)
// Same name in both normalizations with identical contents: the
// non-normalized duplicate is removed.
same := []byte("same contents")
fs.WriteFile(testFs, "same-"+inNFC, same, 0o644)
fs.WriteFile(testFs, "same-"+inNFD, same, 0o644)
// Same name in both normalizations with different contents: nothing is
// touched and the conflict is reported.
fs.WriteFile(testFs, "diff-"+inNFC, []byte("contents one"), 0o644)
fs.WriteFile(testFs, "diff-"+inNFD, []byte("contents two"), 0o644)
cfg, cancel := testConfig()
defer cancel()
cfg.Filesystem = testFs
cfg.AutoNormalize = true
var names []string
var errs []string
for res := range Walk(context.TODO(), cfg) {
if res.Err != nil {
if !errors.Is(res.Err, errUTF8Conflict) {
t.Errorf("unexpected error for %q: %v", res.Path, res.Err)
}
errs = append(errs, res.Path)
continue
}
names = append(names, res.File.Name)
}
slices.Sort(names)
if expected := []string{"diff-" + inNFC, "same-" + inNFC}; !slices.Equal(names, expected) {
t.Errorf("expected scanned files %q, got %q", expected, names)
}
if expected := []string{"diff-" + inNFD}; !slices.Equal(errs, expected) {
t.Errorf("expected conflict errors for %q, got %q", expected, errs)
}
if _, err := testFs.Lstat("same-" + inNFD); !fs.IsNotExist(err) {
t.Errorf("expected identical non-normalized duplicate to be removed, got %v", err)
}
for _, name := range []string{"same-" + inNFC, "diff-" + inNFC, "diff-" + inNFD} {
if _, err := testFs.Lstat(name); err != nil {
t.Errorf("expected %q to be kept: %v", name, err)
}
}
}The test fails without the change. go test ./lib/scanner/ ./lib/model/ passes with it.
Syncthing version
v2.1.1
Platform & operating system
Windows serveur 2026
Browser version
No response
Activity