Skip to content

fix(mapbox): guard getProjection against throw when map style is not ready - #10551

Merged
chrisgervang merged 6 commits into
visgl:masterfrom
waterWang:fix/getProjection-throw-safety
Oct 11, 2026
Merged

chrisgervang merged 6 commits into
visgl:masterfrom
waterWang:fix/getProjection-throw-safety

Conversation

@waterWang

@waterWang waterWang commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

getProjection() in @deck.gl/mapbox's deck-utils.ts can throw when the underlying map's style is not yet assigned, instead of returning undefined the way callers (getDefaultView / MapboxOverlay#_onAddInterleaved) expect for "not ready yet, default to mercator".

The existing optional-chaining (map.getProjection?.()) only guards against the method being absent, not against the call itself throwing. In maplibre-gl, Map.prototype.getProjection() is return this.style.getProjection() with no guard on this.style, so calling it before the style exists throws a TypeError.

This is reproducible under React 19 StrictMode: the dev-only double-invoke of mount effects adds, removes, and re-adds the same overlay, and the third addControl call hits a still-styleless map.

Fix

Wrap the map.getProjection?.() call in a try/catch and return 'mercator' as the safe default. This matches the callers' existing expectation — getDefaultView already treats any non-'globe' return as mercator.

Related


Note

Low Risk
Small defensive change in projection detection during overlay setup; default behavior matches existing mercator fallback expectations.

Overview
getProjection in @deck.gl/mapbox no longer crashes when the underlying map has no style yet. The map.getProjection?.() call is wrapped in try/catch; on throw (e.g. maplibre-gl before style exists), it returns 'mercator' instead of propagating a TypeError. Globe detection is unchanged once projection info is available.

A unit test asserts mercator fallback when getProjection throws and still returns globe when the style exposes {type: 'globe'}.

Reviewed by Cursor Bugbot for commit 19a7f13. Bugbot is set up for automated code reviews on this repo. Configure here.

…ready

map.getProjection() can throw (e.g. maplibre-gl's implementation calls
this.style.getProjection() without a guard on this.style) when invoked
before the map's style is attached — such as on the third addControl
call under React 19 StrictMode's dev-only double-invoke of mount effects.

The existing optional-chaining only guards against the method being
absent, not against the call itself throwing. Wrap the call in try/catch
and return 'mercator' as the safe default, matching what callers
(getDefaultView / MapboxOverlay#_onAddInterleaved) already expect for the
not-ready-yet case.

Fixes visgl#10549
Comment thread modules/mapbox/src/deck-utils.ts Resolved
@chrisgervang

Copy link
Copy Markdown
Collaborator

Thank you for your contribution! Please include unit regression tests

@coveralls

coveralls commented Aug 19, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 83.518% (-0.01%) from 83.53% — waterWang:fix/getProjection-throw-safety into visgl:master

@chrisgervang chrisgervang mentioned this pull request Oct 7, 2026
53 of 66 tasks
Comment thread modules/mapbox/src/deck-utils.ts Resolved
@greptile-apps

greptile-apps Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

getProjection now returns 'mercator' when the map throws before its style is ready.

  • Projection lookup defaults to mercator when the map is not ready.

Reviews (4) · Last reviewed commit: "Merge branch 'master' into fix/getProjec..." · Reviewed by Greptile

Comment thread modules/mapbox/src/deck-utils.ts Resolved
@chrisgervang
chrisgervang merged commit 539eeac into visgl:master Oct 11, 2026
8 checks passed
chrisgervang added a commit that referenced this pull request Oct 22, 2026
…ready (#10551)

* fix(mapbox): guard getProjection against throw when map style is not ready

map.getProjection() can throw (e.g. maplibre-gl's implementation calls
this.style.getProjection() without a guard on this.style) when invoked
before the map's style is attached — such as on the third addControl
call under React 19 StrictMode's dev-only double-invoke of mount effects.

The existing optional-chaining only guards against the method being
absent, not against the call itself throwing. Wrap the call in try/catch
and return 'mercator' as the safe default, matching what callers
(getDefaultView / MapboxOverlay#_onAddInterleaved) already expect for the
not-ready-yet case.

Fixes #10549

* Apply suggestion from @chrisgervang

* test(mapbox): cover getProjection fallback before style is set

---------

Co-authored-by: waterWang <[email protected]>
Co-authored-by: Chris Gervang <[email protected]>
Co-authored-by: Chris Gervang <[email protected]>
(cherry picked from commit 539eeac)
@chrisgervang

Copy link
Copy Markdown
Collaborator

Released in deck.gl 9.4.1. Thanks @waterWang!

@chrisgervang chrisgervang mentioned this pull request Oct 29, 2026
12 tasks done
chrisgervang added a commit that referenced this pull request Oct 30, 2026
…ready (#10551)

* fix(mapbox): guard getProjection against throw when map style is not ready

map.getProjection() can throw (e.g. maplibre-gl's implementation calls
this.style.getProjection() without a guard on this.style) when invoked
before the map's style is attached — such as on the third addControl
call under React 19 StrictMode's dev-only double-invoke of mount effects.

The existing optional-chaining only guards against the method being
absent, not against the call itself throwing. Wrap the call in try/catch
and return 'mercator' as the safe default, matching what callers
(getDefaultView / MapboxOverlay#_onAddInterleaved) already expect for the
not-ready-yet case.

Fixes #10549

* Apply suggestion from @chrisgervang

* test(mapbox): cover getProjection fallback before style is set

---------

Co-authored-by: waterWang <[email protected]>
Co-authored-by: Chris Gervang <[email protected]>
Co-authored-by: Chris Gervang <[email protected]>
(cherry picked from commit 539eeac)
@chrisgervang

Copy link
Copy Markdown
Collaborator

Released in deck.gl 9.3.12. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getProjection() in @deck.gl/mapbox can throw instead of returning undefined when called before the map's style is assigned

4 participants