Gradle outputs.dir doesn't exist, task up-to-date? why?

Why gradle doesn’t detect outputs dir doesn’t exist?
I have to add an additional hint to make this happen, upToDateWhen. Shouldn’t it automatically see if I deleted the node_mofules dir?

task install {
    inputs.files(files(".babelrc", "package.json"))
    outputs.dir "node_modules"
    outputs.upToDateWhen {
        file("node_modules").exists()
    }
    doLast {
        exec {
            commandLine "npm.cmd", "i"
        }
    }
}

The following task works perfectly as expected, even if I manually delete webDir.
So my question is, where is the difference??

task buildWatch {
inputs.files(files(".babelrc", "package.json"))
inputs.dir srcDir
outputs.dir webDir
doLast {
    exec {
        commandLine "preact.cmd", "build", "--no-prerender","--dest", webDir
    }
}
}

I don’t see anything obviously wrong. Which version of Gradle are you using?

You’ve configured the install task to have node_modules as the output directory, but if the exec doesn’t actually create node_modules, Gradle will see the output as a missing directory and record that. So the next time you run the build, Gradle will see that node_modules is missing and the inputs haven’t changed, so the task can be skipped.

My guess would be that the node_modules directory existed even before you first executed the task. So the task would only consider anything new in that directory as an output. So if the task changed nothing, then the output of the task would be considered to be nothing.

If you delete the whole node_modules directory before you first run the task, then it should work as expected I think.