Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[Full changelog](https://github.com/mozilla/glean.js/compare/v2.0.3...main)

* [#1772](https://github.com/mozilla/glean.js/pull/1772): Fix bug where `window.Glean` functions were getting set on non-browser properties.

# v2.0.3 (2023-09-27)

[Full changelog](https://github.com/mozilla/glean.js/compare/v2.0.2...v2.0.3)
Expand Down
24 changes: 24 additions & 0 deletions automation/compat/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ export async function runWebTest(driver) {
await driver.get(`http://localhost:${PORT}/`);
// Give it time to send the ping request.
const successTextContainer = await driver.findElement(By.id("msg"));

const areGleanWindowVarsSet = await driver.executeScript(() => {
// Verify that all Glean `window` vars are properly set.
if (
window &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rosahbruno Might this need to be typeof window !== "undefined"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are actually our browserstack tests. So for this scenario, we want our tests to fail if for some reason we can't find the window object.

The place where we believe this is coming from is fixed in this PR. For whatever reason, our helper function isUndefined isn't working right and is actually leading to the error being thrown. As a fix, we just check directly against window instead.

I tested this by pulling down MDN and running yarn dev with v2.0.4 and reproducing the error. When I retested and did the same thing with the code I pushed to that new branch, I was able to successfully run yarn dev and run the app locally.

Hopefully this should resolve the errors you are seeing whenever we cut a new release!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you! 🙌

Copy link
Contributor

@caugner caugner Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: My gut feeling says that passing an undefined variable to isUndefined() just throws an error before you get into the function, because it uses an undefined variable, whereas an explicit typeof varname !== "undefined" never throws.

window.Glean &&
window.Glean.setDebugViewTag &&
window.Glean.setLogPings &&
window.Glean.setSourceTags
) {
return true;
}

// One of our expected values wasn't set, so the test should fail.
return false;
}).catch(() => {
// In case `window` isn't available or something unexpected happens.
return false;
});

if (!areGleanWindowVarsSet) {
throw new Error("`window.Glean` variables are not set.");
}

await driver.wait(
until.elementTextIs(
successTextContainer,
Expand Down
13 changes: 8 additions & 5 deletions glean/src/core/glean/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,13 @@ declare global {
}
}

window.Glean = {
setLogPings: Glean.setLogPings,
setDebugViewTag: Glean.setDebugViewTag,
setSourceTags: Glean.setSourceTags
};
// Only set `Glean` values whenever running inside of a browser.
if (typeof window !== "undefined") {
window.Glean = {
setLogPings: Glean.setLogPings,
setDebugViewTag: Glean.setDebugViewTag,
setSourceTags: Glean.setSourceTags
};
}

export default Glean;