Skip to content

Commit 695b1ef

Browse files
authored
OutPut the PathTooLong when when longpath is disabled (#11731)
Fixes [#10201](#10201) ### Context When build with dotnet sdk 9.0, during evaluation, it can't find the file Program.cs with main function ![image](https://github.com/user-attachments/assets/e4c133c2-ffd3-48e6-8927-ca7cdb4a35a4) When build with MSBuild.exe, it has the following error ![image](https://github.com/user-attachments/assets/e8e57a06-3b9e-43a5-b7de-a8ddc7860c09) ### Changes Made Throw the PathTooLongException exception in windows when the file path exceeds the maximum length Catch the exception when evaluation and processing the msbuild.exe command. ### Testing ![image](https://github.com/user-attachments/assets/cbdad345-ede5-4b7a-bc83-e612269b79e6) ![image](https://github.com/user-attachments/assets/1f001ad0-0620-42e3-9348-316ffdd861a9) ### Notes
2 parents eb070fa + 9c1622d commit 695b1ef

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

src/Build/Evaluation/Evaluator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ internal static void Evaluate(
345345
{
346346
evaluator.Evaluate();
347347
}
348+
catch (PathTooLongException ex)
349+
{
350+
evaluator._evaluationLoggingContext.LogErrorFromText(null, null, null, new BuildEventFileInfo(root.ProjectFileLocation.File),
351+
ex.Message);
352+
}
348353
finally
349354
{
350355
IEnumerable globalProperties = null;

src/MSBuild/XMake.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,13 @@ public static ExitType Execute(
10451045

10461046
exitType = ExitType.Unexpected;
10471047
}
1048+
catch (PathTooLongException e)
1049+
{
1050+
Console.WriteLine(
1051+
$"{e.Message}{(e.InnerException != null ? $" {e.InnerException.Message}" : string.Empty)}");
1052+
1053+
exitType = ExitType.Unexpected;
1054+
}
10481055
// handle fatal errors
10491056
catch (Exception e)
10501057
{

src/Shared/FileSystem/WindowsFileSystem.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public override IEnumerable<string> EnumerateFileSystemEntries(string path, stri
5555

5656
public override bool DirectoryExists(string path)
5757
{
58+
if (!string.IsNullOrEmpty(path) && FileUtilities.IsPathTooLong(path))
59+
{
60+
// If the path is too long, we can't check if it exists on windows
61+
string message = ResourceUtilities.FormatString(AssemblyResources.GetString("Shared.PathTooLong"), path, NativeMethodsShared.MaxPath);
62+
throw new PathTooLongException(message);
63+
}
5864
return NativeMethodsShared.DirectoryExistsWindows(path);
5965
}
6066

src/Shared/FileUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ internal static string AttemptToShortenPath(string path)
12391239
return FixFilePath(path);
12401240
}
12411241

1242-
private static bool IsPathTooLong(string path)
1242+
public static bool IsPathTooLong(string path)
12431243
{
12441244
// >= not > because MAX_PATH assumes a trailing null
12451245
return path.Length >= NativeMethodsShared.MaxPath;

0 commit comments

Comments
 (0)