Skip to content

[lockfile] add lockfile.Tidy and call from ensurePackagesAreInstalled #1109

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 6 commits into from
Jun 7, 2023

Conversation

savil
Copy link
Collaborator

@savil savil commented Jun 7, 2023

Summary

Problem:
if I change devbox.json from [email protected] to [email protected], and do devbox install, I still see the [email protected] in the devbox.lock (in addition to the new [email protected])

This PR ensures that when we update installed packages, we also update the lockfile
to have the set of packages that match those of the devbox.json config.

implementation note: I needed to rename lockfile.Packages to a different name
since devboxProject is embedded in the lockfile.File struct and we have a name
conflict.

Fixes #1095

How was it tested?

  • will add a testscript unit test that matches the steps below:
# initial devbox.lock file
(devbox) savil@Savil-Srivastavas-MacBook-Pro ~/c/j/devbox (savil/update-lockfile-v2)> cat devbox.lock
{
  "lockfile_version": "1",
  "packages": {
    "[email protected]": {
      "last_modified": "2023-03-31T22:52:29Z",
      "resolved": "github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#actionlint",
      "version": "1.6.23"
    },
    "[email protected]": {
      "last_modified": "2023-05-01T16:53:22Z",
      "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go",
      "version": "1.20.3"
    },
    "[email protected]": {
      "last_modified": "2023-05-01T16:53:22Z",
      "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#golangci-lint",
      "version": "1.52.2"
    }
  }
}

# edit the [email protected] to be [email protected]⏎
(devbox) savil@Savil-Srivastavas-MacBook-Pro ~/c/j/devbox (savil/update-lockfile-v2)> vim devbox.json
(devbox) savil@Savil-Srivastavas-MacBook-Pro ~/c/j/devbox (savil/update-lockfile-v2)> devbox install
Ensuring packages are installed.
Finished installing packages.


(devbox) savil@Savil-Srivastavas-MacBook-Pro ~/c/j/devbox (savil/update-lockfile-v2)> cat devbox.lock
{
  "lockfile_version": "1",
  "packages": {
    "[email protected]": {
      "last_modified": "2023-03-31T22:52:29Z",
      "resolved": "github:NixOS/nixpkgs/242246ee1e58f54d2322227fc5eef53b4a616a31#actionlint",
      "version": "1.6.23"
    },
    "[email protected]": {
      "last_modified": "2023-05-01T16:53:22Z",
      "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#go_1_19",
      "version": "1.19.8"
    },
    "[email protected]": {
      "last_modified": "2023-05-01T16:53:22Z",
      "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#golangci-lint",
      "version": "1.52.2"
    }
  }
}⏎
# Notice that the older [email protected] is now gone

Copy link
Collaborator Author

savil commented Jun 7, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@@ -127,6 +128,10 @@ func IsLegacyPackage(pkg string) bool {
!strings.HasPrefix(pkg, "/")
}

func (l *File) Tidy(project devboxProject) {
l.PackageMap = lo.PickByKeys(l.PackageMap, project.Packages())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeland86 not 100% confident that the keys in the lockfile are intended to match the package-names in devbox.json. Can you confirm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah they should always match. Right now nix.Input uses Input.String() instead of Input.Raw as a key (raw is the package name in devbox.json) but they always match for devbox packages. I think we should probably change those callsites to use input.Raw just to be sure (since for flakes Raw and String() might not be the same)

But for this use case this is safe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see. We should probably change that sooner than later...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@savil savil requested a review from mikeland73 June 7, 2023 01:08
Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, pending revert of Packages name change. You can also remove the Packages() function

Comment on lines 131 to 132
func (l *File) Tidy(project devboxProject) {
l.PackageMap = lo.PickByKeys(l.PackageMap, project.Packages())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need a parameter. The lockFile struct has access to devbox struct (i.e. l.devboxProject.Packages())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh good point

@@ -127,6 +128,10 @@ func IsLegacyPackage(pkg string) bool {
!strings.HasPrefix(pkg, "/")
}

func (l *File) Tidy(project devboxProject) {
l.PackageMap = lo.PickByKeys(l.PackageMap, project.Packages())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah they should always match. Right now nix.Input uses Input.String() instead of Input.Raw as a key (raw is the package name in devbox.json) but they always match for devbox packages. I think we should probably change those callsites to use input.Raw just to be sure (since for flakes Raw and String() might not be the same)

But for this use case this is safe.

@@ -22,7 +23,7 @@ type File struct {
resolver

LockFileVersion string `json:"lockfile_version"`
Packages map[string]*Package `json:"packages"`
PackageMap map[string]*Package `json:"packages"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to rename this. See this draft #1113

You can access conflicting functions on structs by specifying the embedded struct explicitly (e.g. lock.devboxProject.Packages())

That said, when embedded structs get this complicated it's probably better to get rid of them (and just make them explicit fields) (not something that needs to happen in this PR though).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that draft was useful, thanks!

That said, when embedded structs get this complicated it's probably better to get rid of them (and just make them explicit fields) (not something that needs to happen in this PR though).

yeah, that's what savil/update-lockfile-v1 was about. But I thought it may receive pushback so took this more conservative approach. See #1120

@savil savil merged commit dcce2b1 into main Jun 7, 2023
@savil savil deleted the savil/update-lockfile-v2 branch June 7, 2023 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Changing a package version keeps the old package version in the devbox.lock file
2 participants