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

Enable translation of maps into directory trees #2448

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

nikita-volkov
Copy link

Resolves #2460

@nikita-volkov
Copy link
Author

Thought. It might make sense to add a check on paths not being absolute and not containing .. segments to ensure that it's impossible to affect the filesystem outside of the working dir.

Opinions?

@nikita-volkov
Copy link
Author

I went ahead and added the according security checks.

@mmhat
Copy link
Collaborator

mmhat commented Sep 19, 2022

@nikita-volkov I do have a pending PR #2437 with another possibility of constructing a directory tree and I am interested in you feedback there. Would it help you with your use case?
Also, I like your ideas of prohibiting some file paths by default and I think those are valuable on their own.

@nikita-volkov
Copy link
Author

@mmhat I've taken a look at the usage example provided in your PR. It looks like your solution provides a finer grain control, which should definitely find use in non-trivial tasks. However it also comes at a price of a more complicated API for the developer.

I think both solutions should be merged. Mine will be useful for simple cases, providing a simpler API, yours will be useful for advanced cases.

@mmhat
Copy link
Collaborator

mmhat commented Sep 28, 2022

@mmhat I've taken a look at the usage example provided in your PR. It looks like your solution provides a finer grain control, which should definitely find use in non-trivial tasks. However it also comes at a price of a more complicated API for the developer.

I think both solutions should be merged. Mine will be useful for simple cases, providing a simpler API, yours will be useful for advanced cases.

@nikita-volkov Thanks for the feedback; I think its a fair assessment! I also don't see a problem to support both ways and I'd be happy to see it merged.

Copy link
Collaborator

@Gabriella439 Gabriella439 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will let @nikita-volkov and @mmhat work out how to resolve any merge conflicts that might arise

@nikita-volkov
Copy link
Author

nikita-volkov commented Oct 5, 2022

Trying to resolve the conflicts after the changes by @mmhat. The only issue is with the added allowSeparators global option in that change. I don't see an intuitive interpretation of that option with the changes that I've introduced here. Do we really need that option? It does complicate things and I'm failing to see the necessity of it.

@nikita-volkov
Copy link
Author

nikita-volkov commented Oct 5, 2022

Anyway, I've adapted the changes. Now setting allowSeparators to False will effectively disable the tree construction using maps as introduced in this PR. Should be mergeable now.

@mmhat
Copy link
Collaborator

mmhat commented Oct 5, 2022

Great, I'll have another look tomorrow 👍

@mmhat
Copy link
Collaborator

mmhat commented Oct 6, 2022

@nikita-volkov I had a look at your changes and added the CLI flags in nikita-volkov#1. This is a PR against your feature branch; Please review the changes there and give me some feedback.

Regarding path separators in file names: I like the default (do not allow them) as it is now; The directory structure should primarily stem from the Dhall code and not from the content of an opaque text value.

@nikita-volkov
Copy link
Author

@mmhat I don't like the idea of parameterising the compiler with various modes of interpretation of one codebase. This will likely lead to all sorts of confusion for the users and will require authors to inform the users about the compiler settings needed to compile their codebase. That's the reason I'm skeptical about the added allowSeparators flag in the first place, your PR to my fork adds even more options.

There can only be one truth and IMO it's best for build tools to exhibit the same property: there can only be one interpretation of one codebase. That's what makes builds predictable.

Pinging @Gabriella439 to draw some attention.

@mmhat
Copy link
Collaborator

mmhat commented Oct 6, 2022

I don't really disagree with you on the fact that it is a good thing to have one and only one interpretation of a codebase. My point is more about the defaults here: I see all three checks as safety features guarding against programming errors (e.g. no path separators) or security risks (e.g. no ".."). The CLI flags are simply escape hatches in the case You Know What You Are Doing. So to me its more about disabling safety checks than interpreting the program in a different way.

If we disagree here on the defaults of those settings then I am happy to discuss that, but I'd like a switch for enabling/disabling anyway.

Copy link
Collaborator

@Gabriella439 Gabriella439 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think it's fine to add the additional options that @mmhat suggested. I'd be more concerned if the options changed the shape of the expected directory tree type, but that's not the case here


toDirectoryTree allowSeparators (path </> Text.unpack key) value
case keyPathSegments of
-- Fail if path is absolute, which is a security risk.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use System.FilePath.isRelative?

Comment on lines +244 to +254
(dirPath, fileName) <- case reverse keyPathSegments of
h : t ->
return
( Foldable.foldl' (</>) path (reverse t)
, h )
_ ->
die

Directory.createDirectoryIfMissing True dirPath

toDirectoryTree allowSeparators (dirPath </> fileName) value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want something like this:

Suggested change
(dirPath, fileName) <- case reverse keyPathSegments of
h : t ->
return
( Foldable.foldl' (</>) path (reverse t)
, h )
_ ->
die
Directory.createDirectoryIfMissing True dirPath
toDirectoryTree allowSeparators (dirPath </> fileName) value
(dirPath', fileName) <- case reverse keyPathSegments of
h : t ->
return
( Foldable.foldl' (</>) path (reverse t)
, h )
_ ->
die
let dirPath = path </> dirPath'
Directory.createDirectoryIfMissing True dirPath
toDirectoryTree allowSeparators (dirPath </> fileName) value

In other words, dirPath needs to incorporate path as a prefix, otherwise the Map fields will be created as subdirectories relative to PWD when they should be relative to path.

Note that nikita-volkov#1 fixes this, too

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

Successfully merging this pull request may close these issues.

Treat slashes in map keys as instruction to create directories in to-directory-tree mode
3 participants