-
-
Notifications
You must be signed in to change notification settings - Fork 120
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 #301
Draft
adam-enko
wants to merge
2
commits into
node-gradle:main
Choose a base branch
from
adam-enko:npm-install-cacheable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
NPM Install caching #301
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/test/groovy/com/github/gradle/node/npm/task/NpmBuildCache_integTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist/ | ||
build-cache/ |
53 changes: 53 additions & 0 deletions
53
src/test/resources/fixtures/npm-build-cache/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
src/test/resources/fixtures/npm-build-cache/gradle.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of removing the
@Optional @OutputFile
you probably want to follow the pattern belowi.e. add a configurable flag for which behaviour and if it's set to the new behaviour then just return null here immediately
(Though at that point I suspect all the output methods might need to be reworked slightly, but that's boring tedious work and you can happily leave that to the maintainers)