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
Changes from all commits
8c99b57
8d93b1e
ab9f0b3
f9a460c
4f9c395
31ef289
1c6d663
92f0b58
2efd867
57be317
0ff9dbc
1931953
fbdeffd
8c6c647
b0f20e7
d1c52da
fada352
9049e5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import "C" | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"unsafe" | ||
|
||
|
@@ -167,23 +168,28 @@ func PreviousContent(gitRoot, 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: toPackageManager(packageManager), | ||
Workspaces: flatWorkspaces, | ||
} | ||
reqBuf := Marshal(&req) | ||
resBuf := cFunc(reqBuf) | ||
resBuf := C.transitive_closure(reqBuf) | ||
reqBuf.Free() | ||
|
||
resp := ffi_proto.TransitiveDepsResponse{} | ||
|
@@ -195,8 +201,17 @@ 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 | ||
} | ||
|
||
func toPackageManager(packageManager string) ffi_proto.PackageManager { | ||
switch packageManager { | ||
case "npm": | ||
return ffi_proto.PackageManager_NPM | ||
default: | ||
panic(fmt.Sprintf("Invalid package manager string: %s", packageManager)) | ||
} | ||
} | ||
|
||
// NpmSubgraph returns the contents of a npm lockfile subgraph | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was this doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was here instead of proper control flow, if the closure calculation errored we would override the return value with an empty set. This would then get translated to
pkg.TransitiveDeps = make([]lockfile.Package, 0)
and nothing would get added.