From f1eaac5cdcde4e49da6a649cd06e1c3669ecc61e Mon Sep 17 00:00:00 2001
From: Paul Slaughter <pslaughter@gitlab.com>
Date: Fri, 20 Nov 2020 15:06:15 -0600
Subject: [PATCH] Handle entry not found in changeFileContent action

This fixes some console errors being thrown
because of mystereious monaco race conditions.
---
 .../javascripts/ide/stores/actions/file.js       |  7 +++++++
 ...0-fix-entry-not-found-change-file-content.yml |  5 +++++
 spec/frontend/ide/stores/actions/file_spec.js    | 16 +++++++++++++---
 3 files changed, 25 insertions(+), 3 deletions(-)
 create mode 100644 changelogs/unreleased/284930-fix-entry-not-found-change-file-content.yml

diff --git a/app/assets/javascripts/ide/stores/actions/file.js b/app/assets/javascripts/ide/stores/actions/file.js
index d96c245d9128..8b43c7238fd9 100644
--- a/app/assets/javascripts/ide/stores/actions/file.js
+++ b/app/assets/javascripts/ide/stores/actions/file.js
@@ -166,6 +166,13 @@ export const getRawFileData = ({ state, commit, dispatch, getters }, { path }) =
 
 export const changeFileContent = ({ commit, state, getters }, { path, content }) => {
   const file = state.entries[path];
+
+  // It's possible for monaco to hit a race condition where it tries to update renamed files.
+  // See issue https://gitlab.com/gitlab-org/gitlab/-/issues/284930
+  if (!file) {
+    return;
+  }
+
   commit(types.UPDATE_FILE_CONTENT, {
     path,
     content,
diff --git a/changelogs/unreleased/284930-fix-entry-not-found-change-file-content.yml b/changelogs/unreleased/284930-fix-entry-not-found-change-file-content.yml
new file mode 100644
index 000000000000..8194806f9c05
--- /dev/null
+++ b/changelogs/unreleased/284930-fix-entry-not-found-change-file-content.yml
@@ -0,0 +1,5 @@
+---
+title: Fix console error being thrown when file is renamed
+merge_request: 48275
+author:
+type: fixed
diff --git a/spec/frontend/ide/stores/actions/file_spec.js b/spec/frontend/ide/stores/actions/file_spec.js
index cc290fc526ee..744ac086b5f6 100644
--- a/spec/frontend/ide/stores/actions/file_spec.js
+++ b/spec/frontend/ide/stores/actions/file_spec.js
@@ -510,8 +510,6 @@ describe('IDE store file actions', () => {
 
   describe('changeFileContent', () => {
     let tmpFile;
-    const callAction = (content = 'content\n') =>
-      store.dispatch('changeFileContent', { path: tmpFile.path, content });
 
     beforeEach(() => {
       tmpFile = file('tmpFile');
@@ -521,11 +519,23 @@ describe('IDE store file actions', () => {
     });
 
     it('updates file content', () => {
-      return callAction().then(() => {
+      const content = 'content\n';
+
+      return store.dispatch('changeFileContent', { path: tmpFile.path, content }).then(() => {
         expect(tmpFile.content).toBe('content\n');
       });
     });
 
+    it('does nothing if path does not exist', () => {
+      const content = 'content\n';
+
+      return store
+        .dispatch('changeFileContent', { path: 'not/a/real_file.txt', content })
+        .then(() => {
+          expect(tmpFile.content).toBe('\n');
+        });
+    });
+
     it('adds file into stagedFiles array', () => {
       return store
         .dispatch('changeFileContent', {
-- 
GitLab