Skip to content

Commit f73f84a

Browse files
NetailAndarist
andauthored
Correctly resolve new changesets with since option when the .changeset directory is not directly in the git root (#1636)
* fix: correctly fetch changesets (nested changeset directy from git root) * test: add addition unit tests * Update .changeset/big-maps-say.md * Update .changeset/big-maps-say.md --------- Co-authored-by: Mateusz Burzyński <[email protected]>
1 parent 5d01857 commit f73f84a

File tree

6 files changed

+107
-8
lines changed

6 files changed

+107
-8
lines changed

.changeset/big-maps-say.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@changesets/cli": patch
3+
"@changesets/read": patch
4+
"@changesets/git": patch
5+
---
6+
7+
author: @Netail
8+
9+
Correctly resolve new changesets with `since` option when the `.changeset` directory is not directly in the git root

packages/cli/src/commands/version/version.test.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,28 @@ describe("running version in a simple project", () => {
349349

350350
expect(spy).toHaveBeenCalled();
351351

352-
expect(spy).toHaveBeenCalledWith("packages/pkg-a/package.json", cwd);
353-
expect(spy).toHaveBeenCalledWith("packages/pkg-a/CHANGELOG.md", cwd);
352+
expect(spy).toHaveBeenCalledWith(
353+
path.join("packages", "pkg-a", "package.json"),
354+
cwd
355+
);
356+
expect(spy).toHaveBeenCalledWith(
357+
path.join("packages", "pkg-a", "CHANGELOG.md"),
358+
cwd
359+
);
354360

355-
expect(spy).toHaveBeenCalledWith("packages/pkg-b/package.json", cwd);
356-
expect(spy).toHaveBeenCalledWith("packages/pkg-b/CHANGELOG.md", cwd);
361+
expect(spy).toHaveBeenCalledWith(
362+
path.join("packages", "pkg-b", "package.json"),
363+
cwd
364+
);
365+
expect(spy).toHaveBeenCalledWith(
366+
path.join("packages", "pkg-b", "CHANGELOG.md"),
367+
cwd
368+
);
357369

358-
expect(spy).toHaveBeenCalledWith(`.changeset/${ids[0]}.md`, cwd);
370+
expect(spy).toHaveBeenCalledWith(
371+
path.join(".changeset", `${ids[0]}.md`),
372+
cwd
373+
);
359374
});
360375

361376
it("should commit the result if commit config is set", async () => {

packages/git/src/index.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,5 +812,42 @@ describe("git", () => {
812812
});
813813
expect(files).toEqual([`.changeset/${changesetId}.md`]);
814814
});
815+
816+
it("should still get the relative path to the changeset file if git config relative has been set to true", async () => {
817+
const cwd = await gitdir({
818+
"package.json": JSON.stringify({
819+
private: true,
820+
workspaces: ["packages/*"],
821+
}),
822+
"packages/pkg-a/package.json": JSON.stringify({
823+
name: "pkg-a",
824+
}),
825+
".changeset/config.json": JSON.stringify({}),
826+
});
827+
828+
await spawn("git", ["config", "diff.relative", "true"], {
829+
cwd,
830+
});
831+
832+
const changesetId = await writeChangeset(
833+
{
834+
releases: [
835+
{
836+
name: "pkg-a",
837+
type: "minor",
838+
},
839+
],
840+
summary: "Awesome summary",
841+
},
842+
cwd
843+
);
844+
await add(".changeset", cwd);
845+
846+
const files = await getChangedChangesetFilesSinceRef({
847+
cwd: path.join(cwd, ".changeset"),
848+
ref: "main",
849+
});
850+
expect(files).toEqual([`.changeset/${changesetId}.md`]);
851+
});
815852
});
816853
});

packages/read/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"devDependencies": {
3131
"@changesets/test-utils": "*",
32+
"@changesets/write": "*",
3233
"outdent": "^0.5.0"
3334
}
3435
}

packages/read/src/index.test.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import fs from "fs-extra";
2-
import path from "path";
2+
import path from "node:path";
33
import outdent from "outdent";
44

55
import read from "./";
6-
import { silenceLogsInBlock, testdir } from "@changesets/test-utils";
6+
import { gitdir, silenceLogsInBlock, testdir } from "@changesets/test-utils";
7+
import writeChangeset from "@changesets/write";
8+
import { add } from "@changesets/git";
79

810
silenceLogsInBlock();
911

@@ -216,4 +218,39 @@ Awesome feature, hidden behind a feature flag
216218
},
217219
]);
218220
});
221+
it("should read a nested changeset relative to git root", async () => {
222+
const cwd = await gitdir({
223+
"library/package.json": JSON.stringify({
224+
private: true,
225+
workspaces: ["packages/*"],
226+
}),
227+
"library/packages/pkg-a/package.json": JSON.stringify({
228+
name: "pkg-a",
229+
}),
230+
"library/.changeset/config.json": JSON.stringify({}),
231+
});
232+
233+
const changesetId = await writeChangeset(
234+
{
235+
releases: [
236+
{
237+
name: "pkg-a",
238+
type: "minor",
239+
},
240+
],
241+
summary: "Awesome summary",
242+
},
243+
path.join(cwd, "library")
244+
);
245+
await add("library/.changeset", cwd);
246+
247+
const changesets = await read(path.join(cwd, "library"), "main");
248+
expect(changesets).toEqual([
249+
{
250+
releases: [{ name: "pkg-a", type: "minor" }],
251+
summary: "Awesome summary",
252+
id: changesetId,
253+
},
254+
]);
255+
});
219256
});

packages/read/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function filterChangesetsSinceRef(
1414
cwd: changesetBase,
1515
ref: sinceRef,
1616
});
17-
const newHashes = newChangesets.map((c) => c.split("/")[1]);
17+
const newHashes = newChangesets.map((c) => c.split("/").pop());
1818

1919
return changesets.filter((dir) => newHashes.includes(dir));
2020
}

0 commit comments

Comments
 (0)