Skip to content

Commit 63cb177

Browse files
authored
[runx] Add runx validate, lockfile resolve (#1542)
## Summary A few major changes to runx: * Updates newest runx version to allow for github token, validation, etc. * lockfile can now resolve runx packages. Exact versions are saved in lockfile and `install installs deterministic versions according to lockfile information. * devpkg can now validate runx packages ## How was it tested? ```bash devbox add runx:golangci/golangci-lint@latest devbox add runx:golangci/[email protected] devbox add runx:golangci/golangci-lint devbox rm runx:golangci/golangci-lint ``` Inspected devbox.json and devbox.lock in between every command.
1 parent d564fe2 commit 63cb177

File tree

12 files changed

+116
-47
lines changed

12 files changed

+116
-47
lines changed

.github/workflows/cli-tests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ defaults:
3737

3838
env:
3939
HOMEBREW_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40+
DEVBOX_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4041
HOMEBREW_NO_ANALYTICS: 1
4142
HOMEBREW_NO_AUTO_UPDATE: 1
4243
HOMEBREW_NO_EMOJI: 1

devbox.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
}
2323
},
2424
"runx:golangci/golangci-lint@latest": {
25-
"resolved": "runx:golangci/golangci-lint@latest"
25+
"resolved": "golangci/[email protected]",
26+
"version": "v1.54.2"
2627
}
2728
}
2829
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/stretchr/testify v1.8.4
3939
github.com/wk8/go-ordered-map/v2 v2.1.8
4040
github.com/zealic/go2node v0.1.0
41-
go.jetpack.io/pkg v0.0.0-20231002215645-9afeb0623fd3
41+
go.jetpack.io/pkg v0.0.0-20231006204718-f59feb213022
4242
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17
4343
golang.org/x/mod v0.12.0
4444
golang.org/x/sync v0.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ github.com/zealic/go2node v0.1.0 h1:ofxpve08cmLJBwFdI0lPCk9jfwGWOSD+s6216x0oAaA=
337337
github.com/zealic/go2node v0.1.0/go.mod h1:GrkFr+HctXwP7vzcU9RsgtAeJjTQ6Ud0IPCQAqpTfBg=
338338
go.jetpack.io/pkg v0.0.0-20231002215645-9afeb0623fd3 h1:aMydtVCHn7dfotOyV41VAxX5b5OOsCc4TxOXwDt38Yw=
339339
go.jetpack.io/pkg v0.0.0-20231002215645-9afeb0623fd3/go.mod h1:iaf3e/aENp5luwYFlfCxj+GsiwqHagbvRAY3bIdEgGA=
340+
go.jetpack.io/pkg v0.0.0-20231006204718-f59feb213022 h1:8TRpo0lYh1Y6zec8Px0yXbnVRXXs+yUPU+TnHNsREdA=
341+
go.jetpack.io/pkg v0.0.0-20231006204718-f59feb213022/go.mod h1:iaf3e/aENp5luwYFlfCxj+GsiwqHagbvRAY3bIdEgGA=
340342
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
341343
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
342344
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

internal/devconfig/env.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package devconfig
22

33
import (
4+
"context"
5+
46
"go.jetpack.io/devbox/internal/boxcli/usererr"
57
"go.jetpack.io/devbox/internal/integrations/envsec"
68
)
79

8-
func (c *Config) ComputedEnv(projectDir string) (map[string]string, error) {
10+
func (c *Config) ComputedEnv(
11+
ctx context.Context,
12+
projectDir string,
13+
) (map[string]string, error) {
914
env := map[string]string{}
1015
var err error
1116
if c.IsEnvsecEnabled() {
12-
env, err = envsec.Env(projectDir)
17+
env, err = envsec.Env(ctx, projectDir)
1318
if err != nil {
1419
return nil, err
1520
}

internal/devpkg/pkgtype/runx.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
package pkgtype
22

3-
import "strings"
3+
import (
4+
"context"
5+
"os"
6+
"strings"
7+
8+
"go.jetpack.io/pkg/sandbox/runx/impl/registry"
9+
"go.jetpack.io/pkg/sandbox/runx/impl/runx"
10+
)
411

512
const (
6-
RunXScheme = "runx"
7-
RunXPrefix = RunXScheme + ":"
13+
RunXScheme = "runx"
14+
RunXPrefix = RunXScheme + ":"
15+
githubAPITokenVarName = "DEVBOX_GITHUB_API_TOKEN"
816
)
917

18+
var cachedRegistry *registry.Registry
19+
1020
func IsRunX(s string) bool {
1121
return strings.HasPrefix(s, RunXPrefix)
1222
}
23+
24+
func RunXClient() *runx.RunX {
25+
return &runx.RunX{
26+
GithubAPIToken: os.Getenv(githubAPITokenVarName),
27+
}
28+
}
29+
30+
func RunXRegistry(ctx context.Context) (*registry.Registry, error) {
31+
if cachedRegistry == nil {
32+
var err error
33+
cachedRegistry, err = registry.NewLocalRegistry(ctx, os.Getenv(githubAPITokenVarName))
34+
if err != nil {
35+
return nil, err
36+
}
37+
}
38+
return cachedRegistry, nil
39+
}

internal/devpkg/validation.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package devpkg
22

33
import (
4+
"context"
45
"strings"
56

67
"go.jetpack.io/devbox/internal/boxcli/usererr"
78
"go.jetpack.io/devbox/internal/nix"
89
)
910

10-
func (p *Package) ValidateExists() (bool, error) {
11+
func (p *Package) ValidateExists(ctx context.Context) (bool, error) {
1112
if p.IsRunX() {
12-
// TODO implement runx validation
13-
return true, nil
13+
_, err := p.lockfile.Resolve(p.Raw)
14+
return err == nil, err
1415
}
1516
if p.isVersioned() && p.version() == "" {
1617
return false, usererr.New("No version specified for %q.", p.Path)

internal/impl/devbox.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
"github.com/pkg/errors"
2323
"github.com/samber/lo"
2424
"go.jetpack.io/devbox/internal/devpkg"
25+
"go.jetpack.io/devbox/internal/devpkg/pkgtype"
2526
"go.jetpack.io/devbox/internal/impl/envpath"
2627
"go.jetpack.io/devbox/internal/impl/generate"
2728
"go.jetpack.io/devbox/internal/searcher"
2829
"go.jetpack.io/devbox/internal/shellgen"
2930
"go.jetpack.io/devbox/internal/telemetry"
30-
"go.jetpack.io/pkg/sandbox/runx"
3131

3232
"go.jetpack.io/devbox/internal/boxcli/usererr"
3333
"go.jetpack.io/devbox/internal/cmdutil"
@@ -851,13 +851,13 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
851851
)
852852
}
853853

854-
env["PATH"], err = d.addUtilitiesToPath(env["PATH"])
854+
env["PATH"], err = d.addUtilitiesToPath(ctx, env["PATH"])
855855
if err != nil {
856856
return nil, err
857857
}
858858

859859
// Include env variables in devbox.json
860-
configEnv, err := d.configEnvs(env)
860+
configEnv, err := d.configEnvs(ctx, env)
861861
if err != nil {
862862
return nil, err
863863
}
@@ -885,7 +885,7 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
885885
})
886886
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, nixEnvPath)
887887

888-
runXPaths, err := d.RunXPaths()
888+
runXPaths, err := d.RunXPaths(ctx)
889889
if err != nil {
890890
return nil, err
891891
}
@@ -1051,8 +1051,11 @@ func (d *Devbox) checkOldEnvrc() error {
10511051
// their value in the existing env variables. Note, this doesn't
10521052
// allow env variables from outside the shell to be referenced so
10531053
// no leaked variables are caused by this function.
1054-
func (d *Devbox) configEnvs(existingEnv map[string]string) (map[string]string, error) {
1055-
env, err := d.cfg.ComputedEnv(d.ProjectDir())
1054+
func (d *Devbox) configEnvs(
1055+
ctx context.Context,
1056+
existingEnv map[string]string,
1057+
) (map[string]string, error) {
1058+
env, err := d.cfg.ComputedEnv(ctx, d.ProjectDir())
10561059
if err != nil {
10571060
return nil, err
10581061
}
@@ -1213,11 +1216,15 @@ func (d *Devbox) Lockfile() *lock.File {
12131216
return d.lockfile
12141217
}
12151218

1216-
func (d *Devbox) RunXPaths() (string, error) {
1219+
func (d *Devbox) RunXPaths(ctx context.Context) (string, error) {
12171220
packages := lo.Filter(d.InstallablePackages(), devpkg.IsRunX)
12181221
paths := []string{}
12191222
for _, pkg := range packages {
1220-
p, err := runx.Install(pkg.RunXPath())
1223+
lockedPkg, err := d.lockfile.Resolve(pkg.Raw)
1224+
if err != nil {
1225+
return "", err
1226+
}
1227+
p, err := pkgtype.RunXClient().Install(ctx, lockedPkg.Resolved)
12211228
if err != nil {
12221229
return "", err
12231230
}

internal/impl/packages.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
"github.com/pkg/errors"
1717
"github.com/samber/lo"
1818
"go.jetpack.io/devbox/internal/devpkg"
19+
"go.jetpack.io/devbox/internal/devpkg/pkgtype"
1920
"go.jetpack.io/devbox/internal/nix/nixprofile"
2021
"go.jetpack.io/devbox/internal/shellgen"
21-
"go.jetpack.io/pkg/sandbox/runx"
2222

2323
"go.jetpack.io/devbox/internal/boxcli/usererr"
2424
"go.jetpack.io/devbox/internal/debug"
@@ -78,7 +78,7 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
7878
versionedPkg := devpkg.PackageFromString(pkg.Versioned(), d.lockfile)
7979

8080
packageNameForConfig := pkg.Raw
81-
ok, err := versionedPkg.ValidateExists()
81+
ok, err := versionedPkg.ValidateExists(ctx)
8282
if (err == nil && ok) || errors.Is(err, devpkg.ErrCannotBuildPackageOnSystem) {
8383
// Only use versioned if it exists in search. We can disregard the error
8484
// about not building on the current system, since user's can continue
@@ -236,7 +236,7 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
236236
return err
237237
}
238238

239-
if err := d.InstallRunXPackages(); err != nil {
239+
if err := d.InstallRunXPackages(ctx); err != nil {
240240
return err
241241
}
242242

@@ -473,18 +473,17 @@ func resetProfileDirForFlakes(profileDir string) (err error) {
473473
return errors.WithStack(os.Remove(profileDir))
474474
}
475475

476-
func (d *Devbox) InstallRunXPackages() error {
477-
for _, pkg := range d.InstallablePackages() {
478-
if pkg.IsRunX() {
479-
// TODO: Once resolve is implemented, we use whatever version is in the lockfile.
480-
if _, err := d.lockfile.Resolve(pkg.Raw); err != nil {
481-
return err
482-
}
483-
_, err := runx.Install(pkg.RunXPath())
484-
if err != nil {
485-
return fmt.Errorf("error installing runx package %s: %w", pkg, err)
486-
}
487-
476+
func (d *Devbox) InstallRunXPackages(ctx context.Context) error {
477+
for _, pkg := range lo.Filter(d.InstallablePackages(), devpkg.IsRunX) {
478+
lockedPkg, err := d.lockfile.Resolve(pkg.Raw)
479+
if err != nil {
480+
return err
481+
}
482+
if _, err := pkgtype.RunXClient().Install(
483+
ctx,
484+
lockedPkg.Resolved,
485+
); err != nil {
486+
return fmt.Errorf("error installing runx package %s: %w", pkg, err)
488487
}
489488
}
490489
return nil

internal/impl/util.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ func (d *Devbox) addDevboxUtilityPackage(ctx context.Context, pkg string) error
3838
// to (e.g. envsec).
3939
// Question: Should we add utilityBinPath here? That would allow user to use
4040
// process-compose, etc
41-
func (d *Devbox) addUtilitiesToPath(path string) (string, error) {
41+
func (d *Devbox) addUtilitiesToPath(
42+
ctx context.Context,
43+
path string,
44+
) (string, error) {
4245
if d.cfg.IsEnvsecEnabled() {
43-
envsecPath, err := envsec.EnsureInstalled()
46+
envsecPath, err := envsec.EnsureInstalled(ctx)
4447
if err != nil {
4548
return "", err
4649
}

0 commit comments

Comments
 (0)