-
Notifications
You must be signed in to change notification settings - Fork 220
Add various functions explicitly taking a root directory. #508
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
Add various functions explicitly taking a root directory. #508
Conversation
One more wrinkle: the |
src/Dhall/Import/Types.hs
Outdated
-- | State threaded throughout the import process | ||
data Status = Status | ||
{ _stack :: [Import] | ||
{ _root :: FilePath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to add a _root
field. Instead, you can make _stack
a NonEmpty
list of Import
s and set the first one to the directory with a file named "."
@since 1.6 | ||
-} | ||
inputDirFromWith |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure that you want this function to take two FilePath
arguments. Couldn't you infer the starting directory from the path of the source file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not guaranteed to be a path to the file; e.g., it's also <input>
or <stdin>
if that's where the data is actually coming from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, and also thinking more about your answer in the other issue I agree that it's necessary to disentangle the two concepts, so don't worry about this after all
OK, I removed |
src/Dhall/Import.hs
Outdated
-- need to not display the first (innermost) import, and the | ||
-- final (outermost) import is fake to establish the base | ||
-- directory. Also, we need outermost-first. | ||
toDisplay = drop 1 (reverse (drop 1 canonical)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doctest
error appears to be due to the new drop 1
here. According to the comment this appears to be intentional, but are you sure that the innermost import is already displayed by e
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was! :( I'm having a look now too. That reasoning was based on the previous code having a drop 1
in it but I might have confused myself while trying to get this function to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it was on the wrong side of the reverse
anyway for that. Now I don't know why the previous code had the drop 1
in it at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@quasicomputational: I think the reason it had the other drop 1
is because it would implicitly insert ./.
as the root of the the chain of imports before your change and the drop 1
removed it so it wouldn't show up in the chain of imports
Oh, I think I understand what was going on: the previous code was injecting its own fake import and that had to be dropped. We're still doing that, just more systematically. |
Now the importing code is not tied to the current working directory: it can resolve imports relative to any directory passed to it. Fixes dhall-lang#507.
Imports resolve directories relative to their parent import. Hence, we can put in a fake import at the root to control the resolution of relative imports.
My understanding of the previous code was wrong: it was dropping the outermost import because `canonicalizeImport` was already using the fake import trick.
dd2f57e
to
05b9629
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Just one minor comment, but feel free to merge at any time
src/Dhall.hs
Outdated
to, a file to mention in errors as the source, a custom typing | ||
context, and a custom normalization process. | ||
@since 1.6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean @since 1.15
or @since 1.16
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, yes. Might be contagious!
Thanks a bunch! |
You're welcome! Thank you, too 🙂 |
Now the importing code is not tied to the current working directory:
it can resolve imports relative to any directory passed to it.
Fixes #507.
Notes: I haven't added every flavour of
Dir
function that I could have: I only put in the ones relevant to dhall-to-cabal, besides the most general one of each family that contains the actual implementation. These functions are getting a bit dense and we might consider some kind of alternative strategy here (e.g., anInputSettings
type, where today'sinput
becomes equivalent toinput defaultInputSettings
).I didn't add an
importFile
function as suggested in #507 and previously implemented in #467.I put the root directory in the
Status
object because caching relative imports with different root directories isn't safe. However, I note that it should be immutable, which is currently only enforced by convention: theStateT
would allow modification of the root directory, despite that never being the correct thing to do.