Skip to content

Add ability to use npm and node executables installed by node-gradle-plugin #1499

Closed
@simschla

Description

@simschla

When using the node-gradle-plugin to install the node/npm binaries, we have a timing issue:

  • The node-gradle-plugin installs missing node/npm binaries in the execution phase.
  • Spotless tries to run npm install (for preparing formatting servers) in the configuration phase.

Given that scenario, spotless will always fail in a clean setup, when a user tries to configure spotless to use the binaries installed by node-gradle-plugin (though it might succeed in a dirty state, when installation of the binaries has already happened in a previous gradle run).

Snippet that demonstrates the problem:

plugins {
    id 'com.diffplug.spotless'
    id 'com.github.node-gradle.node' version '3.5.1'
}
repositories { mavenCentral() }
node {
    download = true
    version = '18.13.0'
    workDir = file("${buildDir}/nodejs")
}
spotless {
    typescript {
        target 'app/**/*.ts'
        prettier()
            .nodeExecutable("${tasks.named('npmSetup').get().npmDir.get()}/bin/npm")
    }
}
tasks.named('spotlessTypescript').configure {
    it.dependsOn('nodeSetup', 'npmSetup')
}

Calling gradlew spotlessTypesriptApply will fail with something along the lines:

* Exception is:
org.gradle.api.internal.tasks.TaskDependencyResolveException: Could not determine the dependencies of task ':spotlessApply'.
[...]
Caused by: org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreationException: Could not create task ':spotlessMytypescriptApply'.
	at org.gradle.api.internal.tasks.DefaultTaskContainer.taskCreationException(DefaultTaskContainer.java:720)
[...]
Caused by: com.diffplug.spotless.npm.NpmProcess$NpmProcessException: Failed to launch npm command 'npm install --no-audit --no-package-lock --no-fund --prefer-offline'.
[...]
Caused by: java.io.IOException: Cannot run program "/private/var/folders/mb/xcsw5ld95cxbm8m310v0lpn0000141/T/junit7221955071881174664/build/npm/npm-v8.19.2/bin/npm" (in directory "/private/var/folders/mb/xcsw5ld95cxbm8m310v0lpn0000141/T/junit7221955071881174664/build/spotless-node-modules-prettier-format"): error=2, No such file or directory

This issue is to discuss the situation and possible solutions.

(Thanks @jnels124 for bringing this up in #1381 )

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions