Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "Fix UriError when project is on WSL or a UNC path"
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,25 @@ object LspEditorUtil {
private fun toUri(file: File): URI {
try {
// URI scheme specified by language server protocol
return URI("file", "", file.absoluteFile.toURI().path, null)
val uri = URI("file", "", file.absoluteFile.toURI().path, null)
val fallback = file.toPath().toAbsolutePath().normalize().toUri()
return if (uri.isCompliant()) uri else fallback
} catch (e: URISyntaxException) {
LOG.warn { "${e.localizedMessage}: $e" }
return file.absoluteFile.toURI()
}
}

private fun URI.isCompliant(): Boolean {
if (!"file".equals(this.scheme, ignoreCase = true)) return true

val path = this.rawPath ?: this.path.orEmpty()
val noAuthority = this.authority.isNullOrEmpty()

// If the authority component is empty, the path cannot begin with two slash characters ("//")
return !(noAuthority && path.startsWith("//"))
}

/**
* Works but is divergent from [FocusAreaContextExtrator]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import io.mockk.every
import io.mockk.mockk
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledOnOs
import org.junit.jupiter.api.condition.OS
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(ApplicationExtension::class)
Expand Down Expand Up @@ -96,6 +98,24 @@ class FileUriUtilTest {
}
}

@Test
@EnabledOnOs(OS.WINDOWS)
fun `test wsl-like path`() {
val virtualFile = createMockVirtualFile("//wsl.localhost/Ubuntu/home/user/file.sh")
val result = LspEditorUtil.toUriString(virtualFile)
val expected = normalizeFileUri("file://wsl.localhost/Ubuntu/home/user/file.sh")
assertThat(result).isEqualTo(expected)
}

@Test
@EnabledOnOs(OS.WINDOWS)
fun `test UNC path`() {
val virtualFile = createMockVirtualFile("//server/share/path/to/file.txt")
val result = LspEditorUtil.toUriString(virtualFile)
val expected = normalizeFileUri("file://server/share/path/to/file.txt")
assertThat(result).isEqualTo(expected)
}

@Test
fun `test jar protocol conversion`() {
val virtualFile = createMockVirtualFile(
Expand Down