How to Configure sourcekit-lsp with swift slim docker image?

Hey there, I am working on lsp support for an online code editor.
I found the swift:5.2.1-slim docker image which is small in size(210MB) and swift:5.2.1 image size is 1.64GB.
Also, I got to know swift latest versions provide the executable file of sourcekit-lsp in it so we don't need to build. I copied that in the /usr/bin folder of the docker image.
Is there any other dependency I need to add in the swift slim docker image?
The server is running and but the response return is null. And auto-complete, etc IntelliSense is not working.

FROM swift:5.2.1-slim

RUN apt-get -y update \
    && apt-get install -y gnupg2 \
    && apt-get -y install curl \
    && apt-get -y install apt-transport-https apt-utils \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
    && curl -sL https://deb.nodesource.com/setup_10.x | bash - \
    && apt-get -y update \
    && apt-get -y install nodejs \
    && apt-get -y install yarn \
    && apt-get purge --auto-remove -y curl gnupg2 apt-transport-https apt-utils \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /tmp/* \
    && rm -rf /var/tmp/*


ARG LANGUAGE

ENV LSP_SERVER_DIR="/server" \
    LSP_FILES_DIR="/source_files" \
    AWS_CONFIG_DIR="/root/.aws" \
    LANGUAGE=$LANGUAGE

RUN mkdir -p $LSP_SERVER_DIR/build && mkdir $LSP_FILES_DIR && mkdir -p $AWS_CONFIG_DIR

WORKDIR $LSP_SERVER_DIR
#Copying sourcekit-lsp to /usr/bin
COPY servers/$LANGUAGE/build/ /usr/bin/

COPY connector/package.json connector/yarn.lock $LSP_SERVER_DIR/

RUN yarn install

COPY connector/build/ $LSP_SERVER_DIR/build/

COPY servers/$LANGUAGE/serverConfig.json $LSP_SERVER_DIR/build/

COPY config/aws/ $AWS_CONFIG_DIR/

EXPOSE 80/tcp

CMD [ "yarn", "run", "start" ]

And serverConfig.json

{
  "name": "Swift5 LSP Server",
  "language": "SWIFT5",
  "baseLanguage": "swift",
  "metricsCluster": "SWIFT5",
  "host": "0.0.0.0",
  "port": 80,
  "baseUrl": "/",
  "pingUrl": "/ping/",
  "maxConnections": 15,
  "serverCommand": "sourcekit-lsp",
  "serverCommandOptions": []
}

Attaching the screenshot of the response given by the server :

Maybe I need to give the path of the source folder to the sourcekit-lsp server. Is there any way to give that?
Do I need to pass any options while running the server (sourcekit-lsp [OPTIONS])?
Or is there any other way we can do this?

The error shown in the screenshot about the compilation database is only a problem if you were expecting to use compile_commands.json. Is the code you're trying to browse a swiftpm package, a standalone file, or is it really trying to use compile_commands.json?

I suggest running the server with env SOURCEKIT_LOGGING=2 and seeing what other messages you're seeing. If you can include a full log, I may be able to help narrow down what's going wrong.

Thanks for replying.
The error showing is because I am saving the source file of the editor in the source_files
folder and file_<random_string>.swift is the actual file that stores the source code from the editor.
Now that path I need to give in sourckit-lsp server for finding out the code.
I am not able to figure out how to pass that path in the sourcekit-lsp command
or I need to set env variable for it.

Is swift:5.2.1-slim image can be used to run sourcekit-lsp successfully or not? If yes is there any changes that I need to do in my docker file?

Sorry, I don't understand what the issue here is. It looks like we received a textDocument/didOpen for file_<random_string>.swift, since otherwise we would not print that log message.

Is swift:5.2.1-slim image can be used to run sourcekit-lsp successfully or not?

I'm not 100% sure. Since you have the server running, I would have expected it would work normally unless the code you are trying to look at depends on a -dev package to compile or something. You could try the full image and see if it makes a difference.

1 Like

Sorry, I don't understand what the issue here is. It looks like we received a textDocument/didOpen for file_<random_string>.swift , since otherwise we would not print that log message.

Yes, you got it right. It is doing same thing.
But the server is returning null as it is not getting the file path. This is the problem.

I'm not 100% sure. Since you have the server running, I would have expected it would work normally unless the code you are trying to look at depends on a -dev package to compile or something. You could try the full image and see if it makes a difference.

I can use the swift full image and try (but can not use that in production because of its large size).

I also wanted to know do I need to give the command options too with the sourcekit-lsp command.

USAGE: sourcekit-lsp [options]

OPTIONS:
--build-path Specify build/cache directory
--configuration, -c Build with configuration (debug|release) [default: debug]
--log-level Set the logging level (debug|info|warning|error) [default: info]
-Xcc Pass flag through to all C compiler invocations
-Xclangd Pass options to clangd command-line
-Xcxx Pass flag through to all C++ compiler invocations
-Xlinker Pass flag through to all linker invocations
-Xswiftc Pass flag through to all Swift compiler invocations
-index-db-path Override index-database-path from the build system
-index-store-path Override index-store-path from the build system
--help Display available options

Here is there anything so that I can pass the source folder path with the command?

It is also giving could not find manifest, or not a SwiftPM package this error in logs.Why is it so? How to resolve this too?

So the issue is that the file cannot be found on the filesystem that sourcekit-lsp is running on? If that's the case, I would expect that purely syntactic functions would work (e.g. unbalanced parens should give an error), but that semantic functionality like code-completion would fail. It would also be unrelated to being the "slim" image. If you want this to work, particularly if you have a whole swiftpm package you're tyring to look at, you will need to find a way to make it available on the file system that sourcekit-lsp uses.

I also wanted to know do I need to give the command options too with the sourcekit-lsp command.

Usually you don't need to pass any options unless you are overriding what swiftpm would do by default. It sounds like in this case it's not finding a package at all though.

It is also giving could not find manifest, or not a SwiftPM package this error in logs.Why is it so? How to resolve this too?

This means it looked for a Package.swift file in the parent directories of the file you passed and didn't find one. Is the code you're looking at a swiftpm package? Again, this is using the filesystem, so if it cannot find your source file it could have the same issue here.

Hi, I tried by using the normal docker image of swift:5.2.1 and it is working fine.
But it's not working with the slim image...

So the issue is that the file cannot be found on the filesystem that sourcekit-lsp is running on? If that's the case, I would expect that purely syntactic functions would work (e.g. unbalanced parens should give an error), but that semantic functionality like code-completion would fail. It would also be unrelated to being the "slim" image. If you want this to work, particularly if you have a whole swiftpm package you're tyring to look at, you will need to find a way to make it available on the file system that sourcekit-lsp uses.

Do I need to have swift package manager in my /usr/lib directory?
Will it work if I keep swift-package binary that is present in the swift:5.2.1 in it instead?

Usually you don't need to pass any options unless you are overriding what swiftpm would do by default. It sounds like in this case it's not finding a package at all though.

Ok In my case I am not doing any other thing than this which I have mentioned. So what is the command options usage?

This means it looked for a Package.swift file in the parent directories of the file you passed and didn't find one. Is the code you're looking at a swiftpm package? Again, this is using the filesystem, so if it cannot find your source file it could have the same issue here.

So I need to create Package.json file in /usr/bin directory? (Package.json is in sourcekit-lsp repo, that file you are talking about?)

Do I need to build (swift build ) for sourcekit-lsp again by passing custom options for giving the path of a client source file

Hi, I tried by using the normal docker image of swift:5.2.1 and it is working fine.
But it's not working with the slim image...

I took a look at what exactly is in the slim image, and I think it's missing too many things to use sourcekit-lsp.

Here are some key things that are missing (there may be more):

  • /usr/lib/libsourcekitdInProc.so
  • /usr/lib/swift/{dispatch,CoreFoundation,Block,os,shims} (core libraries)
  • /usr/lib/swift/pm (the PackageDescription modules required to load swiftpm packages)

Based on this, I would suggest using the full image.

1 Like

Thank you @blangmuir for helping out and clarify my doubts.
I will go with swift full images only. :slight_smile:

1 Like
Terms of Service

Privacy Policy

Cookie Policy