Skip to content

Commit 095e661

Browse files
committed
Fix: Error when using paths with non-ASCII characters for file watching on Unix/MacOS
1 parent 4072712 commit 095e661

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
@@ -332,7 +332,6 @@ public void testUNCwindowsURI() {
332332
public void testToUri_WindowsUNC() {
333333
File unc = new File("\\\\localhost\\c$\\Windows");
334334
URI uri = LSPEclipseUtils.toUri(unc);
335-
System.err.println(uri.toString());
336335
assertTrue(uri.toString().startsWith("file://localhost/c$/Windows"));
337336

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

346353
@Test
347354
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;
@@ -1345,21 +1344,8 @@ public static URI toUri(IPath absolutePath) {
13451344
}
13461345

13471346
public static URI toUri(File file) {
1348-
// URI scheme specified by language server protocol and LSP
1349-
try {
1350-
final var path = file.getAbsoluteFile().toURI().getPath();
1351-
if (path.startsWith("//")) { // UNC path like //localhost/c$/Windows/ //$NON-NLS-1$
1352-
// split: authority = "localhost", absPath = "/c$/Windows/"
1353-
final int slash = path.indexOf('/', 2);
1354-
final String authority = slash > 2 ? path.substring(2, slash) : path.substring(2);
1355-
final String absPath = slash > 2 ? path.substring(slash) : "/"; //$NON-NLS-1$
1356-
return new URI(FILE_SCHEME, authority, absPath, null);
1357-
}
1358-
return new URI(FILE_SCHEME, "", path, null); //$NON-NLS-1$
1359-
} catch (URISyntaxException e) {
1360-
LanguageServerPlugin.logError(e);
1361-
return file.getAbsoluteFile().toURI();
1362-
}
1347+
// Perform one round-trip to make sure all non-ASCII characters are properly encoded.
1348+
return URI.create((file.toPath().toUri()).toASCIIString());
13631349
}
13641350

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

0 commit comments

Comments
 (0)