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.ChangeExtension with an empty string behaves differently on Linux and Windows platforms #100687

Closed
jasonleenaylor opened this issue Apr 5, 2024 · 8 comments
Labels
area-System.IO needs-author-action An issue or pull request that requires more info or actions from the author.

Comments

@jasonleenaylor
Copy link

jasonleenaylor commented Apr 5, 2024

Description

We have a cross platform application running on Windows and Linux and we noticed a difference between the
ChangeExtension implementation on the two platforms. On Windows calling ChangeExtension with an empty string results in the extension being removed. e.g. "file.extension" becomes "file" but on Linux we get "file."

On windows it must be running the framework version of this code but on Linux we get the core runtime implementation.

Reproduction Steps

Run Path.ChangeExtension("file.extension", "") on Windows and Linux

Expected behavior

The extension is removed leaving a bare file name with no trailing '.'

Actual behavior

The extension is removed leaving a trailing '.'

Regression?

This works as described in .NET Framework

Known Workarounds

Users can use Path.RemoveExtension instead when removing, but if you are adding an optional extension the only workaround is to add a test for empty before using the method

Configuration

No response

Other information

string.Concat(subpath, ".", extension);

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 5, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 5, 2024
jasonleenaylor added a commit to jasonleenaylor/runtime that referenced this issue Apr 5, 2024
- When ChangeExtension is called with an empty string completely
  remove the extension instead of adding a '.'
@vcsjones vcsjones added area-System.IO and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Apr 5, 2024
@jasonleenaylor
Copy link
Author

@vcsjones or anyone I am happy to make a PR for this, but I would like to add a unit test. Can you help me locate other unit tests for System.IO?

@tommysor
Copy link
Contributor

tommysor commented Apr 5, 2024

@jasonleenaylor Tests:

[Theory,
InlineData(null, null, null),
InlineData(null, "exe", null),
InlineData("", "", ""),
InlineData("file.exe", null, "file"),
InlineData("file.exe", "", "file."),
InlineData("file", "exe", "file.exe"),
InlineData("file", ".exe", "file.exe"),
InlineData("file.txt", "exe", "file.exe"),
InlineData("file.txt", ".exe", "file.exe"),
InlineData("file.txt.bin", "exe", "file.txt.exe"),
InlineData("dir/file.t", "exe", "dir/file.exe"),
InlineData("dir/file.exe", "t", "dir/file.t"),
InlineData("dir/file", "exe", "dir/file.exe")]
public void ChangeExtension(string path, string newExtension, string expected)
{
if (expected != null)
expected = expected.Replace('/', Path.DirectorySeparatorChar);
if (path != null)
path = path.Replace('/', Path.DirectorySeparatorChar);
Assert.Equal(expected, Path.ChangeExtension(path, newExtension));
}

jasonleenaylor added a commit to jasonleenaylor/runtime that referenced this issue Apr 5, 2024
- When ChangeExtension is called with an empty string completely
  remove the extension instead of adding a '.'
@MihaZupan
Copy link
Member

Console.WriteLine(Path.ChangeExtension("file.extension", null));
Console.WriteLine(Path.ChangeExtension("file.extension", ""));

prints

file
file.

On .NET Framework, .NET 8 on Windows and .NET 8 on Linux.

@jasonleenaylor
Copy link
Author

@MihaZupan Hmm, we saw this issue when running in .NET 6 on Windows, and .NET 6 on Linux.

It added the period on Linux, but not on Windows.

@jozkee
Copy link
Member

jozkee commented Apr 24, 2024

As @MihaZupan described, this isn't inconsistent between OSes, and it works as documented, including .NET 6.

If extension is an empty string, the returned path string contains the contents of path with any characters following the last period removed.

Can you please double check if you are really passing "" to the extension argument.

@jozkee jozkee added needs-author-action An issue or pull request that requires more info or actions from the author. and removed untriaged New issue has not been triaged by the area owner labels Apr 24, 2024
@jozkee jozkee added this to the Future milestone Apr 24, 2024
Copy link
Contributor

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

Copy link
Contributor

This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

Copy link
Contributor

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@dotnet-policy-service dotnet-policy-service bot removed this from the Future milestone May 23, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO needs-author-action An issue or pull request that requires more info or actions from the author.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants