Skip to content

Commit 8186900

Browse files
fix(export): notify the user when a settings export is refused
exportSettings swallows every failure (the caller awaits it without a try), so the refusal this unit introduced - refuseSymlinkTarget: true at the safeWriteJson call in src/core/config/importExport.ts - was invisible: the save dialog closed, the console line is not user-facing, and the file the user asked for simply did not exist. The notification uses the channel and the key shape the neighbouring import route already uses (settings_import_failed at the end of importSettingsWithFeedback), so no new notifying layer is invented: common:errors.settings_export_failed, interpolated with the writer's own reason, which names the path it refused and the destination it would have written through. - src/core/config/importExport.ts: notify in the catch; the log line and the swallow-everything contract are unchanged. - src/i18n/locales/*/common.json: settings_export_failed added to all 18 catalogues in the same commit as the call site, with the same {{error}} placeholder as the import key. The export noun in each language is the one that catalogue already uses elsewhere (footer.settings.export in webview-ui settings.json, info.mode_exported in the same src common.json). - src/core/config/__tests__/importExport.spec.ts: asserts the notification fires on the refusal path, that the reason travels, that the import key is not reused, and that the export still resolves rather than rejecting. Review thread: PRRT_kwDOSK-bwM60746v (src/utils/safeWriteJson.ts:72). Gates printed before this commit (worktree wt-s3-1405, pinned node v22.23.4): prettier --check <20 touched files> exit 0 eslint --prune-suppressions --max-warnings=0 on the two .ts files exit 0 tsc --noEmit -p tsconfig.json (src) exit 0, 0 error lines vitest run --config vitest.core.config.ts core/config/__tests__/importExport.spec.ts 55 passed (55) node scripts/find-missing-translations.js exit 0, all areas complete node scripts/check-i18n-interpolation.js exit 0, 835 files, no missing interpolation variables (negative control: the same interpolation run at main tip e487cd7 printed 833 files, exit 0 - the +2 are this branch's two added source files)
1 parent 94dc7c4 commit 8186900

20 files changed

Lines changed: 74 additions & 5 deletions

File tree

‎src/core/config/__tests__/importExport.spec.ts‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { safeWriteJson } from "../../../utils/safeWriteJson"
2424

2525
import type { Mock } from "vitest"
2626
import { providerIdentifiers, retiredProviderIdentifiers } from "@roo-code/types/provider-identifiers"
27+
import { t } from "../../../i18n"
2728

2829
function createDeferred<T>() {
2930
let resolve!: (value: T) => void
@@ -1686,10 +1687,52 @@ describe("importExport", () => {
16861687
expect(mockProviderSettingsManager.export).toHaveBeenCalled()
16871688
expect(mockContextProxy.export).toHaveBeenCalled()
16881689
expect(fs.mkdir).toHaveBeenCalledWith("/mock/path", { recursive: true })
1689-
expect(safeWriteJson).toHaveBeenCalled() // safeWriteJson is called, but it will throw
1690-
// The error is caught and the function exits silently.
1691-
// Optionally, ensure no error message was shown if that's part of "silent"
1692-
// expect(vscode.window.showErrorMessage).not.toHaveBeenCalled();
1690+
})
1691+
1692+
it("notifies the user when the export write is refused, and still swallows the failure", async () => {
1693+
;(vscode.window.showSaveDialog as Mock).mockResolvedValue({
1694+
fsPath: "/mock/path/zoo-code-settings.json",
1695+
})
1696+
1697+
mockProviderSettingsManager.export.mockResolvedValue({
1698+
currentApiConfigName: "test",
1699+
apiConfigs: { test: { apiProvider: providerIdentifiers.openai, id: "test-id" } },
1700+
migrations: { rateLimitSecondsMigrated: false },
1701+
})
1702+
mockContextProxy.export.mockResolvedValue({ mode: "code" })
1703+
1704+
// The refusal this unit introduced: refuseSymlinkTarget is set at the call above, so a
1705+
// link at the path the user picked makes the writer reject instead of publishing.
1706+
const refusal = new Error(
1707+
"safeWriteJson: refusing to write through the symlink now at /mock/path/zoo-code-settings.json (before publication); the payload would land at /elsewhere/settings.json",
1708+
)
1709+
;(safeWriteJson as Mock).mockRejectedValueOnce(refusal)
1710+
1711+
const showErrorMessageSpy = vi.spyOn(vscode.window, "showErrorMessage").mockResolvedValue(undefined)
1712+
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
1713+
1714+
// The contract the caller relies on: webviewMessageHandler awaits exportSettings without a
1715+
// try, so a refusal must not become an unhandled rejection.
1716+
await expect(
1717+
exportSettings({
1718+
providerSettingsManager: mockProviderSettingsManager,
1719+
contextProxy: mockContextProxy,
1720+
}),
1721+
).resolves.toBeUndefined()
1722+
1723+
// The user learns the file did not land, through the same channel and the same key shape the
1724+
// import route uses. Expected is computed with the same t() call the module makes, so the
1725+
// assertion holds whether this lane resolves t to the string or to the key.
1726+
expect(showErrorMessageSpy).toHaveBeenCalledTimes(1)
1727+
expect(showErrorMessageSpy).toHaveBeenCalledWith(
1728+
t("common:errors.settings_export_failed", { error: refusal.message }),
1729+
)
1730+
expect(showErrorMessageSpy).toHaveBeenCalledWith(expect.stringContaining("errors.settings_export_failed"))
1731+
expect(showErrorMessageSpy).not.toHaveBeenCalledWith(expect.stringContaining("settings_import_failed"))
1732+
expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to export settings:", refusal)
1733+
1734+
showErrorMessageSpy.mockRestore()
1735+
consoleErrorSpy.mockRestore()
16931736
})
16941737

16951738
it("should handle errors during directory creation", async () => {

‎src/core/config/importExport.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,15 @@ export const exportSettings = async ({ providerSettingsManager, contextProxy }:
349349
await safeWriteJson(uri.fsPath, { providerProfiles, globalSettings }, { refuseSymlinkTarget: true })
350350
} catch (e) {
351351
console.error("Failed to export settings:", e)
352-
// Don't re-throw - the UI will handle showing error messages
352+
// Don't re-throw - this route has no result object for the caller to report, so the
353+
// notification happens here, the way the import route notifies through
354+
// settings_import_failed below. A refusal to publish (a link at the chosen path the user
355+
// never picked) is otherwise invisible: the dialog closes, the log line is not user-facing,
356+
// and the file the user asked for does not exist. The writer's own message names the path it
357+
// refused and the destination it would have written through, so the reason travels with the
358+
// message.
359+
const reason = e instanceof Error ? e.message : String(e)
360+
await vscode.window.showErrorMessage(t("common:errors.settings_export_failed", { error: reason }))
353361
}
354362
}
355363

‎src/i18n/locales/ca/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/de/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/en/common.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"cannot_access_path": "Cannot access path {{path}}: {{error}}",
4848
"path_outside_workspace": "Path is outside the workspace",
4949
"settings_import_failed": "Settings import failed: {{error}}.",
50+
"settings_export_failed": "Settings export failed: {{error}}.",
5051
"mistake_limit_guidance": "This may indicate a failure in the model's thought process or inability to use a tool properly, which can be mitigated with some user guidance (e.g. \"Try breaking down the task into smaller steps\").",
5152
"violated_organization_allowlist": "Failed to run task: the current profile isn't compatible with your organization settings",
5253
"condense_failed": "Failed to condense context",

‎src/i18n/locales/es/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/fr/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/hi/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/id/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/it/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)