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

NPM Install caching #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ abstract class NpmInstallTask : NpmTask() {
return projectFileIfExists("yarn.lock").orNull
}

@Optional
@OutputFile
// does this output overlap with getNodeModulesFiles()?
//@Optional
//@OutputFile
@Internal
protected fun getPackageLockFileAsOutput(): File? {
return npmCommand.flatMap { command ->
if (command[0] == "install") projectFileIfExists("package-lock.json") else providers.provider { null }
Expand Down Expand Up @@ -105,8 +107,10 @@ abstract class NpmInstallTask : NpmTask() {
}
}

// does this output overlap with getNodeModulesFiles()?
@Optional
@OutputFile
// @Internal
protected fun getNodeModulesPackageLock(): File? {
if (isLegacyNpm()) {
return null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.github.gradle.node.npm.task

import com.github.gradle.AbstractIntegTest

import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

class NpmBuildCache_integTest extends AbstractIntegTest {

def 'npmInstall can be loaded from-cache'() {
given:
gradleVersion = gv

copyResources("fixtures/npm-build-cache/")

createFile("build-cache/").deleteDir()
createFile("dist/").deleteDir()


when:
def assemble1Result = build("assemble", "--stacktrace", "-PenableNpmInstallCaching=true")

then:
assemble1Result.task(":npmInstall").outcome == SUCCESS
assemble1Result.task(":npmRunBuild").outcome == SUCCESS

createFile("package-lock.json").exists()
createFile("node_modules").exists()
createFile("dist/app.js").isFile()


when:
def cleanResult = build("clean", "--stacktrace", "-PenableNpmInstallCaching=true")

then:
cleanResult.task(":clean").outcome == SUCCESS
createFile("package-lock.json").exists()
!createFile("node_modules").exists()
!createFile("dist").exists()


when:
def assemble2Result = build("assemble", "--stacktrace", "-PenableNpmInstallCaching=true")

then:
assemble2Result.task(":npmInstall").outcome == FROM_CACHE
assemble2Result.task(":npmRunBuild").outcome == FROM_CACHE
createFile("node_modules").exists()
createFile("dist").exists()
createFile("dist/app.js").isFile()

where:
gv << GRADLE_VERSIONS_UNDER_TEST
}


def 'test npmInstall has cacheable outputs'() {
// check the `org.gradle.caching.debug` logs to verify that the output is cacheable

given:
gradleVersion = gv

copyResources("fixtures/npm-build-cache/")

createFile("build-cache/").deleteDir()
createFile("dist/").deleteDir()


when:
def args = ["assemble", "--stacktrace", "-Dorg.gradle.caching.debug=true", "-PenableNpmInstallCaching=true"]
def assemble1Result = build(*args)
def npmInstall1Output = assemble1Result.output
.takeAfter("> Task :npmInstall")
.takeAfter("\n")
.takeBefore("\n\n")

then:
!npmInstall1Output.contains("Non-cacheable")
!npmInstall1Output.contains("[OVERLAPPING_OUTPUTS]")
!npmInstall1Output.contains("Gradle does not know how file 'node_modules/.package-lock.json' was created")


when:
def assemble2Result = build(*args)
def npmInstall2Output = assemble2Result.output
.takeAfter("> Task :npmInstall")
.takeAfter("\n")
.takeBefore("\n\n")

then:
!npmInstall2Output.contains("Non-cacheable")
!npmInstall2Output.contains("[OVERLAPPING_OUTPUTS]")
!npmInstall2Output.contains("Gradle does not know how file 'node_modules/.package-lock.json' was created")

// the inputs and outputs shouldn't have changed, so the fingerprinted properties should be the same
npmInstall1Output == npmInstall2Output

where:
gv << GRADLE_VERSIONS_UNDER_TEST
}
}
2 changes: 2 additions & 0 deletions src/test/resources/fixtures/npm-build-cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
build-cache/
53 changes: 53 additions & 0 deletions src/test/resources/fixtures/npm-build-cache/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import com.github.gradle.node.npm.task.NpmTask
import org.gradle.api.tasks.PathSensitivity

plugins {
id("com.github.node-gradle.node")
base
}

node {
version.set("16.13.0")

download.set(true)
distBaseUrl.set(null as String?)

//fastNpmInstall.set(true)
}

tasks.npmInstall {
val enableNpmInstallCaching =
providers.gradleProperty("enableNpmInstallCaching").map(String::toBoolean).orElse(false)
inputs.property("enableNpmInstallCaching", enableNpmInstallCaching)
outputs.cacheIf { enableNpmInstallCaching.get() }
}

val distributionDirectory = layout.projectDirectory.dir("dist")

val npmRunBuild by tasks.registering(NpmTask::class) {
dependsOn(tasks.npmInstall)

npmCommand.set(listOf("run", "build"))

inputs.dir("src/main")
.withPropertyName("sources")
.withPathSensitivity(PathSensitivity.RELATIVE)

inputs.file("package.json")
.withPropertyName("packageJson")
.withPathSensitivity(PathSensitivity.RELATIVE)

outputs.dir(distributionDirectory)
.withPropertyName("distributionDirectory")

outputs.cacheIf("always cache, this task produces files") { true }
}

tasks.assemble {
dependsOn(npmRunBuild)
}

tasks.clean {
delete(distributionDirectory)
delete("node_modules")
}
3 changes: 3 additions & 0 deletions src/test/resources/fixtures/npm-build-cache/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.caching.debug=true
Loading
Loading