Skip to content

Fix race when creating the npm install cache directory - #3096

Merged
nedtwigg merged 7 commits into
diffplug:mainfrom
wborn:fix/npm-cache-directory-race
Sep 25, 2026
Merged

nedtwigg merged 7 commits into
diffplug:mainfrom
wborn:fix/npm-cache-directory-race

Conversation

@wborn

@wborn wborn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Concurrent npm-based formatters sharing an initially missing npm install cache directory can report Cache dir could not be created.

NodeModulesCachingNpmProcessFactory.assertDir() checks whether the directory exists before calling mkdirs(). Another factory instance can create the directory between those operations, causing mkdirs() to return false and triggering a spurious exception.

This change:

  • Uses Files.createDirectories() to tolerate concurrent directory creation.
  • Removes synchronized from assertDir(). It only locks the current factory instance, so separate instances sharing the directory can still enter the method concurrently. The replacement does not need that instance-level lock.
  • Preserves actual filesystem failures through UncheckedIOException, including the cache path and original cause.

This addresses a possible cause of intermittent Prettier initialization errors observed in OpenRemote with Spotless 8.10.1. The reported stack trace points to this directory-creation check.

Related to #1984 and #2151.

Multiple NodeModulesCachingNpmProcessFactory instances can share the same cache directory.
If another instance creates it between the exists() check and mkdirs(), mkdirs() returns false and triggers a spurious "Cache dir could not be created." exception.

Use Files.createDirectories() to tolerate concurrent directory creation.
Preserve actual filesystem failures through UncheckedIOException, including the cache path and original cause.

Remove synchronized from assertDir(). It only locks the current factory instance and cannot coordinate other instances sharing the directory.
The replacement no longer needs that instance-level lock.
@nedtwigg

Copy link
Copy Markdown
Member

needs a spotlessApply, also add a changelog entry in plugin-gradle and plugin-maven

@nedtwigg
nedtwigg enabled auto-merge September 25, 2026 19:11
@nedtwigg
nedtwigg merged commit 8ac44c7 into diffplug:main Sep 25, 2026
18 checks passed
@wborn
wborn deleted the fix/npm-cache-directory-race branch September 26, 2026 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants