这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: makes affectedPackages lockfile aware #9795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/turborepo-paths/src/absolute_system_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ impl AbsoluteSystemPath {
)
}

/// Note: This does not handle resolutions, so `../` in a path won't
/// resolve.
pub fn anchor(&self, path: &AbsoluteSystemPath) -> Result<AnchoredSystemPathBuf, PathError> {
AnchoredSystemPathBuf::new(self, path)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/turborepo-paths/src/anchored_system_path_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ impl<'a> From<&'a AnchoredSystemPathBuf> for wax::CandidatePath<'a> {
}

impl AnchoredSystemPathBuf {
/// Note: This does not handle resolutions, so `../` in a path won't
/// resolve.
pub fn new(
root: impl AsRef<AbsoluteSystemPath>,
path: impl AsRef<AbsoluteSystemPath>,
Expand Down
6 changes: 6 additions & 0 deletions crates/turborepo-repository/src/change_mapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ impl<'a, PD: PackageChangeMapper> ChangeMapper<'a, PD> {
pub fn changed_packages(
&self,
changed_files: HashSet<AnchoredSystemPathBuf>,
// None - we don't know if the lockfile changed
//
// Some(None) - we know the lockfile changed, but don't know exactly why (i.e. `git status`
// and the lockfile is there)
//
// Some(Some(content)) - we know the lockfile changed and have the contents
lockfile_change: Option<Option<Vec<u8>>>,
) -> Result<PackageChanges, ChangeMapError> {
if let Some(file) = Self::default_global_file_changed(&changed_files) {
Expand Down
30 changes: 29 additions & 1 deletion packages/turbo-repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ for the JS API.

This package contains scripts to build dev and release versions. `pnpm build && pnpm package` will build and package a dev version of the native library for `darwin-arm64`, or you can pass an additional argument for a specific target. `pnpm build:release` will build a release version of the library

# Publishing
## Setup

Install JS dependencies:

```sh
pnpm install
```

## Build

Build native library and TypeScript wrapper:

```sh
pnpm build
cargo build
```

## Test

```sh
pnpm test
```

## Example Usage

You can see examples in the `__tests__` directory, or see a simple script in `node scripts/test.mjs`.
Note that this may fall out of date over time, but it's meant to be used during the early iterations.

## Publishing

There is now a version bump script in [bump-version.sh](./scripts/bump-version.sh). Passing it the new version will bump the meta package version, as well as the optional dependencies list and native packages.
45 changes: 36 additions & 9 deletions packages/turbo-repository/__tests__/affected-packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { Workspace, Package, PackageManager } from "../js/dist/index.js";
type PackageReduced = Pick<Package, "name" | "relativePath">;

interface AffectedPackagesTestParams {
description: string;
files: string[];
changedLockfile?: string | undefined | null;
expected: PackageReduced[];
description: string;
}

describe("affectedPackages", () => {
Expand All @@ -33,22 +34,48 @@ describe("affectedPackages", () => {
},
{
description:
"global change should be irrelevant but still triggers all packages",
files: ["README.md"],
expected: [
{ name: "app-a", relativePath: "apps/app" },
{ name: "ui", relativePath: "packages/ui" },
],
"a lockfile change will only affect packages impacted by the change",
files: [],
changedLockfile: `lockfileVersion: '6.0'

settings:
autoInstallPeers: true
excludeLinksFromLockfile: false

importers:

.: {}

apps/app:
dependencies:
microdiff:
specifier: ^1.4.0
version: 1.5.0
ui:
specifier: workspace:*
version: link:../../packages/ui

packages/blank: {}

packages/ui: {}

packages:

/microdiff@1.5.0:
resolution: {integrity: sha512-Drq+/THMvDdzRYrK0oxJmOKiC24ayUV8ahrt8l3oRK51PWt6gdtrIGrlIH3pT/lFh1z93FbAcidtsHcWbnRz8Q==}
dev: false
`,
expected: [{ name: "app-a", relativePath: "apps/app" }],
},
];

for (const { description, files, expected } of tests) {
for (const { description, files, expected, changedLockfile } of tests) {
it(description, async () => {
const dir = path.resolve(__dirname, "./fixtures/monorepo");
const workspace = await Workspace.find(dir);

const reduced: PackageReduced[] = (
await workspace.affectedPackages(files)
await workspace.affectedPackages(files, changedLockfile)
).map((pkg) => {
return {
name: pkg.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "app-a",
"dependencies": {
"ui": "*"
"microdiff": "^1.4.0",
"ui": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "basic",
"packageManager": "pnpm@8.1.0"
"packageManager": "pnpm@8.14.0"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/turbo-repository/js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ export class Workspace {
* of strings relative to the monorepo root and use the current system's
* path separator.
*/
affectedPackages(files: Array<string>): Promise<Array<Package>>;
affectedPackages(
files: Array<string>,
changedLockfile?: string | undefined | null
): Promise<Array<Package>>;
}
3 changes: 3 additions & 0 deletions packages/turbo-repository/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ tokio = { workspace = true }
turbopath = { workspace = true }
turborepo-repository = { workspace = true }

[dev-dependencies]
pretty_assertions = { workspace = true }

[build-dependencies]
napi-build = "2.0.1"
21 changes: 3 additions & 18 deletions packages/turbo-repository/rust/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
## Setup:
# `turbo-repository`

Install JS dependencies:
This sub-crate provides repository helpers which are written in Rust and translated to TypeScript with [NAPI-RS](https://napi.rs).

```
pnpm i
```

## Build:

Build native library and js wrapper

```sh
pnpm build
```

## Example Usage

You can see examples in the `__tests__` directory, or see a simple script in `node scripts/test.mjs`.
Note that this may fall out of date over time, but it's meant to be used during the early iterations.
See the [root `README.md` for instructions](../README.md).
19 changes: 12 additions & 7 deletions packages/turbo-repository/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use napi_derive::napi;
use turbopath::{AbsoluteSystemPath, AnchoredSystemPathBuf};
use turborepo_repository::{
change_mapper::{ChangeMapper, DefaultPackageChangeMapper, PackageChanges},
change_mapper::{ChangeMapper, GlobalDepsPackageChangeMapper, PackageChanges},
inference::RepoState as WorkspaceState,
package_graph::{PackageGraph, PackageName, PackageNode, WorkspacePackage, ROOT_PKG_NAME},
};
mod internal;

#[napi]

Check warning on line 16 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 16 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 16 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

unexpected `cfg` condition value: `noop`

Check warning on line 16 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust lints

unexpected `cfg` condition value: `noop`

Check warning on line 16 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 16 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

unexpected `cfg` condition value: `noop`

Check warning on line 16 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 16 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

unexpected `cfg` condition value: `noop`
#[derive(PartialEq, Eq, Hash, Clone)]
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct Package {
pub name: String,
/// The absolute path to the package root.
Expand All @@ -27,7 +27,7 @@

/// Wrapper for dependents and dependencies.
/// Each are a list of package paths, relative to the workspace root.
#[napi]

Check warning on line 30 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 30 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 30 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

unexpected `cfg` condition value: `noop`

Check warning on line 30 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust lints

unexpected `cfg` condition value: `noop`

Check warning on line 30 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 30 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

unexpected `cfg` condition value: `noop`

Check warning on line 30 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 30 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

unexpected `cfg` condition value: `noop`
#[derive(Debug)]
pub struct PackageDetails {
/// the package's dependencies
Expand All @@ -39,14 +39,14 @@
}

#[derive(Clone)]
#[napi]

Check warning on line 42 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 42 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 42 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

unexpected `cfg` condition value: `noop`

Check warning on line 42 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust lints

unexpected `cfg` condition value: `noop`

Check warning on line 42 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 42 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

unexpected `cfg` condition value: `noop`

Check warning on line 42 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 42 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

unexpected `cfg` condition value: `noop`
pub struct PackageManager {
/// The package manager name in lower case.
#[napi(readonly)]
pub name: String,
}

#[napi]

Check warning on line 49 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 49 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 49 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

unexpected `cfg` condition value: `noop`

Check warning on line 49 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust lints

unexpected `cfg` condition value: `noop`

Check warning on line 49 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 49 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

unexpected `cfg` condition value: `noop`

Check warning on line 49 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 49 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

unexpected `cfg` condition value: `noop`
pub struct Workspace {
workspace_state: WorkspaceState,
/// The absolute path to the workspace root.
Expand All @@ -62,7 +62,7 @@
graph: PackageGraph,
}

#[napi]

Check warning on line 65 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 65 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 65 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

unexpected `cfg` condition value: `noop`

Check warning on line 65 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust lints

unexpected `cfg` condition value: `noop`

Check warning on line 65 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 65 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

unexpected `cfg` condition value: `noop`

Check warning on line 65 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 65 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

unexpected `cfg` condition value: `noop`
impl Package {
fn new(
name: String,
Expand Down Expand Up @@ -127,7 +127,7 @@
}
}

#[napi]

Check warning on line 130 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 130 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (ubuntu, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 130 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

unexpected `cfg` condition value: `noop`

Check warning on line 130 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust lints

unexpected `cfg` condition value: `noop`

Check warning on line 130 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 20)

unexpected `cfg` condition value: `noop`

Check warning on line 130 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

unexpected `cfg` condition value: `noop`

Check warning on line 130 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / @turbo/repository (macos, Node 18)

unexpected `cfg` condition value: `noop`

Check warning on line 130 in packages/turbo-repository/rust/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

unexpected `cfg` condition value: `noop`
impl Workspace {
/// Finds the workspace root from the given path, and returns a new
/// Workspace.
Expand Down Expand Up @@ -187,12 +187,15 @@
/// of strings relative to the monorepo root and use the current system's
/// path separator.
#[napi]
pub async fn affected_packages(&self, files: Vec<String>) -> Result<Vec<Package>, Error> {
pub async fn affected_packages(
&self,
files: Vec<String>,
changed_lockfile: Option<String>,
) -> Result<Vec<Package>, Error> {
let workspace_root = match AbsoluteSystemPath::new(&self.absolute_path) {
Ok(path) => path,
Err(e) => return Err(Error::from_reason(e.to_string())),
};

let hash_set_of_paths: HashSet<AnchoredSystemPathBuf> = files
.into_iter()
.filter_map(|path| {
Expand All @@ -203,9 +206,11 @@
.collect();

// Create a ChangeMapper with no ignore patterns
let default_package_detector = DefaultPackageChangeMapper::new(&self.graph);
let mapper = ChangeMapper::new(&self.graph, vec![], default_package_detector);
let package_changes = match mapper.changed_packages(hash_set_of_paths, None) {
let global_deps_package_detector =
GlobalDepsPackageChangeMapper::new(&self.graph, std::iter::empty::<&str>()).unwrap();
let mapper = ChangeMapper::new(&self.graph, vec![], global_deps_package_detector);
let lockfile_change = changed_lockfile.map(|s| Some(s.into_bytes()));
let package_changes = match mapper.changed_packages(hash_set_of_paths, lockfile_change) {
Ok(changes) => changes,
Err(e) => return Err(Error::from_reason(e.to_string())),
};
Expand Down
Loading