Skip to content

Commit 096ada5

Browse files
FlorianKroissrubenporras
authored andcommitted
Fix: Error when using paths with non-ASCII characters for file watching on Unix/MacOS
1 parent 36f8935 commit 096ada5

7 files changed

Lines changed: 26 additions & 22 deletions

File tree

org.eclipse.lsp4e.test/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: Tests for language server bundle (Incubation)
44
Bundle-SymbolicName: org.eclipse.lsp4e.test;singleton:=true
5-
Bundle-Version: 0.16.7.qualifier
5+
Bundle-Version: 0.16.8.qualifier
66
Fragment-Host: org.eclipse.lsp4e
77
Bundle-Vendor: Eclipse LSP4E
88
Bundle-RequiredExecutionEnvironment: JavaSE-21

org.eclipse.lsp4e.test/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</parent>
99
<artifactId>org.eclipse.lsp4e.test</artifactId>
1010
<packaging>eclipse-test-plugin</packaging>
11-
<version>0.16.7-SNAPSHOT</version>
11+
<version>0.16.8-SNAPSHOT</version>
1212

1313
<properties>
1414
<os-jvm-flags /> <!-- for the default case -->

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/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: Language Server Protocol client for Eclipse IDE (Incubation)
44
Bundle-SymbolicName: org.eclipse.lsp4e;singleton:=true
5-
Bundle-Version: 0.19.7.qualifier
5+
Bundle-Version: 0.19.8.qualifier
66
Bundle-RequiredExecutionEnvironment: JavaSE-21
77
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.12.0",
88
org.eclipse.equinox.common;bundle-version="3.8.0",

org.eclipse.lsp4e/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<artifactId>org.eclipse.lsp4e</artifactId>
1212
<packaging>eclipse-plugin</packaging>
13-
<version>0.19.7-SNAPSHOT</version>
13+
<version>0.19.8-SNAPSHOT</version>
1414

1515
<build>
1616
<plugins>

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)