Skip to content

Commit 2519f4b

Browse files
Merge pull request #141 from codacy/fix-codacy-cloud-initializing
fix: codacy cloud stuck in initializing CF-1888
2 parents 9713fed + 46750c1 commit 2519f4b

3 files changed

Lines changed: 78 additions & 6 deletions

File tree

src/extension.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ const registerGitProvider = async (context: vscode.ExtensionContext, codacyCloud
9595
})
9696

9797
git.onDidChangeState(async (state: APIState) => {
98+
Logger.debug(`Git API state changed to: ${state}`)
9899
if (state === 'initialized') {
99100
if (git.repositories.length > 0) {
101+
Logger.debug(`Git API initialized with ${git.repositories.length} repositories`)
100102
codacyCloud.open(git.repositories[0])
101103
} else {
102-
Logger.appendLine('No Git Repositories found')
104+
Logger.appendLine('Git API initialized but no repositories found')
103105
codacyCloud.clear()
104106
}
105107
}
@@ -216,8 +218,13 @@ export async function activate(context: vscode.ExtensionContext) {
216218
)
217219

218220
// check for open repository
219-
if (gitProvider.repositories.length > 0) {
221+
if (gitProvider.state === 'initialized' && gitProvider.repositories.length > 0) {
222+
Logger.debug(`Found ${gitProvider.repositories.length} repositories, opening first one`)
220223
codacyCloud.open(gitProvider.repositories[0])
224+
} else if (gitProvider.state === 'uninitialized') {
225+
Logger.debug('Git API is still initializing, will wait for state change event')
226+
} else {
227+
Logger.appendLine('No Git repositories found in workspace')
221228
}
222229

223230
// coverage decoration

src/git/CodacyCloud.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export class CodacyCloud implements vscode.Disposable {
9090
private _cli: CodacyCli | undefined
9191

9292
private _disposables: vscode.Disposable[] = []
93+
private _stateChangeDisposable: vscode.Disposable | undefined
9394

9495
constructor() {
9596
vscode.commands.executeCommand('setContext', RM_STATE_CONTEXT_KEY, this._state)
@@ -103,7 +104,56 @@ export class CodacyCloud implements vscode.Disposable {
103104
this._current = gitRepository
104105

105106
try {
107+
// Check if repository state is fully populated
108+
if (!gitRepository.state.HEAD && gitRepository.state.remotes?.length === 0) {
109+
Logger.debug('Repository state is not fully populated yet, waiting for state change...')
110+
this.state = CodacyCloudState.Initializing
111+
112+
// Clean up any existing state change listener
113+
if (this._stateChangeDisposable) {
114+
this._stateChangeDisposable.dispose()
115+
this._stateChangeDisposable = undefined
116+
}
117+
118+
// Set up timeout to prevent memory leak
119+
const timeoutMs = 30000
120+
let hasRepositoryStateTimedOut = false
121+
const repositoryStateTimeout = setTimeout(() => {
122+
hasRepositoryStateTimedOut = true
123+
Logger.appendLine(`Repository state change timeout after ${timeoutMs}ms, assuming repository is not valid`)
124+
if (this._stateChangeDisposable) {
125+
this._stateChangeDisposable.dispose()
126+
this._stateChangeDisposable = undefined
127+
}
128+
this.state = CodacyCloudState.NoGitRepository
129+
}, timeoutMs)
130+
131+
// Listen for state changes to detect when repository is fully loaded
132+
this._stateChangeDisposable = gitRepository.state.onDidChange(() => {
133+
if (hasRepositoryStateTimedOut) return // Don't process if we've already timed out
134+
135+
Logger.appendLine(
136+
`Repository state changed - HEAD: ${gitRepository.state.HEAD?.name || 'undefined'}, Remotes: ${
137+
gitRepository.state.remotes?.length || 0
138+
}`
139+
)
140+
if (gitRepository.state.HEAD || gitRepository.state.remotes?.length > 0) {
141+
clearTimeout(repositoryStateTimeout)
142+
if (this._stateChangeDisposable) {
143+
this._stateChangeDisposable.dispose()
144+
this._stateChangeDisposable = undefined
145+
}
146+
openRepository()
147+
}
148+
})
149+
150+
// Add to disposables for cleanup
151+
this._disposables.push(this._stateChangeDisposable)
152+
return
153+
}
154+
106155
if (gitRepository.state.HEAD === undefined) {
156+
Logger.appendLine('Repository HEAD is undefined but remotes are available')
107157
this.state = CodacyCloudState.Initializing
108158
} else {
109159
const remotesWithPushUrl = gitRepository.state.remotes.filter((remote) => remote.pushUrl)
@@ -473,6 +523,14 @@ export class CodacyCloud implements vscode.Disposable {
473523

474524
public close(repository: GitRepository) {
475525
if (this._current === repository) {
526+
Logger.appendLine(`CodacyCloud close: ${repository.rootUri.fsPath}`)
527+
528+
// Clean up state change listener when repository is closed
529+
if (this._stateChangeDisposable) {
530+
this._stateChangeDisposable.dispose()
531+
this._stateChangeDisposable = undefined
532+
}
533+
476534
this.clear()
477535
}
478536
}
@@ -491,7 +549,6 @@ export class CodacyCloud implements vscode.Disposable {
491549

492550
public refresh() {
493551
if (!this._current) return
494-
495552
switch (this._state) {
496553
case CodacyCloudState.Loaded:
497554
case CodacyCloudState.IsAnalyzing:

src/git/GitProvider.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ export class GitProvider implements IGitProvider {
6767
static async init(): Promise<GitProvider | undefined> {
6868
const extension = vscode.extensions.getExtension<GitExtension>('vscode.git')
6969
if (extension) {
70-
await extension.activate()
71-
GitProvider._instance = new GitProvider(extension)
70+
try {
71+
await extension.activate()
72+
GitProvider._instance = new GitProvider(extension)
73+
Logger.appendLine(`GitProvider initialized successfully. Extension enabled: ${extension.exports?.enabled}`)
7274

73-
return GitProvider._instance
75+
return GitProvider._instance
76+
} catch (error) {
77+
Logger.error(`Failed to initialize GitProvider: ${error}`)
78+
return undefined
79+
}
80+
} else {
81+
Logger.error('VS Code Git extension not found')
7482
}
7583
return undefined
7684
}

0 commit comments

Comments
 (0)