node-gradle / gradle-node-plugin

Gradle plugin for integrating NodeJS in your build. :rocket:
Apache License 2.0
599 stars 117 forks source link

npmInstall ignores npm workspaces in up-to-date detection #304

Open hosswald opened 6 months ago

hosswald commented 6 months ago

We have a monorepo with multiple workspaces. The workspaces also reflect the gradle (sub)project structure. Our project structure looks like this:

build.gradle
package.json
package-lock.json
node_modules/
scripts/
\_ build.gradle
|_ package.json

The root package.json defined scripts/ as a workspace:

  "workspaces": [
    "scripts"
  ]

We apply the gradle-node-plugin in the scripts/build.gradle like this:

node {
    nodeProjectDir = rootProject.projectDir
}

task compile(type: NpmTask) {
    dependsOn = [tasks.npmInstall]
    workingDir = rootProject.projectDir
    args = [
        "run",
        "build",
    ]
}

The problem is that npmInstall only registers changes to the root package.json etc., but not to scripts/package.json etc. So if I change a dependency in scripts/package.json, npmInstall might not run. It would be nice if the npmInstall task could check for changes in package.json etc. in all workspaces defined in the root package.json as well. Currently, we need this workaround:

npmInstall {
    outputs.upToDateWhen {false}
}

I don't think the problem is related to our unusual project structure, but likely affects every situation where NPM workspaces are involved - NpmInstallTask just doesn't seem to consider changes to workspace files / npm files outside of nodeProjectDir at all.

wickkidd commented 3 months ago

Ditto for yarn workspaces. I need to tell gradle-node-plugin to use a subfolder for package.json and root for the lock file.

brnhffmnn commented 1 month ago

I just changed my module setup and now I am encountering this. When running a NpxTask in CI I get:

npm error could not determine executable to run

It seems, it is because I have the relevant dependency defined in the parent module.