Skip to content

Commit 4d3b85b

Browse files
Python: Include pyproject.toml in ParseProject results (#6744)
Wire PyProjectTomlParser into PythonRewriteRpc.parseProject() so that pyproject.toml files are parsed and returned alongside .py files from the RPC, with a PythonResolutionResult marker attached.
1 parent 3fbb6ea commit 4d3b85b

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

rewrite-python/src/integTest/java/org/openrewrite/python/ParseProjectIntegTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import org.junit.jupiter.api.io.TempDir;
2323
import org.openrewrite.InMemoryExecutionContext;
2424
import org.openrewrite.SourceFile;
25+
import org.openrewrite.python.marker.PythonResolutionResult;
2526
import org.openrewrite.python.rpc.PythonRewriteRpc;
27+
import org.openrewrite.toml.tree.Toml;
2628

2729
import java.io.IOException;
2830
import java.nio.file.Files;
@@ -160,6 +162,36 @@ void parsesAbsolutePath() throws IOException {
160162
assertThat(sources).hasSize(1);
161163
}
162164

165+
@Test
166+
@Timeout(value = 60, unit = TimeUnit.SECONDS)
167+
void includesPyprojectToml() throws IOException {
168+
Path projectDir = tempDir.resolve("with_pyproject");
169+
Files.createDirectories(projectDir);
170+
171+
Files.writeString(projectDir.resolve("main.py"), "x = 1");
172+
Files.writeString(projectDir.resolve("pyproject.toml"), """
173+
[project]
174+
name = "myapp"
175+
version = "1.0.0"
176+
dependencies = ["requests>=2.28.0"]
177+
""");
178+
179+
List<SourceFile> sources = client()
180+
.parseProject(projectDir, new InMemoryExecutionContext())
181+
.collect(Collectors.toList());
182+
183+
assertThat(sources)
184+
.extracting(sf -> sf.getSourcePath().getFileName().toString())
185+
.contains("main.py", "pyproject.toml");
186+
187+
SourceFile pyproject = sources.stream()
188+
.filter(sf -> sf.getSourcePath().getFileName().toString().equals("pyproject.toml"))
189+
.findFirst()
190+
.orElseThrow();
191+
assertThat(pyproject).isInstanceOf(Toml.Document.class);
192+
assertThat(pyproject.getMarkers().findFirst(PythonResolutionResult.class)).isPresent();
193+
}
194+
163195
private PythonRewriteRpc client() {
164196
return PythonRewriteRpc.getOrStart();
165197
}

rewrite-python/src/main/java/org/openrewrite/python/rpc/PythonRewriteRpc.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import org.openrewrite.tree.ParsingEventListener;
2828
import org.openrewrite.tree.ParsingExecutionContextView;
2929

30+
import org.openrewrite.Parser;
31+
import org.openrewrite.python.PyProjectTomlParser;
32+
3033
import java.io.File;
3134
import java.io.IOException;
3235
import java.io.PrintStream;
@@ -175,7 +178,7 @@ public Stream<SourceFile> parseProject(Path projectPath, @Nullable List<String>
175178
public Stream<SourceFile> parseProject(Path projectPath, @Nullable List<String> exclusions, @Nullable Path relativeTo, ExecutionContext ctx) {
176179
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
177180

178-
return StreamSupport.stream(new Spliterator<SourceFile>() {
181+
Stream<SourceFile> rpcStream = StreamSupport.stream(new Spliterator<SourceFile>() {
179182
private int index = 0;
180183
private @Nullable ParseProjectResponse response;
181184

@@ -214,6 +217,17 @@ public int characteristics() {
214217
return response == null ? ORDERED : ORDERED | SIZED | SUBSIZED;
215218
}
216219
}, false);
220+
221+
// Parse pyproject.toml if present
222+
Path pyprojectPath = projectPath.resolve("pyproject.toml");
223+
if (Files.exists(pyprojectPath)) {
224+
Path effectiveRelativeTo = relativeTo != null ? relativeTo : projectPath;
225+
Parser.Input input = Parser.Input.fromFile(pyprojectPath);
226+
Stream<SourceFile> pyprojectStream = new PyProjectTomlParser().parseInputs(
227+
Collections.singletonList(input), effectiveRelativeTo, ctx);
228+
return Stream.concat(rpcStream, pyprojectStream);
229+
}
230+
return rpcStream;
217231
}
218232

219233
public static Builder builder() {

0 commit comments

Comments
 (0)