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

Refactor import resolver to allow for dynamic loading of modules. #1822

Merged
merged 4 commits into from
Jun 28, 2021

Conversation

kustosz
Copy link
Contributor

@kustosz kustosz commented Jun 25, 2021

Pull Request Description

Fixes #1569
Closes #1768

Important Notes

Checklist

Please include the following checklist in your PR:

  • The documentation has been updated if necessary.
  • All code conforms to the Scala, Java, and Rust style guides.
  • All documentation and configuration conforms to the markdown and YAML style guides.
  • All code has been tested where possible.

@kustosz kustosz added Type: Enhancement p-highest Should be completed ASAP labels Jun 25, 2021
@kustosz kustosz self-assigned this Jun 25, 2021
Copy link
Member

@radeusgd radeusgd left a comment

Choose a reason for hiding this comment

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

Looks good to me.

I think error handling of failed module load should be more granular as indicated below, but I guess that can be further extended in future PRs.
Maybe this PR could at least change the return type of ensurePackageIsLoaded into an Either[PackageLoadingError, Unit] with only one variant defined for PackageLoadingError which can then be later extended.

Comment on lines +395 to +397
public List<Package<TruffleFile>> getPackages() {
return packages;
}
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest renaming to getPreloadedPackages or something like that and indicating in the doc comment that this will be removed in the next iteration. Just to avoid someone using it in a wrong place by mistake.

* @return `true` if the package was already loaded or successfully
* downloaded. `false` otherwise.
*/
def ensurePackageIsLoaded(namespace: String, name: String): Boolean
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest extending this signature a bit, although I guess it is ok if it happens in future PRs.

I think we should here somehow differentiate various kinds of errors - for display purposes there should be a different error saying "package is not defined in the edition that you are using" versus "package could not be downloaded due to network problems" or "could not load the package due to insufficient filesystem permissions", as these are completely different error, so thay cannot be merged into a single false return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair!

@radeusgd
Copy link
Member

After having a second look at the spec I have some doubts if everything is done - technically I guess all features are there, but I think there are things that could be done in this PR instead of the next one.

For example, the getPackages method of the context may be misleading as it only returns the preloaded packages - but as it is only used in the temporary PackageRepository, maybe its logic could be moved into the PackageRepository itself?
Currently the PackageRepository delegates a lot back to the Context, so they are actually quite tightly coupled while the whole purpose of this task was to delegate as much as possible of module handling to the PackageRepository, so that the next task will require less modifications.

I'm also not sure if we should keep modules in Context. I mean I think this is a correct solution, and I appreciate that we only explicitly try to load new packages by calling the ensurePackageIsLoaded method. On the other hand, I feel like having modules there may result in some other code, perhaps added later, getting modules directly from this field and not calling ensurePackageIsLoaded, which could result in forgetting to preload some packages. But maybe it is indeed better to ask to load packages explicitly, rather then implicitly with each getModuleByName request? I'm not sure, just wanted to discuss the alternatives. I would however suggest to rename these methods to indicate that they only refer to loaded modules and will not work if a package was not yet loaded.

@kustosz
Copy link
Contributor Author

kustosz commented Jun 25, 2021

@radeusgd honestly this is just an unblocker for "things in compiler use this, it is obvious where it should be replaced in case the API changes", such that further changes to the packaging logic don't need to propagate far into the compiler. Regarding moving the preloading logic into package repo – I decided against it on the basis of "what's the point moving code that will be removed by the next task?". There will be no preloading, so this whole logic will be removed. Regarding "module access should be mediated through the package repo" – interesting thought, it does have some problems, but may be more elegant. I'll think about it.

@radeusgd
Copy link
Member

Regarding moving the preloading logic into package repo – I decided against it on the basis of "what's the point moving code that will be removed by the next task?"

Fair

Regarding "module access should be mediated through the package repo" – interesting thought, it does have some problems, but may be more elegant. I'll think about it.

That was my initial idea, I think that it reduces the chance of erroneously forgetting to call ensurePackageIsLoaded. But also being explicit about loading also may have some advantages, so yeah it would be good to think through which one will be better overall.

@kustosz kustosz merged commit 1d18310 into main Jun 28, 2021
@kustosz kustosz deleted the wip/mk/import-refactor branch June 28, 2021 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p-highest Should be completed ASAP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor Import Resolution To Use a Package Repository Compiler Crashes on Unavailable Imports
3 participants