Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path.GetFileNameWithoutExtension() does not throw ArgumentException ("Illegal characters in path") on Windows #72307

Closed
arvindshmicrosoft opened this issue Jul 16, 2022 · 10 comments

Comments

@arvindshmicrosoft
Copy link

arvindshmicrosoft commented Jul 16, 2022

Description

I'm migrating a project from .NET Framework 4.7.2 to .NET 6.0. One of my tests is failing, and I have isolated the issue to Path.GetFileNameWithoutExtension seemingly handling the path like it is a Unix path. Consequently, it returns the entire path except the extension, and does not throw if I had invalid (for Windows) characters in the path.

Reproduction Steps

string fileName = @"<<System32\KERNELBASE.dll>>";
string result;
Console.WriteLine(Environment.OSVersion);
Console.WriteLine(Environment.OSVersion.Platform);
result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", fileName, result);

The csproj is below:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

Expected behavior

ArgumentException should be thrown as the < and > characters are invalid for Windows. In .NET Framework 4.7.2 for example, the above code results in the following exception:

[System.ArgumentException: Illegal characters in path.]
   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at System.IO.Path.GetFileName(String path)
   at System.IO.Path.GetFileNameWithoutExtension(String path)

Actual behavior

Microsoft Windows NT 10.0.22000.0
Win32NT
GetFileNameWithoutExtension('<<System32\KERNELBASE.dll>>') returns 'KERNELBASE'

Regression?

Maybe?

Known Workarounds

No response

Configuration

.NET 6.0.7
OS: Windows 11 - 10.0.22000 Build 22000
Architecture: x64

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 16, 2022
@ghost
Copy link

ghost commented Jul 16, 2022

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I'm migrating a project from .NET Framework 4.7.2 to .NET 6.0. One of my tests is failing, and I have isolated the issue to Path.GetFileNameWithoutExtension seemingly handling the path like it is a Unix path. Consequently, it returns the entire path except the extension, and also incidentally does not throw if I had invalid (for Windows) characters in the path.

Reproduction Steps

string fileName = @"C:\mydir\myfile.ext";
string result;

result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", fileName, result);

Expected behavior

// EXPECTED: GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'

Actual behavior

// ACTUAL: GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'C:\mydir\myfile' on .NET 6.0 on Windows

Regression?

Maybe?

Known Workarounds

No response

Configuration

.NET 6.0.7
OS: Windows 11 - 10.0.22000 Build 22000
Architecture: x64

Other information

No response

Author: arvindshmicrosoft
Assignees: -
Labels:

area-System.IO

Milestone: -

@Fabi
Copy link

Fabi commented Jul 16, 2022

Can't reproduce on (x86 64 bit) macOS, linux and w10/11 with latest .net6 and .net 7 prefiew. All return the expected result.

@jkotas
Copy link
Member

jkotas commented Jul 16, 2022

I am not able to reproduce it either. @arvindshmicrosoft Could you please double check the repro steps and the result that you are seeing?

@jkotas jkotas added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jul 16, 2022
@ghost
Copy link

ghost commented Jul 16, 2022

This issue has been marked needs-author-action and may be missing some important information.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 16, 2022
@stephentoub
Copy link
Member

I can't repro it either, and from looking at the code, it's not clear to me how this could happen.

@arvindshmicrosoft
Copy link
Author

Thank you all for looking. Something must be very specific to my case. I will investigate further and get back.

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Jul 16, 2022
@arvindshmicrosoft arvindshmicrosoft changed the title Path.GetFileNameWithoutExtension() returns incorrect result even though platform is Windows Path.GetFileNameWithoutExtension() does not throw ArgumentException ("Illegal characters in path") on Windows Jul 16, 2022
@arvindshmicrosoft
Copy link
Author

arvindshmicrosoft commented Jul 16, 2022

@Fabi @jkotas @stephentoub - in the process of distilling the repro down, I had deviated from the core issue, which seems to be that the erstwhile Illegal characters in path ArgumentException, which used to be raised from CheckInvalidPathChars, does not happen on .NET 6.0 on Windows, and consequently a file name is returned when it should not be. I have updated the title of this Issue, as well as the repro (in the first comment). I hope it helps understand what might be happening here.

@jkotas
Copy link
Member

jkotas commented Jul 16, 2022

That was intentional breaking change in .NET Core 2.1. More details in https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.1#path-apis-dont-throw-an-exception-for-invalid-characters

@stephentoub
Copy link
Member

Illegal characters in path ArgumentException, which used to be raised from CheckInvalidPathChars, does not happen on .NET 6.0 on Windows

That was changed in .NET Core 2.1:
https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.1#path-apis-dont-throw-an-exception-for-invalid-characters
We rely on the OS now for path validation, rather than trying to guess what's valid.

@arvindshmicrosoft
Copy link
Author

@jkotas and @stephentoub thank you for pointing this out. I really appreciate the help.

@adamsitnik adamsitnik removed the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Jul 18, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants