Skip to content

Conversation

quasicomputational
Copy link
Collaborator

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., an InputSettings type, where today's input becomes equivalent to input 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: the StateT would allow modification of the root directory, despite that never being the correct thing to do.

@quasicomputational
Copy link
Collaborator Author

One more wrinkle: the _root field in Status is a relative path. If setCurrentWorkingDirectory is called somewhere else, the cached imports in the Status will be wrong. I tried to fix this by calling makeAbsolute before loadStaticWith but I ran into trouble, since loadWith doesn't have a MonadIO constraint. I suppose that this is already problematic, so this PR isn't making the situation worse.

-- | State threaded throughout the import process
data Status = Status
{ _stack :: [Import]
{ _root :: FilePath
Copy link
Collaborator

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 Imports and set the first one to the directory with a file named "."

@since 1.6
-}
inputDirFromWith
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator

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

@quasicomputational
Copy link
Collaborator Author

OK, I removed _root and now it all goes through canonicalizeImport, which is definitely a bit conceptually nicer but it had a bunch of knock-on refactorings. I've also added a basic smoke test for this new capability.

-- 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))
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator

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

@quasicomputational
Copy link
Collaborator Author

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.
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.

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
Copy link
Collaborator

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?

Copy link
Collaborator Author

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!

@quasicomputational quasicomputational merged commit 8c15d8f into dhall-lang:master Jul 14, 2018
@quasicomputational
Copy link
Collaborator Author

Thanks a bunch!

@Gabriella439
Copy link
Collaborator

You're welcome! Thank you, 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.

CWD independence
2 participants