Exec task succeeding when executed directly but failing when executed via parent project


(Max) #1

Hi,

I have the following task, which exists in a subproject:

def suffix = Os.isFamily(Os.FAMILY_WINDOWS) ? ".cmd" : ""
task cssBuild(type: Exec) {
    def input = 'src/main/sass'
    def output = 'build/web'
    inputs.dir(input)
    outputs.dir(output)
    commandLine "node_modules/.bin/node-sass$suffix", input, '--recursive', '--output', output
}

If I invoke the gradlew clean cssBuild --info from within this subproject, I get

Starting process 'command 'node_modules/.bin/node-sass.cmd''. Working directory: [project]\[subproject] Command: node_modules/.bin/node-sass.cmd src/main/sass --recursive --output build/web
Successfully started process 'command 'node_modules/.bin/node-sass.cmd''
Rendering Complete, saving .css file...
Wrote CSS to [project]\[subproject]\build\web\main.css
Wrote 1 CSS files to [project]\[subproject]\build\web
:cssBuild (Thread[Task worker for ':',5,main]) completed. Took 0.957 secs.

But if, from the parent project, I run gradlew assemble --stacktrace (which hits the above task), I get:

> Task :[subproject]:cssBuild FAILED

FAILURE: Build failed with an exception.
// snip
Caused by: java.io.IOException: Cannot run program "node_modules/.bin/node-sass.cmd" (in directory "[project]\[subproject]"): CreateProcess error=2, The system cannot find the file specified
        at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
        ... 7 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        ... 8 more

Even though the file definitely exists.

Basically, in both cases, the working directory is shown to be the same, and the program is shown to be the same. So why is it failing in one but not the other?

Gradle 4.9, Windows.


(Max) #2

I figured out a workaround. If I update the commandLine bit to this, it works:

commandLine "${projectDir}/node_modules/.bin/node-sass$suffix", input, '--recursive', '--output', output

(Adding ${projectDir}). But why is this necessary? Does commandLine really not factor in the working directory?


(Nicolas) #3

Hi Max,

I had the same issue and figured out the same workaround. I don’t know why but commandLine does not factor in the working directory.