From f9c062c96245ca30ada6b9ad671a5c6c00f84c80 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 24 Sep 2025 13:55:31 +0200 Subject: [PATCH 1/7] Adjust Json PostAction to play nicer with needed usage of global.json --- .../AddJsonPropertyPostActionProcessor.cs | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs index 80ca23402914..5de5ba6226e2 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs +++ b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs @@ -20,6 +20,8 @@ internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase private const string NewJsonPropertyNameArgument = "newJsonPropertyName"; private const string NewJsonPropertyValueArgument = "newJsonPropertyValue"; private const string DetectRepoRootForFileCreation = "detectRepositoryRootForFileCreation"; + private const string IncludeAllDirectoriesInSearch = "includeAllDirectoriesInSearch"; + private const string IncludeAllParentDirectoriesInSearch = "includeAllParentDirectoriesInSearch"; private static readonly JsonSerializerOptions SerializerOptions = new() { @@ -87,7 +89,30 @@ protected override bool ProcessInternal( return false; } - IReadOnlyList jsonFiles = FindFilesInCurrentFolderOrParentFolder(environment.Host.FileSystem, outputBasePath, jsonFileName); + if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRootForFileCreation, "false"), out bool detectRepoRoot)) + { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRootForFileCreation)); + return false; + } + + if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllDirectoriesInSearch, "true"), out bool includeAllDirectories)) + { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllDirectoriesInSearch)); + return false; + } + + if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllParentDirectoriesInSearch, "false"), out bool includeAllParentDirectories)) + { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllParentDirectoriesInSearch)); + return false; + } + + IReadOnlyList jsonFiles = FindFilesInCurrentFolderOrParentFolder( + environment.Host.FileSystem, + outputBasePath, + jsonFileName, + includeAllDirectories ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories, + includeAllParentDirectories ? int.MaxValue : 1); if (jsonFiles.Count == 0) { @@ -103,12 +128,6 @@ protected override bool ProcessInternal( return false; } - if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRootForFileCreation, "false"), out bool detectRepoRoot)) - { - Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRootForFileCreation)); - return false; - } - string newJsonFilePath = Path.Combine(detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : outputBasePath, jsonFileName); environment.Host.FileSystem.WriteAllText(newJsonFilePath, "{}"); jsonFiles = new List { newJsonFilePath }; @@ -216,7 +235,9 @@ protected override bool ProcessInternal( private static string[] FindFilesInCurrentFolderOrParentFolder( IPhysicalFileSystem fileSystem, string startPath, - string matchPattern) + string matchPattern, + SearchOption searchOption, + int maxUpLevels) { string? directory = fileSystem.DirectoryExists(startPath) ? startPath : Path.GetDirectoryName(startPath); @@ -230,7 +251,7 @@ private static string[] FindFilesInCurrentFolderOrParentFolder( do { Reporter.Verbose.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Verbose_AttemptingToFindJsonFile, matchPattern, directory)); - string[] filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, SearchOption.AllDirectories).ToArray(); + string[] filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, searchOption).ToArray(); if (filesInDir.Length > 0) { @@ -240,7 +261,7 @@ private static string[] FindFilesInCurrentFolderOrParentFolder( directory = Path.GetPathRoot(directory) != directory ? Directory.GetParent(directory)?.FullName : null; numberOfUpLevels++; } - while (directory != null && numberOfUpLevels <= 1); + while (directory != null && numberOfUpLevels <= maxUpLevels); return Array.Empty(); } From 05bf30dd5fa05960d4779dcf3cd2c6448310255a Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 24 Sep 2025 14:19:02 +0200 Subject: [PATCH 2/7] Better error handling --- .../LocalizableStrings.Designer.cs | 11 +++++++++++ .../LocalizableStrings.resx | 8 +++++++- .../AddJsonPropertyPostActionProcessor.cs | 6 ++++-- .../xlf/LocalizableStrings.cs.xlf | 9 +++++++++ .../xlf/LocalizableStrings.de.xlf | 9 +++++++++ .../xlf/LocalizableStrings.es.xlf | 9 +++++++++ .../xlf/LocalizableStrings.fr.xlf | 9 +++++++++ .../xlf/LocalizableStrings.it.xlf | 9 +++++++++ .../xlf/LocalizableStrings.ja.xlf | 9 +++++++++ .../xlf/LocalizableStrings.ko.xlf | 9 +++++++++ .../xlf/LocalizableStrings.pl.xlf | 9 +++++++++ .../xlf/LocalizableStrings.pt-BR.xlf | 9 +++++++++ .../xlf/LocalizableStrings.ru.xlf | 9 +++++++++ .../xlf/LocalizableStrings.tr.xlf | 9 +++++++++ .../xlf/LocalizableStrings.zh-Hans.xlf | 9 +++++++++ .../xlf/LocalizableStrings.zh-Hant.xlf | 9 +++++++++ 16 files changed, 139 insertions(+), 3 deletions(-) diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs b/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs index ecf9c58147f5..0b1407d3d11b 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs +++ b/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs @@ -1439,6 +1439,17 @@ internal static string PostAction_ModifyJson_Error_NoJsonFile { } } + /// + /// Looks up a localized string similar to The result of parsing the following JSON was 'null': + /// + ///{0}. + /// + internal static string PostAction_ModifyJson_Error_NullJson { + get { + return ResourceManager.GetString("PostAction_ModifyJson_Error_NullJson", resourceCulture); + } + } + /// /// Looks up a localized string similar to Parent property path '{0}' could not be traversed.. /// diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx b/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx index 01b59dfe8833..d08a680ebae1 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx +++ b/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx @@ -953,4 +953,10 @@ The header is followed by the list of parameters and their errors (might be seve Attempting to find json file '{0}' in '{1}' - + + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + + \ No newline at end of file diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs index 5de5ba6226e2..a12f7815bb1f 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs +++ b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs @@ -169,17 +169,19 @@ protected override bool ProcessInternal( private static JsonNode? AddElementToJson(IPhysicalFileSystem fileSystem, string targetJsonFile, string? propertyPath, string propertyPathSeparator, string newJsonPropertyName, string newJsonPropertyValue, IPostAction action) { - JsonNode? jsonContent = JsonNode.Parse(fileSystem.ReadAllText(targetJsonFile), nodeOptions: null, documentOptions: DeserializerOptions); + var fileContent = fileSystem.ReadAllText(targetJsonFile); + JsonNode? jsonContent = JsonNode.Parse(fileContent, nodeOptions: null, documentOptions: DeserializerOptions); if (jsonContent == null) { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_NullJson, fileContent)); return null; } if (!bool.TryParse(action.Args.GetValueOrDefault(AllowPathCreationArgument, "false"), out bool createPath)) { Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, AllowPathCreationArgument)); - return false; + return null; } JsonNode? parentProperty = FindJsonNode(jsonContent, propertyPath, propertyPathSeparator, createPath); diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf index 45f6d58a1ba7..ecb19bae6585 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve V řešení se nepovedlo najít soubor JSON. + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Cestu k nadřazené vlastnosti „{0}“ nelze projít. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf index eb67ab8bf9bf..0b82cc14850d 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Die JSON-Datei wurde in der Projektmappe nicht gefunden. + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Der übergeordnete Eigenschaftenpfad "{0}" konnte nicht durchlaufen werden. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf index 748e66f30a00..c76d94288839 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve No se encuentra el archivo JSON en la solución + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. No se pudo atravesar la ruta de acceso de la propiedad primaria '{0}'. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf index d86722e36e60..3dd088fe92f2 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Impossible de trouver le fichier json dans la solution + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Le chemin de la propriété parente '{0}' n'a pas pu être traversé. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf index 45d8e4f02e4e..5022ad18a8c4 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Non è possibile trovare il file JSON nella soluzione + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Impossibile attraversare il percorso della proprietà padre '{0}'. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf index 2f9a5c77a259..e27c0eea775e 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve ソリューションで JSON ファイルが見つかりません + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. 親プロパティのパス '{0}' を走査できませんでした。 diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf index 11699454506d..28e0b7ef6ea3 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve 솔루션에서 json 파일을 찾을 수 없습니다. + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. '{0}' 부모 속성 경로를 트래버스할 수 없습니다. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pl.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pl.xlf index 6e5167124b31..edc3a399520b 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pl.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pl.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Nie można odnaleźć pliku JSON w rozwiązaniu + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Nie można przejść przez ścieżkę właściwości nadrzędnej „{0}”. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pt-BR.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pt-BR.xlf index 24f89179cb85..22b848dd30ec 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pt-BR.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pt-BR.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Não é possível encontrar o arquivo json na solução + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. O caminho da propriedade pai '{0}' não pôde ser percorrido. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ru.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ru.xlf index 6578a4d5bf14..b41a1a5ac241 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ru.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ru.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Не удалось найти файл JSON в решении + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Не удалось просмотреть путь к родительскому свойству "{0}". diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.tr.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.tr.xlf index a0d71cff2b96..158582a214b2 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.tr.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.tr.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Çözümde json dosyası bulunamıyor + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Üst özellik '{0}' yolu geçirilemedi. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hans.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hans.xlf index f75d8ad68dae..2df9444a65e5 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hans.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hans.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve 在解决方案中找不到 json 文件 + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. 无法遍历父属性路径“{0}”。 diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hant.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hant.xlf index 56c64a9d9b91..1c17c1adc32a 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hant.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hant.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve 在解決方案中找不到 JSON 檔案 + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. 無法周遊父屬性路徑 '{0}'。 From 475767f98c38c1ef8ebd94d10ab760e86cbfc607 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 24 Sep 2025 16:32:00 +0200 Subject: [PATCH 3/7] Update templates --- .../content/MSTest-FSharp/.template.config/template.json | 4 +++- .../content/MSTest-VisualBasic/.template.config/template.json | 4 +++- .../Playwright-MSTest-CSharp/.template.config/template.json | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json index 022ffe70b9cf..521db35a6ac6 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json @@ -240,7 +240,9 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true + "detectRepositoryRootForFileCreation": true, + "includeAllDirectoriesInSearch": false, + "includeAllParentDirectoriesInSearch": true }, "continueOnError": true }, diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json index cca88e73c3cd..d2069b81a7a9 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json @@ -240,7 +240,9 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true + "detectRepositoryRootForFileCreation": true, + "includeAllDirectoriesInSearch": false, + "includeAllParentDirectoriesInSearch": true }, "continueOnError": true }, diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json index b55dff5e27a9..c1c581baeeae 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json @@ -260,7 +260,9 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true + "detectRepositoryRootForFileCreation": true, + "includeAllDirectoriesInSearch": false, + "includeAllParentDirectoriesInSearch": true }, "continueOnError": true }, From c7f1829645ebe005b800b0f5fece53982b3fe642 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 24 Sep 2025 17:20:53 +0200 Subject: [PATCH 4/7] Fix typo --- .../PostActionProcessors/AddJsonPropertyPostActionProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs index a12f7815bb1f..6f9dc85e9008 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs +++ b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs @@ -111,7 +111,7 @@ protected override bool ProcessInternal( environment.Host.FileSystem, outputBasePath, jsonFileName, - includeAllDirectories ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories, + includeAllDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly, includeAllParentDirectories ? int.MaxValue : 1); if (jsonFiles.Count == 0) From 1884abb896212ac06e2a69a1e5686dc46cbad50b Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 24 Sep 2025 19:37:27 +0200 Subject: [PATCH 5/7] Don't go outside repo root --- .../AddJsonPropertyPostActionProcessor.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs index 6f9dc85e9008..5669f70120ff 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs +++ b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs @@ -101,6 +101,8 @@ protected override bool ProcessInternal( return false; } + string? repoRoot = detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : null; + if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllParentDirectoriesInSearch, "false"), out bool includeAllParentDirectories)) { Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllParentDirectoriesInSearch)); @@ -112,7 +114,8 @@ protected override bool ProcessInternal( outputBasePath, jsonFileName, includeAllDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly, - includeAllParentDirectories ? int.MaxValue : 1); + includeAllParentDirectories ? int.MaxValue : 1, + repoRoot); if (jsonFiles.Count == 0) { @@ -128,7 +131,7 @@ protected override bool ProcessInternal( return false; } - string newJsonFilePath = Path.Combine(detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : outputBasePath, jsonFileName); + string newJsonFilePath = Path.Combine(repoRoot ?? outputBasePath, jsonFileName); environment.Host.FileSystem.WriteAllText(newJsonFilePath, "{}"); jsonFiles = new List { newJsonFilePath }; } @@ -239,7 +242,8 @@ private static string[] FindFilesInCurrentFolderOrParentFolder( string startPath, string matchPattern, SearchOption searchOption, - int maxUpLevels) + int maxUpLevels, + string? repoRoot) { string? directory = fileSystem.DirectoryExists(startPath) ? startPath : Path.GetDirectoryName(startPath); @@ -260,6 +264,13 @@ private static string[] FindFilesInCurrentFolderOrParentFolder( return filesInDir; } + if (repoRoot is not null && directory == repoRoot) + { + // The post action wants to detect the "repo root". + // We have already processed up to the repo root and didn't find any matching files, so we shouldn't go up any further. + return Array.Empty(); + } + directory = Path.GetPathRoot(directory) != directory ? Directory.GetParent(directory)?.FullName : null; numberOfUpLevels++; } From 86cb58559c92ee420c478d14dca869be96867eac Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 24 Sep 2025 19:39:24 +0200 Subject: [PATCH 6/7] Rename --- .../AddJsonPropertyPostActionProcessor.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs index 5669f70120ff..f517a6b363ba 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs +++ b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs @@ -19,7 +19,7 @@ internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase private const string ParentPropertyPathArgument = "parentPropertyPath"; private const string NewJsonPropertyNameArgument = "newJsonPropertyName"; private const string NewJsonPropertyValueArgument = "newJsonPropertyValue"; - private const string DetectRepoRootForFileCreation = "detectRepositoryRootForFileCreation"; + private const string DetectRepoRoot = "detectRepositoryRoot"; private const string IncludeAllDirectoriesInSearch = "includeAllDirectoriesInSearch"; private const string IncludeAllParentDirectoriesInSearch = "includeAllParentDirectoriesInSearch"; @@ -89,9 +89,9 @@ protected override bool ProcessInternal( return false; } - if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRootForFileCreation, "false"), out bool detectRepoRoot)) + if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRoot, "false"), out bool detectRepoRoot)) { - Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRootForFileCreation)); + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRoot)); return false; } From d72fe2300173d42d14dfb1d3977136e98ba02038 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 25 Sep 2025 09:43:49 +0200 Subject: [PATCH 7/7] Fix templates --- .../content/MSTest-CSharp/.template.config/template.json | 4 +++- .../content/MSTest-FSharp/.template.config/template.json | 2 +- .../content/MSTest-VisualBasic/.template.config/template.json | 2 +- .../Playwright-MSTest-CSharp/.template.config/template.json | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-CSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-CSharp/.template.config/template.json index 01977ec20d99..642d2f3b5a19 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-CSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-CSharp/.template.config/template.json @@ -240,7 +240,9 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true + "detectRepositoryRoot": true, + "includeAllDirectoriesInSearch": false, + "includeAllParentDirectoriesInSearch": true }, "continueOnError": true }, diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json index 521db35a6ac6..ed388b0b28da 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json @@ -240,7 +240,7 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true, + "detectRepositoryRoot": true, "includeAllDirectoriesInSearch": false, "includeAllParentDirectoriesInSearch": true }, diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json index d2069b81a7a9..a110969e6973 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json @@ -240,7 +240,7 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true, + "detectRepositoryRoot": true, "includeAllDirectoriesInSearch": false, "includeAllParentDirectoriesInSearch": true }, diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json index c1c581baeeae..cca0c7262957 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json @@ -260,7 +260,7 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true, + "detectRepositoryRoot": true, "includeAllDirectoriesInSearch": false, "includeAllParentDirectoriesInSearch": true },