Skip to content

Commit 0d1ae91

Browse files
committed
fix: delete lsconfig content if invalid json
1 parent 627b730 commit 0d1ae91

File tree

7 files changed

+116
-14
lines changed

7 files changed

+116
-14
lines changed

.github/upload-to-s3.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function uploadFile() {
6868
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
6969
copyOrDownloadToTemp $FILENAME_SRC "$FILENAME_DST" $DRY_RUN
7070

71-
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_darwin_arm64/snyk-ls"
71+
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_darwin_arm64_v8.0/snyk-ls"
7272
FILENAME_DST="snyk-ls_${VERSION}_darwin_arm64"
7373
# shellcheck disable=SC2086
7474
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
@@ -80,19 +80,19 @@ function uploadFile() {
8080
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
8181
copyOrDownloadToTemp $FILENAME_SRC "$FILENAME_DST" $DRY_RUN
8282

83-
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_linux_386/snyk-ls"
83+
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_linux_386_sse2/snyk-ls"
8484
FILENAME_DST="snyk-ls_${VERSION}_linux_386"
8585
# shellcheck disable=SC2086
8686
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
8787
copyOrDownloadToTemp $FILENAME_SRC "$FILENAME_DST" $DRY_RUN
8888

89-
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_linux_arm64/snyk-ls"
89+
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_linux_arm64_v8.0/snyk-ls"
9090
FILENAME_DST="snyk-ls_${VERSION}_linux_arm64"
9191
# shellcheck disable=SC2086
9292
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
9393
copyOrDownloadToTemp $FILENAME_SRC "$FILENAME_DST" $DRY_RUN
9494

95-
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_windows_386/snyk-ls.exe"
95+
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_windows_386_sse2/snyk-ls.exe"
9696
FILENAME_DST="snyk-ls_${VERSION}_windows_386.exe"
9797
# shellcheck disable=SC2086
9898
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN

.github/workflows/build.yaml

+28-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,28 @@ on:
1818
pull_request:
1919

2020
jobs:
21+
lint:
22+
name: lint
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Prepare git
26+
run: git config --global core.autocrlf false
27+
28+
- uses: actions/checkout@v4
29+
30+
- name: Set up Go
31+
uses: actions/setup-go@v5
32+
with:
33+
go-version-file: "./go.mod"
34+
cache: "true"
35+
36+
- name: Lint source code
37+
run: |
38+
make tools lint
39+
2140
unit-tests:
2241
name: unit tests
42+
needs: [lint]
2343
runs-on: ubuntu-latest
2444
steps:
2545
- name: Prepare git
@@ -49,10 +69,6 @@ jobs:
4969
run: |
5070
make tools
5171
52-
- name: Lint source code
53-
run: |
54-
make tools lint
55-
5672
- name: Run tests
5773
env:
5874
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
@@ -70,6 +86,7 @@ jobs:
7086
7187
integration-tests:
7288
name: integration-tests
89+
needs: [lint]
7390
runs-on: ${{ matrix.os }}
7491
strategy:
7592
matrix:
@@ -99,12 +116,13 @@ jobs:
99116
run: |
100117
make tools
101118
102-
- name: Run integration tests with Pact
119+
- name: Run integration & smoke tests with Pact
103120
if: matrix.os == 'ubuntu-latest'
104121
env:
105122
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
106123
SNYK_TOKEN: ${{secrets.SNYK_TOKEN }}
107124
INTEG_TESTS: "true"
125+
SMOKE_TESTS: "true"
108126
run: |
109127
export PATH=$PATH:~/pact/bin
110128
@@ -121,6 +139,7 @@ jobs:
121139
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
122140
SNYK_TOKEN: ${{secrets.SNYK_TOKEN }}
123141
INTEG_TESTS: "true"
142+
SMOKE_TESTS: "true"
124143
run: |
125144
export PATH=$PATH:~/pact/bin
126145
@@ -136,12 +155,14 @@ jobs:
136155
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
137156
SNYK_TOKEN: ${{secrets.SNYK_TOKEN }}
138157
INTEG_TESTS: "true"
158+
SMOKE_TESTS: "true"
139159
run: |
140160
make clean test
141161
142162
143163
proxy-test:
144164
name: proxy-test
165+
needs: [lint]
145166
runs-on: ubuntu-latest
146167
steps:
147168
- uses: actions/checkout@v4
@@ -154,6 +175,7 @@ jobs:
154175
155176
race-tests:
156177
name: race-test
178+
needs: [lint]
157179
runs-on: ubuntu-latest
158180
steps:
159181
- uses: actions/checkout@v4
@@ -216,6 +238,7 @@ jobs:
216238
push: true
217239
test-release:
218240
name: test-release
241+
needs: [lint, unit-tests]
219242
runs-on: ubuntu-latest
220243
steps:
221244
- uses: actions/checkout@v4
@@ -259,4 +282,3 @@ jobs:
259282
AWS_S3_BUCKET_NAME: ${{ secrets.AWS_S3_BUCKET_NAME }}
260283
run: |
261284
.github/upload-to-s3.sh --dryrun
262-

.github/workflows/release.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,30 @@ jobs:
133133
chmod +x build/snyk-ls_linux_amd64_v1/snyk-ls
134134
build/snyk-ls_linux_amd64_v1/snyk-ls -licenses
135135
136+
# we only want to upload when we consciously release, not on merge
136137
- name: Login to AWS
138+
if: github.event_name == 'workflow_dispatch'
137139
run: |
138140
.github/setup_aws_credentials.py \
139141
--role-arn "arn:aws:iam::198361731867:role/Snyk-Assets-WriteOnly" \
140142
--region "${{ secrets.AWS_REGION }}"
141143
142144
- name: Upload binaries to static.snyk.io
145+
if: github.event_name == 'workflow_dispatch'
143146
env:
144147
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
145148
AWS_S3_BUCKET_NAME: ${{ secrets.AWS_S3_BUCKET_NAME }}
146149
run: |
147150
.github/upload-to-s3.sh
148151
152+
# creating PR in cli repository needs ssh agent
149153
- uses: webfactory/[email protected]
154+
if: github.event_name != 'workflow_dispatch'
150155
with:
151156
ssh-private-key: ${{ secrets.TEAM_IDE_USER_SSH }}
152157

153158
- name: Create PR in CLI to integrate LS
159+
if: github.event_name != 'workflow_dispatch'
154160
env:
155161
GH_TOKEN: ${{ secrets.HAMMERHEAD_GITHUB_PAT_SNYKLS }}
156162
GITHUB_TOKEN: ${{ secrets.HAMMERHEAD_GITHUB_PAT_SNYKLS }}

application/server/server_smoke_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ func checkOnlyOneQuickFixCodeAction(t *testing.T, jsonRPCRecorder *testutil.Json
550550

551551
// "tap": "^11.1.3", 12 fixable, 11 unfixable
552552
if issue.Range.Start.Line == 46 && isQuickfixAction {
553-
assert.Contains(t, action.Title, "and fix 23 issues")
553+
assert.Contains(t, action.Title, "and fix 24 issues")
554554
}
555555
}
556556
// no issues should have more than one quickfix
@@ -602,7 +602,7 @@ func checkOnlyOneCodeLens(t *testing.T, jsonRPCRecorder *testutil.JsonRPCRecorde
602602

603603
// "tap": "^11.1.3", 12 fixable, 11 unfixable
604604
if lens.Range.Start.Line == 46 {
605-
assert.Contains(t, lens.Command.Title, "and fix 23 issues")
605+
assert.Contains(t, lens.Command.Title, "and fix 24 issues")
606606
}
607607
}
608608
}

infrastructure/oss/oss_integration_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424
"testing"
2525

26+
"github.com/snyk/go-application-framework/pkg/configuration"
2627
"github.com/stretchr/testify/assert"
2728

2829
"github.com/snyk/snyk-ls/application/config"
@@ -69,6 +70,11 @@ func Test_Scan(t *testing.T) {
6970
workingDir, _ := os.Getwd()
7071
path, _ := filepath.Abs(filepath.Join(workingDir, "testdata", "package.json"))
7172

73+
// temporary until policy engine doesn't output to stdout anymore
74+
t.Setenv("SNYK_LOG_LEVEL", "info")
75+
c.ConfigureLogging(nil)
76+
c.Engine().GetConfiguration().Set(configuration.DEBUG, false)
77+
7278
issues, _ := scanner.Scan(ctx, path, workingDir)
7379

7480
assert.NotEqual(t, 0, len(issues))

internal/storage/storage.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ package storage
1818

1919
import (
2020
"context"
21+
"encoding/json"
22+
"errors"
23+
"os"
24+
"sync"
2125
"time"
2226

2327
"github.com/adrg/xdg"
@@ -36,33 +40,72 @@ type StorageWithCallbacks interface {
3640
type storage struct {
3741
callbacks map[string]StorageCallbackFunc
3842
jsonStorage *configuration.JsonStorage
43+
storageFile string
44+
mutex sync.RWMutex
3945
}
4046

4147
func (s *storage) Refresh(config configuration.Configuration, key string) error {
42-
return s.jsonStorage.Refresh(config, key)
48+
s.mutex.Lock()
49+
50+
contents, err := os.ReadFile(s.storageFile)
51+
if err != nil {
52+
s.mutex.Unlock()
53+
return err
54+
}
55+
doc := map[string]interface{}{}
56+
err = json.Unmarshal(contents, &doc)
57+
if err != nil {
58+
s.mutex.Unlock()
59+
return err
60+
}
61+
62+
s.mutex.Unlock()
63+
if value, ok := doc[key]; ok {
64+
config.Set(key, value)
65+
}
66+
return nil
4367
}
4468

4569
func (s *storage) Lock(ctx context.Context, retryDelay time.Duration) error {
70+
s.mutex.Lock()
71+
defer s.mutex.Unlock()
4672
return s.jsonStorage.Lock(ctx, retryDelay)
4773
}
4874

4975
func (s *storage) Unlock() error {
76+
s.mutex.Lock()
77+
defer s.mutex.Unlock()
5078
return s.jsonStorage.Unlock()
5179
}
5280

5381
type storageOption func(*storage)
5482

5583
func (s *storage) Set(key string, value any) error {
84+
s.mutex.Lock()
85+
defer s.mutex.Unlock()
86+
5687
callback := s.callbacks[key]
5788

5889
if callback != nil {
5990
callback(key, value)
6091
}
6192

6293
err := s.jsonStorage.Set(key, value)
63-
if err != nil {
94+
95+
var syntaxError *json.SyntaxError
96+
if errors.As(err, &syntaxError) {
97+
err = os.WriteFile(s.storageFile, []byte("{}"), 0666)
98+
if err != nil {
99+
return err
100+
}
101+
err = s.jsonStorage.Set(key, value)
102+
if err != nil {
103+
return err
104+
}
105+
} else if err != nil {
64106
return err
65107
}
108+
66109
return nil
67110
}
68111

@@ -101,5 +144,6 @@ func WithCallbacks(callbacks map[string]StorageCallbackFunc) func(*storage) {
101144
func WithStorageFile(file string) func(*storage) {
102145
return func(s *storage) {
103146
s.jsonStorage = configuration.NewJsonStorage(file)
147+
s.storageFile = file
104148
}
105149
}

internal/storage/storage_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ func Test_StorageCallsRegisterCallbacksForKeys(t *testing.T) {
4949
}, 5*time.Second, time.Millisecond, "callback was not called")
5050
}
5151

52+
func Test_StorageCallsRegisterCallbacks_InvalidJsonContent_ShouldClean(t *testing.T) {
53+
called := make(chan bool, 1)
54+
callbacks := map[string]StorageCallbackFunc{}
55+
file := filepath.Join(t.TempDir(), t.Name())
56+
err := os.WriteFile(file, []byte("{\"INTERNAL_OAUTH_TOKEN_STORAGE\":\"{\\\"access_token\\\":\\\"mytoken\\\",\\\"token_type\\\":\\\"bearer\\\",\\\"refresh_token\\\":\\\"myrefreshtoken\\\",\\\"expiry\\\":\\\"2024-10-01T21:43:26.209852+02:00\\\"}\"}\"snyk_token\":\"\"}"), 0644)
57+
assert.NoError(t, err)
58+
myCallback := func(_ string, _ any) { called <- true }
59+
60+
key := "test"
61+
value := "test"
62+
callbacks[key] = myCallback
63+
s, err := NewStorageWithCallbacks(WithCallbacks(callbacks), WithStorageFile(file))
64+
require.NoError(t, err)
65+
66+
err = s.Set(key, value)
67+
require.NoError(t, err)
68+
content, err := os.ReadFile(file)
69+
assert.NoError(t, err)
70+
assert.Equal(t, "{\"test\":\"test\"}", string(content))
71+
require.Eventuallyf(t, func() bool {
72+
return <-called
73+
}, 5*time.Second, time.Millisecond, "callback was not called")
74+
}
75+
5276
func Test_ParallelFileLocking(t *testing.T) {
5377
t.Run("should respect locking order", func(t *testing.T) {
5478
file := filepath.Join(t.TempDir(), t.Name())

0 commit comments

Comments
 (0)