Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: file recovery not working if file has comma in it. don't save not be treated as crash, new project dialog size change #2082

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/document/DocumentCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,7 @@ define(function (require, exports, module) {
* @private - tracks our closing state if we get called again
*/
var _windowGoingAway = false;
let exitWaitPromises = [];

/**
* @private
Expand All @@ -1634,12 +1635,15 @@ define(function (require, exports, module) {

return CommandManager.execute(Commands.FILE_CLOSE_ALL, { promptOnly: true })
.done(function () {
exitWaitPromises = [];
_windowGoingAway = true;

// Give everyone a chance to save their state - but don't let any problems block
// us from quitting
try {
ProjectManager.trigger("beforeAppClose");
// if someone wats to do any deferred tasks, they should add
// their promise to the wait promises list.
ProjectManager.trigger("beforeAppClose", exitWaitPromises);
} catch (ex) {
console.error(ex);
}
Expand Down Expand Up @@ -2016,10 +2020,13 @@ define(function (require, exports, module) {
_isReloading = true;

return CommandManager.execute(Commands.FILE_CLOSE_ALL, { promptOnly: true }).done(function () {
exitWaitPromises = [];
// Give everyone a chance to save their state - but don't let any problems block
// us from quitting
try {
ProjectManager.trigger("beforeAppClose");
// if someone wats to do any deferred tasks, they should add
// their promise to the wait promises list.
ProjectManager.trigger("beforeAppClose", exitWaitPromises);
} catch (ex) {
console.error(ex);
}
Expand All @@ -2037,7 +2044,8 @@ define(function (require, exports, module) {

// Defer for a more successful reload - issue #11539
window.setTimeout(function () {
raceAgainstTime(window.PhStore.flushDB()) // wither wait for flush or time this out
exitWaitPromises.push(window.PhStore.flushDB());
raceAgainstTime(Promise.all(exitWaitPromises)) // wither wait for flush or time this out
.finally(()=>{
raceAgainstTime(_safeNodeTerminate(), 4000)
.finally(()=>{
Expand Down Expand Up @@ -2210,7 +2218,8 @@ define(function (require, exports, module) {
event.preventDefault();
_handleWindowGoingAway(null, closeSuccess=>{
console.log('close success: ', closeSuccess);
raceAgainstTime(_safeFlushDB())
exitWaitPromises.push(_safeFlushDB());
raceAgainstTime(Promise.all(exitWaitPromises))
.finally(()=>{
raceAgainstTime(_safeNodeTerminate())
.finally(()=>{
Expand Down
23 changes: 18 additions & 5 deletions src/extensionsIntegrated/NavigationAndHistory/FileRecovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,25 @@ define(function (require, exports, module) {
if(!input){
return null;
}
const parts = input.split(',', 2);
// Find the first comma
const firstCommaIndex = input.indexOf(',');

if (parts.length !== 2) {
// If there's no comma or it's at the very beginning, the input is invalid
if (firstCommaIndex === -1 || firstCommaIndex === 0) {
return null;
}

// Extract the parts
const expectedLengthPart = input.slice(0, firstCommaIndex);
const actualString = input.slice(firstCommaIndex + 1);

// Parse the length part (should be the first part before the comma)
const expectedLength = parseInt(parts[0], 10);
const expectedLength = parseInt(expectedLengthPart, 10);
if (isNaN(expectedLength)) {
return null;
}

// The second part is the actual string after the comma
const actualString = parts[1];
// The second part is the actual string after the commas
if (actualString.length === expectedLength) {
return actualString;
}
Expand Down Expand Up @@ -471,6 +476,14 @@ define(function (require, exports, module) {
// for the startup project. So we call manually.
projectOpened(null, currentProjectRoot);
}
ProjectManager.on("beforeAppClose", (_evt, waitPromises)=>{
// this is a safe exit, we should delete all restore data.
let exitProjectRoot = ProjectManager.getProjectRoot();
if(exitProjectRoot) {
const restoreRoot = getProjectRestoreRoot(exitProjectRoot.fullPath);
waitPromises.push(silentlyRemoveDirectory(restoreRoot));
}
});
}

function init() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="modal" style="background-color:#3C3F41; width:min(50%, 850px); height: 570px; display: flex; flex-direction: column; justify-content: space-between">
<div class="modal new-project-modal-dialog">
<iframe title="New Project" id="newProjectFrame" width="100%" height="100%"
src={{newProjectURL}} style="border: 0px;visibility:hidden;" onload="this.style.visibility = 'visible';">
</iframe>
Expand Down
9 changes: 9 additions & 0 deletions src/styles/brackets.less
Original file line number Diff line number Diff line change
Expand Up @@ -3548,3 +3548,12 @@ label input {
opacity: 1;
}
}

.new-project-modal-dialog{
background-color:#3C3F41;
width: 750px;
height: 570px;
display: flex;
flex-direction: column;
justify-content: space-between
}
16 changes: 8 additions & 8 deletions test/spec/Extn-NavigationAndHistory-integ-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ define(function (require, exports, module) {
const tempRestorePath = SpecRunnerUtils.getTestRoot() + "/navigationTestsRestore/";

let FileViewController, // loaded from brackets.test
ProjectManager, // loaded from brackets.test;
CommandManager,
Commands,
testWindow,
Expand All @@ -57,7 +56,6 @@ define(function (require, exports, module) {
brackets = testWindow.brackets;
$ = testWindow.$;
FileViewController = brackets.test.FileViewController;
ProjectManager = brackets.test.ProjectManager;
CommandManager = brackets.test.CommandManager;
Commands = brackets.test.Commands;
EditorManager = brackets.test.EditorManager;
Expand All @@ -76,7 +74,6 @@ define(function (require, exports, module) {

afterAll(async function () {
FileViewController = null;
ProjectManager = null;
testWindow = null;
brackets = null;
await deletePath(testPath);
Expand Down Expand Up @@ -124,13 +121,16 @@ define(function (require, exports, module) {

}

// multiple , in file tested as we have , for `size,"content"` integrity checks
const unsavedTextFragment = "hello,this is ,a test";

it("Should create restore folders and backup files", async function () {
await initFileRestorer("file.js");
let projectRestorePath = testWindow._FileRecoveryExtensionForTests.getProjectRestoreRoot(testPath);

// now edit a file so that its backup is created
let editor = EditorManager.getActiveEditor();
editor.document.setText("hello");
editor.document.setText(unsavedTextFragment);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath, true);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + "file.js", false);
await closeSession();
Expand All @@ -142,7 +142,7 @@ define(function (require, exports, module) {

// now edit a file so that its backup is created
let editor = EditorManager.getActiveEditor();
editor.document.setText("hello");
editor.document.setText(unsavedTextFragment);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + "toDelete1/file.js", false);
await awaitsForDone(CommandManager.execute(Commands.FILE_SAVE_ALL), "saving all file");
await SpecRunnerUtils.waitTillPathNotExists(projectRestorePath.fullPath + "toDelete1/file.js", false);
Expand All @@ -154,7 +154,7 @@ define(function (require, exports, module) {
let projectRestorePath = testWindow._FileRecoveryExtensionForTests.getProjectRestoreRoot(testPath);

// now edit a file so that its backup is created
const unsavedText = "hello" + Math.random();
const unsavedText = unsavedTextFragment + Math.random();
let editor = EditorManager.getActiveEditor();
editor.document.setText(unsavedText);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + "toDelete1/file.js", false);
Expand Down Expand Up @@ -186,7 +186,7 @@ define(function (require, exports, module) {
let projectRestorePath = testWindow._FileRecoveryExtensionForTests.getProjectRestoreRoot(testPath);

// now edit a file so that its backup is created
const unsavedText = "hello" + Math.random();
const unsavedText = unsavedTextFragment + Math.random();
let editor = EditorManager.getActiveEditor();
editor.document.setText(unsavedText);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + fileNameToRestore, false);
Expand Down Expand Up @@ -343,7 +343,7 @@ define(function (require, exports, module) {
await openFile(file3);
await _expectNavButton(false, true, "nav forward disabled due to new file open");
}

it("Should navigate back and forward between text files", async function () {
await navigateResetStack();
await _validateNavForFiles("test.css", "test.html", "test.js");
Expand Down
Loading