-
Notifications
You must be signed in to change notification settings - Fork 16
fix: reset summary panel on stop-scan [IDE-1035] #1324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
acke
wants to merge
14
commits into
main
Choose a base branch
from
fix/IDE-1035_reset-summary-on-stop-scan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
65ff59e
fix: reset summary panel on stop-scan [IDE-1035]
acke 89251fb
fix: correct misspelling cancelled → canceled in test comment [IDE-1035]
acke b6005ed
fix(server): gate stop-scan reset to scan tokens; converge after goro…
acke 7dc9f44
style(server,scanner): address golangci-lint feedback [IDE-1035]
acke 3a99f23
Merge branch 'main' into fix/IDE-1035_reset-summary-on-stop-scan
acke 55b02e7
Merge branch 'main' into fix/IDE-1035_reset-summary-on-stop-scan
acke 9c73dc2
fix(server): defer progress.Cancel so RegisterCancelCallback register…
acke 1f0e9ab
refactor(server): delete unreferenced resetSummaryPanelOnStopScan wra…
acke 6008315
refactor(scanner,server): lift RegisterCancelCallback to interface, t…
acke b483330
refactor(scanner): collapse setupScannerWithResolver into setupScanne…
acke e511bbb
refactor(progress): collapse scanTokens registry into Tracker.isScan …
acke 9d5aadf
Merge remote-tracking branch 'origin/main' into fix/IDE-1035_reset-su…
acke c716183
style: fix gofmt alignment + misspell in cancel-handler test [IDE-1035]
acke 84955a2
fix(lint): remove trailing newline in configuration_test.go (goimports)
acke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| /* | ||
| * © 2026 Snyk Limited | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package server | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/snyk/snyk-ls/domain/scanstates" | ||
| scanner2 "github.com/snyk/snyk-ls/domain/snyk/scanner" | ||
| ctx2 "github.com/snyk/snyk-ls/internal/context" | ||
| "github.com/snyk/snyk-ls/internal/progress" | ||
| "github.com/snyk/snyk-ls/internal/testutil" | ||
| "github.com/snyk/snyk-ls/internal/testutil/workspaceutil" | ||
| "github.com/snyk/snyk-ls/internal/types" | ||
| ) | ||
|
|
||
| // fakeScanner records RegisterCancelCallback invocations so the handler test | ||
| // can assert which folders were wired without spinning up a real | ||
| // DelegatingConcurrentScanner. End-to-end ordering with SetScanDone is covered | ||
| // at the scanner layer by TestScan_CancelCallback_CalledAfterGoroutinesFinish. | ||
| type fakeScanner struct { | ||
| scanner2.TestScanner | ||
| mu sync.Mutex | ||
| callbacks map[types.FilePath]func() | ||
| } | ||
|
|
||
| func (f *fakeScanner) RegisterCancelCallback(folderPath types.FilePath, fn func()) { | ||
| f.mu.Lock() | ||
| defer f.mu.Unlock() | ||
| if f.callbacks == nil { | ||
| f.callbacks = make(map[types.FilePath]func()) | ||
| } | ||
| f.callbacks[folderPath] = fn | ||
| } | ||
|
|
||
| func (f *fakeScanner) registered() map[types.FilePath]func() { | ||
| f.mu.Lock() | ||
| defer f.mu.Unlock() | ||
| out := make(map[types.FilePath]func(), len(f.callbacks)) | ||
| for k, v := range f.callbacks { | ||
| out[k] = v | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // Handler contract: a scan token registers reset-on-cancel for every | ||
| // workspace folder via the Scanner interface, then progress.Cancel fires | ||
| // (defer). The registration happens BEFORE the cancel — that is the | ||
| // IDE-1035 register-vs-consume race fix exercised end-to-end. | ||
| func TestHandleWindowWorkDoneProgressCancel_ScanToken_RegistersBeforeCancel(t *testing.T) { | ||
| engine := testutil.UnitTest(t) | ||
| conf := engine.GetConfiguration() | ||
|
|
||
| folderA := types.FilePath(t.TempDir()) | ||
| folderB := types.FilePath(t.TempDir()) | ||
| _, _ = workspaceutil.SetupWorkspace(t, engine, folderA, folderB) | ||
|
|
||
| scanner := &fakeScanner{} | ||
| agg := scanstates.NewNoopStateAggregator() | ||
| ctx := ctx2.NewContextWithDependencies(t.Context(), map[string]any{ | ||
| ctx2.DepScanners: scanner, | ||
| ctx2.DepScanStateAggregator: agg, | ||
| }) | ||
|
|
||
| logger := engine.GetLogger() | ||
| tracker := progress.NewScanTracker(true, logger) | ||
| token := tracker.GetToken() | ||
| require.True(t, progress.IsScanToken(token), "precondition: NewScanTracker must register a scan token") | ||
|
|
||
| _, err := handleWindowWorkDoneProgressCancel(ctx, | ||
| types.WorkdoneProgressCancelParams{Token: token}, | ||
| conf, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| // Both workspace folders must have had a callback registered. | ||
| got := scanner.registered() | ||
| assert.Contains(t, got, folderA, "callback must be registered for folder A") | ||
| assert.Contains(t, got, folderB, "callback must be registered for folder B") | ||
| assert.Len(t, got, 2, "exactly one callback per folder") | ||
|
|
||
| // The deferred progress.Cancel must have run by the time the handler returns, | ||
| // so the scan token is no longer recognized. This is the ordering guarantee | ||
| // that prevents the register-vs-consume race. | ||
| assert.False(t, progress.IsScanToken(token), | ||
| "progress.Cancel must have fired (deferred) — registration happened first, then cancel") | ||
| } | ||
|
|
||
| // Non-scan tokens (e.g. CLI download progress) must not register any reset | ||
| // callback. The cancel must still fire so the download stops. | ||
| func TestHandleWindowWorkDoneProgressCancel_NonScanToken_NoRegistration(t *testing.T) { | ||
| engine := testutil.UnitTest(t) | ||
| conf := engine.GetConfiguration() | ||
|
|
||
| folderA := types.FilePath(t.TempDir()) | ||
| _, _ = workspaceutil.SetupWorkspace(t, engine, folderA) | ||
|
|
||
| scanner := &fakeScanner{} | ||
| agg := scanstates.NewNoopStateAggregator() | ||
| ctx := ctx2.NewContextWithDependencies(t.Context(), map[string]any{ | ||
| ctx2.DepScanners: scanner, | ||
| ctx2.DepScanStateAggregator: agg, | ||
| }) | ||
|
|
||
| logger := engine.GetLogger() | ||
| tracker := progress.NewTracker(true, logger) // plain tracker — NOT a scan token | ||
| token := tracker.GetToken() | ||
| require.False(t, progress.IsScanToken(token), "precondition: NewTracker must NOT register as a scan token") | ||
|
|
||
| _, err := handleWindowWorkDoneProgressCancel(ctx, | ||
| types.WorkdoneProgressCancelParams{Token: token}, | ||
| conf, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Empty(t, scanner.registered(), | ||
| "non-scan tokens must not register a reset callback — generic progress must not wipe scan results") | ||
| assert.True(t, progress.IsCanceled(token), | ||
| "progress.Cancel must still fire for non-scan tokens so the download is stopped") | ||
| } | ||
|
|
||
| // When the scanner is missing from context (early startup / tests that don't | ||
| // wire DI), the handler must NOT fall back to a synchronous reset that races | ||
| // in-flight SetScanDone writes. It should log and return cleanly. | ||
| func TestHandleWindowWorkDoneProgressCancel_ScanToken_NoScanner_NoSyncFallback(t *testing.T) { | ||
| engine := testutil.UnitTest(t) | ||
| conf := engine.GetConfiguration() | ||
|
|
||
| folderA := types.FilePath(t.TempDir()) | ||
| _, _ = workspaceutil.SetupWorkspace(t, engine, folderA) | ||
|
|
||
| // Aggregator is present, but scanner is intentionally missing. | ||
| agg := scanstates.NewNoopStateAggregator() | ||
| ctx := ctx2.NewContextWithDependencies(t.Context(), map[string]any{ | ||
| ctx2.DepScanStateAggregator: agg, | ||
| }) | ||
|
|
||
| logger := engine.GetLogger() | ||
| tracker := progress.NewScanTracker(true, logger) | ||
| token := tracker.GetToken() | ||
|
|
||
| _, err := handleWindowWorkDoneProgressCancel(ctx, | ||
| types.WorkdoneProgressCancelParams{Token: token}, | ||
| conf, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| // progress.Cancel still fires (defer). No reset happened — the racy sync | ||
| // fallback the reviewer flagged on #3382206417 is gone. | ||
| assert.False(t, progress.IsScanToken(token)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should fix — cancel resets every folder, not the cancelled one. This loop registers a reset callback for every workspace folder, but
params.Tokenidentifies a single per-product tracker belonging to one folder. In a multi-folder workspace, cancelling folder A's scan also registers reset callbacks on folders B, C… Those callbacks are not consumed now (no in-flight scan there) and instead fire on each of those folders' nextScan()completion (scanner.goconsumeCancelCallback), wiping valid results the user never asked to clear. Consider mapping the cancelled token back to its owning folder and registering the reset only for that folder.(Three of the four reviewers independently flagged this as the root cause of a cluster of data-loss bugs.)
— AI review