Skip to content

Commit 2af50c6

Browse files
Append profile to databrickscfg instead of writing the full file (#1060)
## Changes * Writing the full .databrickscfg leads to unintended consequences such as deletion of comments and writing invalid profiles. * We should append to the file instead. ## Tests <!-- How is this tested? --> --------- Co-authored-by: Ilia Babanov <ilia.babanov@databricks.com>
1 parent fd8ae92 commit 2af50c6

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/databricks-vscode/src/configuration/LoginWizard.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import {
2828
} from "@databricks/databricks-sdk";
2929
import {randomUUID} from "crypto";
3030
import ini from "ini";
31-
import {copyFile, writeFile} from "fs/promises";
31+
import {appendFile, copyFile} from "fs/promises";
3232
import path from "path";
33+
import os from "os";
3334

3435
interface AuthTypeQuickPickItem extends QuickPickItem {
3536
authType?: SdkAuthType;
@@ -370,13 +371,17 @@ export async function saveNewProfile(
370371
if (!iniData) {
371372
throw new Error("Can't save empty auth provider to a profile");
372373
}
373-
const {path: configFilePath, iniFile} = await loadConfigFile(
374+
375+
const {path: configFilePath} = await loadConfigFile(
374376
workspaceConfigs.databrickscfgLocation
375377
);
376-
iniFile[profileName] = Object.fromEntries(
378+
379+
const profile: any = {};
380+
profile[profileName] = Object.fromEntries(
377381
Object.entries(iniData).filter((kv) => kv[1] !== undefined)
378382
);
379-
383+
const iniStr = ini.stringify(profile);
384+
const finalStr = `${os.EOL};This profile is autogenerated by the Databricks Extension for VS Code${os.EOL}${iniStr}`;
380385
// Create a backup for .databrickscfg
381386
const backup = path.join(
382387
path.dirname(configFilePath),
@@ -388,7 +393,7 @@ export async function saveNewProfile(
388393
);
389394

390395
// Write the new profile to .databrickscfg
391-
await writeFile(configFilePath, ini.stringify(iniFile));
396+
await appendFile(configFilePath, finalStr);
392397

393398
return new ProfileAuthProvider(authProvider.host, profileName, true);
394399
}

0 commit comments

Comments
 (0)