node-gradle / gradle-node-plugin

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

Specifying work dir / path to package.json #266

Closed milgner closed 1 year ago

milgner commented 1 year ago

My package.json isn't in the same directory as my build.gradle.kts but in src/frontend.

I tried this

tasks.named<PnpmTask>("pnpmInstall") {
    workingDir.set(project.rootDir.resolve("src/frontend"))
    args.set(listOf("--frozen-lockfile"))
}

along with

node {
    download.set(false)
    version.set("18.14.2")
    pnpmVersion.set("7.27.1")
    workDir.set(project.rootDir.resolve("src/frontend"))
    nodeProjectDir.set(project.rootDir.resolve("src/frontend"))
}

but none of these made any difference: I still get

  - In plugin 'com.github.node-gradle.node' type 'com.github.gradle.node.pnpm.task.PnpmInstallTask' property 'packageJsonFile' doesn't have a configured value.

It also strikes as a little bit strange that I'd have to call workingDir.set(...) instead of workingDir = ... but I guess that might be because Groovy and Kotlin having different concepts of mutability.

In any case, it's not clear whether I'm just doing something wrong, there is a bug or a missing feature? I couldn't find any equivalent to specifying the path to package.json.

deepy commented 1 year ago

For the syntax, this is a Gradle thing and with 8.1-rc-1 there's experimental support for enabling that kind of syntax in the Kotlin DSL as well

For the issue at hand this is unfortunately a limitation in the plugin, package.json is picked up from the project folder and I guess the recommendation here would be to move package.json to the same directory as your build script or possibly make a separate project just for the frontend (but I suspect that it might look weird with your layout)

There isn't any good reason to not allow you to manually do this change, but I'd have to add that support and I'm suffering through dental issues right now so it'll take me a while to get that done and released.

In short, it's a missing feature but with the project layouts I've previously seen this hasn't been an issue. Though most of them have been like the examples/simple-node project

milgner commented 1 year ago

First of all, I hope that your dental issues get better, soon. If it's indeed just a missing feature, I can look into adding support for that, too.

deepy commented 1 year ago

Oh actually, this might already exist, I was looking at the wrong branch

If you set nodeProjectDir in the node extension (as you have) then this should work, so this might actually be a bug rather than a missing feature

https://github.com/node-gradle/gradle-node-plugin/blob/3.x/src/main/kotlin/com/github/gradle/node/pnpm/task/PnpmInstallTask.kt#L45-L48

deepy commented 1 year ago

The weird part here is, there's actually a test for this and it's been passing: https://github.com/node-gradle/gradle-node-plugin/blob/0125322/src/test/groovy/com/github/gradle/node/pnpm/task/PnpmInstall_integTest.groovy#L91-L111

rdehuyss commented 1 year ago

Hi there, I'm Ronald, the creator of JobRunr and I was currently evaluating whether this plugin would for the JobRunr.

I'm also in need of the workingDir setting as the REACT code is in the folder src/main/resources/org/jobrunr/dashboard/frontend.

I noticed that I'm also bumping into this bug:

task npmRunBuild(type: NodeTask) {
    workingDir = new File(projectDir, 'src/main/resources/org/jobrunr/dashboard/frontend')
    logger.warn('Setting working dir to ' + new File(projectDir, 'src/main/resources/org/jobrunr/dashboard/frontend'))
    script = file('package.json')
    args = ['run', 'build']
}

The output:

...
Setting working dir to /Users/rdehuyss/Projects/Personal/jobrunr/jobrunr/core/src/main/resources/org/jobrunr/dashboard/frontend
...
* What went wrong:
A problem was found with the configuration of task ':core:npmRunBuild' (type 'NodeTask').
  - In plugin 'com.github.node-gradle.node' type 'com.github.gradle.node.task.NodeTask' property 'script' specifies file '/Users/rdehuyss/Projects/Personal/jobrunr/jobrunr/core/package.json' which doesn't exist.
deepy commented 1 year ago

@rdehuyss iirc all you should need to set is nodeProjectDir on the extension itself like here

I guess in this case it's very un-intuitive that the tasks themselves configure some things

deepy commented 1 year ago

I'm not able to reproduce this and the tests pass, this is probably our documentation being bad. But if anyone has a reproducer let me know and I'll fix it immediately

deepy commented 1 year ago

(It may also have been accidentally fixed in 6.0.0, but the tests passed even before my changes)