Skip to content

Commit 0bb6a3c

Browse files
authored
fix(amazonq): hardcode amazon-internal glibc patch (#5836)
Due to a quirk on how the internal glibc patch works, remote desktop sessions do not automatically get the patch therefore hardcode until this can be fixed internally
1 parent d51abfe commit 0bb6a3c

File tree

2 files changed

+54
-11
lines changed
  • plugins/amazonq/shared/jetbrains-community

2 files changed

+54
-11
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
package software.aws.toolkits.jetbrains.services.amazonq.lsp
55

66
import com.intellij.execution.configurations.GeneralCommandLine
7+
import com.intellij.util.system.CpuArch
8+
import com.intellij.util.text.nullize
9+
import software.aws.toolkits.core.utils.exists
710
import software.aws.toolkits.core.utils.getLogger
811
import software.aws.toolkits.core.utils.info
912
import java.nio.file.Path
13+
import java.nio.file.Paths
1014

1115
/**
1216
* Hacky nonsense to support old glibc platforms like AL2
@@ -16,13 +20,14 @@ import java.nio.file.Path
1620
object NodeExePatcher {
1721
const val GLIBC_LINKER_VAR = "VSCODE_SERVER_CUSTOM_GLIBC_LINKER"
1822
const val GLIBC_PATH_VAR = "VSCODE_SERVER_CUSTOM_GLIBC_PATH"
23+
const val INTERNAL_AARCH64_LINKER = "/opt/vsc-sysroot/lib/ld-linux-aarch64.so.1"
24+
const val INTERNAL_X86_64_LINKER = "/opt/vsc-sysroot/lib/ld-linux-x86-64.so.2"
25+
const val INTERNAL_GLIBC_PATH = "/opt/vsc-sysroot/lib/"
1926

2027
fun patch(node: Path): GeneralCommandLine {
21-
val linker = System.getenv(GLIBC_LINKER_VAR)
22-
val glibc = System.getenv(GLIBC_PATH_VAR)
2328
val nodePath = node.toAbsolutePath().toString()
2429

25-
return if (!linker.isNullOrEmpty() && !glibc.isNullOrEmpty()) {
30+
return if (!linker.isNullOrEmpty() && glibc.isNotEmpty() && Paths.get(linker).exists() && Paths.get(glibc).exists()) {
2631
GeneralCommandLine(linker)
2732
.withParameters("--library-path", glibc, nodePath)
2833
.also {
@@ -32,4 +37,18 @@ object NodeExePatcher {
3237
GeneralCommandLine(nodePath)
3338
}
3439
}
40+
41+
private val linker
42+
get() = System.getenv(GLIBC_LINKER_VAR).nullize(true) ?: let {
43+
if (CpuArch.isArm64()) {
44+
INTERNAL_AARCH64_LINKER
45+
} else if (CpuArch.isIntel64()) {
46+
INTERNAL_X86_64_LINKER
47+
} else {
48+
null
49+
}
50+
}
51+
52+
private val glibc
53+
get() = System.getenv(GLIBC_PATH_VAR).nullize(true) ?: INTERNAL_GLIBC_PATH
3554
}

plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/NodeExePatcherTest.kt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,55 @@
44
package software.aws.toolkits.jetbrains.services.amazonq.lsp
55

66
import com.intellij.execution.configurations.GeneralCommandLine
7+
import com.intellij.testFramework.rules.TempDirectory
8+
import com.intellij.testFramework.utils.io.createFile
9+
import com.intellij.util.system.CpuArch
710
import org.assertj.core.api.Assertions.assertThat
11+
import org.junit.Assume.assumeTrue
812
import org.junit.Rule
913
import org.junit.Test
1014
import software.aws.toolkits.core.rules.EnvironmentVariableHelper
11-
import kotlin.io.path.Path
15+
import software.aws.toolkits.core.utils.exists
16+
import java.nio.file.Paths
1217

1318
class NodeExePatcherTest {
1419
@get:Rule
1520
val envVarHelper = EnvironmentVariableHelper()
1621

17-
private val pathToNode = Path("/path/to/node").toAbsolutePath().toString()
22+
@get:Rule
23+
val tempDir = TempDirectory()
24+
25+
private val pathToNode = Paths.get("/path/to/node").toAbsolutePath().toString()
1826

1927
@Test
20-
fun `patches if path available`() {
21-
envVarHelper[NodeExePatcher.GLIBC_LINKER_VAR] = "/opt/vsc-sysroot/lib/ld-linux-x86-64.so.2"
22-
envVarHelper[NodeExePatcher.GLIBC_PATH_VAR] = "/opt/vsc-sysroot/lib/"
28+
fun `patches if environment variables are available`() {
29+
val path = tempDir.newDirectory("vsc-sysroot").toPath().toAbsolutePath()
30+
val linker = Paths.get(path.toString(), "someSharedLibrary").createFile()
31+
32+
envVarHelper[NodeExePatcher.GLIBC_LINKER_VAR] = linker.toString()
33+
envVarHelper[NodeExePatcher.GLIBC_PATH_VAR] = path.toString()
34+
35+
assertThat(NodeExePatcher.patch(Paths.get("/path/to/node")))
36+
.usingComparator(Comparator.comparing { it.commandLineString })
37+
.isEqualTo(GeneralCommandLine(linker.toString(), "--library-path", path.toString(), pathToNode))
38+
}
39+
40+
@Test
41+
fun `patches if hardcoded paths exists`() {
42+
val path = Paths.get(NodeExePatcher.INTERNAL_GLIBC_PATH)
43+
// too many permission issues otherwise
44+
assumeTrue(path.exists())
45+
46+
val linker = Paths.get(if (CpuArch.isArm64()) NodeExePatcher.INTERNAL_AARCH64_LINKER else NodeExePatcher.INTERNAL_X86_64_LINKER)
2347

24-
assertThat(NodeExePatcher.patch(Path("/path/to/node")))
48+
assertThat(NodeExePatcher.patch(Paths.get("/path/to/node")))
2549
.usingComparator(Comparator.comparing { it.commandLineString })
26-
.isEqualTo(GeneralCommandLine("/opt/vsc-sysroot/lib/ld-linux-x86-64.so.2", "--library-path", "/opt/vsc-sysroot/lib/", pathToNode))
50+
.isEqualTo(GeneralCommandLine(linker.toString(), "--library-path", path.toString(), pathToNode))
2751
}
2852

2953
@Test
3054
fun `noop if no patch available`() {
31-
assertThat(NodeExePatcher.patch(Path("/path/to/node")))
55+
assertThat(NodeExePatcher.patch(Paths.get("/path/to/node")))
3256
.usingComparator(Comparator.comparing { it.commandLineString })
3357
.isEqualTo(GeneralCommandLine(pathToNode))
3458
}

0 commit comments

Comments
 (0)