Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ dependencies {

testImplementation(libs.junit)
testImplementation(libs.junit.jupiter.api)
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

// Set the JVM language level used to build the project.
Expand Down Expand Up @@ -206,6 +207,7 @@ changelog {
}

tasks.withType<Test> {
useJUnitPlatform()
environment("GRADLE_RELEASE_REPOSITORY","https://services.gradle.org/distributions")
systemProperty("idea.log.leaked.projects.in.tests", "false")
systemProperty( "idea.maven.test.mirror", "https://repo1.maven.org/maven2")
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,7 @@ private static Language doGetFileLanguage(@NotNull VirtualFile file, @NotNull Pr
}

public static @NotNull URI toUri(@NotNull File file) {
// URI scheme specified by language server protocol and LSP
try {
return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null); //$NON-NLS-1$ //$NON-NLS-2$
} catch (URISyntaxException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
return file.getAbsoluteFile().toURI();
}
return file.toPath().toUri();
}

public static @Nullable URI toUri(@NotNull PsiFile psiFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected FormattingTask createFormattingTask(@NotNull AsyncFormattingRequest fo
if (ioFile == null) {
return null;
}
VirtualFile file = LSPIJUtils.findResourceFor(ioFile.toURI());
VirtualFile file = LSPIJUtils.findResourceFor(LSPIJUtils.toUri(ioFile).toString());
if (file == null) {
return null;
}
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/com/redhat/devtools/lsp4ij/LSPIJUtils_toUriTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.redhat.devtools.lsp4ij;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;

import java.io.File;
import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

public class LSPIJUtils_toUriTest {

@Test
@EnabledOnOs(OS.WINDOWS)
void testToUriWithWslPath() {
// Given: Create a File object from a UNC path string
// Note: File object may normalize the path, so we test with the actual behavior
String wslPath = "\\\\wsl$\\Ubuntu\\home\\user\\project\\file.txt";
File wslFile = new File(wslPath);

// When
URI resultUri = LSPIJUtils.toUri(wslFile);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());

// The URI should either:
// 1. Have authority="wsl$" and path="/Ubuntu/home/user/project/file.txt" (correct UNC handling)
// 2. Or at minimum, not produce the old broken format with 4 slashes
String uriString = resultUri.toString();
assertNotNull(uriString);

// Verify it doesn't have the broken 4-slash format
if (uriString.contains("file:////")) {
fail("URI should not contain file://// (broken UNC format): " + uriString);
}
}

@Test
void testToUriWithLocalPath() {
// Given
File localFile = new File("/home/user/project/file.txt");
if (System.getProperty("os.name").toLowerCase().contains("win")) {
localFile = new File("C:\\Users\\user\\project\\file.txt");
}


// When
URI resultUri = LSPIJUtils.toUri(localFile);

// Then
assertNotNull(resultUri);
assertEquals("file", resultUri.getScheme());
if (System.getProperty("os.name").toLowerCase().contains("win")) {
assertEquals("/C:/Users/user/project/file.txt", resultUri.getPath());
} else {
assertEquals("/home/user/project/file.txt", resultUri.getPath());
}
}
}