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

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

y9san9
Copy link

@y9san9 y9san9 commented Feb 28, 2025

Hello! I am a very naive first-time contributor. I created a ticket in youtrack and assume I can give it a shot and implement it on my own. This is my first iteration which might be completely wrong. So I ask for help to get some directions to me in order if this one is way off.

Right now I just skip execution of npmInstall completely, but I don't know what are the consequences of that. Hope you understand and will give me this ability to try and do this feature.

@y9san9 y9san9 force-pushed the master branch 2 times, most recently from b13cb98 to da4dad3 Compare March 1, 2025 10:09
@@ -41,8 +41,12 @@ 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.

@JSMonk
Copy link
Member

JSMonk commented Mar 14, 2025

Hi @y9san9.
Thank you for your fantastic contribution 🤩
Let's tune it a bit. After discussing it with @ilgonmic, I realized that instead of just ignoring the running task, we could try running it in offline mode, and yarn/npm could try downloading the dependencies from the local cache.
This could be done by providing the yarn/npm CLI with the argument --offline. Instead of skipping the task execution, let's just give this argument to the npmResolutionManager.
Of course, this means that if a dependency cannot be fetched either by network or from the cache, we can't build a WasmJs or JS project without the required dependencies.

add("install")
addAll(args)
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.

@@ -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

@y9san9
Copy link
Author

y9san9 commented Mar 14, 2025

Hello @JSMonk! Thank you for your time. I did what you asked for.

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants