Skip to content

Commit acdd5ad

Browse files
authored
Enable development in Codespaces (#84)
* Enable Codespaces - add default Go files Codespaces[1] allows us to use a remote vscode environment embedded in the browser in GitHub[1] This commit adds the pre-built container configuration[2] for Go[3]. We can customise this configuration as we wish in future commits. [1] https://github.com/features/codespaces/ [2] https://docs.github.com/en/github/developing-online-with-codespaces/configuring-codespaces-for-your-project#using-a-pre-built-container-configuration [3] https://github.com/microsoft/vscode-dev-containers/tree/master/containers/go * Suppress/fix devcontainer Dockerfile warnings These were hadolint warnings, and there were three of them, all on .devcontainer/Dockerfile#L17: DL3003 Use WORKDIR to switch to a directory DL3008 Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>` DL3015 Avoid additional packages by specifying `--no-install-recommends` Suppressed the warnings apart from the DL3015 one, which was easy to fix. Not fixing the other warnings (for now anyway) as this Dockerfile is code that has been lifted and shifted from the `.devcontainer/Dockerfile` in: https://github.com/microsoft/vscode-dev-containers/tree/master/containers/go * Update module settings to "on" as we use modules See: https://dev.to/maelvls/why-is-go111module-everywhere-and-everything-about-go-modules-24k * Add shellcheck vscode extension to devcontainer https://www.shellcheck.net/ https://marketplace.visualstudio.com/items?itemName=timonwong.shellcheck * Use vscode go language server on devcontainer https://github.com/golang/vscode-go/blob/master/docs/gopls.md * Stop installing go dependencies on devcontainer There is no need to install them as we are using go modules - see: microsoft/vscode-go#2836 * Set vscode editor go tabsize to correct size of 8 See: microsoft/vscode-go#2479 (comment) * Use golangci-lint for vscode devcontainer linting https://github.com/golangci/golangci-lint * No longer install Guru on devcontainer It does not support Go modules: https://github.com/golang/vscode-go/blob/master/docs/tools.md#guru * Remove unnecessary gorename from devcontainer Not needed when using go modules and gopls language server: https://github.com/golang/vscode-go/blob/master/docs/tools.md#gorename * Remove unnecessary godoctor from devcontainer It is not needed when using go modules and the gopls language server: https://github.com/golang/vscode-go/blob/master/docs/tools.md#godoctor * Remove unnecessary goimports from devcontainer Imports are instead handled by the gopls language server: golang/go#33587 (comment) * Remove unnecessary golint from devcontainer Not needed as we are using golangci-lint * Remove unnecessary gotests from devcontainer gotests autogenerates table tests boilerplate code. We don't need this for this project. * Remove unnecessary goplay module from devcontainer We don't need this functionality: https://github.com/haya14busa/goplay/ * Remove unnecessary gometalinter from devcontainer Not needed as we are using golangci-lint for linting. * Remove unnecessary impl module from devcontainer It does not have support for go modules: golang/go#37537 * Remove unnecessary fillstruct from devcontainer fillstruct fills a struct literal with default values[1] This functionality is now available in the gopls language server[2] [1] https://github.com/davidrjenni/reftools/tree/master/cmd/fillstruct [2] golang/go#37576 (comment) * Add comment to explain why gopkgs is still needed It is still needed even with gopls: microsoft/vscode-go#3050 (comment) * Fix installation of golangci-lint on devcontainer Prior to this commit, vscode was saying "Analysis tools missing" and prompting to install it. Fix was to install golangci-lint via go get, as per: golangci/golangci-lint#1037 (comment) * Remove unnecessary gogetdoc from devcontainer Similar functionality is available in the gopls language server: fatih/vim-go#2808 (comment) * Remove unnecessary revive linter from devcontainer We are using golangci-lint for linting, so no need for revive. * Remove unnecessary go-tools from devcontainer go-tools primarily contains staticcheck, which we don't need as we are using golangci-lint for linting. * Output go version after building devcontainer Doing this just to provide assurance that the container has started successfully, after building. * Move settings which are not container-specific The guideline for`devcontainer.json` settings is that they should only contain settings which _must_ be be changed in a container (e.g. absolute paths)[1]. Moved the other settings to `.vscode/settings.json` so that they are more visible, and easier to use when working locally (i.e. not in a container). [1] microsoft/vscode-remote-release#874 (comment) * Up tests timeout as tests are slower on container The tests seem to run much slower (c. 100 seconds compared to c. 50 seconds) when running on a remote container, compared to locally. Will investigate to see if this is the same when running on Codespaces. If the tests are taking 100 seconds we may need to create an issue to speed them up. * Add recommended vscode go settings As per the recommendations at: https://github.com/golang/tools/blob/master/gopls/doc/vscode.md * Set dark colour theme for vscode in devcontainer * Set devcontainer vscode to autosave after 500ms Adding these settings to the devcontainer settings file rather than the vscode settings file, as we want them to apply at the machine level. https://code.visualstudio.com/docs/editor/codebasics#_save-auto-save
1 parent 30966a8 commit acdd5ad

File tree

3 files changed

+130
-2
lines changed

3 files changed

+130
-2
lines changed

.devcontainer/Dockerfile

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#-------------------------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
4+
#-------------------------------------------------------------------------------------------------------------
5+
6+
FROM golang:1
7+
8+
# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
9+
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
10+
# will be updated to match your local UID/GID (when using the dockerFile property).
11+
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
12+
ARG USERNAME=vscode
13+
ARG USER_UID=1000
14+
ARG USER_GID=$USER_UID
15+
16+
# Configure apt, install packages and tools
17+
# hadolint ignore=DL3003,DL3008
18+
RUN apt-get update \
19+
&& export DEBIAN_FRONTEND=noninteractive \
20+
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
21+
#
22+
# Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
23+
&& apt-get -y install --no-install-recommends git openssh-client less iproute2 procps lsb-release \
24+
#
25+
# Build Go tools w/module support
26+
&& mkdir -p /tmp/gotools \
27+
&& cd /tmp/gotools \
28+
&& GOPATH=/tmp/gotools GO111MODULE=on go get -v golang.org/x/tools/gopls@latest 2>&1 \
29+
&& GOPATH=/tmp/gotools GO111MODULE=on go get -v \
30+
github.com/mdempsky/gocode@latest \
31+
github.com/sqs/goreturns@latest \
32+
# gopkgs is still needed even with gopls: https://github.com/microsoft/vscode-go/issues/3050#issuecomment-592263369
33+
github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest \
34+
github.com/ramya-rao-a/go-outline@latest \
35+
github.com/acroca/go-symbols@latest \
36+
github.com/rogpeppe/godef@latest \
37+
github.com/fatih/gomodifytags@latest \
38+
github.com/go-delve/delve/cmd/dlv@latest 2>&1 \
39+
github.com/golangci/golangci-lint/cmd/[email protected] \
40+
#
41+
# Build gocode-gomod
42+
&& GOPATH=/tmp/gotools go get -x -d github.com/stamblerre/gocode 2>&1 \
43+
&& GOPATH=/tmp/gotools go build -o gocode-gomod github.com/stamblerre/gocode \
44+
#
45+
# Install Go tools
46+
&& mv /tmp/gotools/bin/* /usr/local/bin/ \
47+
&& mv gocode-gomod /usr/local/bin/ \
48+
#
49+
# Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
50+
&& groupadd --gid $USER_GID $USERNAME \
51+
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
52+
# [Optional] Add sudo support
53+
&& apt-get install -y --no-install-recommends sudo \
54+
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
55+
&& chmod 0440 /etc/sudoers.d/$USERNAME \
56+
#
57+
# Clean up
58+
&& apt-get autoremove -y \
59+
&& apt-get clean -y \
60+
&& rm -rf /var/lib/apt/lists/* /tmp/gotools
61+
62+
# Update this to "on" or "off" as appropriate
63+
# Updated from "auto" to "on" as we are using Go modules for this project
64+
# See https://dev.to/maelvls/why-is-go111module-everywhere-and-everything-about-go-modules-24k
65+
ENV GO111MODULE=on

.devcontainer/devcontainer.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "Go",
3+
"dockerFile": "Dockerfile",
4+
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
5+
6+
// Set *default* container specific settings.json values on container create.
7+
"settings": {
8+
"terminal.integrated.shell.linux": "/bin/bash",
9+
"go.gopath": "/go",
10+
"workbench.colorTheme": "Default Dark+",
11+
"files.autoSave": "afterDelay",
12+
"files.autoSaveDelay": 500,
13+
},
14+
15+
// Add the IDs of extensions you want installed when the container is created.
16+
"extensions": [
17+
"golang.Go",
18+
"timonwong.shellcheck"
19+
],
20+
21+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
22+
// "forwardPorts": [],
23+
24+
// Use 'postCreateCommand' to run commands after the container is created.
25+
"postCreateCommand": "go version",
26+
27+
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
28+
// "remoteUser": "vscode"
29+
}

.vscode/settings.json

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
11
{
2-
"go.testTimeout": "90s",
3-
"go.testEnvFile": "${workspaceFolder}/.env"
2+
"go.useLanguageServer": true,
3+
"[go]": {
4+
"editor.tabSize": 8,
5+
"editor.formatOnSave": true,
6+
"editor.codeActionsOnSave": {
7+
"source.organizeImports": true,
8+
},
9+
// Optional: Disable snippets, as they conflict with completion ranking.
10+
"editor.snippetSuggestions": "none",
11+
},
12+
"[go.mod]": {
13+
"editor.formatOnSave": true,
14+
"editor.codeActionsOnSave": {
15+
"source.organizeImports": true,
16+
},
17+
},
18+
"go.installDependenciesWhenBuilding": false, // the default is only true for backwards compatibility - see https://github.com/microsoft/vscode-go/issues/2836
19+
"go.lintTool": "golangci-lint",
20+
"go.lintFlags": ["--enable-all","--exclude-use-default=false"],
21+
"go.lintOnSave": "file",
22+
"go.testTimeout": "240s",
23+
"go.testEnvFile": "${workspaceFolder}/.env",
24+
25+
// "VSCode will complain about the "gopls" settings, but they will still work.
26+
// Once we have a consistent set of settings, we will make the changes in the
27+
// VSCode plugin necessary to remove the errors."
28+
// https://github.com/golang/tools/blob/master/gopls/doc/vscode.md
29+
"gopls": {
30+
// Add parameter placeholders when completing a function.
31+
"usePlaceholders": true,
32+
33+
// If true, enable additional analyses with staticcheck.
34+
// Warning: This will significantly increase memory usage.
35+
"staticcheck": false,
36+
}
37+
438
}

0 commit comments

Comments
 (0)