Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.python.internal.UvExecutor;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -27,11 +28,7 @@
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.*;

import static java.util.Collections.synchronizedMap;

Expand Down Expand Up @@ -93,8 +90,12 @@ static Path getOrCreateWorkspace(String pyprojectContent, Map<String, String> en

// Check in-memory cache
Path cached = cache.get(hash);
if (cached != null && isWorkspaceValid(cached)) {
return cached;
if (cached != null) {
if (isWorkspaceValid(cached)) {
return cached;
}
cache.remove(hash);
ensureTempDirIsAbsent(cached);
}

// Check disk cache
Expand Down Expand Up @@ -189,8 +190,12 @@ static Path getOrCreateWorkspace(String pyprojectContent, Map<String, String> en

// Check in-memory cache
Path cached = cache.get(hash);
if (cached != null && isRequirementsWorkspaceValid(cached)) {
return cached;
if (cached != null) {
if (isRequirementsWorkspaceValid(cached)) {
return cached;
}
cache.remove(hash);
ensureTempDirIsAbsent(cached);
}

// Check disk cache
Expand Down Expand Up @@ -296,8 +301,12 @@ static Path getOrCreateWorkspace(String pyprojectContent, Map<String, String> en

// Check in-memory cache
Path cached = cache.get(hash);
if (cached != null && isRequirementsWorkspaceValid(cached)) {
return cached;
if (cached != null) {
if (isRequirementsWorkspaceValid(cached)) {
return cached;
}
cache.remove(hash);
ensureTempDirIsAbsent(cached);
}

// Check disk cache
Expand Down Expand Up @@ -490,4 +499,15 @@ private static void initializeCacheFromDisk() {
// Ignore - cache will be populated as needed
}
}

private static void ensureTempDirIsAbsent(Path cached) {
File file = cached.toFile();
if (file.exists()) {
try {
file.delete();
} catch (Exception e) {
//Just a safety to make sure we can write to the directory later on.
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.python.RequirementsTxtParser;
import org.openrewrite.python.internal.PyProjectHelper;
import org.openrewrite.text.Find;
import org.openrewrite.python.marker.PythonResolutionResult;
import org.openrewrite.text.Find;
import org.openrewrite.text.PlainText;
import org.openrewrite.text.PlainTextVisitor;
import org.openrewrite.trait.SimpleTraitMatcher;

import java.util.*;
Expand Down Expand Up @@ -213,9 +216,27 @@ public RequirementsFile withDependencySearchMarkers(Map<String, String> packageM
return this;
}
PlainText result = (PlainText) getTree();
Set<Marker> existingMarkers = new PlainTextVisitor<Set<Marker>>() {
@Override
public <M extends Marker> M visitMarker(Marker marker, Set<Marker> ctx) {
ctx.add(marker);
return (M) marker;
}
}.reduce(result, new HashSet<>());
for (Map.Entry<String, String> entry : packageMessages.entrySet()) {
Find find = new Find(entry.getKey(), null, false, null, null, null, null, null);
result = (PlainText) find.getVisitor().visitNonNull(result, ctx);
PlainText markedResult = (PlainText) find.getVisitor().visitNonNull(result, ctx);
if (markedResult != result) {
result = new PlainTextVisitor<ExecutionContext>() {
@Override
public <M extends Marker> M visitMarker(Marker marker, ExecutionContext executionContext) {
if (!existingMarkers.contains(marker) && marker instanceof SearchResult && ((SearchResult) marker).getDescription() == null) {
return (M) ((SearchResult) marker).withDescription(entry.getValue());
}
return super.visitMarker(marker, executionContext);
}
}.visitText(markedResult, ctx);
}
}
if (result != getTree()) {
return new RequirementsFile(new Cursor(cursor.getParentOrThrow(), result), marker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -620,7 +619,7 @@ void searchMarkersOnVulnerableDependency() {
singletonMap("requests", "CVE-2023-1234"))),
requirementsTxt(
"requests>=2.28.0\nflask>=2.0",
"~~>requests>=2.28.0\nflask>=2.0"
"~~(CVE-2023-1234)~~>requests>=2.28.0\nflask>=2.0"
)
);
}
Expand Down Expand Up @@ -675,7 +674,7 @@ void searchMarkersNullScopeMatchesAllFiles() {
singletonMap("requests", "CVE-2023-1234"), null, null)),
requirementsTxt(
"requests>=2.28.0",
"~~>requests>=2.28.0",
"~~(CVE-2023-1234)~~>requests>=2.28.0",
s -> s.path("requirements-dev.txt")
)
);
Expand All @@ -688,7 +687,7 @@ void searchMarkersMatchingScopeMarksFile() {
singletonMap("requests", "CVE-2023-1234"), "", null)),
requirementsTxt(
"requests>=2.28.0",
"~~>requests>=2.28.0"
"~~(CVE-2023-1234)~~>requests>=2.28.0"
)
);
}
Expand Down
Loading