diff --git a/nix_update/update.py b/nix_update/update.py index e077b37..e574085 100644 --- a/nix_update/update.py +++ b/nix_update/update.py @@ -308,7 +308,17 @@ def disable_copystat(): cwd=tempdir, ) - shutil.copy(tempdir + "/Cargo.lock", Path(filename).parent / "Cargo.lock") + if ( + lockfile_in_subdir := Path(tempdir) + / opts.lockfile_metadata_path + / "Cargo.lock" + ).exists(): + # if Cargo.toml is outside a workspace, Cargo.lock is generated in the same directory as Cargo.toml + lockfile = lockfile_in_subdir + else: + lockfile = Path(tempdir) / "Cargo.lock" + + shutil.copy(lockfile, Path(filename).parent / "Cargo.lock") def update_composer_deps_hash(opts: Options, filename: str, current_hash: str) -> None: diff --git a/tests/test_cargo_lock_generate.py b/tests/test_cargo_lock_generate.py index bcf777e..2b6c347 100644 --- a/tests/test_cargo_lock_generate.py +++ b/tests/test_cargo_lock_generate.py @@ -85,3 +85,50 @@ def test_with_lockfile_metadata_path(helpers: conftest.Helpers) -> None: assert ( "https://github.com/lancedb/lancedb/compare/python-v0.11.0...0.12.0" in diff ) + + +def test_with_lockfile_metadata_path_outside_workspace( + helpers: conftest.Helpers, +) -> None: + """A test for a project where the target Cargo.toml is outside a workspace. + + In this case, Cargo.lock is generated in the subdirectory where Cargo.toml is located, not in the project root. + For example, https://github.com/lancedb/lance/blob/v0.16.1/Cargo.toml + """ + with helpers.testpkgs(init_git=True) as path: + main( + [ + "--file", + str(path), + "--commit", + "cargoLock.generate.with-lockfile-metadata-path-outside-workspace", + "--version", + "v0.16.1", + "--generate-lockfile", + "--lockfile-metadata-path", + "python", + ] + ) + subprocess.run( + [ + "nix", + "eval", + "--raw", + "--extra-experimental-features", + "nix-command", + "-f", + path, + "cargoLock.generate.with-lockfile-metadata-path-outside-workspace.cargoDeps", + ], + check=True, + text=True, + stdout=subprocess.PIPE, + ).stdout.strip() + diff = subprocess.run( + ["git", "-C", path, "show"], + text=True, + stdout=subprocess.PIPE, + check=True, + ).stdout.strip() + print(diff) + assert "https://github.com/lancedb/lance/compare/v0.15.0...v0.16.1" in diff diff --git a/tests/testpkgs/cargo-lock-generate/with-lockfile-metadata-path-outside-workspace/Cargo.lock b/tests/testpkgs/cargo-lock-generate/with-lockfile-metadata-path-outside-workspace/Cargo.lock new file mode 100644 index 0000000..e69de29 diff --git a/tests/testpkgs/cargo-lock-generate/with-lockfile-metadata-path-outside-workspace/default.nix b/tests/testpkgs/cargo-lock-generate/with-lockfile-metadata-path-outside-workspace/default.nix new file mode 100644 index 0000000..f63def8 --- /dev/null +++ b/tests/testpkgs/cargo-lock-generate/with-lockfile-metadata-path-outside-workspace/default.nix @@ -0,0 +1,23 @@ +{ python3Packages +, rustPlatform +, fetchFromGitHub +}: + +python3Packages.buildPythonPackage rec { + pname = "pylance"; + version = "0.15.0"; + + src = fetchFromGitHub { + owner = "lancedb"; + repo = "lance"; + rev = "v${version}"; + hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; + }; + + nativeBuildInputs = [ + rustPlatform.cargoSetupHook + rustPlatform.maturinBuildHook + ]; + + cargoDeps = rustPlatform.importCargoLock { lockFile = ./Cargo.lock; }; +} diff --git a/tests/testpkgs/default.nix b/tests/testpkgs/default.nix index 3e37c52..006bfc0 100644 --- a/tests/testpkgs/default.nix +++ b/tests/testpkgs/default.nix @@ -5,6 +5,7 @@ cargoLock.expand = pkgs.callPackage ./cargo-lock-expand { }; cargoLock.generate.simple = pkgs.callPackage ./cargo-lock-generate/simple { }; cargoLock.generate.with-lockfile-metadata-path = pkgs.callPackage ./cargo-lock-generate/with-lockfile-metadata-path { }; + cargoLock.generate.with-lockfile-metadata-path-outside-workspace = pkgs.callPackage ./cargo-lock-generate/with-lockfile-metadata-path-outside-workspace { }; cargoLock.update = pkgs.callPackage ./cargo-lock-update { }; composer = pkgs.callPackage ./composer.nix { }; crate = pkgs.callPackage ./crate.nix { };