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

Library Loading with %use only works when homeDir is not null, but invalid homeDir works fine #359

Closed
fmagin opened this issue Mar 15, 2022 · 0 comments
Labels
problem Some user problem that is not really a bug
Milestone

Comments

@fmagin
Copy link
Contributor

fmagin commented Mar 15, 2022

All of the examples are on https://github.com/Kotlin/kotlin-jupyter/blob/stable-kotlin-2 but it doesn't look like this changed compared to current master

Problem

Usually I embed the kernel via

IkotlinKt.embedKernel(connectionFile, resolutionInfoProvider, Collections.singletonList(context))

but in the resulting kernel it is not possible to load a library definition, even if explicitly specifying a full path
because in:

val library = libraries?.resolve(libRef, vars)
?: throw ReplException("Unknown library '$libRef'")

libraries is null, and the ReplException is thrown.

this happens because embedKernel passes null for a homeDir,

then the resolverConfig isn't created:

resolverConfig = homeDir?.let { loadResolverConfig(it.toString(), resolutionInfoProvider) },

and then there are no libraries to pass when the LibrariesProcessor is constructed:
private val librariesProcessor: LibrariesProcessor = LibrariesProcessorImpl(resolverConfig?.libraries, runtimeProperties.version)

If I instead use the following code to create the kernel it works, despite the homeDir being a completely invalid directory:

        var config = KernelConfig.Companion.fromConfig(
                KernelJupyterParams.Companion.fromFile(connectionFile),
                EmptyResolutionInfoProvider.INSTANCE,
                cp,
                new File("/NOTEXISTING/"),
                true);
        IkotlinKt.kernelServer(config, ConfigKt.getDefaultRuntimeProperties(), Collections.singletonList(context));

with this I can also run %use example in the kernel, and it will correctly load the file at /home/myusername/.jupyter_kotlin/libraries/example.json, so some other code knows the default homedir anyway.

Possible Fixes

Create resolverConfig with null homeDir

loadResolverConfig has a signature that requires the homeDir to be not null

fun loadResolverConfig(homeDir: String, resolutionInfoProvider: ResolutionInfoProvider) = ResolverConfig(defaultRepositories, getStandardResolver(homeDir, resolutionInfoProvider))

but the called getStandardResolver is fine with a homeDir of null again.

fun getStandardResolver(homeDir: String? = null, infoProvider: ResolutionInfoProvider): LibraryResolver {
// Standard resolver doesn't cache results in memory
var res: LibraryResolver = FallbackLibraryResolver
val librariesDir: File? = homeDir?.let { KERNEL_LIBRARIES.homeLibrariesDir(File(it)) }
res = LocalLibraryResolver(res, librariesDir)
res = DefaultInfoLibraryResolver(res, infoProvider, listOf(KERNEL_LIBRARIES.userLibrariesDir))
return res
}

Change the signature of embedKernel to allow passing a homeDir

I don't know what this homeDir value actually achieves, and it might be useful for code using embedKernel to correctly set this. But because a null homeDir doesn't raise an issues in my case it should probably be an optional parameter. And then it should IMO still be possible to load library definitions from a full path or from the .jupyter_kotlin/libraries/ directory even if no homeDir is passed.

@ileasile ileasile added the problem Some user problem that is not really a bug label Apr 13, 2022
@ileasile ileasile added this to the 0.12.0 milestone May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem Some user problem that is not really a bug
Projects
None yet
Development

No branches or pull requests

2 participants