Fix crash on clean startup (#6276)

* Convert ReleaseChannel into an InitializableState

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix init timing for DefaultUpdateChannel

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Make UserStore loading seperate from creation to fix initialization timing errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix overrides of UserStore

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix UserStore tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move userStoreFileMigration call to the init runnable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add global override for userStoreFileMigratiom

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-09-20 08:19:09 +02:00
1 parent dad6dcacda
commit 4c49134dbc
18 files changed
+159 -49

No files matched your search

+11 -1
View File
@@ -33,6 +33,8 @@ import writeFileInjectable from "../fs/write-file.injectable";
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
import getConfigurationFileModelInjectable from "../get-configuration-file-model/get-configuration-file-model.injectable";
import storeMigrationVersionInjectable from "../vars/store-migration-version.injectable";
import releaseChannelInjectable from "../vars/release-channel.injectable";
import defaultUpdateChannelInjectable from "../application-update/selected-update-channel/default-update-channel.injectable";
console = new Console(stdout, stderr);
@@ -40,7 +42,7 @@ describe("user store tests", () => {
let userStore: UserStore;
let di: DiContainer;
beforeEach(() => {
beforeEach(async () => {
di = getDiForUnitTesting({ doGeneralOverrides: true });
mockFs();
@@ -50,6 +52,12 @@ describe("user store tests", () => {
di.permitSideEffects(getConfigurationFileModelInjectable);
di.permitSideEffects(userStoreInjectable);
di.override(releaseChannelInjectable, () => ({
get: () => "latest" as const,
init: async () => {},
}));
await di.inject(defaultUpdateChannelInjectable).init();
di.unoverride(userStoreInjectable);
});
@@ -62,6 +70,7 @@ describe("user store tests", () => {
mockFs({ "some-directory-for-user-data": { "config.json": "{}", "kube_config": "{}" }});
userStore = di.inject(userStoreInjectable);
userStore.load();
});
it("allows setting and retrieving lastSeenAppVersion", () => {
@@ -119,6 +128,7 @@ describe("user store tests", () => {
di.override(storeMigrationVersionInjectable, () => "10.0.0");
userStore = di.inject(userStoreInjectable);
userStore.load();
});
it("sets last seen app version to 0.0.0", () => {
@@ -8,7 +8,7 @@ import { updateChannels } from "../update-channels";
const defaultUpdateChannelInjectable = createInitializableState({
id: "default-update-channel",
init: (di) => updateChannels[di.inject(releaseChannelInjectable)],
init: (di) => updateChannels[di.inject(releaseChannelInjectable).get()],
});
export default defaultUpdateChannelInjectable;
@@ -20,6 +20,7 @@ const initializeSentryReportingWithInjectable = getInjectable({
instantiate: (di): InitializeSentryReportingWith => {
const sentryDataSourceName = di.inject(sentryDataSourceNameInjectable);
const isProduction = di.inject(isProductionInjectable);
const userStore = di.inject(userStoreInjectable);
if (!sentryDataSourceName) {
return () => {};
@@ -27,9 +28,6 @@ const initializeSentryReportingWithInjectable = getInjectable({
return (initSentry) => initSentry({
beforeSend: (event) => {
// TODO: remove loading from userStoreInjectable so that this can be moved out
const userStore = di.inject(userStoreInjectable);
if (userStore.allowErrorReporting) {
return event;
}
@@ -0,0 +1,9 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "../test-utils/get-global-override";
import userStoreFileNameMigrationInjectable from "./file-name-migration.injectable";
export default getGlobalOverride(userStoreFileNameMigrationInjectable, () => async () => {});
@@ -9,27 +9,32 @@ import directoryForUserDataInjectable from "../app-paths/directory-for-user-data
import { isErrnoException } from "../utils";
import { getInjectable } from "@ogre-tools/injectable";
export type UserStoreFileNameMigration = () => Promise<void>;
const userStoreFileNameMigrationInjectable = getInjectable({
id: "user-store-file-name-migration",
instantiate: (di) => {
instantiate: (di): UserStoreFileNameMigration => {
const userDataPath = di.inject(directoryForUserDataInjectable);
const configJsonPath = path.join(userDataPath, "config.json");
const lensUserStoreJsonPath = path.join(userDataPath, "lens-user-store.json");
try {
fse.moveSync(configJsonPath, lensUserStoreJsonPath);
} catch (error) {
if (error instanceof Error && error.message === "dest already exists.") {
fse.removeSync(configJsonPath);
} else if (isErrnoException(error) && error.code === "ENOENT" && error.path === configJsonPath) {
// (No such file or directory)
return; // file already moved
} else {
// pass other errors along
throw error;
return async () => {
try {
await fse.move(configJsonPath, lensUserStoreJsonPath);
} catch (error) {
if (error instanceof Error && error.message === "dest already exists.") {
await fse.remove(configJsonPath);
} else if (isErrnoException(error) && error.code === "ENOENT" && error.path === configJsonPath) {
// (No such file or directory)
return; // file already moved
} else {
// pass other errors along
throw error;
}
}
}
};
},
causesSideEffects: true,
});
export default userStoreFileNameMigrationInjectable;
@@ -3,8 +3,6 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { ipcMain } from "electron";
import userStoreFileNameMigrationInjectable from "./file-name-migration.injectable";
import { UserStore } from "./user-store";
import selectedUpdateChannelInjectable from "../application-update/selected-update-channel/selected-update-channel.injectable";
@@ -14,10 +12,6 @@ const userStoreInjectable = getInjectable({
instantiate: (di) => {
UserStore.resetInstance();
if (ipcMain) {
di.inject(userStoreFileNameMigrationInjectable);
}
return UserStore.createInstance({
selectedUpdateChannel: di.inject(selectedUpdateChannelInjectable),
});
-1
View File
@@ -34,7 +34,6 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
});
makeObservable(this);
this.load();
}
@observable lastSeenAppVersion = "0.0.0";
@@ -2,13 +2,13 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { ReleaseChannel } from "../application-update/update-channels";
import { createInitializableState } from "../initializable-state/create";
import buildSemanticVersionInjectable from "./build-semantic-version.injectable";
const releaseChannelInjectable = getInjectable({
const releaseChannelInjectable = createInitializableState({
id: "release-channel",
instantiate: (di): ReleaseChannel => {
init: (di): ReleaseChannel => {
const buildSemanticVersion = di.inject(buildSemanticVersionInjectable);
const currentReleaseChannel = buildSemanticVersion.get().prerelease[0];