Skip to content

Commit e7f8710

Browse files
Handle RPC deserialization errors in PythonRewriteRpc#parseProject() (#6812)
1 parent 35854f8 commit e7f8710

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

rewrite-python/rewrite/src/rewrite/rpc/server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ def parse_python_source(source: str, path: str = "<unknown>", relative_to: Optio
272272
local_objects[obj_id] = cu
273273
return {
274274
'id': obj_id,
275-
'sourceFileType': 'org.openrewrite.python.tree.Py$CompilationUnit'
275+
'sourceFileType': 'org.openrewrite.python.tree.Py$CompilationUnit',
276+
'sourcePath': str(source_path)
276277
}
277278
except ImportError as e:
278279
logger.exception(f"Failed to import parser: {e}")
@@ -319,7 +320,7 @@ def _create_parse_error(path: str, message: str, source: str = '') -> dict:
319320

320321
obj_id = str(parse_error.id)
321322
local_objects[obj_id] = parse_error
322-
return {'id': obj_id, 'sourceFileType': 'org.openrewrite.tree.ParseError'}
323+
return {'id': obj_id, 'sourceFileType': 'org.openrewrite.tree.ParseError', 'sourcePath': path}
323324

324325

325326
def _infer_project_root(inputs: list) -> Optional[str]:

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,10 @@ static class Item {
4040
* Example: org.openrewrite.python.tree.Py$CompilationUnit
4141
*/
4242
String sourceFileType;
43+
44+
/**
45+
* The relative source path of the file.
46+
*/
47+
String sourcePath;
4348
}
4449
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import org.openrewrite.python.marker.PythonResolutionResult;
2626
import org.openrewrite.python.marker.PythonResolutionResult.Dependency;
2727
import org.openrewrite.python.marker.PythonResolutionResult.ResolvedDependency;
28+
import org.openrewrite.marker.Markers;
2829
import org.openrewrite.python.tree.Py;
2930
import org.openrewrite.rpc.RewriteRpc;
3031
import org.openrewrite.rpc.RewriteRpcProcess;
3132
import org.openrewrite.rpc.RewriteRpcProcessManager;
33+
import org.openrewrite.tree.ParseError;
3234
import org.openrewrite.tree.ParsingEventListener;
3335
import org.openrewrite.tree.ParsingExecutionContextView;
3436

@@ -41,6 +43,7 @@
4143
import java.io.InputStream;
4244
import java.io.PrintStream;
4345
import java.io.UncheckedIOException;
46+
import java.nio.charset.StandardCharsets;
4447
import java.nio.file.Files;
4548
import java.nio.file.Path;
4649
import java.nio.file.Paths;
@@ -204,9 +207,24 @@ public boolean tryAdvance(Consumer<? super SourceFile> action) {
204207
ParseProjectResponse.Item item = response.get(index);
205208
index++;
206209

207-
SourceFile sourceFile = getObject(item.getId(), item.getSourceFileType());
208-
// for status update messages
209-
parsingListener.startedParsing(Parser.Input.fromFile(sourceFile.getSourcePath()));
210+
SourceFile sourceFile;
211+
try {
212+
sourceFile = getObject(item.getId(), item.getSourceFileType());
213+
parsingListener.startedParsing(Parser.Input.fromFile(sourceFile.getSourcePath()));
214+
} catch (Exception e) {
215+
sourceFile = new ParseError(
216+
Tree.randomId(),
217+
new Markers(Tree.randomId(), Collections.singletonList(
218+
ParseExceptionResult.build(PythonParser.class, e, null))),
219+
Paths.get(item.getSourcePath()),
220+
null,
221+
StandardCharsets.UTF_8.name(),
222+
false,
223+
null,
224+
e.getMessage(),
225+
null
226+
);
227+
}
210228
action.accept(sourceFile);
211229
return true;
212230
}

0 commit comments

Comments
 (0)