Skip to content

[Gradle][K/JS] skip npm install task execution while in offline mode #5410

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

Open
wants to merge 3 commits into
base: master
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 @@ -141,12 +141,12 @@ class Npm internal constructor(
) {
val progressLogger = objects.newBuildOpLogger()
execWithProgress(progressLogger, description, execOps = execOps) { execSpec ->
val arguments: List<String> = mutableListOf<String>().apply {
val arguments = buildList {
Copy link
Author

@y9san9 y9san9 Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are quite unrelated to what I generally do in this PR, so I can either revert them or submit a different PR, or leave them

add("install")
addAll(args)
addAll(args.filter(String::isNotEmpty))
if (logger.isDebugEnabled) add("--verbose")
if (environment.ignoreScripts) add("--ignore-scripts")
}.filter { it.isNotEmpty() }
Copy link
Author

@y9san9 y9san9 Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we leave it there for cases when someone will pass empty arguments in the list?

Before, it was used so we can write code like this:

listOf(
  if (something) test else ""
)

Later, it was migrated to the approach with mutableList, but filter is still in place.

}

if (!environment.standalone) {
val nodeExecutable = nodeJs.nodeExecutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ abstract class KotlinNpmInstallTask :
@get:Internal
abstract val nodeModules: DirectoryProperty

private val isOffline = project.gradle.startParameter.isOffline()
Copy link
Author

@y9san9 y9san9 Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


@TaskAction
fun resolve() {
val args = buildList {
addAll(args)
if (isOffline) add("--offline")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I also ask you to introduce a test for this logic?
@ilgonmic, is it possible to somehow emulate the "offline" mode in our Gradle integration test infrastructure?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I would love to cover this with tests, but where can I see some examples?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have integration tests for the Gradle plugin. You can explore the JS-specific here: https://github.com/JetBrains/kotlin/blob/master/libraries%2Ftools%2Fkotlin-gradle-plugin-integration-tests%2Fsrc%2Ftest%2Fkotlin%2Forg%2Fjetbrains%2Fkotlin%2Fgradle%2FKotlin2JsGradlePluginIT.kt

The rest of the integration tests are located in the same Gradle sub-project

}

npmResolutionManager.get()
.installIfNeeded(
args = args,
Expand All @@ -56,4 +63,4 @@ abstract class KotlinNpmInstallTask :
companion object {
const val NAME = "kotlinNpmInstall"
}
}
}