srs / gradle-node-plugin

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

Yarn tasks do not benefit from node.nodeModulesDir if they are not used in the build (and instead called directly) #289

Open muryoh opened 6 years ago

muryoh commented 6 years ago

The problem is basically the way the YarnTask is configured: in its constructor, a project.afterEvaluate hook is registered to setup the working dir of the task in order to use the setup node.nodeModulesDir

However that hook is not always called, especially not when the task is created after the project was evaluated (probably because at this point you can no longer register after evaluation blocks)

Here's a simple reproduction script:

apply plugin: 'com.moowork.node'

tasks.yarn_somescript {
    // some setup
}

node {
    nodeModulesDir = file('foobar')
}

project.afterEvaluate {
    [tasks.yarn, tasks.yarn_somescript].each {
        println "${it.name} working dir: ${it.runner.workingDir}"
    }
}

Now, here my use case is that i have 1 scripts in my package.json called somescript. somescript requires some setup so it is referenced in the build.

If you simply run gradle on that script you'll see:

./gradlew yarn working dir: /project/foobar yarn_somescript working dir: /project/foobar ...

But now if you no longer need any setup on the yarn_somescript task and remove it from the build, then re-running will yield:

./gradlew yarn working dir: /project/foobar yarn_somescript working dir: null ...

A simple workaround is to simply reference them in the build. But it's still a workaround :-)