Skip to content

Commit e83f303

Browse files
fix regression and tests
1 parent d731dd6 commit e83f303

File tree

6 files changed

+69
-45
lines changed

6 files changed

+69
-45
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/turborepo-repository/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ biome_diagnostics = { workspace = true }
1717
biome_json_parser = { workspace = true }
1818
biome_json_syntax = { workspace = true }
1919

20+
either = { workspace = true }
2021
globwalk = { version = "0.1.0", path = "../turborepo-globwalk" }
2122
itertools = { workspace = true }
2223
lazy-regex = "2.5.0"

crates/turborepo-repository/src/change_mapper/package.rs

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ pub trait PackageChangeMapper {
2424
fn detect_package(&self, file: &AnchoredSystemPath) -> PackageMapping;
2525
}
2626

27+
impl<L, R> PackageChangeMapper for either::Either<L, R>
28+
where
29+
L: PackageChangeMapper,
30+
R: PackageChangeMapper,
31+
{
32+
fn detect_package(&self, file: &AnchoredSystemPath) -> PackageMapping {
33+
match self {
34+
either::Either::Left(l) => l.detect_package(file),
35+
either::Either::Right(r) => r.detect_package(file),
36+
}
37+
}
38+
}
39+
2740
/// Detects package by checking if the file is inside the package.
2841
///
2942
/// Does *not* use the `globalDependencies` in turbo.json.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it } from "node:test";
1+
import { beforeEach, describe, it } from "node:test";
22
import { strict as assert } from "node:assert";
33
import * as path from "node:path";
44
import { Workspace, Package, PackageManager } from "../js/dist/index.js";
@@ -8,7 +8,6 @@ type PackageReduced = Pick<Package, "name" | "relativePath">;
88
interface AffectedPackagesTestParams {
99
description: string;
1010
files: string[];
11-
changedLockfile?: string | undefined | null;
1211
expected: PackageReduced[];
1312
}
1413

@@ -33,49 +32,22 @@ describe("affectedPackages", () => {
3332
],
3433
},
3534
{
36-
description:
37-
"a lockfile change will only affect packages impacted by the change",
38-
files: [],
39-
changedLockfile: `lockfileVersion: '6.0'
40-
41-
settings:
42-
autoInstallPeers: true
43-
excludeLinksFromLockfile: false
44-
45-
importers:
46-
47-
.: {}
48-
49-
apps/app:
50-
dependencies:
51-
microdiff:
52-
specifier: ^1.4.0
53-
version: 1.5.0
54-
ui:
55-
specifier: workspace:*
56-
version: link:../../packages/ui
57-
58-
packages/blank: {}
59-
60-
packages/ui: {}
61-
62-
packages:
63-
64-
65-
resolution: {integrity: sha512-Drq+/THMvDdzRYrK0oxJmOKiC24ayUV8ahrt8l3oRK51PWt6gdtrIGrlIH3pT/lFh1z93FbAcidtsHcWbnRz8Q==}
66-
dev: false
67-
`,
68-
expected: [{ name: "app-a", relativePath: "apps/app" }],
35+
description: "a lockfile change will affect all packages",
36+
files: ["pnpm-lock.yaml"],
37+
expected: [
38+
{ name: "app-a", relativePath: "apps/app" },
39+
{ name: "ui", relativePath: "packages/ui" },
40+
],
6941
},
7042
];
7143

72-
for (const { description, files, expected, changedLockfile } of tests) {
44+
for (const { description, files, expected } of tests) {
7345
it(description, async () => {
7446
const dir = path.resolve(__dirname, "./fixtures/monorepo");
7547
const workspace = await Workspace.find(dir);
7648

7749
const reduced: PackageReduced[] = (
78-
await workspace.affectedPackages(files, changedLockfile)
50+
await workspace.affectedPackages(files)
7951
).map((pkg) => {
8052
return {
8153
name: pkg.name,
@@ -86,4 +58,15 @@ packages:
8658
assert.deepEqual(reduced, expected);
8759
});
8860
}
61+
62+
describe("optimizedLockfileUpdates", () => {
63+
it("errors if not provided comparison ref", async () => {
64+
const dir = path.resolve(__dirname, "./fixtures/monorepo");
65+
const workspace = await Workspace.find(dir);
66+
67+
assert.rejects(
68+
workspace.affectedPackages(["pnpm-lock.yaml"], null, true)
69+
);
70+
});
71+
});
8972
});

packages/turbo-repository/rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["cdylib"]
1111
workspace = true
1212

1313
[dependencies]
14+
either = { workspace = true }
1415
napi = { version = "2.14.0", features = ["tokio_rt"] }
1516
napi-derive = "2.14.0"
1617
thiserror = { workspace = true }

packages/turbo-repository/rust/src/lib.rs

+32-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ use std::{
33
hash::Hash,
44
};
55

6+
use either::Either;
67
use napi::Error;
78
use napi_derive::napi;
89
use tracing::debug;
910
use turbopath::{AbsoluteSystemPath, AnchoredSystemPath, AnchoredSystemPathBuf};
1011
use turborepo_repository::{
1112
change_mapper::{
12-
ChangeMapper, GlobalDepsPackageChangeMapper, LockfileContents, PackageChanges,
13+
ChangeMapper, DefaultPackageChangeMapper, GlobalDepsPackageChangeMapper, LockfileContents,
14+
PackageChanges,
1315
},
1416
inference::RepoState as WorkspaceState,
1517
package_graph::{PackageGraph, PackageName, PackageNode, WorkspacePackage, ROOT_PKG_NAME},
@@ -190,15 +192,15 @@ impl Workspace {
190192
&self,
191193
changed_files: &HashSet<AnchoredSystemPathBuf>,
192194
workspace_root: &AbsoluteSystemPath,
193-
from_commit: Option<&str>,
195+
from_commit: &str,
194196
) -> LockfileContents {
195197
let lockfile_name = self.graph.package_manager().lockfile_name();
196198
changed_files
197199
.contains(AnchoredSystemPath::new(&lockfile_name).unwrap())
198200
.then(|| {
199201
let git = SCM::new(workspace_root);
200202
let anchored_path = workspace_root.join_component(lockfile_name);
201-
git.previous_content(from_commit, &anchored_path)
203+
git.previous_content(Some(from_commit), &anchored_path)
202204
.map(LockfileContents::Changed)
203205
.inspect_err(|e| debug!("{e}"))
204206
.ok()
@@ -218,6 +220,16 @@ impl Workspace {
218220
comparison: Option<&str>, // this is required when optimize_global_invalidations is true
219221
optimize_global_invalidations: Option<bool>,
220222
) -> Result<Vec<Package>, Error> {
223+
let comparison = optimize_global_invalidations
224+
.unwrap_or(false)
225+
.then(|| {
226+
comparison.ok_or_else(|| {
227+
Error::from_reason(
228+
"optimizeGlobalInvalidations true, but no comparison commit given",
229+
)
230+
})
231+
})
232+
.transpose()?;
221233
let workspace_root = match AbsoluteSystemPath::new(&self.absolute_path) {
222234
Ok(path) => path,
223235
Err(e) => return Err(Error::from_reason(e.to_string())),
@@ -232,12 +244,24 @@ impl Workspace {
232244
.collect();
233245

234246
// Create a ChangeMapper with no ignore patterns
235-
let global_deps_package_detector =
236-
GlobalDepsPackageChangeMapper::new(&self.graph, std::iter::empty::<&str>()).unwrap();
237-
let mapper = ChangeMapper::new(&self.graph, vec![], global_deps_package_detector);
247+
let change_detector = comparison
248+
.is_some()
249+
.then(|| {
250+
Either::Left(
251+
GlobalDepsPackageChangeMapper::new(&self.graph, std::iter::empty::<&str>())
252+
.unwrap(),
253+
)
254+
})
255+
.unwrap_or_else(|| Either::Right(DefaultPackageChangeMapper::new(&self.graph)));
256+
let mapper = ChangeMapper::new(&self.graph, vec![], change_detector);
238257

239-
let lockfile_contents = if let Some(true) = optimize_global_invalidations {
240-
self.get_lockfile_contents(&changed_files, &workspace_root, comparison)
258+
let lockfile_contents = if let Some(comparison) = comparison {
259+
self.get_lockfile_contents(&changed_files, workspace_root, comparison)
260+
} else if changed_files.contains(
261+
AnchoredSystemPath::new(self.graph.package_manager().lockfile_name())
262+
.expect("the lockfile name will not be an absolute path"),
263+
) {
264+
LockfileContents::UnknownChange
241265
} else {
242266
LockfileContents::Unknown
243267
};

0 commit comments

Comments
 (0)