Skip to content

Commit 54c557c

Browse files
committed
chore: use default project at startup if last project crashed
1 parent 8d48f0d commit 54c557c

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

src/project/ProjectManager.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,11 @@ define(function (require, exports, module) {
910910
return startupProjectPath;
911911
}
912912
startupProjectPath = updateWelcomeProjectPath(PreferencesManager.getViewState("projectPath"));
913-
const dirExists = await _dirExists(startupProjectPath);
914-
if(dirExists){
915-
return startupProjectPath;
913+
if(startupProjectPath && _isProjectSafeToStartup(startupProjectPath)){
914+
const dirExists = await _dirExists(startupProjectPath);
915+
if(dirExists){
916+
return startupProjectPath;
917+
}
916918
}
917919
return getWelcomeProjectPath();
918920
}
@@ -1914,12 +1916,39 @@ define(function (require, exports, module) {
19141916
.setEnabled(!Phoenix.VFS.isLocalDiscPath(projectRoot.fullPath));
19151917
}
19161918

1919+
const UNSAFE_PROJECT_EXIT_PREFIX = "_Unafe_project_exit_";
1920+
function _flagProjectNotExitedSafely(projectRootPath) {
1921+
// we store this in local storage as these checks happen in app exit/startup flows where
1922+
// phstore is not reliable. It's ok to lose this data.
1923+
localStorage.setItem(UNSAFE_PROJECT_EXIT_PREFIX+projectRootPath, "true");
1924+
}
1925+
function _flagProjectExitedSafely(projectRootPath) {
1926+
localStorage.removeItem(UNSAFE_PROJECT_EXIT_PREFIX+projectRootPath);
1927+
}
1928+
function _isProjectSafeToStartup(projectRootPath) {
1929+
// In some cases, For eg: user tries to open a whole drive with phcode (or any project that makes phcode crash),
1930+
// phoenix may get stuck and never open again with the bad project as we try to open the same bad project
1931+
// on restart. So we keep a safe exit flag with each project. We only start the project on boot if it has
1932+
// been marked safe at previous exit or project switch. So on unsafe exit, the default project will be opened.
1933+
const unsafeExit = (localStorage.getItem(UNSAFE_PROJECT_EXIT_PREFIX+projectRootPath) === "true");
1934+
return !unsafeExit;
1935+
}
1936+
19171937
exports.on(EVENT_PROJECT_OPEN, (_evt, projectRoot)=>{
19181938
_reloadProjectPreferencesScope();
19191939
_saveProjectPath();
19201940
_setProjectDownloadCommandEnabled(_evt, projectRoot);
1941+
_flagProjectNotExitedSafely(projectRoot.fullPath);
1942+
});
1943+
1944+
exports.on(EVENT_PROJECT_CLOSE, (_evt, projectRoot)=>{
1945+
_flagProjectExitedSafely(projectRoot.fullPath);
1946+
});
1947+
exports.on("beforeAppClose", ()=> {
1948+
_saveProjectPath();
1949+
_flagProjectExitedSafely(getProjectRoot().fullPath);
1950+
_unwatchProjectRoot();
19211951
});
1922-
exports.on("beforeAppClose", _unwatchProjectRoot);
19231952

19241953
// Due to circular dependencies, not safe to call on() directly for other modules' events
19251954
EventDispatcher.on_duringInit(FileViewController, "documentSelectionFocusChange", _documentSelectionFocusChange);

0 commit comments

Comments
 (0)