Stop glfwInit changing the working directory on macOS - #6819
Merged
Conversation
GLFW_COCOA_CHDIR_RESOURCES defaults to true, so glfwInit() chdirs into the .app bundle's Contents/Resources when that directory exists. It runs before anything reads the command line, so every relative path the app is given resolves from inside the bundle: a mesh file, a python script, a credentials file. Nothing here wants that - resources are located through SystemPath, not the working directory. Measured on a packaged MeshInspector.app: a file placed in Contents/Resources opens when passed as ./name.json, while the same file in the working directory does not, and ../../.. from the command line reaches the directory holding the .app. A bundle without Contents/Resources, such as a build tree one, is unaffected because the chdir silently fails, which is why this went unnoticed.
Grantim
approved these changes
Sep 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
One line:
glfwInitHint( GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE )beforeglfwInit(), so the viewer stops changing its own working directory on macOS.Why
GLFW_COCOA_CHDIR_RESOURCESdefaults toGLFW_TRUE, which makesglfwInit()chdirintoContents/Resourcesof the.appbundle -- documented as happening "if present". It runs early inViewer::launchInit_, before anything parsesargv, so every relative path the app is handed resolves from inside the bundle: a mesh file, a.pyscript, a credentials file.Nothing in MeshLib appears to want this. Resources are found through
SystemPath, which derives its directories from the executable location, not from the working directory.How it was found
Chasing why
-creds ./scripts/creds.jsonsilently failed for a packaged MeshInspector while the same argument worked for a build tree one. Probed on a runner against the packaged.app:-creds./probe.json<app>/Contents/Resources/probe.json./probe.json./probe.jsonContents/MacOS./../../../scripts/creds.json<dir holding the .app>/scripts/creds.jsonSo relative paths are not broken, they resolve from
Contents/Resources. The first and fourth rows pin it exactly.A bundle without
Contents/Resourcesis unaffected, because thechdirfails and the inherited working directory survives. That is why this has gone unnoticed: build tree bundles have noContents/Resources, so developers and CI see correct behaviour, and only the packaged app misbehaves.Risk
Behavioural for every macOS consumer of
MRViewer: anything that has come to rely on the working directory beingContents/ResourcesafterlaunchInit_would change. I found nothing that does -- nocurrent_pathorchdircall anywhere in MeshLib or MeshInspectorCode, and resource lookup goes throughSystemPath. Worth a second opinion from someone who knows the macOS packaging history.Test plan
.app, so it is verified downstream after the submodule bump. The check is that a relative path passed to/Applications/MeshInspector.app/Contents/MacOS/MeshInspectorfrom a terminal resolves against the terminal's directory.Other platforms are disabled: the change is inside
#if defined( __APPLE__ ).