diff --git a/firebase-vscode/CHANGELOG.md b/firebase-vscode/CHANGELOG.md index 98d1c7eafbf..8ac9660b24b 100644 --- a/firebase-vscode/CHANGELOG.md +++ b/firebase-vscode/CHANGELOG.md @@ -1,5 +1,7 @@ ## NEXT +- [Fixed] Fixed an issue where generating an ad-hoc file would break codelenses + ## 0.10.8 - Updated internal firebase-tools dependency to 13.25.0 diff --git a/firebase-vscode/src/data-connect/ad-hoc-mutations.ts b/firebase-vscode/src/data-connect/ad-hoc-mutations.ts index 710e24bd8e0..a5de72d1e69 100644 --- a/firebase-vscode/src/data-connect/ad-hoc-mutations.ts +++ b/firebase-vscode/src/data-connect/ad-hoc-mutations.ts @@ -148,12 +148,11 @@ query { documentPath: string, ) { // generate content for the file - const preamble = - "# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file."; - const introspect = await dataConnectService.introspect(); if (!introspect.data) { - vscode.window.showErrorMessage("Failed to generate mutation. Please check your compilation errors."); + vscode.window.showErrorMessage( + "Failed to generate mutation. Please check your compilation errors.", + ); return; } const schema = buildClientSchema(introspect.data); @@ -162,41 +161,24 @@ query { return; } - const adhocMutation = print( - await makeAdHocMutation( - Object.values(dataType.getFields()), - ast.name.value, - ), - ); - const content = [preamble, adhocMutation].join("\n"); - // get root where dataconnect.yaml lives const configs = await firstWhereDefined(dataConnectConfigs); const dataconnectConfig = configs.tryReadValue?.findEnclosingServiceForPath(documentPath); const basePath = dataconnectConfig?.path; - const filePath = vscode.Uri.file(`${basePath}/${ast.name.value}_insert.gql`); - const doesFileExist = await checkIfFileExists(filePath); - - if (!doesFileExist) { - // opens unsaved text document with name "[mutationName]_insert.gql" + const filePath = vscode.Uri.file( + `${basePath}/${ast.name.value}_insert.gql`, + ); - vscode.workspace - .openTextDocument(filePath.with({ scheme: "untitled" })) - .then((doc) => { - vscode.window.showTextDocument(doc).then((openDoc) => { - openDoc.edit((edit) => { - edit.insert(new vscode.Position(0, 0), content); - }); - }); - }); - } else { - // Opens existing text document - vscode.workspace.openTextDocument(filePath).then((doc) => { - vscode.window.showTextDocument(doc); - }); - } + await upsertFile(filePath, () => { + const preamble = + "# This is a file for you to write an un-named mutation. \n# Only one un-named mutation is allowed per file."; + const adhocMutation = print( + makeAdHocMutation(Object.values(dataType.getFields()), ast.name.value), + ); + return [preamble, adhocMutation].join("\n"); + }); } function makeAdHocMutation( diff --git a/firebase-vscode/src/data-connect/file-utils.ts b/firebase-vscode/src/data-connect/file-utils.ts index 386873010da..d7939744774 100644 --- a/firebase-vscode/src/data-connect/file-utils.ts +++ b/firebase-vscode/src/data-connect/file-utils.ts @@ -24,16 +24,11 @@ export async function upsertFile( ): Promise { const doesFileExist = await checkIfFileExists(uri); - if (!doesFileExist) { - const doc = await vscode.workspace.openTextDocument( - uri.with({ scheme: "untitled" }), - ); - const editor = await vscode.window.showTextDocument(doc); - await editor.edit((edit) => - edit.insert(new vscode.Position(0, 0), content()), - ); - return; + // Have to write to file system first before opening + // otherwise we can't save it without closing it + if (!doesFileExist) { + vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(content())); } // Opens existing text document diff --git a/firebase-vscode/src/test/integration/fishfood/graphql.ts b/firebase-vscode/src/test/integration/fishfood/graphql.ts index 1097736e233..5e09b667831 100644 --- a/firebase-vscode/src/test/integration/fishfood/graphql.ts +++ b/firebase-vscode/src/test/integration/fishfood/graphql.ts @@ -135,7 +135,7 @@ firebaseSuite("GraphQL", async function () { await addDataButton.click(); // Wait a bit for the mutation to be generated - await browser.pause(1500); + await browser.pause(5000); // Verify the generated mutation const activeEditor = await editorView.getActiveEditor(); @@ -150,7 +150,9 @@ firebaseSuite("GraphQL", async function () { }"`); expect(editorTitle).toBe("Post_insert.gql"); - await editorView.closeCurrentEditor(); + // file should be created, saved, then opened + expect(activeEditor?.document.isDirty).toBe(false); + await editorView.closeAllEditors(); }, ); @@ -185,7 +187,7 @@ firebaseSuite("GraphQL", async function () { await readDataButton.click(); // Wait a bit for the query to be generated - await browser.pause(1000); + await browser.pause(5000); // Verify the generated query const activeEditor = await editorView.getActiveEditor(); @@ -199,6 +201,10 @@ firebaseSuite("GraphQL", async function () { } }`); expect(editorTitle).toBe("Post_read.gql"); + + // file should be created, saved, then opened + expect(activeEditor?.document.isDirty).toBe(false); + await editorView.closeAllEditors(); }, ); @@ -242,7 +248,7 @@ firebaseSuite("GraphQL", async function () { "test_projects/fishfood/dataconnect/Post_insert.gql", ); - await editorView.closeCurrentEditor(); + await editorView.closeAllEditors(); }, ); @@ -282,8 +288,10 @@ firebaseSuite("GraphQL", async function () { // Verify the generated query file path const activeEditor = await editorView.getActiveEditor(); const filePath = activeEditor?.document.fileName; - expect(filePath).toContain("test_projects/fishfood/dataconnect/Post_read.gql"); - await editorView.closeCurrentEditor(); + expect(filePath).toContain( + "test_projects/fishfood/dataconnect/Post_read.gql", + ); + await editorView.closeAllEditors(); }, ); });