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

DirectoryAliases.MakeAbsolute computes the wrong path if the path contains .. #4413

Open
2 tasks done
paulomorgado opened this issue Dec 27, 2024 · 4 comments
Open
2 tasks done

Comments

@paulomorgado
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched issues to ensure it has not already been reported

Cake runner

Cake Frosting

Cake version

5.0.0

Operating system

Windows

Operating system architecture

x64

CI Server

No response

What are you seeing?

var b = context.MakeAbsolute(context.Directory(".")); // C:/repos/repo/build
var d = context.MakeAbsolute(b.Combine(new DirectoryPath("../Dashboards"))); // C:/repos/repo/build/../Dashboards

What is expected?

var b = context.MakeAbsolute(context.Directory(".")); // C:/repos/repo/build
var d = context.MakeAbsolute(b.Combine(new DirectoryPath("../Dashboards"))); // C:/repos/repo/build/Dashboards

Steps to Reproduce

var b = context.MakeAbsolute(context.Directory("."));
var d = context.MakeAbsolute(b.Combine(new DirectoryPath("../Dashboards")));

Output log

No response

@patriksvensson
Copy link
Member

This is by design (as I implemented it back in the days), and the path (containing ..) is a valid path, so saying "invalid" is stretching it a bit. "Not what I expected" is probably better choice of words here 🙂

@paulomorgado
Copy link
Contributor Author

Fair enough.

But how about this?

context.Environment.WorkingDirectory = context.MakeAbsolute(context.Directory("C:/temp/repo/build"));
var b = context.MakeAbsolute(context.Directory(".")); // C:/temp/repo/build
var d = context.MakeAbsolute(b.Combine(new DirectoryPath("../Dashboards"))); // C:/temp/repo/build/../Dashboards
var f = context.File("C:/repos/repo/build/Dashboards/file.ext"); // C:/temp/repo/build/Dashboards/file.ext
var r = context.MakeRelative(f, d); // ../../../../../repos/repo/build/Dashboards/file.ext
var a = context.MakeAbsolute(r); // C:/repos/repo/build/Dashboards/file.ext

I this code, shouldn't r b just tile.ext? The resulting value is higher than the file system's root.

Is there a way to normalize the path and get rid of all . and ..?

@paulomorgado
Copy link
Contributor Author

I think I found out what I was looking for: Collapse method.

Using Collapse on RelativePathResolver.Resolve would make it easier and more correct.

@patriksvensson
Copy link
Member

You might be able to use the collapse method on DirectoryPath as well: https://cakebuild.net/api/Cake.Core.IO/DirectoryPath/4CE45D81

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants