Skip to content

Commit aa35d80

Browse files
Auto-upgrade: skip project-local installs in postrun hook
When the postrun hook triggers auto-upgrade, it now passes `{autoupgrade: true}` to `runCLIUpgrade`. With that flag set, project-local installs are skipped so the silent background flow doesn't surprise users by mutating their app's `package.json` / lockfile. Explicit `shopify upgrade` invocations still upgrade local projects \u2014 this only changes the implicit postrun-triggered path.
1 parent 83ba075 commit aa35d80

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/cli-kit': patch
3+
---
4+
5+
Auto-upgrade now skips project-local installs when triggered by the postrun hook. Running `shopify upgrade` explicitly still upgrades the project's `package.json` / lockfile; only the silent background flow is affected, so users aren't surprised by unsolicited diffs in their app project.

packages/cli-kit/src/public/node/hooks/postrun.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async function performAutoUpgrade(newerVersion: string): Promise<void> {
9696
}
9797

9898
try {
99-
await runCLIUpgrade()
99+
await runCLIUpgrade({autoupgrade: true})
100100
await metadata.addPublicMetadata(() => ({env_auto_upgrade_success: true}))
101101
// eslint-disable-next-line no-catch-all/no-catch-all
102102
} catch (error) {

packages/cli-kit/src/public/node/upgrade.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ describe('runCLIUpgrade', () => {
208208
// Then
209209
expect(exec).not.toHaveBeenCalled()
210210
})
211+
212+
test('skips project-local upgrade when called from the auto-upgrade postrun hook', async () => {
213+
// Given
214+
vi.mocked(currentProcessIsGlobal).mockReturnValue(false)
215+
216+
// When
217+
await runCLIUpgrade({autoupgrade: true})
218+
219+
// Then
220+
expect(exec).not.toHaveBeenCalled()
221+
})
211222
})
212223

213224
describe('versionToAutoUpgrade', () => {

packages/cli-kit/src/public/node/upgrade.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,27 @@ export function cliInstallCommand(): string | undefined {
4141
}
4242
}
4343

44+
/**
45+
* Options for {@link runCLIUpgrade}.
46+
*/
47+
export interface RunCLIUpgradeOptions {
48+
/**
49+
* `true` when the upgrade is being triggered by the automatic postrun hook.
50+
* In that case we skip project-local upgrades — those should only happen when the
51+
* user explicitly runs `shopify upgrade` so we don't surprise them by mutating
52+
* their `package.json` / lockfile in the background.
53+
*/
54+
autoupgrade?: boolean
55+
}
56+
4457
/**
4558
* Runs the CLI upgrade using the appropriate package manager.
4659
* Determines the install command and executes it.
4760
*
61+
* @param options - See {@link RunCLIUpgradeOptions}.
4862
* @throws AbortError if the package manager or command cannot be determined.
4963
*/
50-
export async function runCLIUpgrade(): Promise<void> {
64+
export async function runCLIUpgrade(options: RunCLIUpgradeOptions = {}): Promise<void> {
5165
// Path where the current project is (app/hydrogen)
5266
const path = sniffForPath() ?? cwd()
5367
const projectDir = getProjectDir(path)
@@ -61,6 +75,15 @@ export async function runCLIUpgrade(): Promise<void> {
6175
return
6276
}
6377

78+
// When triggered by the automatic postrun hook, skip project-local upgrades.
79+
// Bumping `package.json` / lockfile silently in the background would surprise users
80+
// and produce noisy diffs; explicit `shopify upgrade` invocations still upgrade the
81+
// local project.
82+
if (options.autoupgrade && !isGlobal) {
83+
outputDebug('Auto-upgrade: Skipping project-local upgrade triggered by the postrun hook.')
84+
return
85+
}
86+
6487
// Generate the install command for the global CLI and execute it
6588
if (isGlobal) {
6689
const installCommand = cliInstallCommand()

0 commit comments

Comments
 (0)