spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Better method for managing python black package

Open nedtwigg opened this issue 5 years ago • 8 comments

We are able to call black by shelling out to it on the system path, and we are able to cache its results by enforcing a version check on the binary. However, if it isn't on the path, or the version is wrong, we just show a helpful error message and let the user figure it out from there:

https://github.com/diffplug/spotless/blob/608e128381c89260f8a38fba985415c1f962ec7b/lib/src/main/java/com/diffplug/spotless/python/BlackStep.java#L65-L66

It would be nice if we could handle this better, or at least have better instructions. There are a few python plugins in the gradle ecosystem, I'll reach out to them to see if they are interested.

nedtwigg avatar Aug 25 '20 06:08 nedtwigg

When this is run on linux the black --version gives output

black, 22.6.0 (compiled: yes) Python (CPython) 3.9.6

This doesn't parse with the regex version (\S*) . We should ideally have a way to enforce this regex as well, since different executable version can have different way of showing versions too.

I see this has already been fixed, sorry for the confusion

japoorv avatar Jul 21 '22 16:07 japoorv

One way to "bundle" black would be to bootstrap a python-environment with black using gradle-python-envs from JetBrains: build.gradle:

plugins{
    id "com.jetbrains.python.envs" version "0.0.31"
}
envs {
    bootstrapDirectory = new File(buildDir, 'bootstrap')
    envsDirectory = file(buildDir)
    condaenv "py_env", "3.10", [condaPackage("black")]
}
task show_black_version(type: Exec, dependsOn: 'build_envs'){
    commandLine("./build/py_env/bin/black", "--version")
}

However, this always bootstraps MiniConda and the environment, independent of whether another python installation with black already exists. What do you think about such an approach?

leicmi avatar Aug 20 '22 14:08 leicmi

This would work for the Gradle plugin, but not the Maven one, and that's fine with me! One way to incorporate something like this would be to add documentation to the manual on how to glue the two plugins together: https://github.com/diffplug/spotless/tree/main/plugin-gradle#black

and then do black().pathToExe('./build/py_env/bin/black')

nedtwigg avatar Aug 21 '22 19:08 nedtwigg

Unfortunately, it seems like this approach won't work. I tried to set a dependency on the python-environment using spotlessPythonApply.dependsOn('build_envs') (in build.gradle), however as of now ForeignExe.confirmVersionAndGetAbsolutePath() (of spotless) seems to be executed before any Gradle tasks.

leicmi avatar Aug 22 '22 13:08 leicmi

Ahh, yes. confirmVersionAndGetAbsolutePath is called as part of up-to-date checking... I really hope the Gradle depenency model learns to play nice with PyPI/NPM/RubyGems/etc someday...

nedtwigg avatar Aug 22 '22 22:08 nedtwigg

@ibabel-chwy if you have a specific problem you should open a new issue and include the requested info (version, logs, etc)

nedtwigg avatar Mar 13 '23 06:03 nedtwigg