Skip to content

Commit 113e749

Browse files
authored
fix: handle Windows UNC paths correctly (#1360)
This PR fixes the underlying issue of eclipse-wildwebdeveloper/wildwebdeveloper#162 When one tries to open a language server supported file on an Windows UNC Path. Currently the moment one tries to open the file the UI pops-up error dialogs in loops making it even hard to close the file again.
1 parent 47718a7 commit 113e749

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,20 @@ public void testURICreationUnix() {
297297
Assert.assertEquals("file:///test%20with%20space", LSPEclipseUtils.toUri(new File("/test with space")).toString());
298298
}
299299

300+
@Test
301+
public void testToUri_WindowsDriveLetter() {
302+
Assume.assumeTrue(Platform.OS_WIN32.equals(Platform.getOS()));
303+
// Use a synthetic drive path (doesn't need to exist)
304+
File drivePath = new File("C:\\Temp\\with space");
305+
URI uri = LSPEclipseUtils.toUri(drivePath);
306+
// Should be file:///C:/... and percent-encode spaces
307+
Assert.assertTrue(uri.toString().startsWith("file:///C:/Temp/"));
308+
Assert.assertFalse(uri.toString().contains(" "));
309+
Assert.assertTrue(uri.toString().contains("with%20space"));
310+
// Should not contain quadruple slashes
311+
Assert.assertFalse(uri.toString().startsWith("file:////"));
312+
}
313+
300314
@Test
301315
public void testUNCwindowsURI() {
302316
Assume.assumeTrue(Platform.OS_WIN32.equals(Platform.getOS()));
@@ -308,6 +322,22 @@ public void testUNCwindowsURI() {
308322
Assert.assertEquals(file1, file2);
309323
}
310324

325+
@Test
326+
public void testToUri_WindowsUNC() {
327+
Assume.assumeTrue(Platform.OS_WIN32.equals(Platform.getOS()));
328+
File unc = new File("\\\\localhost\\c$\\Windows");
329+
URI uri = LSPEclipseUtils.toUri(unc);
330+
System.err.println(uri.toString());
331+
Assert.assertTrue(uri.toString().startsWith("file://localhost/c$/Windows"));
332+
333+
File uncWithSpaces = new File("\\\\server-name\\shared folder\\dir with space");
334+
URI uriWithSpaces = LSPEclipseUtils.toUri(uncWithSpaces);
335+
Assert.assertTrue(uriWithSpaces.toString().startsWith("file://server-name/shared%20folder/dir%20with%20space"));
336+
337+
// Ensure there is an authority and no malformed quadruple slashes
338+
Assert.assertFalse(uriWithSpaces.toString().startsWith("file:////"));
339+
}
340+
311341
@Test
312342
public void testToWorkspaceFolder() {
313343
WorkspaceFolder folder = LSPEclipseUtils.toWorkspaceFolder(project);

org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,15 @@ public static URI toUri(IPath absolutePath) {
13321332
public static URI toUri(File file) {
13331333
// URI scheme specified by language server protocol and LSP
13341334
try {
1335-
return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null); //$NON-NLS-1$ //$NON-NLS-2$
1335+
final var path = file.getAbsoluteFile().toURI().getPath();
1336+
if (path.startsWith("//")) { // UNC path like //localhost/c$/Windows/ //$NON-NLS-1$
1337+
// split: authority = "localhost", absPath = "/c$/Windows/"
1338+
final int slash = path.indexOf('/', 2);
1339+
final String authority = slash > 2 ? path.substring(2, slash) : path.substring(2);
1340+
final String absPath = slash > 2 ? path.substring(slash) : "/"; //$NON-NLS-1$
1341+
return new URI(FILE_SCHEME, authority, absPath, null);
1342+
}
1343+
return new URI(FILE_SCHEME, "", path, null); //$NON-NLS-1$
13361344
} catch (URISyntaxException e) {
13371345
LanguageServerPlugin.logError(e);
13381346
return file.getAbsoluteFile().toURI();

0 commit comments

Comments
 (0)