Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/auto-upgrade-supported-node.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/cli-kit': patch
'@shopify/cli': patch
---

Auto-upgrade no longer installs a release that doesn't support your Node version
41 changes: 41 additions & 0 deletions packages/cli-kit/src/private/node/cli-version-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,47 @@ test('does not select a higher stable release in the current major than the late
)
})

test('does not select a release whose engines exclude the running Node version', async () => {
const unsupportedVersion = new SemVer(CLI_KIT_VERSION).inc('minor').version
await withRegistry(
http.get('https://registry.npmjs.org/*', () =>
HttpResponse.json({
'dist-tags': {latest: unsupportedVersion},
versions: {
[currentMajorVersion]: {version: currentMajorVersion, engines: {node: `>=${process.versions.node}`}},
[unsupportedVersion]: {version: unsupportedVersion, engines: {node: `>${process.versions.node}`}},
},
}),
),
async () => {
await expect(checkForCLIVersions()).resolves.toEqual({
latest: unsupportedVersion,
currentMajor: currentMajorVersion,
})
expect(versionToAutoUpgrade()).toBe(currentMajorVersion)
},
)
})

test('keeps a release that declares no engines eligible', async () => {
const newerVersion = new SemVer(CLI_KIT_VERSION).inc('minor').version
await withRegistry(
http.get('https://registry.npmjs.org/*', () =>
HttpResponse.json({
'dist-tags': {latest: newerVersion},
versions: {
[currentMajorVersion]: {version: currentMajorVersion, engines: {node: `>=${process.versions.node}`}},
[newerVersion]: {version: newerVersion},
},
}),
),
async () => {
await expect(checkForCLIVersions()).resolves.toEqual({latest: newerVersion, currentMajor: newerVersion})
expect(versionToAutoUpgrade()).toBe(newerVersion)
},
)
})

test('public ranged checks cannot seed or overwrite the vetted auto-upgrade target with a deprecated release', async () => {
const deprecatedVersion = new SemVer(CLI_KIT_VERSION).inc('minor').version
let requests = 0
Expand Down
13 changes: 11 additions & 2 deletions packages/cli-kit/src/private/node/cli-version-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface CLIVersions {

interface PackageMetadata {
'dist-tags': {latest?: string}
versions: Record<string, {version: string; deprecated?: string}>
versions: Record<string, {version: string; deprecated?: string; engines?: {node?: string}}>
}

const pendingLookups = new Map<string, Promise<CLIVersions>>()
Expand Down Expand Up @@ -85,7 +85,10 @@ async function fetchAndCacheVersions(registry: string): Promise<CLIVersions> {
if (!latest || !valid(latest) || !metadata.versions[latest]) {
throw new Error('The npm registry did not return a stable latest CLI version.')
}
const eligibleVersions = Object.keys(metadata.versions).filter((version) => !metadata.versions[version]?.deprecated)
const eligibleVersions = Object.keys(metadata.versions).filter((version) => {
const release = metadata.versions[version]
return !release?.deprecated && supportsRunningNode(release?.engines?.node)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth fixing before the minor that raises the Node floor: this filter runs only when this CLI fetches. The target goes to npm-package-@shopify/cli@5.x:registry:<hash>, a key that every installed 5.x CLI shares, and versionToAutoUpgrade reads it with no Node check.

A common case: the fixed CLI is installed globally, and an app's package.json pins an older 5.x @shopify/cli. The older CLI's prerun lookup writes the unfiltered target to that key. The next global command reads it from the cache, sends no request, and auto-upgrades into a release that excludes its Node. One CLI does the same when the user switches Node versions within a day.

I proved it at this head with a temporary test. I seeded both keys with a newer release whose engines.node excludes the running Node, as an older CLI would. Then checkForNewVersionInBackground() sent 0 requests, and versionToAutoUpgrade() returned the excluded release.

Suggested fix, on main first and then here: add the running Node to the current-major key, for example a :node:${process.version} suffix, everywhere the key is written or read, getCachedCurrentMajorCLIVersion included. Older CLIs keep writing the old key, so they can't feed the new one. The cost is one more registry request per Node version per day.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, confirmed. The fix follows your suggestion: the target is now cached under a key that names the running Node, everywhere it's read or written, getCachedCurrentMajorCLIVersion included, so older CLIs can't feed it. Two new tests cover an older CLI's cached target and a Node switch, and both fail without the fix.

The same gap reached shopify upgrade and the upgrade notice, which read a cached latest that an older CLI wrote without its Node range. A cached latest now counts only when the lookup cached its range too. Otherwise the lookup runs again.

Both fixes are in one pull request for main, and you're requested on it: "Stop auto-upgrade and shopify upgrade from acting on another CLI's update check". Its backport to stable/5.2 comes after this one and the shopify upgrade backport, before 5.2.2.

})
// The latest dist-tag gates rollout in its own major, so a rolled-back release is never selected.
const eligibleRange = satisfies(latest, currentMajorRange) ? `${currentMajorRange} <=${latest}` : currentMajorRange
const currentMajor = maxSatisfying(eligibleVersions, eligibleRange)
Expand All @@ -102,3 +105,9 @@ async function fetchAndCacheVersions(registry: string): Promise<CLIVersions> {
return {}
}
}

// A release whose engines exclude the running Node installs with a warning under npm and fails to install under
// Yarn 1, so auto-upgrade must leave the CLI on the newest release that supports this Node.
function supportsRunningNode(nodeRange: string | undefined): boolean {
return nodeRange === undefined || satisfies(process.version, nodeRange, {includePrerelease: true})
}
Loading