Skip to content

Commit d10c07d

Browse files
authored
Fixes for version collisions when generating global version (#20544)
* Fixes for version collisions when generating global version 1. When adding global version, task versions initalized to major version 0 instead of major version 2. To avoid collisions, when determining global version, we need to look at versions across all tasks, not just the one being built * cleanup
1 parent e0b1a32 commit d10c07d

File tree

1 file changed

+78
-35
lines changed

1 file changed

+78
-35
lines changed

BuildConfigGen/Program.cs

+78-35
Original file line numberDiff line numberDiff line change
@@ -155,57 +155,97 @@ private static void MainInner(string? task, string? configs, int? currentSprintN
155155
Dictionary<string, TaskStateStruct> taskVersionInfo = [];
156156

157157
{
158-
var tasks = MakeOptionsReader.ReadMakeOptions(gitRootPath).AsEnumerable()
158+
IEnumerable<KeyValuePair<string, MakeOptionsReader.AgentTask>> allTasksList = MakeOptionsReader.ReadMakeOptions(gitRootPath).AsEnumerable()
159159
.Where(c => c.Value.Configs.Any()); // only tasks with configs
160160

161-
if (!allTasks)
162-
{
163-
var taskList = task!.Split(',', '|');
161+
IEnumerable<KeyValuePair<string, MakeOptionsReader.AgentTask>> tasks;
164162

165-
tasks = tasks.Where(s => taskList.Where(tl => string.Equals(tl, s.Key, StringComparison.OrdinalIgnoreCase)).Any());
163+
if (allTasks)
164+
{
165+
tasks = allTasksList;
166166
}
167-
168-
foreach (var t in tasks)
167+
else
169168
{
170-
taskVersionInfo.Add(t.Value.Name, new TaskStateStruct([], []));
169+
var taskList = task!.Split(',', '|');
170+
tasks = allTasksList.Where(s => taskList.Where(tl => string.Equals(tl, s.Key, StringComparison.OrdinalIgnoreCase)).Any());
171171
}
172172

173-
foreach (var t in tasks)
173+
if (includeLocalPackagesBuildConfig)
174174
{
175-
IEnumerable<string> configsList = FilterConfigsForTask(configs, t);
176-
HashSet<Config.ConfigRecord> targetConfigs = GetConfigRecords(configsList, writeUpdates);
177-
UpdateVersionsForTask(t.Value.Name, taskVersionInfo[t.Value.Name], targetConfigs, currentSprint, globalVersionPath, ref maxPatchForCurrentSprint, globalVersion, generatedFolder);
178-
CheckForDuplicates(t.Value.Name, taskVersionInfo[t.Value.Name].configTaskVersionMapping);
179-
}
175+
Console.WriteLine("Updating global version...");
176+
var tasksNeedingUpdates = new List<string>();
177+
178+
// when generating global version, we must enumerate all tasks to generate correct maxPatchForCurrentSprint across all tasks to avoid collisions
179+
foreach (var t in allTasksList)
180+
{
181+
taskVersionInfo.Add(t.Value.Name, new TaskStateStruct([], []));
182+
IEnumerable<string> configsList = FilterConfigsForTask(configs, t);
183+
HashSet<Config.ConfigRecord> targetConfigs = GetConfigRecords(configsList, writeUpdates);
184+
UpdateVersionsForTask(t.Value.Name, taskVersionInfo[t.Value.Name], targetConfigs, currentSprint, globalVersionPath, globalVersion, generatedFolder);
180185

181-
// bump patch number for global if any tasks invalidated.
182-
if (taskVersionInfo.Values.Any(x => x.versionsUpdated.Any()))
186+
bool taskTargettedForUpdating = allTasks || tasks.Where(x => x.Key == t.Value.Name).Any();
187+
bool taskVersionsNeedUpdating = taskVersionInfo[t.Value.Name].versionsUpdated.Any();
188+
189+
if (taskVersionsNeedUpdating && !taskTargettedForUpdating)
190+
{
191+
tasksNeedingUpdates.Add(t.Value.Name);
192+
}
193+
194+
UpdateMaxPatchForSprint(taskVersionInfo[t.Value.Name], currentSprint, ref maxPatchForCurrentSprint);
195+
CheckForDuplicates(t.Value.Name, taskVersionInfo[t.Value.Name].configTaskVersionMapping);
196+
}
197+
198+
if (tasksNeedingUpdates.Count > 0)
199+
{
200+
throw new Exception($"The following tasks have versions that need updating (needed for updating global version): {string.Join(", ", tasksNeedingUpdates)}. Please run 'node make.js build --task [taskname]' to update");
201+
}
202+
203+
// bump patch number for global if any tasks invalidated or if there is no existing global version
204+
if (taskVersionInfo.Values.Any(x => x.versionsUpdated.Any()) || globalVersion is null)
205+
{
206+
maxPatchForCurrentSprint = maxPatchForCurrentSprint + 1;
207+
208+
Console.WriteLine($"Global version: maxPatchForCurrentSprint = maxPatchForCurrentSprint + 1");
209+
}
210+
211+
Console.WriteLine($"Global version update: globalVersion = {globalVersion} maxPatchForCurrentSprint={maxPatchForCurrentSprint}" );
212+
}
213+
else
183214
{
184-
maxPatchForCurrentSprint = maxPatchForCurrentSprint + 1;
215+
foreach (var t in tasks)
216+
{
217+
taskVersionInfo.Add(t.Value.Name, new TaskStateStruct([], []));
218+
IEnumerable<string> configsList = FilterConfigsForTask(configs, t);
219+
HashSet<Config.ConfigRecord> targetConfigs = GetConfigRecords(configsList, writeUpdates);
220+
UpdateVersionsForTask(t.Value.Name, taskVersionInfo[t.Value.Name], targetConfigs, currentSprint, globalVersionPath, globalVersion, generatedFolder);
221+
CheckForDuplicates(t.Value.Name, taskVersionInfo[t.Value.Name].configTaskVersionMapping);
222+
}
185223
}
186224

187225
foreach (var t in tasks)
188226
{
189227
IEnumerable<string> configsList = FilterConfigsForTask(configs, t);
190228

229+
int taskMajorVersion = taskVersionInfo[t.Value.Name].configTaskVersionMapping[Config.Default].Major;
230+
191231
if (includeLocalPackagesBuildConfig)
192232
{
193233
if (globalVersion is null)
194234
{
195-
globalVersion = new TaskVersion(0, currentSprint, maxPatchForCurrentSprint);
235+
globalVersion = new TaskVersion(taskMajorVersion, currentSprint, maxPatchForCurrentSprint);
196236
}
197237
else
198238
{
199239
if (globalVersion.Minor == currentSprint)
200240
{
201241
globalVersion = globalVersion.CloneWithMinorAndPatch(currentSprint, Math.Max(maxPatchForCurrentSprint, globalVersion.Patch));
202-
globalVersion = globalVersion.CloneWithMajor(taskVersionInfo[t.Value.Name].configTaskVersionMapping[Config.Default].Major);
242+
globalVersion = globalVersion.CloneWithMajor(taskMajorVersion);
203243
}
204244
else
205245
{
206246
// this could fail if there is a task with a future-sprint version, which should not be the case. If that happens, CheckForDuplicates will throw
207247
globalVersion = globalVersion.CloneWithMinorAndPatch(currentSprint, 0);
208-
globalVersion = globalVersion.CloneWithMajor(taskVersionInfo[t.Value.Name].configTaskVersionMapping[Config.Default].Major);
248+
globalVersion = globalVersion.CloneWithMajor(taskMajorVersion);
209249
}
210250
}
211251
}
@@ -226,7 +266,7 @@ private static void MainInner(string? task, string? configs, int? currentSprintN
226266
}
227267

228268
ThrowWithUserFriendlyErrorToRerunWithWriteUpdatesIfVeriferError("(global)", skipContentCheck: false);
229-
269+
230270
foreach (var t in tasks)
231271
{
232272
IEnumerable<string> configsList = FilterConfigsForTask(configs, t);
@@ -1035,7 +1075,7 @@ private static void CopyConfig(string gitRootPath, string taskTargetPathOrUnders
10351075
}
10361076
}
10371077

1038-
private static void UpdateVersionsForTask(string task, TaskStateStruct taskState, HashSet<Config.ConfigRecord> targetConfigs, int currentSprint, string globalVersionPath, ref int maxPatchForCurrentSprint, TaskVersion? globalVersion, string generatedFolder)
1078+
private static void UpdateVersionsForTask(string task, TaskStateStruct taskState, HashSet<Config.ConfigRecord> targetConfigs, int currentSprint, string globalVersionPath, TaskVersion? globalVersion, string generatedFolder)
10391079
{
10401080
string currentDir = Environment.CurrentDirectory;
10411081
string gitRootPath = GitUtil.GetGitRootPath(currentDir);
@@ -1102,23 +1142,23 @@ private static void UpdateVersionsForTask(string task, TaskStateStruct taskState
11021142
}
11031143
}
11041144

1105-
TaskVersion baseVersion = maxVersion;
1145+
if (!allConfigsMappedAndValid)
1146+
{
1147+
TaskVersion baseVersion = maxVersion;
11061148

1107-
bool baseVersionIsCurrentSprint = baseVersion.Minor == currentSprint;
1149+
bool baseVersionIsCurrentSprint = baseVersion.Minor == currentSprint;
11081150

1109-
int offset = 0;
1151+
int offset = 0;
11101152

1111-
if (baseVersionIsCurrentSprint)
1112-
{
1113-
offset = 1;
1114-
}
1115-
else
1116-
{
1117-
baseVersion = inputVersion.CloneWithMinorAndPatch(currentSprint, 0);
1118-
}
1153+
if (baseVersionIsCurrentSprint)
1154+
{
1155+
offset = 1;
1156+
}
1157+
else
1158+
{
1159+
baseVersion = inputVersion.CloneWithMinorAndPatch(currentSprint, 0);
1160+
}
11191161

1120-
if (!allConfigsMappedAndValid)
1121-
{
11221162
var old = new Dictionary<Config.ConfigRecord, TaskVersion>();
11231163
foreach (var x in taskState.configTaskVersionMapping)
11241164
{
@@ -1181,7 +1221,10 @@ private static void UpdateVersionsForTask(string task, TaskStateStruct taskState
11811221
}
11821222
}
11831223
}
1224+
}
11841225

1226+
private static void UpdateMaxPatchForSprint(TaskStateStruct taskState, int currentSprint, ref int maxPatchForCurrentSprint)
1227+
{
11851228
foreach (var x in taskState.configTaskVersionMapping)
11861229
{
11871230
// accumulate the max patch, excluding existing globalversion (used for providing a new global version)

0 commit comments

Comments
 (0)