Skip to content

Commit

Permalink
Fixed failure querying the task inputs/outputs on 8+ #259
Browse files Browse the repository at this point in the history
This only happens if you're on Gradle 8+ _and_ have configuration-cache
enabled, _and_ actually query the TaskInputs/TaskOutputs
  • Loading branch information
deepy committed Aug 7, 2023
1 parent 2f80985 commit 06c0d5d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fixes configuration-cache issue in Gradle 8.4
* Removes the entire `PlatformHelper` as the functionality has migrated into top-level functions
* Removes the deprecated `HelperExecution`
* Fixed failure querying the task inputs/outputs on 8+ with configuration-cache enabled [#259](https://github.com/node-gradle/gradle-node-plugin/issues/259)

## Version 5.0.0 *(2023-05-13)*
* Support configuration-cache on Gradle 8.1 [#271](https://github.com/node-gradle/gradle-node-plugin/issues/271)
Expand Down
28 changes: 14 additions & 14 deletions src/main/kotlin/com/github/gradle/node/npm/task/NpmInstallTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,42 @@ abstract class NpmInstallTask : NpmTask() {
@PathSensitive(RELATIVE)
@InputFile
@SkipWhenEmpty
protected fun getPackageJsonFile(): Provider<File> {
return projectFileIfExists("package.json")
protected fun getPackageJsonFile(): File? {
return projectFileIfExists("package.json").orNull
}

@PathSensitive(RELATIVE)
@Optional
@InputFile
protected fun getNpmShrinkwrap(): Provider<File> {
return projectFileIfExists("npm-shrinkwrap.json")
protected fun getNpmShrinkwrap(): File? {
return projectFileIfExists("npm-shrinkwrap.json").orNull
}

@PathSensitive(RELATIVE)
@Optional
@InputFile
protected fun getPackageLockFileAsInput(): Provider<File> {
protected fun getPackageLockFileAsInput(): File? {
return npmCommand.flatMap { command ->
if (command[0] == "ci") projectFileIfExists("package-lock.json") else providers.provider { null }
}
}.orNull
}

@PathSensitive(RELATIVE)
@Optional
@InputFile
protected fun getYarnLockFile(): Provider<File> {
return projectFileIfExists("yarn.lock")
protected fun getYarnLockFile(): File? {
return projectFileIfExists("yarn.lock").orNull
}

@Optional
@OutputFile
protected fun getPackageLockFileAsOutput(): Provider<File> {
protected fun getPackageLockFileAsOutput(): File? {
return npmCommand.flatMap { command ->
if (command[0] == "install") projectFileIfExists("package-lock.json") else providers.provider { null }
}
}.orNull
}

private fun projectFileIfExists(name: String): Provider<File> {
private fun projectFileIfExists(name: String): Provider<File?> {
return nodeExtension.nodeProjectDir.map { it.file(name).asFile }
.flatMap { if (it.exists()) providers.provider { it } else providers.provider { null } }
}
Expand Down Expand Up @@ -107,12 +107,12 @@ abstract class NpmInstallTask : NpmTask() {

@Optional
@OutputFile
protected fun getNodeModulesPackageLock(): Provider<File> {
protected fun getNodeModulesPackageLock(): File? {
if (isLegacyNpm()) {
return providers.provider { null }
return null
}

return projectFileIfExists("node_modules/.package-lock.json")
return projectFileIfExists("node_modules/.package-lock.json").orNull
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/github/gradle/node/npm/task/NpmTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.gradle.node.task.BaseTask
import com.github.gradle.node.util.DefaultProjectApiHelper
import org.gradle.api.Action
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
Expand All @@ -18,6 +19,7 @@ import org.gradle.kotlin.dsl.mapProperty
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.property
import org.gradle.process.ExecSpec
import java.io.File
import javax.inject.Inject

abstract class NpmTask : BaseTask() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ abstract class PnpmInstallTask : PnpmTask() {

@PathSensitive(RELATIVE)
@InputFile
protected fun getPackageJsonFile(): Provider<File> {
return projectFileIfExists("package.json")
protected fun getPackageJsonFile(): File? {
return projectFileIfExists("package.json").orNull
}

@Optional
@OutputFile
protected fun getPnpmLockAsOutput(): Provider<File> {
return projectFileIfExists("pnpm-lock.yaml")
protected fun getPnpmLockAsOutput(): File? {
return projectFileIfExists("pnpm-lock.yaml").orNull
}

private fun projectFileIfExists(name: String): Provider<File> {
private fun projectFileIfExists(name: String): Provider<File?> {
return nodeExtension.nodeProjectDir.map { it.file(name).asFile }
.flatMap { if (it.exists()) providers.provider { it } else providers.provider { null } }
.flatMap { if (it.exists()) providers.provider { it } else providers.provider { null } }
}

@Optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ abstract class YarnInstallTask : YarnTask() {
@PathSensitive(RELATIVE)
@Optional
@InputFile
protected fun getPackageJsonFile(): Provider<File> {
return projectFileIfExists("package.json")
protected fun getPackageJsonFile(): File? {
return projectFileIfExists("package.json").orNull
}

@PathSensitive(RELATIVE)
@Optional
@InputFile
protected fun getYarnLockFile(): Provider<File> {
return projectFileIfExists("yarn.lock")
protected fun getYarnLockFile(): File? {
return projectFileIfExists("yarn.lock").orNull
}

@Optional
@OutputFile
protected fun getYarnLockFileAsOutput(): Provider<File> {
return projectFileIfExists("yarn.lock")
protected fun getYarnLockFileAsOutput(): File? {
return projectFileIfExists("yarn.lock").orNull
}

private fun projectFileIfExists(name: String): Provider<File> {
private fun projectFileIfExists(name: String): Provider<File?> {
return nodeExtension.nodeProjectDir.map { it.file(name).asFile }
.flatMap { if (it.exists()) providers.provider { it } else providers.provider { null } }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ class NpmInstall_integTest extends AbstractIntegTest {
gv << GRADLE_VERSIONS_UNDER_TEST
}
// FIXME: https://github.com/node-gradle/gradle-node-plugin/issues/259
@IgnoreIf({ gv >= GradleVersion.version("8.0-milestone-1") })
def 'verify npm install inputs/outputs (#gv.version)'() {
given:
gradleVersion = gv
Expand All @@ -215,9 +213,10 @@ class NpmInstall_integTest extends AbstractIntegTest {
}

def lock = file('package-lock.json')
def installTask = tasks.named("npmInstall").get()
def outputs = installTask.outputs.files
def inputs = installTask.inputs.files
task verifyIO {
def outputs = tasks.named("npmInstall").get().outputs.files
def inputs = tasks.named("npmInstall").get().inputs.files
doLast {
if (!outputs.contains(lock)) {
throw new RuntimeException("package-lock.json is not in INSTALL'S outputs!")
Expand All @@ -241,8 +240,6 @@ class NpmInstall_integTest extends AbstractIntegTest {
gv << GRADLE_VERSIONS_UNDER_TEST
}
// FIXME: https://github.com/node-gradle/gradle-node-plugin/issues/259
@IgnoreIf({ gv >= GradleVersion.version("8.0-milestone-1") })
def 'verify npm ci inputs/outputs (#gv.version)'() {
given:
gradleVersion = gv
Expand All @@ -257,9 +254,10 @@ class NpmInstall_integTest extends AbstractIntegTest {
}

def lock = file('package-lock.json')
def installTask = tasks.named("npmInstall").get()
def outputs = installTask.outputs.files
def inputs = installTask.inputs.files
task verifyIO {
def outputs = tasks.named("npmInstall").get().outputs.files
def inputs = tasks.named("npmInstall").get().inputs.files
doLast {
if (outputs.contains(lock)) {
throw new RuntimeException("package-lock.json is in CI'S outputs!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ class PnpmInstall_integTest
}

def lock = file('pnpm-lock.yaml')
def installTask = tasks.named("pnpmInstall").get()
def outputs = installTask.outputs.files
def inputs = installTask.inputs.files
task verifyIO {
def outputs = tasks.named("pnpmInstall").get().outputs.files
def inputs = tasks.named("pnpmInstall").get().inputs.files

doLast {
if (!outputs.contains(lock)) {
throw new RuntimeException("pnpm-lock.yaml is not in INSTALL'S outputs!")
Expand Down

0 comments on commit 06c0d5d

Please sign in to comment.