diff --git a/jedi/api/refactoring/__init__.py b/jedi/api/refactoring/__init__.py index 84513da0..9e08cc21 100644 --- a/jedi/api/refactoring/__init__.py +++ b/jedi/api/refactoring/__init__.py @@ -42,11 +42,17 @@ class ChangedFile: if self._from_path is None: from_p = '' else: - from_p = self._from_path.relative_to(project_path) + try: + from_p = self._from_path.relative_to(project_path) + except ValueError: # Happens it the path is not on th project_path + from_p = self._from_path if self._to_path is None: to_p = '' else: - to_p = self._to_path.relative_to(project_path) + try: + to_p = self._to_path.relative_to(project_path) + except ValueError: + to_p = self._to_path diff = difflib.unified_diff( old_lines, new_lines, fromfile=str(from_p), diff --git a/test/test_api/test_refactoring.py b/test/test_api/test_refactoring.py index c229c129..f61af13d 100644 --- a/test/test_api/test_refactoring.py +++ b/test/test_api/test_refactoring.py @@ -70,3 +70,19 @@ def test_diff_without_ending_newline(Script): -a +c ''') + + +def test_diff_path_outside_of_project(Script): + script = Script( + code='foo = 1', + path='/unknown_dir/file.py', + project=jedi.get_default_project() + ) + diff = script.rename(line=1, column=0, new_name='bar').get_diff() + assert diff == dedent('''\ + --- /unknown_dir/file.py + +++ /unknown_dir/file.py + @@ -1 +1 @@ + -foo = 1 + +bar = 1 + ''')