Skip to content

Fix removeSource throwing TypeError in Terrain.update under globe projection#13705

Open
mhayk wants to merge 1 commit into
mapbox:mainfrom
mhayk:fix-remove-source-globe-terrain
Open

Fix removeSource throwing TypeError in Terrain.update under globe projection#13705
mhayk wants to merge 1 commit into
mapbox:mainfrom
mhayk:fix-remove-source-globe-terrain

Conversation

@mhayk

@mhayk mhayk commented Jul 15, 2026

Copy link
Copy Markdown

Fixes #13665
Fixes #13703

Summary

Calling map.removeSource(id) on a globe-projection map whose style has no terrain block throws TypeError: Cannot read properties of undefined (reading 'get') synchronously, originating in Terrain.update.

Root cause

Style#_createTerrain left terrain.properties uninitialised — it was only assigned by Terrain#recalculate, which first runs when Style#update is called from Map#_render. Under globe projection, setTerrainForDraping() creates a terrain on style load, so Map#removeSourceMap#_updateTerrainTerrain#update could dereference terrainProps.get('source') before the first render frame evaluated the properties.

Fix

Evaluate the terrain properties synchronously in Style#_createTerrain, so they are always available to consumers regardless of whether a render frame has run yet.

Testing

Added a unit test in test/unit/terrain/terrain.test.ts that reproduces the report (globe projection, style without terrain, addSource + removeSource before the first render). It fails with the exact TypeError from the issue without the fix and passes with it.

Launch Checklist

  • Make sure the PR title is descriptive and preferably reflects the change from the user's perspective.
  • Add additional detail and context in the PR description (with screenshots/videos if there are visual changes).
  • Manually test the debug page.
  • Write tests for all new functionality and make sure the CI checks pass.
  • Document any changes to public APIs.
  • Post benchmark scores if the change could affect performance.
  • Tag @mapbox/map-design-team @mapbox/static-apis if this PR includes style spec API or visual changes.
  • Tag @mapbox/gl-native if this PR includes shader changes or needs a native port.
  • Tag @mapbox/gl-native if this PR disables any test because it also needs to be disabled on their side.
  • Create a ticket for gl-native to groom in the MAPSNAT JIRA queue if this PR includes shader changes or features not present in the native side or if it disables a test that's not disabled there.

…jection

Style#_createTerrain left terrain.properties uninitialised until the first
Style#update ran from Map#_render. Under globe projection, the draping
terrain created on style load could therefore be consumed by Terrain#update
(via Map#removeSource -> Map#_updateTerrain) before the properties were
evaluated, throwing 'TypeError: Cannot read properties of undefined
(reading get)'.

Evaluate the terrain properties synchronously in Style#_createTerrain so
they are always available to consumers.

Fixes mapbox#13665
Fixes mapbox#13703
@mhayk
mhayk requested a review from a team as a code owner July 15, 2026 14:35
@mhayk
mhayk requested review from ibesora and removed request for a team July 15, 2026 14:35
@CLAassistant

CLAassistant commented Jul 15, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown

Hey, @mhayk 👋 Thanks for your contribution to Mapbox GL JS!

Important: This repository does not accept direct merges. All changes go through our internal review process.

What happens next:

  1. A team member will review your PR here first
  2. If it looks good, they will import it to our internal repository for further review
  3. If approved, changes will be synced back here via our release process

Please respond to any review comments on this PR. For more details, see CONTRIBUTING.md.

@ibesora

ibesora commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Thanks for this @mhayk , I'll take a look and report back

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

Labels

None yet

Projects

None yet

3 participants