Skip to content

Conversation

@bioball
Copy link
Member

@bioball bioball commented Sep 21, 2024

This implements changes to put native libs in ~/.pkl/editor-support/. When loading the parser, it first looks for libraries there, and if they don't exist, they get copied from bundled resources.

When building libraries, the os name and architecture get included in the resource path.

This enables starting the jar plainly without needing any extra argument (e.g. no need for CLI flag -Djava.library.path, and no need for the LD_LIBRARY_PATH env var), and eliminates the need to copy native libraries to the current working dir.

Note: java-tree-sitter doesn't provide any way to provide the library path. To get around this, I'm doing a pretty ugly hack to rewrite the TreeSitter.java source file to make it load the tree-sitter library from our custom dir.

@bioball bioball force-pushed the improve-native-libs branch 6 times, most recently from 386c66a to 9ade31f Compare September 21, 2024 14:17

// The thread where all tree-sitter allocations happen
val astExecutor: ExecutorService by lazy { Executors.newSingleThreadExecutor() }
val pklParser: PklParser by lazy { PklParser(this) }
Copy link
Member Author

Choose a reason for hiding this comment

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

Unrelated: make PklParser a component, and remove astExecutor because it's only used by the parser anyway.

@bioball bioball force-pushed the improve-native-libs branch from 9ade31f to 9fe850b Compare September 21, 2024 14:25
This implements changes to put native libs in `~/.pkl/editor-support/`.
When loading the parser, it first looks for libraries there, and if
they don't exist, they get copied from bundled resources.

When building libraries, the os name and architecture get included in
the resource path.

This enables starting the jar plainly without needing any extra argument
(e.g. no need for -Djava.library.path), and eliminates the need to copy
native libraries to the current working dir.
@bioball bioball force-pushed the improve-native-libs branch from 9fe850b to fc73ec0 Compare September 21, 2024 14:49
mapOf(
"version" to buildInfo.pklLspVersion,
"treeSitterVersion" to libs.versions.treeSitterRepo.get(),
"treeSitterPklVersion" to libs.versions.treeSitterPklRepo.get(),
Copy link
Member Author

Choose a reason for hiding this comment

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

Provide versions of these libraries to runtime, because this affects where these libraries get loaded from. For example, on macOS, the tree-sitter lib lives at ~/.pkl/editor-support/native-libs/tree-sitter/v0.23.0/libtree-sitter.dylib.

This ensures that the version of the native lib matches what the LSP expects.

// chmod a+x
outFile.setExecutable(true, false)
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Tangent: I introduced BuildInfo too, and moved both BuildInfo and ExecutableJar into buildSrc to mimick how we set things up in the pkl repo.

Copy link
Contributor

@stackoverflow stackoverflow left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 30 to 32
val os: org.gradle.internal.os.OperatingSystem by lazy {
org.gradle.internal.os.OperatingSystem.current()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
val os: org.gradle.internal.os.OperatingSystem by lazy {
org.gradle.internal.os.OperatingSystem.current()
}
val os: OperatingSystem by lazy {
OperatingSystem.current()
}

Better to import this class.

Comment on lines +30 to +32
* https://skife.org/java/unix/2011/06/20/really_executable_jars.html
*/
abstract class ExecutableJar : DefaultTask() {
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need an executable jar. You never run the LSP by hand and the clients need to find a suitable Java version (>=22).

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree. But will keep that out of the scope of this PR.

val libraryPath: Path by lazy {
when {
// optimization: if the resource file is a normal file, we can use it directly.
resourcePath.fileSystem == FileSystems.getDefault() -> resourcePath
Copy link
Contributor

Choose a reason for hiding this comment

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

I imagine this will be true for local development?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup

@bioball bioball merged commit dc6a603 into apple:main Sep 23, 2024
@bioball bioball deleted the improve-native-libs branch September 23, 2024 18:04
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.

2 participants