Repository: syncthing/syncthing (language: go)

Issue:
Different lengths used for short device IDs in UI
### Does your log mention database corruption?
No

### Include required information

Please be sure to include at least:

 - which version of Syncthing and what operating system you are using
   - 1.27.1, but it's even in `main`
 - what happened
   - IDs are of different length from API and in client in specific sections
 - what you expected to happen instead
   - same-length IDs

https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/lib/protocol/deviceid.go#L18 says 7 and therefore from, let's say `/rest/system/connections` I get first 7 characters as if split on `-`
https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/gui/default/syncthing/core/syncthingController.js#L1476 and https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/gui/default/syncthing/core/syncthingController.js#L1493 however https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/gui/default/syncthing/core/syncthingController.js#L1463 has the matching one and https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/gui/default/index.html#L913 has even less than both - `5`

Shouldn't it be the same everywhere? Would you welcome a patch or is this something you are aware of and it's kept because of some backward compatibility reason?
Different lengths used for short device IDs in UI
### Does your log mention database corruption?
No

### Include required information

Please be sure to include at least:

 - which version of Syncthing and what operating system you are using
   - 1.27.1, but it's even in `main`
 - what happened
   - IDs are of different length from API and in client in specific sections
 - what you expected to happen instead
   - same-length IDs

https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/lib/protocol/deviceid.go#L18 says 7 and therefore from, let's say `/rest/system/connections` I get first 7 characters as if split on `-`
https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/gui/default/syncthing/core/syncthingController.js#L1476 and https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/gui/default/syncthing/core/syncthingController.js#L1493 however https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/gui/default/syncthing/core/syncthingController.js#L1463 has the matching one and https://github.com/syncthing/syncthing/blob/86a08eb87df2aa6cf1ab631120a0b918617a98b3/gui/default/index.html#L913 has even less than both - `5`

Shouldn't it be the same everywhere? Would you welcome a patch or is this something you are aware of and it's kept because of some backward compatibility reason?


Discussion / hints:
That very much looks unintentional indeed. And at first glance it's all "presentation" strings, so no other logic should depend on it. As in yes, a patch would be welcome.
That very much looks unintentional indeed. And at first glance it's all "presentation" strings, so no other logic should depend on it. As in yes, a patch would be welcome.

Fix patch:
diff --git a/gui/default/index.html b/gui/default/index.html
index 729e50580b7..6e26461cef3 100644
--- a/gui/default/index.html
+++ b/gui/default/index.html
@@ -910,7 +910,7 @@ <h4 class="panel-title">
                       </tr>
                       <tr ng-if="deviceCfg.introducedBy">
                         <th><span class="far fa-fw fa-handshake-o"></span>&nbsp;<span translate>Introduced By</span></th>
-                        <td class="text-right">{{ deviceName(devices[deviceCfg.introducedBy]) || deviceCfg.introducedBy.substring(0, 5) }}</td>
+                        <td class="text-right">{{ deviceName(devices[deviceCfg.introducedBy]) || deviceShortID(deviceCfg.introducedBy) }}</td>
                       </tr>
                       <tr ng-if="deviceCfg.autoAcceptFolders">
                         <th><span class="fa fa-fw fa-level-down"></span>&nbsp;<span translate>Auto Accept</span></th>
diff --git a/gui/default/syncthing/app.js b/gui/default/syncthing/app.js
index 0a961f50869..2731445cd6d 100644
--- a/gui/default/syncthing/app.js
+++ b/gui/default/syncthing/app.js
@@ -18,6 +18,9 @@ var syncthing = angular.module('syncthing', [
 var urlbase = 'rest';
 var authUrlbase = urlbase + '/noauth/auth';
 
+// keep consistent with ShortIDStringLength in lib/protocol/deviceid.go
+var shortIDStringLength = 7;
+
 syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvider) {
     // language and localisation
 
diff --git a/gui/default/syncthing/core/syncthingController.js b/gui/default/syncthing/core/syncthingController.js
index 32fca420e26..b31f9418830 100755
--- a/gui/default/syncthing/core/syncthingController.js
+++ b/gui/default/syncthing/core/syncthingController.js
@@ -1460,7 +1460,7 @@ angular.module('syncthing.core')
 
         $scope.friendlyNameFromShort = function (shortID) {
             var matches = Object.keys($scope.devices).filter(function (id) {
-                return id.substr(0, 7) === shortID;
+                return id.substr(0, shortIDStringLength) === shortID;
             });
             if (matches.length !== 1) {
                 return shortID;
@@ -1473,7 +1473,7 @@ angular.module('syncthing.core')
             if (match) {
                 return $scope.deviceName(match);
             }
-            return deviceID.substr(0, 6);
+            return deviceID.substr(0, shortIDStringLength);
         };
 
         $scope.deviceName = function (deviceCfg) {
@@ -1490,7 +1490,7 @@ angular.module('syncthing.core')
             if (typeof deviceID === 'undefined') {
                 return "";
             }
-            return deviceID.substr(0, 6);
+            return deviceID.substr(0, shortIDStringLength);
         };
 
         $scope.thisDeviceName = function () {
@@ -1501,7 +1501,7 @@ angular.module('syncthing.core')
             if (device.name) {
                 return device.name;
             }
-            return device.deviceID.substr(0, 6);
+            return device.deviceID.substr(0, shortIDStringLength);
         };
 
         $scope.showDeviceIdentification = function (deviceCfg) {
diff --git a/lib/protocol/deviceid.go b/lib/protocol/deviceid.go
index fee2f6d65e1..25ecbc1a33d 100644
--- a/lib/protocol/deviceid.go
+++ b/lib/protocol/deviceid.go
@@ -14,7 +14,8 @@ import (
 )
 
 const (
-	DeviceIDLength      = 32
+	DeviceIDLength = 32
+	// keep consistent with shortIDStringLength in gui/default/syncthing/app.js
 	ShortIDStringLength = 7
 )
 
diff --git a/next-gen-gui/src/app/api-utils.ts b/next-gen-gui/src/app/api-utils.ts
index 13d4137c725..a75e216ea16 100644
--- a/next-gen-gui/src/app/api-utils.ts
+++ b/next-gen-gui/src/app/api-utils.ts
@@ -1,6 +1,7 @@
 import { environment } from '../environments/environment'
 
 export const deviceID = (): String => {
+    // keep consistent with ShortIDStringLength in lib/protocol/deviceid.go
     return environment.production ? globalThis.metadata['deviceIDShort'] : '1234567';
 }
 


Test patch:
diff --git a/lib/protocol/deviceid_test.go b/lib/protocol/deviceid_test.go
index 90fa3e000ec..00d09e4a3d5 100644
--- a/lib/protocol/deviceid_test.go
+++ b/lib/protocol/deviceid_test.go
@@ -84,6 +84,7 @@ func TestShortIDString(t *testing.T) {
 	id, _ := DeviceIDFromString(formatted)
 
 	sid := id.Short().String()
+	// keep consistent with ShortIDStringLength in lib/protocol/deviceid.go
 	if len(sid) != 7 {
 		t.Errorf("Wrong length for short ID: got %d, want 7", len(sid))
 	}