-
Notifications
You must be signed in to change notification settings - Fork 191
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
[perf] Improve install and ensure perf #2076
Merged
+101
−33
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,49 +48,77 @@ func StorePathsFromInstallable(ctx context.Context, installable string, allowIns | |
|
||
return nil, err | ||
} | ||
return parseStorePathFromInstallableOutput(installable, resultBytes) | ||
|
||
validPaths, err := parseStorePathFromInstallableOutput(resultBytes) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse path-info for %s: %w", installable, err) | ||
} | ||
|
||
return maps.Keys(validPaths), nil | ||
} | ||
|
||
// StorePathsAreInStore returns true if the store path is in the store | ||
// It relies on `nix store ls` to check if the store path is in the store | ||
func StorePathsAreInStore(ctx context.Context, storePaths []string) (bool, error) { | ||
// StorePathsAreInStore a map of store paths to whether they are in the store. | ||
func StorePathsAreInStore(ctx context.Context, storePaths []string) (map[string]bool, error) { | ||
defer debug.FunctionTimer().End() | ||
if len(storePaths) == 0 { | ||
return map[string]bool{}, nil | ||
} | ||
args := append([]string{"path-info", "--offline", "--json"}, storePaths...) | ||
cmd := commandContext(ctx, args...) | ||
debug.Log("Running cmd %s", cmd) | ||
output, err := cmd.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
validPaths, err := parseStorePathFromInstallableOutput(output) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := map[string]bool{} | ||
for _, storePath := range storePaths { | ||
cmd := commandContext(ctx, "store", "ls", storePath) | ||
debug.Log("Running cmd %s", cmd) | ||
if err := cmd.Run(); err != nil { | ||
if exitErr := (&exec.ExitError{}); errors.As(err, &exitErr) { | ||
return false, nil | ||
} | ||
return false, err | ||
} | ||
_, ok := validPaths[storePath] | ||
result[storePath] = ok | ||
} | ||
return true, nil | ||
|
||
return result, nil | ||
} | ||
|
||
// Older nix versions (like 2.17) are an array of objects that contain path and valid fields | ||
type pathInfoLegacy struct { | ||
Path string `json:"path"` | ||
Valid bool `json:"valid"` | ||
} | ||
|
||
// parseStorePathFromInstallableOutput parses the output of `nix store path-from-installable --json` | ||
// This function is decomposed out of StorePathFromInstallable to make it testable. | ||
func parseStorePathFromInstallableOutput(installable string, output []byte) ([]string, error) { | ||
// Newer nix versions (like 2.20) | ||
func parseStorePathFromInstallableOutput(output []byte) (map[string]any, error) { | ||
// Newer nix versions (like 2.20) have output of the form | ||
// {"<store-path>": {}} | ||
// if a store path is used as an installable, the keys will be present even if invalid but | ||
// the values will be null. | ||
var out1 map[string]any | ||
if err := json.Unmarshal(output, &out1); err == nil { | ||
return maps.Keys(out1), nil | ||
maps.DeleteFunc(out1, func(k string, v any) bool { | ||
return v == nil | ||
}) | ||
return out1, nil | ||
} | ||
|
||
// Older nix versions (like 2.17) | ||
var out2 []struct { | ||
Path string `json:"path"` | ||
Valid bool `json:"valid"` | ||
} | ||
var out2 []pathInfoLegacy | ||
|
||
if err := json.Unmarshal(output, &out2); err == nil { | ||
res := []string{} | ||
res := map[string]any{} | ||
for _, outValue := range out2 { | ||
res = append(res, outValue.Path) | ||
if outValue.Valid { | ||
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. 👍🏾 |
||
res[outValue.Path] = true | ||
} | ||
} | ||
return res, nil | ||
} | ||
|
||
return nil, fmt.Errorf("failed to parse store path from installable (%s) output: %s", installable, output) | ||
return nil, fmt.Errorf("failed to parse path-info output: %s", output) | ||
} | ||
|
||
// DaemonError reports an unsuccessful attempt to connect to the Nix daemon. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
oh good find!