openrewrite / openrewrite/rewrite

Issue discovered on `src/test/resources/dataflow-functional-tests/ArchiveAnalyzer.java` with `Use Files#createTempDirectory`

Open
#4,437 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

Problem

The recipe org.openrewrite.java.security.UseFilesCreateTempDirectory made a change that deletes a bunch of code that may or may not be needed.

Expected behavior

I expected the recipe to only make the required set of changes. The result I got changes the underlying behavior.

Example diff

From: src/test/resources/dataflow-functional-tests/ArchiveAnalyzer.java

 
import javax.annotation.concurrent.ThreadSafe;
import java.io.*;
+import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public void prepareFileTypeAnalyzer(Engine engine) throws InitializationException {
try {
  final File baseDir = getSettings().getTempDirectory();
-            tempFileLocation = File.createTempFile("check", "tmp", baseDir);
-            if (!tempFileLocation.delete()) {
-                setEnabled(false);
-                final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
-                throw new InitializationException(msg);
-            }
-            if (!tempFileLocation.mkdirs()) {
-                setEnabled(false);
-                final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath());
-                throw new InitializationException(msg);
-            }
+            tempFileLocation = Files.createTempDirectory(baseDir.toPath(), "check" + "tmp").toFile();
} catch (IOException ex) {
  setEnabled(false);
  throw new InitializationException("Unable to create a temporary file", ex);
Recipes in example diff:
  • org.openrewrite.java.security.UseFilesCreateTempDirectory
References:
  • View original result
  • Recipe ID: org.openrewrite.java.security.UseFilesCreateTempDirectory
  • Recipe Name: Use Files#createTempDirectory``
  • Repository: openrewrite/rewrite-analysis/main
  • Created at Thu Aug 22 2024 09:49:22 GMT-0300 (Horário Padrão da Argentina)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the transformation produced by org.openrewrite.java.security.UseFilesCreateTempDirectory for src/test/resources/dataflow-functional-tests/ArchiveAnalyzer.java, using the linked original result as the comparison. Reproduce the example and inspect why the recipe replaces the deletion, enablement, and directory-creation checks. Done means the recipe makes only the required temporary-directory change without altering the surrounding behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.