8000 Merge pull request #128 from arduino/check-go · arduino/arduino-language-server@24cb630 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 24cb630

Browse files
authored
Merge pull request #128 from arduino/check-go
Add CI workflow to lint and check formatting of Go code
2 parents c1ceab9 + 39e09a4 commit 24cb630

12 files changed

+507
-174
lines changed

.github/workflows/check-go-task.yml

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md
2+
name: Check Go
3+
4+
env:
5+
# See: https://github.com/actions/setup-go/tree/main#supported-version-syntax
6+
GO_VERSION: "1.18.3"
7+
8+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/check-go-task.ya?ml"
14+
- "Taskfile.ya?ml"
15+
- "**/go.mod"
16+
- "**/go.sum"
17+
- "**.go"
18+
pull_request:
19+
paths:
20+
- ".github/workflows/check-go-task.ya?ml"
21+
- "Taskfile.ya?ml"
22+
- "**/go.mod"
23+
- "**/go.sum"
24+
- "**.go"
25+
schedule:
26+
# Run periodically to catch breakage caused by external changes.
27+
- cron: "0 7 * * WED"
28+
workflow_dispatch:
29+
repository_dispatch:
30+
31+
jobs:
32+
run-determination:
33+
runs-on: ubuntu-latest
34+
outputs: 57AE
35+
result: ${{ steps.determination.outputs.result }}
36+
steps:
37+
- name: Determine if the rest of the workflow should run
38+
id: determination
39+
run: |
40+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
41+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
42+
if [[
43+
"${{ github.event_name }}" != "create" ||
44+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
45+
]]; then
46+
# Run the other jobs.
47+
RESULT="true"
48+
else
49+
# There is no need to run the other jobs.
50+
RESULT="false"
51+
fi
52+
53+
echo "::set-output name=result::$RESULT"
54+
55+
check-errors:
56+
name: check-errors (${{ matrix.module.path }})
57+
needs: run-determination
58+
if: needs.run-determination.outputs.result == 'true'
59+
runs-on: ubuntu-latest
60+
61+
strategy:
62+
fail-fast: false
63+
64+
matrix:
65+
module:
66+
- path: ./
67+
68+
steps:
69+
- name: Checkout repository
70+
uses: actions/checkout@v3
71+
72+
- name: Install Go
73+
uses: actions/setup-go@v3
74+
with:
75+
go-version: ${{ env.GO_VERSION }}
76+
77+
- name: Install Task
78+
uses: arduino/setup-task@v1
79+
with:
80+
repo-token: ${{ secrets.GITHUB_TOKEN }}
81+
version: 3.x
82+
83+
- name: Check for errors
84+
env:
85+
GO_MODULE_PATH: ${{ matrix.module.path }}
86+
run: task go:vet
87+
88+
check-outdated:
89+
name: check-outdated (${{ matrix.module.path }})
90+
needs: run-determination
91+
if: needs.run-determination.outputs.result == 'true'
92+
runs-on: ubuntu-latest
93+
94+
strategy:
95+
fail-fast: false
96+
97+
matrix:
98+
module:
99+
- path: ./
100+
101+
steps:
102+
- name: Checkout repository
103+
uses: actions/checkout@v3
104+
105+
- name: Install Go
106+
uses: actions/setup-go@v3
107+
with:
108+
go-version: ${{ env.GO_VERSION }}
109+
110+
- name: Install Task
111+
uses: arduino/setup-task@v1
112+
with:
113+
repo-token: ${{ secrets.GITHUB_TOKEN }}
114+
version: 3.x
115+
116+
- name: Modernize usages of outdated APIs
117+
env:
118+
GO_MODULE_PATH: ${{ matrix.module.path }}
119+
run: task go:fix
120+
121+
- name: Check if any fixes were needed
122+
run: git diff --color --exit-code
123+
124+
check-style:
125+
name: check-style (${{ matrix.module.path }})
126+
needs: run-determination
127+
if: needs.run-determination.outputs.result == 'true'
128+
runs-on: ubuntu-latest
129+
130+
strategy:
131+
fail-fast: false
132+
133+
matrix:
134+
module:
135+
- path: ./
136+
137+
steps:
138+
- name: Checkout repository
139+
uses: actions/checkout@v3
140+
141+
- name: Install Go
142+
uses: actions/setup-go@v3
143+
with:
144+
go-version: ${{ env.GO_VERSION }}
145+
146+
- name: Install Task
147+
uses: arduino/setup-task@v1
148+
with:
149+
repo-token: ${{ secrets.GITHUB_TOKEN }}
150+
version: 3.x
151+
152+
- name: Install golint
153+
run: go install golang.org/x/lint/golint@latest
154+
155+
- name: Check style
156+
env:
157+
GO_MODULE_PATH: ${{ matrix.module.path }}
158+
run: task --silent go:lint
159+
160+
check-formatting:
161+
name: check-formatting (${{ matrix.module.path }})
162+
needs: run-determination
163+
if: needs.run-determination.outputs.result == 'true'
164+
runs-on: ubuntu-latest
165+
166+
strategy:
167+
fail-fast: false
168+
169+
matrix:
170+
module:
171+
- path: ./
172+
173+
steps:
174+
- name: Checkout repository
175+
uses: actions/checkout@v3
176+
177+
- name: Install Go
178+
uses: actions/setup-go@v3
179+
with:
180+
go-version: ${{ env.GO_VERSION }}
181+
182+
- name: Install Task
183+
uses: arduino/setup-task@v1
184+
with:
185+
repo-token: ${{ secrets.GITHUB_TOKEN }}
186+
version: 3.x
187+
188+
- name: Format code
189+
env:
190+
GO_MODULE_PATH: ${{ matrix.module.path }}
191+
run: task go:format
192+
193+
- name: Check formatting
194+
run: git diff --color --exit-code
195+
196+
check-config:
197+
name: check-config (${{ matrix.module.path }})
198+
needs: run-determination
199+
if: needs.run-determination.outputs.result == 'true'
200+
runs-on: ubuntu-latest
201+
202+
strategy:
203+
fail-fast: false
204+
205+
matrix:
206+
module:
207+
- path: ./
208+
209+
steps:
210+
- name: Checkout repository
211+
uses: actions/checkout@v3
212+
213+
- name: Install Go
214+
uses: actions/setup-go@v3
215+
with:
216+
go-version: ${{ env.GO_VERSION }}
217+
218+
- name: Run go mod tidy
219+
working-directory: ${{ matrix.module.path }}
220+
run: go mod tidy
221+
222+
- name: Check whether any tidying was needed
223+
run: git diff --color --exit-code

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Arduino Language Server
44

55
[![Check Taskfiles status](https://github.com/arduino/arduino-language-server/actions/workflows/check-taskfiles.yml/badge.svg)](https://github.com/arduino/arduino-language-server/actions/workflows/check-taskfiles.yml)
6+
[![Check Go status](https://github.com/arduino/arduino-language-server/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/arduino-language-server/actions/workflows/check-go-task.yml)
67

78
The **Arduino Language Server** is the tool that powers the autocompletion of the new [Arduino IDE 2][arduino-ide-repo]. It implements the standard [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) so it can be used with other IDEs as well.
89

Taskfile.yml

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@ version: "3"
33
includes:
44
dist: ./DistTasks.yml
55

6+
vars:
7+
PROJECT_NAME: "arduino-language-server"
8+
DIST_DIR: "dist"
9+
# Path of the project's primary Go module:
10+
DEFAULT_GO_MODULE_PATH: ./
11+
DEFAULT_GO_PACKAGES:
12+
sh: |
13+
echo $(cd {{default "./" .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"')
14+
# build vars
15+
COMMIT:
16+
sh: echo "$(git log --no-show-signature -n 1 --format=%h)"
17+
TIMESTAMP:
18+
sh: echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
19+
TIMESTAMP_SHORT:
20+
sh: echo "{{now | date "20060102"}}"
21+
TAG:
22+
sh: echo "$(git tag --points-at=HEAD 2> /dev/null | head -n1)"
23+
VERSION: "{{if .NIGHTLY}}nightly-{{.TIMESTAMP_SHORT}}{{else if .TAG}}{{.TAG}}{{else}}{{.PACKAGE_NAME_PREFIX}}git-snapshot{{end}}"
24+
CONFIGURATION_PACKAGE: "github.com/arduino/arduino-language-server/version"
25+
LDFLAGS: >-
26+
-ldflags
27+
'
28+
-X {{.CONFIGURATION_PACKAGE}}.versionString={{.VERSION}}
29+
-X {{.CONFIGURATION_PACKAGE}}.commit={{.COMMIT}}
30+
-X {{.CONFIGURATION_PACKAGE}}.date={{.TIMESTAMP}}
31+
'
32+
# test vars
33+
GOFLAGS: "-timeout 10m -v -coverpkg=./... -covermode=atomic"
34+
TEST_VERSION: "0.0.0-test.preview"
35+
TEST_COMMIT: "deadbeef"
36+
TEST_LDFLAGS: >-
37+
-ldflags
38+
'
39+
-X {{.CONFIGURATION_PACKAGE}}.versionString={{.TEST_VERSION}}
40+
-X {{.CONFIGURATION_PACKAGE}}.commit={{.TEST_COMMIT}}
41+
-X {{.CONFIGURATION_PACKAGE}}.date={{.TIMESTAMP}}
42+
'
43+
644
tasks:
745
docs:generate:
846
desc: Create all generated documentation content
@@ -320,39 +358,3 @@ tasks:
320358
- task: poetry:install-deps
321359
cmds:
322360
- poetry run mkdocs serve
323-
324-
vars:
325-
PROJECT_NAME: "arduino-language-server"
326-
DIST_DIR: "dist"
327-
DEFAULT_GO_PACKAGES:
328-
sh: |
329-
echo $(cd {{default "./" .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"')
330-
# build vars
331-
COMMIT:
332-
sh: echo "$(git log --no-show-signature -n 1 --format=%h)"
333-
TIMESTAMP:
334-
sh: echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
335-
TIMESTAMP_SHORT:
336-
sh: echo "{{now | date "20060102"}}"
337-
TAG:
338-
sh: echo "$(git tag --points-at=HEAD 2> /dev/null | head -n1)"
339-
VERSION: "{{if .NIGHTLY}}nightly-{{.TIMESTAMP_SHORT}}{{else if .TAG}}{{.TAG}}{{else}}{{.PACKAGE_NAME_PREFIX}}git-snapshot{{end}}"
340-
CONFIGURATION_PACKAGE: "github.com/arduino/arduino-language-server/version"
341-
LDFLAGS: >-
342-
-ldflags
343-
'
344-
-X {{.CONFIGURATION_PACKAGE}}.versionString={{.VERSION}}
345-
-X {{.CONFIGURATION_PACKAGE}}.commit={{.COMMIT}}
346-
-X {{.CONFIGURATION_PACKAGE}}.date={{.TIMESTAMP}}
347-
'
348-
# test vars
349-
GOFLAGS: "-timeout 10m -v -coverpkg=./... -covermode=atomic"
350-
TEST_VERSION: "0.0.0-test.preview"
351-
TEST_COMMIT: "deadbeef"
352-
TEST_LDFLAGS: >-
353-
-ldflags
354-
'
355-
-X {{.CONFIGURATION_PACKAGE}}.versionString={{.TEST_VERSION}}
356-
-X {{.CONFIGURATION_PACKAGE}}.commit={{.TEST_COMMIT}}
357-
-X {{.CONFIGURATION_PACKAGE}}.date={{.TIMESTAMP}}
358-
'

ls/builder.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ import (
2525
"google.golang.org/grpc"
2626
)
2727

28-
type SketchRebuilder struct {
28+
type sketchRebuilder struct {
2929
ls *INOLanguageServer
3030
trigger chan chan<- bool
3131
cancel func()
3232
mutex sync.Mutex
3333
}
3434

35-
func NewSketchBuilder(ls *INOLanguageServer) *SketchRebuilder {
36-
res := &SketchRebuilder{
35+
// newSketchBuilder makes a new SketchRebuilder and returns its pointer
36+
func newSketchBuilder(ls *INOLanguageServer) *sketchRebuilder {
37+
res := &sketchRebuilder{
3738
trigger: make(chan chan<- bool, 1),
3839
cancel: func() {},
3940
ls: ls,
@@ -57,7 +58,8 @@ func (ls *INOLanguageServer) triggerRebuild() {
5758
ls.sketchRebuilder.TriggerRebuild(nil)
5859
}
5960

60-
func (r *SketchRebuilder) TriggerRebuild(completed chan<- bool) {
61+
// TriggerRebuild schedule a sketch rebuild (it will be executed asynchronously)
62+
func (r *sketchRebuilder) TriggerRebuild(completed chan<- bool) {
6163
r.mutex.Lock()
6264
defer r.mutex.Unlock()
6365

@@ -68,7 +70,7 @@ func (r *SketchRebuilder) TriggerRebuild(completed chan<- bool) {
6870
}
6971
}
7072

71-
func (r *SketchRebuilder) rebuilderLoop() {
73+
func (r *sketchRebuilder) rebuilderLoop() {
7274
logger := NewLSPFunctionLogger(color.HiMagentaString, "SKETCH REBUILD: ")
7375
for {
7476
completed := <-r.trigger
@@ -104,7 +106,7 @@ func (r *SketchRebuilder) rebuilderLoop() {
104106
}
105107
}
106108

107-
func (r *SketchRebuilder) doRebuildArduinoPreprocessedSketch(ctx context.Context, logger jsonrpc.FunctionLogger) error {
109+
func (r *sketchRebuilder) doRebuildArduinoPreprocessedSketch(ctx context.Context, logger jsonrpc.FunctionLogger) error {
108110
ls := r.ls
109111
if success, err := ls.generateBuildEnvironment(ctx, !r.ls.config.SkipLibrariesDiscoveryOnRebuild, logger); err != nil {
110112
return err
@@ -207,8 +209,8 @@ func (ls *INOLanguageServer) generateBuildEnvironment(ctx context.Context, fullB
207209
Verbose: true,
208210
SkipLibrariesDiscovery: !fullBuild,
209211
}
210-
compileReqJson, _ := json.MarshalIndent(compileReq, "", " ")
211-
logger.Logf("Running build with: %s", string(compileReqJson))
212+
compileReqJSON, _ := json.MarshalIndent(compileReq, "", " ")
213+
logger.Logf("Running build with: %s", string(compileReqJSON))
212214

213215
compRespStream, err := client.Compile(context.Background(), compileReq)
214216
if err != nil {

0 commit comments

Comments
 (0)
0