fix(plugins): force the checkout of a pinned plugin commit, so a build's changed package-lock.json can't block it (#2639)

This commit is contained in:
saberzero1 committed 2026-11-22 01:06:13 +01:00
1 parent 3960252445
commit 166d09b876
3 files changed
+61 -3

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
`quartz plugin install` moves a plugin to its pinned commit even when the plugin's build left tracked files changed (its `package-lock.json`). In CI, a plugin cache restored through `restore-keys` kept those changes, and installing an updated plugin failed with "Please commit your changes or stash them" (#2639).
+4 -2
View File
@@ -176,11 +176,13 @@ async function cloneAtCommitAsync({ url, ref, commit, dir }) {
/**
* Move a shallow clone to a pinned commit. The commit is fetched by SHA (GitHub and most hosts
* allow fetching reachable commits), so a pin that is no longer the branch tip works (#2559).
* The checkout is forced: the plugin directory is Quartz's clone, and its build (npm install)
* rewrites tracked files such as package-lock.json, which a restored CI cache keeps (#2639).
*/
async function checkoutPinnedCommitAsync(dir, commit) {
export async function checkoutPinnedCommitAsync(dir, commit) {
assertCommit(commit)
await gitAsync(["fetch", "--depth", "1", "origin", commit], { cwd: dir })
await gitAsync(["checkout", "--quiet", commit], { cwd: dir })
await gitAsync(["checkout", "--quiet", "--force", commit], { cwd: dir })
}
/**
+52 -1
View File
@@ -4,7 +4,13 @@ import { execSync } from "child_process"
import fs from "fs"
import os from "os"
import path from "path"
import { buildPluginAsync, linkPeerPlugins, pluginSourceChanged } from "./plugin-git-handlers.js"
import { pathToFileURL } from "url"
import {
buildPluginAsync,
checkoutPinnedCommitAsync,
linkPeerPlugins,
pluginSourceChanged,
} from "./plugin-git-handlers.js"
let root
let hostPreact
@@ -161,3 +167,48 @@ describe("pluginSourceChanged", () => {
assert.strictEqual(pluginSourceChanged(priorEntry, "github:owner/new-repo"), true)
})
})
describe("checkoutPinnedCommitAsync", () => {
const git = (cwd, cmd) =>
execSync(`git -c user.name=t -c user.email=t@t -c commit.gpgsign=false ${cmd}`, {
cwd,
stdio: "pipe",
timeout: 30000,
})
.toString()
.trim()
function makeRemote() {
const remote = path.join(root, "remote")
fs.mkdirSync(remote)
git(remote, "init -q -b main")
git(remote, "config uploadpack.allowAnySHA1InWant true")
fs.writeFileSync(path.join(remote, "package-lock.json"), `{ "v": 1 }`)
git(remote, "add -A")
git(remote, "commit -q -m one")
const first = git(remote, "rev-parse HEAD")
fs.writeFileSync(path.join(remote, "package-lock.json"), `{ "v": 2 }`)
git(remote, "commit -q -am two")
return { remote, first }
}
test("moves to a pinned commit that is not the branch tip", async () => {
const { remote, first } = makeRemote()
const dir = path.join(root, "plugin")
git(root, `clone -q --depth 1 ${pathToFileURL(remote).href} "${dir}"`)
await checkoutPinnedCommitAsync(dir, first)
assert.strictEqual(git(dir, "rev-parse HEAD"), first)
})
// a plugin's build (npm install) rewrites its tracked package-lock.json; a restored CI cache
// keeps that change, and the checkout refused to move ("commit your changes or stash them")
test("moves even when the plugin's build left tracked files changed (#2639)", async () => {
const { remote, first } = makeRemote()
const dir = path.join(root, "plugin")
git(root, `clone -q --depth 1 ${pathToFileURL(remote).href} "${dir}"`)
fs.writeFileSync(path.join(dir, "package-lock.json"), `{ "v": "rewritten by npm" }`)
await checkoutPinnedCommitAsync(dir, first)
assert.strictEqual(git(dir, "rev-parse HEAD"), first)
assert.strictEqual(fs.readFileSync(path.join(dir, "package-lock.json"), "utf-8"), `{ "v": 1 }`)
})
})