From 76cbaff581f0c1b0a86806028730f82049af7ec0 Mon Sep 17 00:00:00 2001 From: factyy Date: Tue, 7 Dec 2021 01:08:55 +0300 Subject: [PATCH 1/5] [wip] Match config mounts against the container's --- lsp-docker.el | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lsp-docker.el b/lsp-docker.el index 7ac7709..a1a1aac 100644 --- a/lsp-docker.el +++ b/lsp-docker.el @@ -324,6 +324,22 @@ the docker container to run the language server." (f-same? (f-canonical (car it)) (f-canonical (lsp-workspace-root)))) path-mappings)) +(defun lsp-docker-get-path-mappings-from-container (container-name) + "Get path mappings from a container" + (-let (( + (inspection-command-program . inspection-command-arguments) + (--map-when + (equal it "'{{.Mounts}}'") + "'{{json .Mounts}}'" + (s-split " " (format "%s container inspect -f '{{.Mounts}}' %s" lsp-docker-command container-name))))) + (-let (((exit-code . output) (with-temp-buffer + (cons + (apply #'call-process inspection-command-program nil "lsp-docker-testing" nil inspection-command-arguments) + (buffer-string))))) + (if (equal exit-code 0) + (message "Nice!") + (user-error "Cannot analyze the following container: %s" container-name))))) + (defun lsp-docker-launch-existing-container (docker-container-name &rest _unused) "Return the docker command to be executed on host. Argument DOCKER-CONTAINER-NAME name to use for container." From 52ceeb8639b74d27efd23170db71e444753ca1dc Mon Sep 17 00:00:00 2001 From: factyy Date: Wed, 8 Dec 2021 00:32:08 +0300 Subject: [PATCH 2/5] Match config mounts against the container's --- lsp-docker.el | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lsp-docker.el b/lsp-docker.el index a1a1aac..49b5cf1 100644 --- a/lsp-docker.el +++ b/lsp-docker.el @@ -324,6 +324,16 @@ the docker container to run the language server." (f-same? (f-canonical (car it)) (f-canonical (lsp-workspace-root)))) path-mappings)) +(defun lsp-docker-verify-path-mappings-against-container (path-mappings container-name) + "Verify that specified path mappings are all included in container's path mappings" + (--all? (let ((source (car it)) + (destination (cdr it))) + (-any? (lambda (mapping) + (and (f-same? source (car mapping)) + (equal destination (cdr mapping)))) + (lsp-docker-get-path-mappings-from-container container-name))) + path-mappings)) + (defun lsp-docker-get-path-mappings-from-container (container-name) "Get path mappings from a container" (-let (( @@ -332,13 +342,16 @@ the docker container to run the language server." (equal it "'{{.Mounts}}'") "'{{json .Mounts}}'" (s-split " " (format "%s container inspect -f '{{.Mounts}}' %s" lsp-docker-command container-name))))) - (-let (((exit-code . output) (with-temp-buffer + (-let (((exit-code . raw-output) (with-temp-buffer (cons - (apply #'call-process inspection-command-program nil "lsp-docker-testing" nil inspection-command-arguments) + (apply #'call-process inspection-command-program nil (current-buffer) nil inspection-command-arguments) (buffer-string))))) (if (equal exit-code 0) - (message "Nice!") - (user-error "Cannot analyze the following container: %s" container-name))))) + (let* ((output (s-chop-prefix "'" (s-chop-suffix "'" (s-chomp raw-output)))) + (raw-mappings (append (json-parse-string output) nil)) ; using append to convert a vector to a list + (bind-mappings (--filter (and (equal (gethash "Type" it) "bind") (equal (gethash "RW" it) t)) raw-mappings))) + (--map (cons (f-canonical (gethash "Source" it)) (f-canonical (gethash "Destination" it))) bind-mappings)) + (user-error "Cannot analyze the following container: %s, exit code: %d" container-name exit-code))))) (defun lsp-docker-launch-existing-container (docker-container-name &rest _unused) "Return the docker command to be executed on host. From 0a369e2bf202ee3a6b9307750edde604adfeeb5d Mon Sep 17 00:00:00 2001 From: factyy Date: Wed, 8 Dec 2021 00:42:00 +0300 Subject: [PATCH 3/5] Integrate mappings checks into the server registering code --- lsp-docker.el | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lsp-docker.el b/lsp-docker.el index 49b5cf1..e772cd4 100644 --- a/lsp-docker.el +++ b/lsp-docker.el @@ -445,18 +445,20 @@ Argument DOCKER-CONTAINER-NAME name to use for container." :priority lsp-docker-default-priority :server-command server-launch-command :launch-server-cmd-fn #'lsp-docker-launch-new-container)) - ('container (lsp-docker-register-client-with-activation-fn - :server-id regular-server-id - :docker-server-id server-id - :path-mappings path-mappings - :docker-image-id nil - :docker-container-name server-container-name - :docker-container-name-suffix nil - :activation-fn (lsp-docker-create-activation-function-by-project-dir (lsp-workspace-root)) - :priority lsp-docker-default-priority - :server-command server-launch-command - :launch-server-cmd-fn #'lsp-docker-launch-existing-container)))))) - (user-error "Invalid LSP docker config: unsupported server type and/or subtype"))) + ('container (if (lsp-docker-verify-path-mappings-against-container path-mappings server-container-name) + (lsp-docker-register-client-with-activation-fn + :server-id regular-server-id + :docker-server-id server-id + :path-mappings path-mappings + :docker-image-id nil + :docker-container-name server-container-name + :docker-container-name-suffix nil + :activation-fn (lsp-docker-create-activation-function-by-project-dir (lsp-workspace-root)) + :priority lsp-docker-default-priority + :server-command server-launch-command + :launch-server-cmd-fn #'lsp-docker-launch-existing-container) + (user-error "Container path mappings don't match the config ones!"))))))) + (user-error "Invalid LSP docker config: unsupported server type and/or subtype or malformed mappings!"))) (user-error (format "Current file: %s is not in a registered project!" (buffer-file-name))))) (defun lsp-docker-start () From fc470bed18bb01997e76448f1d370032203be141 Mon Sep 17 00:00:00 2001 From: Andrei Mochalov Date: Wed, 8 Dec 2021 00:48:13 +0300 Subject: [PATCH 4/5] Add path mappings checks to the readme --- README.org | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.org b/README.org index 6a939bc..5f11931 100644 --- a/README.org +++ b/README.org @@ -140,6 +140,8 @@ lsp: # (selected by a server id specified above) in stdio mode # Note: launch_command is not used with container subtype servers # as a command is embedded in a container itself and serves as an entrypoint + # Note: config path mappings are compared against an actual container (with container subtype servers): + # container mappings must contain all the config mappings (destination paths inside containers have to be absolute) mappings: - source: "/your/host/source/path" destination: "/your/local/path/inside/a/container" From 5f736af5abdecbd49c55608ce27ceaab451a8aa5 Mon Sep 17 00:00:00 2001 From: Andrei Mochalov Date: Wed, 8 Dec 2021 00:49:42 +0300 Subject: [PATCH 5/5] Reword an incorrect sentence in the readme --- README.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.org b/README.org index 5f11931..e6478ca 100644 --- a/README.org +++ b/README.org @@ -140,7 +140,7 @@ lsp: # (selected by a server id specified above) in stdio mode # Note: launch_command is not used with container subtype servers # as a command is embedded in a container itself and serves as an entrypoint - # Note: config path mappings are compared against an actual container (with container subtype servers): + # Note: config path mappings are compared with an actual container (with container subtype servers): # container mappings must contain all the config mappings (destination paths inside containers have to be absolute) mappings: - source: "/your/host/source/path"