diff --git a/src/features/run/npmInstall.test.ts b/src/features/run/npmInstall.test.ts index 8a4dc059e0..18d08d1df6 100644 --- a/src/features/run/npmInstall.test.ts +++ b/src/features/run/npmInstall.test.ts @@ -17,7 +17,13 @@ vi.mock("./store", () => run); import { emitLocalIdeEvent, type IdeEvent, type RunConfig } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { provideNotificationService } from "@/workbench/contributions"; -import { installDue, isWorkspaceMember, pnpmWorkspacePackages, registerNpmInstall } from "./npmInstall"; +import { + dependencySignature, + installDue, + isWorkspaceMember, + pnpmWorkspacePackages, + registerNpmInstall, +} from "./npmInstall"; const ok = (data: T) => Promise.resolve({ status: "ok" as const, data }); const err = () => Promise.resolve({ status: "error" as const, error: { kind: "notFound", message: "" } }); @@ -99,6 +105,8 @@ it("asks once for a fresh clone and installs with the detected package manager i await open(); expect(titles()).toEqual(["Dependencies are not installed"]); expect(notify.mock.calls[0]![0].body).toBe("Run 'pnpm install' to install them."); + // In the IDE only: branch switches would otherwise raise an OS notification each time. + expect(notify.mock.calls[0]![0].system).toBe(false); await fsChanged("/p/package.json"); expect(titles()).toHaveLength(1); @@ -134,6 +142,48 @@ it("asks when the lockfile changes after the install (a pull), not for package.j expect(titles()).toHaveLength(2); }); +it("signs only what an install resolves from, ignoring key order", () => { + const sig = (extra: object) => dependencySignature(JSON.parse(pkg({ react: "^19.0.0", vite: "^7.0.0" }, extra))); + const base = sig({}); + expect(sig({ version: "2.0.0", scripts: { build: "vite build" }, engines: { node: ">=22" } })).toBe(base); + expect(sig({ dependencies: { vite: "^7.0.0", react: "^19.0.0" } })).toBe(base); + for (const extra of [ + { overrides: { lodash: "4.17.21" } }, + { resolutions: { lodash: "4.17.21" } }, + { pnpm: { overrides: { lodash: "4.17.21" } } }, + { pnpm: { patchedDependencies: { react: "patches/react.patch" } } }, + { peerDependencies: { react: "*" } }, + { workspaces: ["packages/*"] }, + ]) + expect(sig(extra), JSON.stringify(extra)).not.toBe(base); + expect(sig({ pnpm: { onlyBuiltDependencies: ["esbuild"] } })).toBe(base); +}); + +it("asks for an overrides change, not for a package with only overrides", async () => { + write("/p/package.json", 1_000, pkg({}, { overrides: { lodash: "4.17.21" } })); + await open(); + expect(notify).not.toHaveBeenCalled(); + dispose(); + + write("/p/package.json", 1_000, pkg({ react: "^19.0.0" })); + write("/p/node_modules/.package-lock.json", 2_000); + await open(); + write("/p/package.json", 60_000, pkg({ react: "^19.0.0" }, { overrides: { lodash: "4.17.21" } })); + await fsChanged("/p/package.json"); + expect(titles()).toEqual(["Dependencies changed since the last install"]); +}); + +it("does not take baselines stored before the signature widened as changed", async () => { + localStorage.setItem( + "openidea.npmInstall.v1", + JSON.stringify({ baseline: { "/p/package.json": JSON.stringify([{ react: "^19.0.0" }, null, null, null]) } }), + ); + write("/p/package.json", 60_000, pkg({ react: "^19.0.0" }, { version: "1.0.1" })); + write("/p/node_modules/.package-lock.json", 2_000); + await open(); + expect(notify).not.toHaveBeenCalled(); +}); + it("asks once at the workspace root for a member's dependency change", async () => { write("/p/package.json", 1_000, pkg({}, { workspaces: ["packages/*"] })); write("/p/yarn.lock", 1_000); diff --git a/src/features/run/npmInstall.ts b/src/features/run/npmInstall.ts index 3ce81986cc..778840d7a6 100644 --- a/src/features/run/npmInstall.ts +++ b/src/features/run/npmInstall.ts @@ -16,10 +16,26 @@ const LOCKS = ["package-lock.json", "npm-shrinkwrap.json", "pnpm-lock.yaml", "ya /** Rewritten by every install: npm, pnpm, Yarn classic, Yarn's node-modules linker. */ const MARKERS = [".package-lock.json", ".modules.yaml", ".yarn-integrity", ".yarn-state.yml"]; const DEP_FIELDS = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"] as const; +/** What an install resolves from, compared between checks: the dependency lists plus overrides, patches and workspaces. */ +const INSTALL_FIELDS = [ + ...DEP_FIELDS, + "bundleDependencies", + "bundledDependencies", + "peerDependenciesMeta", + "dependenciesMeta", + "overrides", + "resolutions", + "workspaces", + "pnpm.overrides", + "pnpm.patchedDependencies", + "pnpm.packageExtensions", + "pnpm.peerDependencyRules", +]; // ponytail: bun writes bun.lock just after node_modules and leaves no marker, so a lockfile this // close to the install counts as written by it. const SLACK_MS = 2000; -const STORE_KEY = "openidea.npmInstall.v1"; +// v2: the signature gained `INSTALL_FIELDS` beyond `DEP_FIELDS`; v1 baselines would all read as changed. +const STORE_KEY = "openidea.npmInstall.v2"; export type InstallDue = "missing" | "stale" | null; @@ -38,9 +54,25 @@ export function installDue(f: { return Math.max(f.lockAt ?? 0, f.editedAt ?? 0) > f.installedAt + SLACK_MS ? "stale" : null; } -/** The dependency fields of a `package.json`, as compared between checks (scripts and the rest do not count). */ +/** `value` as JSON with object keys sorted, so reordering entries is no change. */ +const canonical = (value: unknown): unknown => + Array.isArray(value) + ? value.map(canonical) + : value && typeof value === "object" + ? Object.fromEntries( + Object.keys(value) + .sort() + .map((k) => [k, canonical((value as Record)[k])]), + ) + : value; + +/** The install fields of a `package.json`, as compared between checks (scripts, version and the rest do not count). */ export function dependencySignature(pkg: Record | null): string { - return JSON.stringify(DEP_FIELDS.map((f) => pkg?.[f] ?? null)); + const at = (path: string) => + path + .split(".") + .reduce((v, k) => (v && typeof v === "object" ? (v as Record)[k] : undefined), pkg); + return JSON.stringify(INSTALL_FIELDS.map((f) => canonical(at(f) ?? null))); } interface Store { @@ -276,6 +308,8 @@ function check(root: string) { group: "run", level: "warning", key, + // Branch switches rewrite lockfiles: this waits for the user's return, not the desktop. + system: false, title: due === "missing" ? "Dependencies are not installed" : "Dependencies changed since the last install", body: rel ? tx`Run '${command}' in '${rel}' to install them.` : tx`Run '${command}' to install them.`, actions: [ diff --git a/src/workbench/contributions.ts b/src/workbench/contributions.ts index 86c966ba9d..19e857cac5 100644 --- a/src/workbench/contributions.ts +++ b/src/workbench/contributions.ts @@ -989,6 +989,12 @@ export interface NotificationService { userInitiated?: boolean; /** Skip the chime even when "Play sound" is on (e.g. native OS sound already played). */ silent?: boolean; + /** + * Set to false to keep it inside the IDE: no OS notification (or the dock badge that comes + * with one) and no chime while OpenIDEA is in the background. For state the user only needs + * on return, such as dependencies to install after a branch switch. + */ + system?: boolean; }): void; /** Hides the balloon of the notification with `key` (it stays in the log), e.g. once its cause is gone. */ expire?(key: string): void; diff --git a/src/workbench/notificationStore.test.ts b/src/workbench/notificationStore.test.ts index 4f06099688..98fd8405e5 100644 --- a/src/workbench/notificationStore.test.ts +++ b/src/workbench/notificationStore.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { notify } from "./notificationStore"; +import { notify, useNotifications } from "./notificationStore"; const { showSystemNotification } = vi.hoisted(() => ({ showSystemNotification: vi.fn(async (_title: string, _body: string) => ({ status: "ok", data: null })), @@ -68,6 +68,13 @@ describe("system toast coalescing", () => { expect(showSystemNotification.mock.calls[1]![0]).toBe("Edit 29"); }); + it("keeps a notification with `system: false` inside the IDE", () => { + const id = notify({ level: "warning", title: "Dependencies changed since the last install", system: false }); + vi.advanceTimersByTime(3000); + expect(showSystemNotification).not.toHaveBeenCalled(); + expect(useNotifications.getState().items.find((x) => x.id === id)).toMatchObject({ balloon: true, read: false }); + }); + it("falls back to the count alone when a balloon has no body", () => { notify({ level: "warning", title: "Cannot reload Maven projects" }); notify({ level: "warning", title: "Cannot reload Gradle projects" }); diff --git a/src/workbench/notificationStore.ts b/src/workbench/notificationStore.ts index cf0a19482e..1bfc7f5aaf 100644 --- a/src/workbench/notificationStore.ts +++ b/src/workbench/notificationStore.ts @@ -206,8 +206,9 @@ export function notify(n: Parameters[0]): number // OS toast owns the chime when system notifications are on and the app is backgrounded. const systemOn = useProject.getState().settings?.notifications?.systemNotifications !== false; const backgrounded = typeof document !== "undefined" && !document.hasFocus(); - if (balloon && prefs.sound && !n.silent && !(systemOn && backgrounded)) playSound(); - if (balloon && isTauri()) { + const system = n.system !== false; + if (balloon && prefs.sound && !n.silent && !(backgrounded && (systemOn || !system))) playSound(); + if (balloon && system && isTauri()) { // In the UI language, as the balloon shows it; a plugin's own text stays as written. const shown = (text: string) => (n.verbatim ? text : txTemplate(text)); const plain = typeof n.body === "string" ? shown(n.body) : "";