microsoft / azure-devops-extension-sdk

Client SDK for developing Azure DevOps extensions
MIT License
129 stars 40 forks source link

SDK.getWebContext() failing "before init() was complete." #103

Open jakobsack-tag opened 10 months ago

jakobsack-tag commented 10 months ago

I'm trying to implement a dashboard widget using the azure-devops-extension-sdk and azure-devops-extension-api libraries. However, the widget fails to load on Azure DevOps. Whenever I'm trying to call SDK.getWebContext() I'm getting the error Attempted to call getWebContext() before init() was complete. Wait for init to complete or place within a ready() callback.

This error even appears after adding

await SDK.ready();
const context = SDK.getWebContext();

To me it looks like the change introduced with #89 actually raised the issue, at least in my context. Intercepting the handshake reveals that handshakeData.context does not contain a key pageContext, however there is a full key handshakeData.pageContext.

Locally I could fix the issue by applying this patch:

diff --git a/src/SDK.ts b/src/SDK.ts
index cdf2c6e..6694a94 100644
--- a/src/SDK.ts
+++ b/src/SDK.ts
@@ -217,6 +217,7 @@ interface IExtensionHandshakeOptions extends IExtensionInitOptions {
 }

 interface IExtensionHandshakeResult {
+    pageContext?: IPageContext;
     contributionId: string;
     context: {
         extension: IExtensionContext,
@@ -282,7 +283,7 @@ export function init(options?: IExtensionInitOptions): Promise<void> {

         parentChannel.invokeRemoteMethod<IExtensionHandshakeResult>("initialHandshake", hostControlId, [initOptions]).then((handshakeData) => {
             const context = handshakeData.context;
-            hostPageContext = context.pageContext;
+            hostPageContext = context.pageContext || handshakeData.pageContext;
             webContext = hostPageContext ? hostPageContext.webContext : undefined;
             teamContext = webContext ? webContext.team : undefined;

Is this a valid solution? Then I'd create a PR for it. Otherwise, what should I do? Are there any more information you need?

GABRIELNGBTUC commented 8 months ago

Could the maintainers look into this?

Now that we the api repo implemented the necessary types for the dashboard extension to work, we expected being able to author dashboard widgets with the new SDK.

However, the init function pretty much never works (and so do attempts to register a callback to the ready function using //@ts-ignore.

Being able to use the new SDK for dashboard widget has been asked for years and now that we are one step away from the finish line, it would be great to look into this ticket and implement what looks like a 5 min fix.

nigelevansesws commented 8 months ago

I would really appreciate if this can be resolved a.s.a.p. given there's a proposed solution.

ArthurDumas commented 6 months ago

handshakeData.context.pageContext.webContext is now present.

But pageContext doesn't have all the properties of IPageContext (globalization and timeZonesConfiguration are missing) and webContext doesn't have all properties of IWebContext (project and team are missing).

antonychiu2 commented 2 months ago

Any updates here? I'm also getting this same issue

CrovettoFederico commented 1 day ago

Still no updates? Doing workarounds but SDK is not working properly when obtaining most contexts