Skip to content

Commit bdf2be2

Browse files
committed
fix(amazonq): hardcode amazon-internal glibc patch
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 b56813a commit bdf2be2

File tree

2 files changed

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

2 files changed

+64
-11
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
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 software.aws.toolkits.core.utils.exists
79
import software.aws.toolkits.core.utils.getLogger
810
import software.aws.toolkits.core.utils.info
911
import java.nio.file.Path
12+
import java.nio.file.Paths
1013

1114
/**
1215
* Hacky nonsense to support old glibc platforms like AL2
@@ -16,13 +19,14 @@ import java.nio.file.Path
1619
object NodeExePatcher {
1720
const val GLIBC_LINKER_VAR = "VSCODE_SERVER_CUSTOM_GLIBC_LINKER"
1821
const val GLIBC_PATH_VAR = "VSCODE_SERVER_CUSTOM_GLIBC_PATH"
22+
const val INTERNAL_AARCH64_LINKER = "/opt/vsc-sysroot/lib/ld-linux-aarch64.so.1"
23+
const val INTERNAL_X86_64_LINKER = "/opt/vsc-sysroot/lib/ld-linux-x86-64.so.2"
24+
const val INTERNAL_GLIBC_PATH = "/opt/vsc-sysroot/lib/"
1925

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

25-
return if (!linker.isNullOrEmpty() && !glibc.isNullOrEmpty()) {
29+
return if (Paths.get(linker).exists() && Paths.get(glibc).exists()) {
2630
GeneralCommandLine(linker)
2731
.withParameters("--library-path", glibc, nodePath)
2832
.also {
@@ -32,4 +36,16 @@ object NodeExePatcher {
3236
GeneralCommandLine(nodePath)
3337
}
3438
}
39+
40+
private val linker
41+
get() = System.getenv(GLIBC_LINKER_VAR) ?: let {
42+
if (CpuArch.isArm64()) {
43+
INTERNAL_AARCH64_LINKER
44+
} else {
45+
INTERNAL_X86_64_LINKER
46+
}
47+
}
48+
49+
private val glibc
50+
get() = System.getenv(GLIBC_PATH_VAR) ?: INTERNAL_GLIBC_PATH
3551
}

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

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

66
import com.intellij.execution.configurations.GeneralCommandLine
7+
import com.intellij.openapi.util.SystemInfo
8+
import com.intellij.testFramework.rules.TempDirectory
9+
import com.intellij.testFramework.utils.io.createDirectory
10+
import com.intellij.testFramework.utils.io.createFile
711
import org.assertj.core.api.Assertions.assertThat
12+
import org.junit.Assume.assumeTrue
813
import org.junit.Rule
914
import org.junit.Test
1015
import software.aws.toolkits.core.rules.EnvironmentVariableHelper
11-
import kotlin.io.path.Path
16+
import software.aws.toolkits.core.utils.exists
17+
import java.nio.file.Paths
1218

1319
class NodeExePatcherTest {
1420
@get:Rule
1521
val envVarHelper = EnvironmentVariableHelper()
1622

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

1928
@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/"
29+
fun `patches if environment variables are available`() {
30+
val path = tempDir.newDirectory("vsc-sysroot").toPath().toAbsolutePath()
31+
val linker = Paths.get(path.toString(), "someSharedLibrary").createFile()
2332

24-
assertThat(NodeExePatcher.patch(Path("/path/to/node")))
33+
envVarHelper[NodeExePatcher.GLIBC_LINKER_VAR] = linker.toString()
34+
envVarHelper[NodeExePatcher.GLIBC_PATH_VAR] = path.toString()
35+
36+
assertThat(NodeExePatcher.patch(Paths.get("/path/to/node")))
2537
.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))
38+
.isEqualTo(GeneralCommandLine(linker.toString(), "--library-path", path.toString(), pathToNode))
39+
}
40+
41+
@Test
42+
fun `patches if hardcoded paths exists`() {
43+
// explicitly linux because can't run on mac
44+
assumeTrue(SystemInfo.isLinux)
45+
46+
val path = Paths.get(NodeExePatcher.INTERNAL_GLIBC_PATH)
47+
val linker = Paths.get(NodeExePatcher.INTERNAL_X86_64_LINKER)
48+
val needsCreate = !path.exists() && !linker.exists()
49+
if (needsCreate) {
50+
path.createDirectory()
51+
linker.createFile()
52+
}
53+
54+
try {
55+
assertThat(NodeExePatcher.patch(Paths.get("/path/to/node")))
56+
.usingComparator(Comparator.comparing { it.commandLineString })
57+
.isEqualTo(GeneralCommandLine(linker.toString(), "--library-path", path.toString(), pathToNode))
58+
} finally {
59+
if (needsCreate) {
60+
linker.toFile().delete()
61+
path.toFile().deleteRecursively()
62+
}
63+
}
2764
}
2865

2966
@Test
3067
fun `noop if no patch available`() {
31-
assertThat(NodeExePatcher.patch(Path("/path/to/node")))
68+
assertThat(NodeExePatcher.patch(Paths.get("/path/to/node")))
3269
.usingComparator(Comparator.comparing { it.commandLineString })
3370
.isEqualTo(GeneralCommandLine(pathToNode))
3471
}

0 commit comments

Comments
 (0)