Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(studio): schema copy bug in safari (#42161)

Charis3fcc8b51a19c
diff --git a/apps/studio/components/layouts/TableEditorLayout/EntityListItem.tsx b/apps/studio/components/layouts/TableEditorLayout/EntityListItem.tsx
index fd41515e838..0aff209ffac 100644
--- a/apps/studio/components/layouts/TableEditorLayout/EntityListItem.tsx
+++ b/apps/studio/components/layouts/TableEditorLayout/EntityListItem.tsx
@@ -3,6 +3,7 @@ import Link from 'next/link'
import { type CSSProperties } from 'react'
import { toast } from 'sonner'
+import { formatSql } from '@/lib/formatSql'
import { useFlag, useParams } from 'common'
import { useTableFilter } from 'components/grid/hooks/useTableFilter'
import { buildTableEditorUrl } from 'components/grid/SupabaseGrid.utils'
@@ -18,7 +19,6 @@ import type { TableApiAccessData, TableApiAccessMap } from 'data/privileges/tabl
import { useTableRowsCountQuery } from 'data/table-rows/table-rows-count-query'
import { useQuerySchemaState } from 'hooks/misc/useSchemaQueryState'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
-import { formatSql } from 'lib/formatSql'
import {
useRoleImpersonationStateSnapshot,
type RoleImpersonationState,
@@ -263,23 +263,22 @@ export const EntityListItem = ({
e.stopPropagation()
const toastId = toast.loading('Getting table schema...')
- const tableDefinition = await getTableDefinition({
+ const formattedSchema = getTableDefinition({
id: entity.id,
projectRef: project?.ref,
connectionString: project?.connectionString,
+ }).then((tableDefinition) => {
+ if (!tableDefinition) {
+ throw new Error('Failed to get table schema')
+ }
+ return formatSql(tableDefinition)
})
- if (!tableDefinition) {
- return toast.error('Failed to get table schema', { id: toastId })
- }
try {
- const formatted = formatSql(tableDefinition)
- await copyToClipboard(formatted)
+ await copyToClipboard(formattedSchema)
toast.success('Table schema copied to clipboard', { id: toastId })
} catch (err: any) {
- toast.error('Failed to copy schema: ' + (err.message || err), {
- id: toastId,
- })
+ toast.error('Failed to copy schema: ' + (err.message || err), { id: toastId })
}
}}
>
diff --git a/packages/eslint-config-supabase/next.js b/packages/eslint-config-supabase/next.js
index 8ba786d0579..c8f9848a5e3 100644
--- a/packages/eslint-config-supabase/next.js
+++ b/packages/eslint-config-supabase/next.js
@@ -7,12 +7,22 @@ const tanstackQuery = require('@tanstack/eslint-plugin-query')
const tseslint = require('@typescript-eslint/eslint-plugin')
const tsparser = require('@typescript-eslint/parser')
+// Custom Supabase rules
+const noAwaitBeforeCopyToClipboard = require('./rules/no-await-before-copy-to-clipboard')
+
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
})
+// Custom Supabase ESLint plugin
+const supabasePlugin = {
+ rules: {
+ 'no-await-before-copy-to-clipboard': noAwaitBeforeCopyToClipboard,
+ },
+}
+
// TypeScript ESLint config for TypeScript files
const typescriptConfig = {
name: 'typescript',
@@ -26,9 +36,11 @@ const typescriptConfig = {
},
plugins: {
'@typescript-eslint': tseslint,
+ supabase: supabasePlugin,
},
rules: {
'@typescript-eslint/no-explicit-any': 'warn',
+ 'supabase/no-await-before-copy-to-clipboard': 'error',
},
}
diff --git a/packages/eslint-config-supabase/rules/no-await-before-copy-to-clipboard.js b/packages/eslint-config-supabase/rules/no-await-before-copy-to-clipboard.js
new file mode 100644
index 00000000000..8a33136a4cc
--- /dev/null
+++ b/packages/eslint-config-supabase/rules/no-await-before-copy-to-clipboard.js
@@ -0,0 +1,140 @@
+/**
+ * ESLint rule to prevent calling copyToClipboard after an await expression.
+ *
+ * Safari doesn't support async clipboard operations - the clipboard write must happen
+ * synchronously with the user gesture. The copyToClipboard function accepts a Promise<string>
+ * to handle this, but if you await before calling it, Safari will fail.
+ *
+ * BAD:
+ * const data = await fetchData()
+ * copyToClipboard(data)
+ *
+ * GOOD:
+ * copyToClipboard(fetchData())
+ * // or
+ * copyToClipboard(fetchData().then(format))
+ */
+
+/** @type {import('eslint').Rule.RuleModule} */
+module.exports = {
+ meta: {
+ type: 'problem',
+ docs: {
+ description:
+ 'Disallow calling copyToClipboard after an await expression (breaks Safari clipboard)',
+ recommended: true,
+ },
+ messages: {
+ noAwaitBeforeCopy:
+ 'Do not call copyToClipboard after an await. Safari requires clipboard operations to be synchronous with user gestures. Pass a Promise directly to copyToClipboard instead.',
+ },
+ schema: [],
+ },
+
+ create(context) {
+ // Track await expressions per function scope
+ const functionScopes = new Map()
+
+ /**
+ * Get the nearest function scope for a node
+ */
+ function getFunctionScope(node) {
+ let current = node.parent
+ while (current) {
+ if (
+ current.type === 'FunctionDeclaration' ||
+ current.type === 'FunctionExpression' ||
+ current.type === 'ArrowFunctionExpression'
+ ) {
+ return current
+ }
+ current = current.parent
+ }
+ return null
+ }
+
+ /**
+ * Check if nodeA comes before nodeB in source order
+ */
+ function isBefore(nodeA, nodeB) {
+ return nodeA.range[1] <= nodeB.range[0]
+ }
+
+ /**
+ * Check if the await is in a path that leads to the copyToClipboard call.
+ */
+ function isAwaitInPathTo(awaitNode, copyNode) {
+ // Simple heuristic: if the await comes before the copy in source order
+ // and they're in the same function, it's likely in the execution path.
+ // This may have some false positives for complex control flow, but it's
+ // better to be safe for Safari compatibility.
+ return isBefore(awaitNode, copyNode)
+ }
+
+ return {
+ // Track when we enter a function that could be async
+ ':function'(node) {
+ functionScopes.set(node, { awaitExpressions: [], isAsync: node.async })
+ },
+
+ // Track when we exit a function
+ ':function:exit'(node) {
+ functionScopes.delete(node)
+ },
+
+ // Record all await expressions
+ AwaitExpression(node) {
+ const funcScope = getFunctionScope(node)
+ if (funcScope && functionScopes.has(funcScope)) {
+ functionScopes.get(funcScope).awaitExpressions.push(node)
+ }
+ },
+
+ // Check copyToClipboard calls
+ CallExpression(node) {
+ // Check if this is a call to copyToClipboard
+ const callee = node.callee
+ let isCopyToClipboard = false
+
+ if (callee.type === 'Identifier' && callee.name === 'copyToClipboard') {
+ isCopyToClipboard = true
+ } else if (
+ callee.type === 'MemberExpression' &&
+ callee.property.type === 'Identifier' &&
+ callee.property.name === 'copyToClipboard'
+ ) {
+ isCopyToClipboard = true
+ }
+
+ if (!isCopyToClipboard) {
+ return
+ }
+
+ // Find the function scope
+ const funcScope = getFunctionScope(node)
+ if (!funcScope || !functionScopes.has(funcScope)) {
+ return
+ }
+
+ const scopeInfo = functionScopes.get(funcScope)
+
+ // Only check async functions (only they can have await)
+ if (!scopeInfo.isAsync) {
+ return
+ }
+
+ // Check if any await expression comes before this copyToClipboard call
+ for (const awaitExpr of scopeInfo.awaitExpressions) {
+ if (isAwaitInPathTo(awaitExpr, node)) {
+ context.report({
+ node,
+ messageId: 'noAwaitBeforeCopy',
+ })
+ // Only report once per call
+ return
+ }
+ }
+ },
+ }
+ },
+}