Skip to content
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

chore: move lockfile ffi boundary #4629

Merged
merged 18 commits into from Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/internal/ffi/bindings.h
Expand Up @@ -16,6 +16,6 @@ struct Buffer changed_files(struct Buffer buffer);

struct Buffer previous_content(struct Buffer buffer);

struct Buffer npm_transitive_closure(struct Buffer buf);
struct Buffer transitive_closure(struct Buffer buf);

struct Buffer npm_subgraph(struct Buffer buf);
35 changes: 20 additions & 15 deletions cli/internal/ffi/ffi.go
Expand Up @@ -168,23 +168,28 @@ func PreviousContent(repoRoot, fromCommit, filePath string) ([]byte, error) {
return []byte(content), nil
}

// NpmTransitiveDeps returns the transitive external deps of a given package based on the deps and specifiers given
func NpmTransitiveDeps(content []byte, pkgDir string, unresolvedDeps map[string]string) ([]*ffi_proto.LockfilePackage, error) {
return transitiveDeps(npmTransitiveDeps, content, pkgDir, unresolvedDeps)
}

func npmTransitiveDeps(buf C.Buffer) C.Buffer {
return C.npm_transitive_closure(buf)
}

func transitiveDeps(cFunc func(C.Buffer) C.Buffer, content []byte, pkgDir string, unresolvedDeps map[string]string) ([]*ffi_proto.LockfilePackage, error) {
// TransitiveDeps returns the transitive external deps for all provided workspaces
func TransitiveDeps(content []byte, packageManager string, workspaces map[string]map[string]string) (map[string]*ffi_proto.LockfilePackageList, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Confirming understanding: this has to do a full walk because if there are floating versions the top-level deps could have not changed, but somewhere way down the tree could have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, if someone blows away the lockfile and does a fresh install all of the top level specifiers will remain the same, but what version gets installed might change.

flatWorkspaces := make(map[string]*ffi_proto.PackageDependencyList)
for workspace, deps := range workspaces {
packageDependencyList := make([]*ffi_proto.PackageDependency, len(deps))
i := 0
for name, version := range deps {
packageDependencyList[i] = &ffi_proto.PackageDependency{
Name: name,
Range: version,
}
i++
}
flatWorkspaces[workspace] = &ffi_proto.PackageDependencyList{List: packageDependencyList}
}
req := ffi_proto.TransitiveDepsRequest{
Contents: content,
WorkspaceDir: pkgDir,
UnresolvedDeps: unresolvedDeps,
PackageManager: packageManager,
Workspaces: flatWorkspaces,
}
reqBuf := Marshal(&req)
resBuf := cFunc(reqBuf)
resBuf := C.transitive_closure(reqBuf)
reqBuf.Free()

resp := ffi_proto.TransitiveDepsResponse{}
Expand All @@ -196,8 +201,8 @@ func transitiveDeps(cFunc func(C.Buffer) C.Buffer, content []byte, pkgDir string
return nil, errors.New(err)
}

list := resp.GetPackages()
return list.GetList(), nil
dependencies := resp.GetDependencies()
return dependencies.GetDependencies(), nil
}

// NpmSubgraph returns the contents of a npm lockfile subgraph
Expand Down