Repository navigation
Keep NANSEN user data outside MATLAB's preference directory - #126
ehennestad wants to merge 20 commits into
Conversation
ReviewVerdict: needs rework. The startup migration and Several of the fixes need a design decision first, so no code was changed on this branch. a99005d..dcb6c3f only merges Findings, most severe first:
Unverified:
Description vs code: three claims do not hold. "Puts back the ones moved before it, so the data is never split" (findings 1-2). "Anything else a user keeps in a chosen directory is left alone" (finding 3). "Tries again at the next startup" retries a move that cannot succeed (finding 4). The UI offers a shared folder "to carry this setup between machines", but Tests: Out of scope (exists on Merge notes: this is the second on-disk layout change next to #119–#121. #127 also edits 🤖 Generated with Claude Code |
Review fixesNine of the ten findings from the review are fixed on the branch (head a884c0e). The description is rewritten to match: the three claims the review disputed now hold, and the sentence about carrying the setup between machines is removed.
Tests: the new Not fixed:
Choices made in the fixes, for review:
The merge notes on #127 in the review still apply. 🤖 Generated with Claude Code |
Merge of
|
d3b521c to
d7ad22e
Compare
reset looped over numel(obj.ProjectCache), but ProjectCache is a
containers.Map and numel of a Map is the number of Map objects, which is
always 1. The loop therefore deleted only the first cached project, and
on an empty cache it indexed keys{1} into an empty cell array and threw.
Loop over the key list instead, whose numel is the number of entries.
The method has no callers yet, so nothing was hitting this. It is the
first thing that a caller clearing the cache would have run into.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The project catalog lived in a "projects" folder under MATLAB's preference directory. That directory belongs to a single MATLAB release, so upgrading MATLAB left the new release without any projects. Add a ProjectCatalogDirectory user preference naming the directory that holds the catalog. An empty value keeps the previous default, so nothing changes for an installation that does not set it. ProjectManager gains setCatalogDirectory, which moves the catalog and the project configurations stored next to it, then remembers the location. Passing an empty value moves everything back to the preference directory. The move refuses a destination that already holds a project catalog, so an existing catalog is never overwritten, and resolves every entry it will write before moving anything. A move that fails partway puts back what it already moved, so the catalog is never split across two directories. A legacy installation can keep project folders inside the catalog directory; those entries are repointed at their new path. Machine specific project configurations, such as local data root paths and task lists, used to sit directly in the catalog directory, one folder per project. They now live under local/<machineId>, so that a catalog directory placed on a shared or synchronized folder does not have two machines overwriting each other's configurations. The machine key is the identifier the data location model already uses as its SourceID, so there is one notion of machine identity rather than two. Existing installations are migrated to that layout on the next startup; a folder that is a project's own path is left alone, because that is project data rather than configuration. The preference is read from file rather than from the user session, because the session builds its project manager before the session singleton is reachable. Note that the catalog stores absolute project paths. Sharing a catalog between machines therefore still requires the project folders to be reachable at the same paths on both. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The Manage Projects tab now shows where the project catalog is saved and offers a button to move it. The button explains why the location matters, opens a folder dialog, and reports what happened, so the location can be changed without calling setCatalogDirectory from the command line. The table and the new controls share the tab, so positioning them is one step: the table reserves a strip along the bottom, and the controls are placed in it. The button keeps a fixed width and the label takes the remainder, with the full path in its tooltip because a long preference directory path does not fit in a small window. setProjectTablePosition read the InnerPosition of the second tab while the table it positions is on the third. Sibling tabs report the same InnerPosition, so this went unnoticed. Look the tab up by title instead, so the table and the new controls share one reference. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…er UI The label and the dialog spelled out the catalog directory as a path. In the default location that path is a long preference directory that says little, and it is the location the feature exists to move away from. Report it as "MATLAB's preference folder" instead, and keep the path in the label tooltip and in the dialog body. The dialog explains in that case that the folder belongs to a single MATLAB release, which is the reason to move the catalog. A catalog that has been moved is still shown by its path, since that is what identifies it. Add isCatalogDirectoryDefault to answer the question, so that the UI does not derive the default location itself, and give ProjectManager one private helper for that location rather than building it in three places. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
MATLAB's preference directory belongs to a single release. A developer who moves between releases gets a separate project catalog, separate project configurations and a separate add-on manifest in each one, and an upgrade only carries them forward by copying the previous release's directory once. Replace the ProjectCatalogDirectory preference, which named a folder inside that directory, with UserDataDirectory, which names the directory itself. It defaults to <userpath>/Nansen/profiles/<profile>, which does not belong to a release, and can be set to any absolute path, including a shared or synchronized folder. The profiles are kept in their own folder so that a profile name can not collide with the Projects, Add-Ons and Backup folders NANSEN already keeps under the userpath. The directory is owned by NansenUserSession rather than ProjectManager, because it holds more than the project catalog. ProjectManager derives its catalog directory from it and is repointed by the session when it moves. Existing data is moved out of the preference directory on the next startup, and project folders kept inside it are repointed in the catalog. A move that fails leaves the data where it is and records the preference to match, so a failed migration cannot start NANSEN with an empty project catalog. Preference files written before this can name preferences that no longer exist, so loading now skips unknown names rather than erroring on them. Move the path predicates and the folder move into nansen.util.path and nansen.internal.system, where the session and the project manager can both reach them and where they can be tested directly. Two locations that were still release specific: autoUpdateProjectPath defaulted new project folders to a "projects" folder in the preference directory rather than to DefaultProjectPath. The settings and custom_options folders, and the add-on manifest, still live in the preference directory. They move in a later change. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The settings and custom_options folders held user configuration in the preference directory, so they were left behind on a MATLAB upgrade like the project catalog was. Move them into the user data directory, and migrate existing ones on the next startup. Both hold plain user preferences: app behaviour, metatable column layout, and the option presets for session methods. None of them name a path or any other state of one machine, so they are shared when the user data directory is. Two files are not like that. The task list names queued work and the watch folder catalog names paths, both belonging to the machine they were made on, and both resolved under the settings folder. Sharing that folder would have shared them too. Resolve them under a machine specific directory instead, and move them there during the migration. That directory moves up out of the catalog folder to nansen.localdatadir, since it now holds more than project configurations. The project configurations keep their own subfolder within it. The machine identifier moves to nansen.internal.system.getMachineIdentifier, next to the other system helpers, since the project manager is no longer its only caller. nansen.localpath no longer caches entries that resolve under the user data directory, because that directory can move while MATLAB is running. The user_data keyword named the preference directory, which is no longer where the user's data is kept. The add-on manifest needed nothing. It is already written to the add-on folder under the userpath, which does not belong to a MATLAB release, and the copies in the preference directory are read only leftovers of that earlier migration. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Review of the branch found two defects that would fail its own tests, a guarantee the migration did not keep, and a test fixture that leaked. setUserDataDirectory failed on every fresh target. The check for an existing catalog called getCatalogPath, which created the catalog folder as a side effect, so the move that followed found its own target already present and refused. getCatalogPath no longer creates anything; the folder is created where it is needed, in saveCatalog. The startup migration resolved the machine specific folder through the session singleton, which is not registered until the constructor has returned, so it resolved for the anonymous user and put the task list in the wrong profile. It now derives the folder from the directory it was given; localdatadir takes that directory as an optional argument. The migration promised not to start NANSEN with an empty catalog after a failed move, but only rolled back within one folder. A failure on the second folder left the first already moved. The session now moves a named list of folders as a unit and puts back the completed ones on failure. Moving named folders rather than the directory also keeps the preference file in place when the source is the preference directory, and leaves alone anything else a user keeps in a directory they chose. A failed migration no longer pins the preference to the preference directory, which was permanent despite the message saying "for now". The session uses the old location for this run only and the move is attempted again at the next startup. ProjectFixture removed the test profile's preference directory but not its user data directory, so every run left a folder under the userpath. Resolution no longer reads the preference file on every path lookup. An active session answers from memory; the file is read only when there is no session. The empty userpath warning moves out of the resolver, which runs on every lookup, into the constructor, which runs once. ProjectManager keeps the directory it was built for and derives its locations from it, rather than re-resolving through the session. The repointing of project paths that moved with a directory is one function shared by the manager and the migration. The Manage Projects controls are named for the user data location they manage, the label shows the path since the default is short now, and stale references to configurations living in the catalog folder are corrected. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
stripTrailingSeparator turned Z:\ into Z:, which names the current folder on drive Z and which isAbsolutePath rejects, so a drive root could not be chosen as the user data directory. A separator after a drive letter is now kept. isSamePath compared case-sensitively on macOS, whose default APFS volumes do not distinguish case. It now ignores case on macOS as on Windows, and isSubPath follows the same rule. isSubPath appended a separator to a root that already ends with one, so no path was inside / or Z:\, and it reported a path with a trailing separator as inside the folder that path names. Both are fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rollback after a failed move called moveFolderContents with the directories swapped, which moves every entry of the target folder, not only the entries the move put there. It also visited folders that were skipped, because numMoved only recorded the highest index reached, and numMoved was set after the rmdir of the emptied source folder, so a failed rmdir was not undone. A release whose preference directory held an empty projects folder and a settings file that another release had already moved could carry that release's project catalog back into its own preference directory. moveFolderContents now returns the names of the entries it moved, and moves only named entries when given EntryNames. The folder move is a function, nansen.internal.user.moveUserDataFolders, that checks every entry of every folder for a collision before anything moves, records the moved folders in a logical array before removing the emptied source folder, and moves back exactly the entries it moved. setUserDataDirectory now refuses a directory that is inside the current one or contains it, and a directory that already contains a projects, settings, custom_options or local entry, through nansen.internal.user.validateUserDataTarget. The first stops a folder from being moved into itself, as when <current>/local is chosen. The second stops the data from being merged into a folder of the user's own, and two profiles from sharing a directory. The folder dialog opens at the parent of the current directory, and no longer suggests that a synchronized folder carries the setup between machines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A release whose preference directory still held its own catalog collided with the catalog that another release, or another machine sharing the directory, had already moved to the user data directory. The move failed, the session fell back to the preference directory, and the move was attempted, and failed, at every startup. In that state TaskList and WatchFolderCatalog resolved to <prefdir>/local/<id> while the files stayed in settings, because the machine local files were only moved after a successful migration. The startup migration now goes through nansen.internal.user.migrateUserDataFolders. A folder that the user data directory already holds with content is used as it is, and this release's copy is renamed to <name>.unmigrated in the preference directory instead of merged. An empty copy is removed. The other folders move as before, and a failure undoes the renames along with the moves. The preference directory is left without user data folders, so the migration does not run again. One warning names the renamed folders and says how to add their projects to the catalog in use. moveMachineLocalSettings is a function and runs at every startup in the directory the session uses, so the task list and the watch folder catalog are where nansen.localpath looks for them, also when the preference directory is used. It no longer overwrites a file that the machine folder already holds. Catalog paths are repointed only when this release's catalog was the one moved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The UserDataDirectory preference is read from <prefdir>/Nansen/<profile>, which belongs to one MATLAB release. A location chosen in one release was unknown to the others: they resolved the default directory, moved their own copy of the data into it, and reported that the data was moved. When setUserDataDirectory chooses a directory other than the default, it now also writes that directory to user_data_redirect.txt in the default directory, <userpath>/Nansen/profiles/<profile>, which every release reads. A release whose own preference is unset uses the directory that the file names, if it is an absolute path to an existing folder. Moving the data back to the default removes the file. Without a userpath the default directory is the preference directory, and no file is written. nansen.internal.user.UserDataRedirect reads and writes the file. The session reads the redirect file once at startup. If the file names a folder that does not exist, for example on a drive that is not connected, the session warns, uses the default directory for that session, and moves nothing out of the preference directory, so that the data is not moved to a directory the next session will not use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
getMachineIdentifier hashed the address that ifconfig reports for en0, through utility.system.getComputerName. With private Wi-Fi addressing on, that is an address macOS assigns per Wi-Fi network, so local/<id> changed with the network, and the task list, the watch folder catalog and the local project configurations were no longer found. On macOS the identifier is now the MD5 hash of the IOPlatformUUID that /usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice reports, in the same 32 character form as before. If ioreg reports no UUID, and on other platforms, the identifier is still utility.system.getComputerName(true). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without a userpath the default user data directory is this release's preference directory. After a startup migration that directory no longer holds the data, so the catalog appeared empty, while the warning said that NANSEN keeps its data there. The session now records the directory it uses in a LastUserDataDirectory preference, whenever that is not the preference directory, at startup and after setUserDataDirectory. With an empty userpath the warning names the preference directory as the one used and the recorded directory as the one that holds the earlier data, and says to set the userpath again. Without a record the warning is unchanged. The preference is hidden from the preference display, because the session sets it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
clear_tutorial_data removed only the tutorial profile's preference directory, while its catalog and configurations now live in <userpath>/Nansen/profiles/tutorial. The script now removes that directory as well, unless it is the preference directory, as it is when userpath is empty. Either removal is skipped when its folder is missing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…a session The task list and the watch folder catalog are moved out of the settings folder at every startup. An error there, for example from a settings folder that can not be written, or from resolving the machine identifier without a Java virtual machine, stopped the user session from being created. The session now reports such an error as a warning and starts. moveMachineLocalSettings also returns before resolving the machine folder when the settings folder holds neither file, which is the case at most startups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without a userpath the default user data directory is the preference directory, and setUserDataDirectory writes no redirect file, so the test can not check one. It is filtered in that case, like the test of the default location. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
listUserNames, from #127, listed the folders in prefdir/Nansen, which belongs to one MATLAB release. A profile created in another release has only its user data directory, <userpath>/Nansen/profiles/<name>, in this one. The confirmation prompt then called it unknown, and the capitalisation check could not see it: nansen("John") could create a profile next to an existing "john" from another release, and on macOS and Windows the two used the same user data directory. listUserNames now also lists the default user data root. The path of that root moves into getDefaultUserDataRoot, which getDefaultUserDataDirectory also uses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The default user data directory is under MATLAB's userpath. With an empty userpath the session fell back on the preference directory, which belongs to one MATLAB release. A fresh MATLAB never reached the fallback, because the add-on manager requires a userpath. Where it was reached, setUserDataDirectory wrote no redirect file and gave no warning. An empty userpath now stops the session with NANSEN:UserSession:EmptyUserpath. The check comes before the profile's preference directory is created, so a failed start leaves no profile behind. The fallback, its startup warning and the LastUserDataDirectory preference are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The help text of NansenUserSession.instance placed a profile's projects in its preference directory. They are in its user data directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MATLAB applies a pulled change to a running session at once, so the session looks for its data where the new code expects it before the startup that moves the data has run. A file saved in that window made the next startup treat the new location as data another release had moved there, and set the real folder aside as <name>.unmigrated. A session now stores the layout version in the user data directory. A directory with neither that version nor a project catalog has not been set up, so what it holds is left over from a running session: those folders are renamed to <name>.leftover and the real data moves into place. The version follows the data in setUserDataDirectory. A session compares the current layout version with the one it started with and tells the user once to run nansen.restart. saveCatalog takes the folder from CatalogPath, because a session started by older code has no CatalogDirectory and every save failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
e30694c to
44abca3
Compare
Problem
MATLAB's preference directory belongs to a single MATLAB release. A developer moving between releases gets a separate project catalog, separate settings and separate session-method option presets in each one. An upgrade only carries them forward because MATLAB copies the previous release's directory once; moving back and forth diverges.
Change
A
UserDataDirectorypreference names the directory holding this user's NANSEN data. It defaults to<userpath>/Nansen/profiles/<profile>, which does not belong to a MATLAB release, and can be set to any absolute path that is not inside the current directory, does not contain it, and has noprojects,settings,custom_optionsorlocalentry of its own.A session requires a userpath, because the default directory is under it. With an empty
userpath(),NansenUserSessionstops with the errorNANSEN:UserSession:EmptyUserpathbefore it creates the profile's preference directory or moves any data. The default add-on folder,DefaultProjectPathand the backup paths are also under the userpath.The preference is saved in one release's preference directory, so
setUserDataDirectoryalso writes a directory other than the default touser_data_redirect.txtin the default directory, which every release reads. A release without its ownUserDataDirectorypreference uses the directory that file names, if it is an absolute path to an existing folder.setUserDataDirectory("")removes the file.The Manage Projects tab shows the current location and offers a
Change Location...button that does the same through a folder dialog, which opens at the parent of the current directory.The directory is owned by
NansenUserSession, notProjectManager, because it holds more than the project catalog.nansen.userdatadirresolves it, mirroringnansen.prefdir;nansen.localdatadirresolves the machine specific area within it.Profiles get their own folder under the userpath so a profile name cannot collide with the
Projects,Add-OnsandBackupfolders NANSEN already keeps there. Each profile resolves independently.Layout
What is shared and what is not
settings/andcustom_options/hold plain user preferences — app behaviour, metatable column layout, and the option presets for session methods. None name a path or any other state of one machine.The task list and the watch folder catalog are not like that: one names queued work, the other names paths, both belonging to the machine they were made on. They resolved under
settings/. They now resolve underlocal/<machineId>, and every startup moves them there fromsettings/if they are found there. A file that the machine folder already holds is not overwritten.<machineId>is the MD5 hash of the hardware UUID (IOPlatformUUIDfromioreg) on macOS, which does not change with the Wi-Fi network, andutility.system.getComputerName(true)on other platforms.The add-on manifest needed nothing. It is already written to
<userpath>/Nansen/Add-Ons/installed_addons.json, which does not belong to a release, andensureAddonDependenciesOnPathre-adds add-ons to the search path each session. The copies in the preference directory are read-only leftovers of that earlier migration.Migration — please read
Existing data is moved out of the preference directory on the next startup, per profile, as each profile's session is opened. When this release's catalog is the one moved, its entries for project folders inside the preference directory are repointed to the user data directory.
The session moves a named list of folders —
projects,settings,custom_options,local— never the directory itself, so the preference file that names the location stays put. Every entry of every folder is checked for a name collision at the target before anything moves. If a move fails, exactly the entries that were moved go back, so the data is never split across two directories; the session then uses the preference directory for that run, leaves the preference unset, and tries again at the next startup.If the user data directory already holds one of these folders with content, because another release or a machine sharing the directory migrated first, that data is used. This release's copy of the folder is renamed to
<name>.unmigratedin the preference directory instead of being merged into it, and an empty copy is removed. One warning names the renamed folders and says how to add their projects to the catalog in use. The preference directory is then left without user data folders, so the migration does not run again.A pull takes effect in a running session at once, because MATLAB applies changed class files to objects that already exist. The session then looks for its data in the user data directory before the startup that moves the data has run. The session warns once, with
NANSEN:UserSession:UpdatedWhileRunning, to runnansen.restart. Until the restart, settings and option presets can look missing, and looking up a project's local folder fails. The rule above applies to a directory that a session has set up, which is one holdinguser_data_layout_version.txtor a project catalog. Anything in a directory that has neither was left by such a running session: each folder there is renamed to<name>.leftover, the folder from the preference directory takes its place, and a warning names the renamed folders.If the redirect file names a folder that does not exist, for example on a drive that is not connected, the session warns, uses the default directory for that run, and moves nothing out of the preference directory.
setUserDataDirectorymoves the same folders with the same all-or-nothing rule. It refuses a directory that contains any of them, so the data is never merged into a folder of the user's own, and other files in either directory stay where they are, exceptuser_data_layout_version.txt, which follows the data.Also in here
NansenUserSession.listUserNamesalso lists the folders in<userpath>/Nansen/profiles. The prompt thatnansenshows for an unknown profile name, and its refusal of a name that differs from an existing profile only by capitalisation, then also see a profile whose preference directory belongs to another MATLAB release.autoUpdateProjectPathdefaulted new project folders to aprojectsfolder inside the preference directory rather than toDefaultProjectPath, so project data could be created under the preference directory.nansen.localpathno longer caches entries resolving under the user data directory, since that directory can move while MATLAB is running.Path predicates are
nansen.util.path.isSamePath/isSubPath/isAbsolutePath/stripTrailingSeparator.isSamePathandisSubPathignore case on Windows and macOS, and a drive root such asZ:\keeps its separator.nansen.internal.system.moveFolderContentsreturns the entries it moved and can move a named subset of entries back. The move, the startup migration, the check of a chosen directory, the redirect file and the move of the machine local files arenansen.internal.user.moveUserDataFolders,migrateUserDataFolders,validateUserDataTarget,UserDataRedirectandmoveMachineLocalSettings, whichUserDataFoldersTesttests on temporary folders without a user session.nansen.internal.user.UserDataLayoutholds the layout version, which a later migration reads to know where to start.Earlier commits in this branch
ProjectManager.resetlooped overnumel(obj.ProjectCache).ProjectCacheis acontainers.Mapandnumelof a Map is always 1, so it deleted only the first cached project and threw on an empty cache.setProjectTablePositionread theInnerPositionof the second tab while the table it positions is on the third.Known limitations
The catalog stores absolute project paths, and
LocalRootPathManagerremaps data root paths per machine withutility.system.getComputerName, a different key from<machineId>on macOS. Sharing a directory between machines needs those two mechanisms converged and the catalog's project paths routed through them. That is not in this change, andsetUserDataDirectoryrefuses a directory that already holds another setup's data, so a second machine cannot adopt a shared one.A release with its own
UserDataDirectorypreference does not read the redirect file. If another release moves the data again, that preference still names the old directory.setUserDataDirectorydeselects the current project during the move. With the App open, the App'sCurrentProjectSetlistener closes the task list, which stops a running task without the prompt thatApp.changeProjectshows. Changing project from the Manage Projects dialog does the same ondev.🤖 Generated with Claude Code