Skip to content

Commit 104c9e8

Browse files
committed
Fix: Error when using paths with non-ASCII characters for file watching on Unix/MacOS
1 parent 36f8935 commit 104c9e8

3 files changed

Lines changed: 22 additions & 18 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ public void testUNCwindowsURI() {
329329
public void testToUri_WindowsUNC() {
330330
File unc = new File("\\\\localhost\\c$\\Windows");
331331
URI uri = LSPEclipseUtils.toUri(unc);
332-
System.err.println(uri.toString());
333332
assertTrue(uri.toString().startsWith("file://localhost/c$/Windows"));
334333

335334
File uncWithSpaces = new File("\\\\server-name\\shared folder\\dir with space");
@@ -339,6 +338,14 @@ public void testToUri_WindowsUNC() {
339338
// Ensure there is an authority and no malformed quadruple slashes
340339
assertFalse(uriWithSpaces.toString().startsWith("file:////"));
341340
}
341+
342+
@Test
343+
void testFileUriWithNonAsciiPath() throws Exception {
344+
// File name contains a German Eszett and a Japanese Kana
345+
String fileName = "foo ßア";
346+
IFile targetFile = project.getFile(fileName);
347+
assertEquals(fileName, Paths.get(LSPEclipseUtils.toUri(targetFile)).getFileName().toString());
348+
}
342349

343350
@Test
344351
public void testToWorkspaceFolder() {

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/files/FileSystemWatcherManagerTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
*******************************************************************************/
1313
package org.eclipse.lsp4e.test.files;
1414

15-
import static org.junit.jupiter.api.Assertions.*;
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1617

1718
import java.net.URI;
1819
import java.nio.file.Path;
1920
import java.nio.file.Paths;
2021
import java.util.List;
2122

23+
import org.eclipse.lsp4e.LSPEclipseUtils;
2224
import org.eclipse.lsp4e.internal.files.FileSystemWatcherManager;
2325
import org.eclipse.lsp4j.FileSystemWatcher;
2426
import org.eclipse.lsp4j.WatchKind;
@@ -128,6 +130,15 @@ void watcherKindFiltering() {
128130
assertNoMatchFile(deleteUri, WatchKind.Change);
129131
assertMatchFile(deleteUri, WatchKind.Delete);
130132
}
133+
134+
@Test
135+
void nonAsciiFile() {
136+
registerWatchers("watcher-kind", List.of(
137+
new FileSystemWatcher(Either.forLeft("*.txt"), null)));
138+
// We use LSPEclipseUtils.toUri because this is also used to create the URI to check
139+
URI createUri = LSPEclipseUtils.toUri(projectDir.resolve("fooß.txt").toFile());
140+
assertMatchFile(createUri, WatchKind.Create);
141+
}
131142

132143
@Test
133144
void globMatchingSimple() {

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.io.OutputStream;
3232
import java.lang.reflect.Method;
3333
import java.net.URI;
34-
import java.net.URISyntaxException;
3534
import java.nio.file.Paths;
3635
import java.util.ArrayList;
3736
import java.util.Arrays;
@@ -1354,21 +1353,8 @@ public static URI toUri(IPath absolutePath) {
13541353
}
13551354

13561355
public static URI toUri(File file) {
1357-
// URI scheme specified by language server protocol and LSP
1358-
try {
1359-
final var path = file.getAbsoluteFile().toURI().getPath();
1360-
if (path.startsWith("//")) { // UNC path like //localhost/c$/Windows/ //$NON-NLS-1$
1361-
// split: authority = "localhost", absPath = "/c$/Windows/"
1362-
final int slash = path.indexOf('/', 2);
1363-
final String authority = slash > 2 ? path.substring(2, slash) : path.substring(2);
1364-
final String absPath = slash > 2 ? path.substring(slash) : "/"; //$NON-NLS-1$
1365-
return new URI(FILE_SCHEME, authority, absPath, null);
1366-
}
1367-
return new URI(FILE_SCHEME, "", path, null); //$NON-NLS-1$
1368-
} catch (URISyntaxException e) {
1369-
LanguageServerPlugin.logError(e);
1370-
return file.getAbsoluteFile().toURI();
1371-
}
1356+
// Perform one round-trip to make sure all non-ASCII characters are properly encoded.
1357+
return URI.create((file.toPath().toUri()).toASCIIString());
13721358
}
13731359

13741360
public static @Nullable IFile getFile(@Nullable IDocument document) {

0 commit comments

Comments
 (0)