Skip to content

Commit f1d9fd1

Browse files
committed
fix(uri): validate file: URI and fallback to Path.toUri() when path starts with // and no authority
1 parent 07c5741 commit f1d9fd1

File tree

1 file changed

+13
-1
lines changed
  • plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/util

1 file changed

+13
-1
lines changed

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/util/LspEditorUtil.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,25 @@ object LspEditorUtil {
4747
private fun toUri(file: File): URI {
4848
try {
4949
// URI scheme specified by language server protocol
50-
return URI("file", "", file.absoluteFile.toURI().path, null)
50+
val uri = URI("file", "", file.absoluteFile.toURI().path, null)
51+
val fallback = file.toPath().toAbsolutePath().normalize().toUri()
52+
return if (uri.isCompliant()) uri else fallback
5153
} catch (e: URISyntaxException) {
5254
LOG.warn { "${e.localizedMessage}: $e" }
5355
return file.absoluteFile.toURI()
5456
}
5557
}
5658

59+
private fun URI.isCompliant(): Boolean {
60+
if (!"file".equals(this.scheme, ignoreCase = true)) return true
61+
62+
val path = this.rawPath ?: this.path ?: ""
63+
val noAuthority = this.authority.isNullOrEmpty()
64+
65+
// If the authority component is empty, the path cannot begin with two slash characters ("//")
66+
return !(noAuthority && path.startsWith("//"))
67+
}
68+
5769
/**
5870
* Works but is divergent from [FocusAreaContextExtrator]
5971
*/

0 commit comments

Comments
 (0)