Skip to content

Prevents Q UriError : Validate file URI and fallback to Path.toUri() for WSL #5960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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 ?: ""
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 @@ -96,6 +96,23 @@ class FileUriUtilTest {
}
}

@Test
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
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