From 4a576c67b08fb30729eb54f07889684669e1af26 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Sun, 1 Dec 2024 10:51:29 -0300 Subject: [PATCH] Get file absolute path from java.nio.Path. --- .../python/pydev/shared_core/io/FileUtils.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugins/org.python.pydev.shared_core/src/org/python/pydev/shared_core/io/FileUtils.java b/plugins/org.python.pydev.shared_core/src/org/python/pydev/shared_core/io/FileUtils.java index f97dbd190c..91e916b193 100644 --- a/plugins/org.python.pydev.shared_core/src/org/python/pydev/shared_core/io/FileUtils.java +++ b/plugins/org.python.pydev.shared_core/src/org/python/pydev/shared_core/io/FileUtils.java @@ -232,6 +232,23 @@ public static String getFileAbsolutePath(String f) { return getFileAbsolutePath(new File(f)); } + public static String getFileAbsolutePath(Path path) { + try { + if (!PlatformUtils.isWindowsPlatform()) { + // We don't want to follow links on Linux. + return path.toRealPath(LinkOption.NOFOLLOW_LINKS).toString(); + } else { + // On Windows, this is needed to get the proper case of files (because it's case-preserving + // and we have to get the proper case when resolving module names). + // Especially annoying if something starts with 'C:' and sometimes is entered with 'c:'. + // Note: this doesn't resolve `substs` on Windows (which is good). + return path.toFile().getCanonicalPath(); + } + } catch (IOException e) { + return path.toFile().getAbsolutePath(); + } + } + /** * This version does not resolve links on Linux. */ @@ -244,6 +261,7 @@ public static String getFileAbsolutePathNotFollowingLinks(File f) { // On Windows, this is needed to get the proper case of files (because it's case-preserving // and we have to get the proper case when resolving module names). // Especially annoying if something starts with 'C:' and sometimes is entered with 'c:'. + // Note: this doesn't resolve `substs` on Windows (which is good). return f.getCanonicalPath(); } } catch (IOException e) {