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

feat: supportAllowWildCard and AllowNotFound #7437

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 28 additions & 2 deletions ci/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,36 @@ type SDK struct {
Elixir *ElixirSDK
// Develop the Dagger Rust SDK (experimental)
Rust *RustSDK
// Develop the Dagger Java SDK (experimental)
Java *JavaSDK
// Develop the Dagger PHP SDK (experimental)
PHP *PHPSDK
// Develop the Dagger Java SDK (experimental)
Java *JavaSDK
}

func (sdk *SDK) All() *AllSDK {
return &AllSDK{
SDK: sdk,
}
}

type sdkBase interface {
Lint(ctx context.Context) error
Test(ctx context.Context) error
Generate(ctx context.Context) (*Directory, error)
Bump(ctx context.Context, version string) (*Directory, error)
}

func (sdk *SDK) allSDKs() []sdkBase {
return []sdkBase{
sdk.Go,
sdk.Python,
sdk.Typescript,
sdk.Elixir,
sdk.Rust,
sdk.PHP,
// java isn't properly integrated to our release process yet
// sdk.Java,
}
}

func (ci *Dagger) installer(ctx context.Context, name string) (func(*Container) *Container, error) {
Expand Down
95 changes: 95 additions & 0 deletions ci/sdk_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"context"

"golang.org/x/sync/errgroup"
)

type AllSDK struct {
SDK *SDK // +private
}

var _ sdkBase = AllSDK{}

func (t AllSDK) Lint(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
for _, sdk := range t.SDK.allSDKs() {
sdk := sdk
eg.Go(func() error {
return sdk.Lint(ctx)
})
}
return eg.Wait()
}

func (t AllSDK) Test(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
for _, sdk := range t.SDK.allSDKs() {
sdk := sdk
eg.Go(func() error {
return sdk.Test(ctx)
})
}
return eg.Wait()
}

func (t AllSDK) Generate(ctx context.Context) (*Directory, error) {
eg, ctx := errgroup.WithContext(ctx)
dirs := make([]*Directory, len(t.SDK.allSDKs()))
for i, sdk := range t.SDK.allSDKs() {
i, sdk := i, sdk
eg.Go(func() error {
dir, err := sdk.Generate(ctx)
if err != nil {
return err
}
dir, err = dir.Sync(ctx)
if err != nil {
return err
}
dirs[i] = dir
return nil
})
}
err := eg.Wait()
if err != nil {
return nil, err
}

dir := dag.Directory()
for _, dir2 := range dirs {
dir = dir.WithDirectory("", dir2)
}
return dir, nil
}

func (t AllSDK) Bump(ctx context.Context, version string) (*Directory, error) {
eg, ctx := errgroup.WithContext(ctx)
dirs := make([]*Directory, len(t.SDK.allSDKs()))
for i, sdk := range t.SDK.allSDKs() {
i, sdk := i, sdk
eg.Go(func() error {
dir, err := sdk.Bump(ctx, version)
if err != nil {
return err
}
dir, err = dir.Sync(ctx)
if err != nil {
return err
}
dirs[i] = dir
return nil
})
}
err := eg.Wait()
if err != nil {
return nil, err
}

dir := dag.Directory()
for _, dir2 := range dirs {
dir = dir.WithDirectory("", dir2)
}
return dir, nil
}
2 changes: 1 addition & 1 deletion ci/sdk_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (t GoSDK) Publish(
}

// Bump the Go SDK's Engine dependency
func (t GoSDK) Bump(version string) (*Directory, error) {
func (t GoSDK) Bump(ctx context.Context, version string) (*Directory, error) {
// trim leading v from version
version = strings.TrimPrefix(version, "v")

Expand Down
2 changes: 1 addition & 1 deletion ci/sdk_php.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (t PHPSDK) Publish(
}

// Bump the PHP SDK's Engine dependency
func (t PHPSDK) Bump(version string) (*Directory, error) {
func (t PHPSDK) Bump(ctx context.Context, version string) (*Directory, error) {
version = strings.TrimPrefix(version, "v")
content := fmt.Sprintf(`<?php /* Code generated by dagger. DO NOT EDIT. */ return '%s';
`, version)
Expand Down
2 changes: 1 addition & 1 deletion ci/sdk_python.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (t PythonSDK) Publish(
}

// Bump the Python SDK's Engine dependency
func (t PythonSDK) Bump(version string) (*dagger.Directory, error) {
func (t PythonSDK) Bump(ctx context.Context, version string) (*dagger.Directory, error) {
// trim leading v from version
version = strings.TrimPrefix(version, "v")
engineReference := fmt.Sprintf("# Code generated by dagger. DO NOT EDIT.\n\nCLI_VERSION = %q\n", version)
Expand Down
2 changes: 1 addition & 1 deletion ci/sdk_typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ always-auth=true`, plaintext)
}

// Bump the Typescript SDK's Engine dependency
func (t TypescriptSDK) Bump(version string) (*Directory, error) {
func (t TypescriptSDK) Bump(ctx context.Context, version string) (*Directory, error) {
// trim leading v from version
version = strings.TrimPrefix(version, "v")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@

{{- with $optionals }}
{{- if $required }}, {{ end }}
{{- "" }}...opts{{- if gt (len $enums) 0 -}}, __metadata: metadata{{- end -}}
{{- "" }}...opts
{{- end }}
{{- if gt (len $enums) 0 -}}, __metadata: metadata{{- end -}}
{{- "" }} },
{{- end }}
},
Expand Down
2 changes: 1 addition & 1 deletion core/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ func (container *Container) WithoutPath(ctx context.Context, destPath string) (*
container = container.Clone()

return container.writeToPath(ctx, path.Dir(destPath), func(dir *Directory) (*Directory, error) {
return dir.Without(ctx, path.Base(destPath))
return dir.Without(ctx, path.Base(destPath), true, true)
})
}

Expand Down
9 changes: 7 additions & 2 deletions core/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,16 +677,21 @@ func (dir *Directory) Diff(ctx context.Context, other *Directory) (*Directory, e
return dir, nil
}

func (dir *Directory) Without(ctx context.Context, path string) (*Directory, error) {
func (dir *Directory) Without(ctx context.Context, path string, allowWildCard bool, allowNotFound bool) (*Directory, error) {
dir = dir.Clone()

st, err := dir.State()
if err != nil {
return nil, err
}

fmt.Println("path", path)
fmt.Println("allowNotfound", allowNotFound)
fmt.Println("allowWildCard", allowWildCard)


path = filepath.Join(dir.Dir, path)
err = dir.SetState(ctx, st.File(llb.Rm(path, llb.WithAllowWildcard(true), llb.WithAllowNotFound(true))))
Copy link
Member

@jedevc jedevc May 22, 2024

Choose a reason for hiding this comment

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

Huh. I didn't know we defaulted to this.

This feels slightly inconsistent with other stuff - see another discussion on magic processing in #7173. A bit strange that some paths can be globs/wildcards/expansions and some aren't.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes definitely, I think that's why we wanted to add the parameter.

err = dir.SetState(ctx, st.File(llb.Rm(path, llb.WithAllowWildcard(allowWildCard), llb.WithAllowNotFound(allowNotFound))))
if err != nil {
return nil, err
}
Expand Down
13 changes: 13 additions & 0 deletions core/integration/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,13 @@
require.NoError(t, err)
require.Equal(t, []string{"some-dir", "some-file"}, entries)

// Test without dir with allow not found = false
_, err = dir1.
WithoutDirectory("non-existant", dagger.DirectoryWithoutDirectoryOpts{AllowNotFound: false}).

Check failure on line 526 in core/integration/directory_test.go

View workflow job for this annotation

GitHub Actions / lint / dagger-runner-v2

`existant` is a misspelling of `existent` (misspell)

Check failure on line 526 in core/integration/directory_test.go

View workflow job for this annotation

GitHub Actions / lint / dagger-runner-v2

`existant` is a misspelling of `existent` (misspell)
Copy link
Member

Choose a reason for hiding this comment

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

@TomChv this is your issue.

In go, the empty struct defaults to false - therefore, we have the API constraint today that all boolean options must default to false.

What you need is to invert the option, to something like MustExist: true, so that the default is false.

Entries(ctx)

require.Error(t, err)

dir := c.Directory().
WithNewFile("foo.txt", "foo").
WithNewFile("a/bar.txt", "bar").
Expand Down Expand Up @@ -550,6 +557,12 @@
require.NoError(t, err)
require.Equal(t, []string{"data.json"}, entries)

_, err = dir.
WithoutDirectory("b/*.txt", dagger.DirectoryWithoutDirectoryOpts{AllowWildCard: false}).
Entries(ctx)

require.Error(t, err)

entries, err = dir.
WithoutFile("c/*a1*").
Entries(ctx, dagger.DirectoryEntriesOpts{Path: "c"})
Expand Down
27 changes: 23 additions & 4 deletions core/integration/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,18 @@ func TestServiceStartStopKill(t *testing.T) {
require.NoError(t, err)
require.Empty(t, out)

// attempt to SIGABRT the service
_, err = httpSrv.Signal(ctx, dagger.Sigabrt)
require.NoError(t, err)

// ensures that the subprocess gets the above signals
require.Eventually(t, func() bool {
out, err = fetch()
require.NoError(t, err)
fmt.Println(out)
return strings.Contains(out, "Aborted\n")
}, time.Minute, time.Second)

eg := errgroup.Group{}
eg.Go(func() error {
// attempt to SIGTERM the service
Expand All @@ -1285,16 +1297,23 @@ func TestServiceStartStopKill(t *testing.T) {
_, err := httpSrv.Stop(ctx)
return err
})
eg.Go(func() error {
// attempt to SIGINT the service (same as above)
_, err := httpSrv.Stop(ctx, dagger.ServiceStopOpts{
Signal: dagger.Sigint,
})
return err
})

// ensures that the subprocess gets SIGTERM
// ensures that the subprocess gets the above signals
require.Eventually(t, func() bool {
out, err = fetch()
require.NoError(t, err)
return out == "Terminated\n"
return strings.Contains(out, "Terminated\n") && strings.Contains(out, "Interrupt\n")
}, time.Minute, time.Second)

eg.Go(func() error {
// attempt to SIGKILL the serive (this will work)
// attempt to SIGKILL the service (this will work)
_, err := httpSrv.Stop(ctx, dagger.ServiceStopOpts{Kill: true})
return err
})
Expand Down Expand Up @@ -1832,7 +1851,7 @@ import time
def print_signal(signum, frame):
with open("./signals.txt", "a+") as f:
print(signal.strsignal(signum), file=f)
for sig in [signal.SIGINT, signal.SIGTERM]:
for sig in [signal.SIGINT, signal.SIGTERM, signal.SIGABRT]:
signal.signal(sig, print_signal)

with socketserver.TCPServer(("", 8000), http.server.SimpleHTTPRequestHandler) as httpd:
Expand Down
16 changes: 12 additions & 4 deletions core/schema/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func (s *directorySchema) Install() {
ArgDoc("permissions", `Permission given to the copied file (e.g., 0600).`),
dagql.Func("withoutFile", s.withoutFile).
Doc(`Retrieves this directory with the file at the given path removed.`).
ArgDoc("path", `Location of the file to remove (e.g., "/file.txt").`),
ArgDoc("path", `Location of the file to remove (e.g., "/file.txt").`).
ArgDoc("allowWildCard", `Allow wildcards in the path (e.g., "*.txt").`).
ArgDoc("allowNotFound", `Allow the operation to not fail if the directory does not exist.`),
Comment on lines +58 to +60
Copy link
Member

@jedevc jedevc May 22, 2024

Choose a reason for hiding this comment

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

FYI - we would also want this on container. Side note (and not sure how to resolve this), it feels weird that we have so much repetition with the 4 methods here - Container/File.withoutFile/withoutDirectory.

dagql.Func("directory", s.subdirectory).
Doc(`Retrieves a directory at the given path.`).
ArgDoc("path", `Location of the directory to retrieve (e.g., "/src").`),
Expand All @@ -71,7 +73,9 @@ func (s *directorySchema) Install() {
ArgDoc("permissions", `Permission granted to the created directory (e.g., 0777).`),
dagql.Func("withoutDirectory", s.withoutDirectory).
Doc(`Retrieves this directory with the directory at the given path removed.`).
ArgDoc("path", `Location of the directory to remove (e.g., ".github/").`),
ArgDoc("path", `Location of the directory to remove (e.g., ".github/").`).
ArgDoc("allowWildCard", `Allow wildcards in the path (e.g., "*.txt").`).
ArgDoc("allowNotFound", `Allow the operation to not fail if the directory does not exist.`),
dagql.Func("diff", s.diff).
Doc(`Gets the difference between this directory and an another directory.`).
ArgDoc("other", `Identifier of the directory to compare.`),
Expand Down Expand Up @@ -233,18 +237,22 @@ func (s *directorySchema) withFiles(ctx context.Context, parent *core.Directory,

type withoutDirectoryArgs struct {
Path string
AllowWildCard bool `default:"true"`
AllowNotFound bool `default:"true"`
}

func (s *directorySchema) withoutDirectory(ctx context.Context, parent *core.Directory, args withoutDirectoryArgs) (*core.Directory, error) {
return parent.Without(ctx, args.Path)
return parent.Without(ctx, args.Path, args.AllowWildCard, args.AllowNotFound)
}

type withoutFileArgs struct {
Path string
AllowWildCard bool `default:"true"`
AllowNotFound bool `default:"true"`
}

func (s *directorySchema) withoutFile(ctx context.Context, parent *core.Directory, args withoutFileArgs) (*core.Directory, error) {
return parent.Without(ctx, args.Path)
return parent.Without(ctx, args.Path, args.AllowWildCard, args.AllowNotFound)
}

type diffArgs struct {
Expand Down
1 change: 1 addition & 0 deletions core/schema/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (s *querySchema) Install() {
core.CacheSharingModes.Install(s.srv)
core.TypeDefKinds.Install(s.srv)
core.ModuleSourceKindEnum.Install(s.srv)
core.SignalTypesEnum.Install(s.srv)

dagql.MustInputSpec(PipelineLabel{}).Install(s.srv)
dagql.MustInputSpec(core.PortForward{}).Install(s.srv)
Expand Down