Skip to content

Support for directory arg filtering via views - #6857

Merged
sipsma merged 10 commits into
dagger:mainfrom
sipsma:views
Mar 19, 2024
Merged

sipsma merged 10 commits into
dagger:mainfrom
sipsma:views

Conversation

@sipsma

@sipsma sipsma commented Mar 8, 2024 •

Copy link
Copy Markdown
Contributor

Fixes #6155

Adds support for views as described in the linked issue to support filtering individual directory args.

This PR also includes new CLI subcommands under dagger config for configuring views and more generally anything described in #6606.

  • However, in the interest of getting the support for views out ASAP those commands are currently hidden and only used in the integ tests.
  • They will be unhidden and doc'd in a follow-up PR, so for now just want to merge this based on the lower-level implementation and assume users will hand-edit dagger.json in the meantime.

Assuming hand-edits of dagger.json to something like this:

{
  "name": "example",
  "sdk": "go",
  "views": [
    {
      "name": "cool-view",
      "patterns": [
        "foo",
        "**/*.go",
        "!bin"
      ]
    }
  ]
}

Then any function in the module can be invoked with the view applied to the directory like:

dagger call some-func --some-dir-arg some/path:cool-view
Details on CLI interface for configuring these

The set/add/remove commands are implemented such that we can easily re-use the pattern for any other config values. I also added support for dagger config include for configuring the pre-existing global include settings in dagger.json so far.

Examples

The -n flag used below is valid on the top level views command and any subcommands, have a mix of examples.

The output has terminal formatting to bold+underline headers that's not carried over through the md below, so it looks a little nicer when you actually run it.

Create a new view named cool-view

$ dagger config views set -n cool-view '**/*.go' '!bin/'
View "cool-view" set to:
!bin
**/*.go

$ dagger config views --json set -n cool-view '**/*.go' '!bin'
[
  "!bin",
  "**/*.go"
]

$ dagger config views set -n other-view foo bar baz
View "other-view" set to:
bar
baz
foo

Use the view

dagger call fn --dir-arg /some/path:cool-view

See the view(s)

$ dagger config views -n cool-view
!bin
**/*.go

$ dagger config views
"cool-view"
!bin
**/*.go

"other-view"
bar
baz
foo

Add more patterns to cool-view

$ dagger config views -n cool-view add '!node_modules/'
View "cool-view" updated to:
!bin
!node_modules/
**/*.go

Remove some existing patterns from cool-view

$ dagger config views -n cool-view remove '!bin'
View "cool-view" updated to:
!node_modules/
**/*.go

Remove cool-view entirely

$ dagger config views remove -n cool-view
View "cool-view" removed

$ dagger config views
"other-view"
bar
baz
foo

@marcosnils

Copy link
Copy Markdown
Contributor

👋 looks very nice, just checking if dagger config views set --name cool-view '**/*.go' '!bin/' will also work as upsert in case the view already exists 🙏

@helderco

Copy link
Copy Markdown
Contributor

How do you feel about using set/add/remove instead of set/append/clear?

And instead of a flag for --name, make it be a positional argument?

dagger config views [name]
dagger config views set <name> <pattern>...
dagger config views add <name> <pattern>...
dagger config views remove <name> [pattern...]

The examples in --help use patterns without single quotes (**/*.txt), so that will expand in the shell first.

Adding an exclude is shown in the description:

dagger config views set --name cool-view '**/*.go' '!bin/'

But that adds it to an "include" key in dagger.json. Either split into include/exclude or rename to "patterns"? The ! can be a CLI convenience, but still store separately.

Finally, I suggest adding a --json option where it makes sense, for example in dagger config.

@gerhard

gerhard commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

Moving this to 0.10.x milestone since this didn't make it in time for 0.10.2.

@gerhard gerhard modified the milestones: v0.10.2, v0.10.x Mar 12, 2024
@sipsma

sipsma commented Mar 12, 2024

Copy link
Copy Markdown
Contributor Author

👋 looks very nice, just checking if dagger config views set --name cool-view '**/*.go' '!bin/' will also work as upsert in case the view already exists 🙏

yep exactly, it will create it if it doesn't exist and overwrite it if it does exist

@sipsma

sipsma commented Mar 12, 2024

Copy link
Copy Markdown
Contributor Author

How do you feel about using set/add/remove instead of set/append/clear?

Yeah SGTM I like those names more

And instead of a flag for --name, make it be a positional argument?

dagger config views [name]
dagger config views set <name> <pattern>...
dagger config views add <name> <pattern>...
dagger config views remove <name> [pattern...]

Sure yeah I like that it's less verbose, there is a little ambiguity in the remove case since it's valid to do dagger config views remove <name> to remove the whole thing in addition to dagger config remove <name> [pattern..] to just remove some patterns from a view, but the cases where a pattern is the same value as a view are obscure enough that we can accept that I think.

The examples in --help use patterns without single quotes (**/*.txt), so that will expand in the shell first.

Good catch, will fix

Adding an exclude is shown in the description:

dagger config views set --name cool-view '**/*.go' '!bin/'

But that adds it to an "include" key in dagger.json. Either split into include/exclude or rename to "patterns"? The ! can be a CLI convenience, but still store separately.

Yeah I think that I'm going to avoid a separate exclude key if possible, so agree that patterns would make more sense at that point. I need to double check this though to confirm that !foo has the precise same effect as putting foo in the exclude key in terms of buildkit's logic. There's just a bunch of subtleties to all this and want to make sure we don't prevent certain use cases.

Finally, I suggest adding a --json option where it makes sense, for example in dagger config.

Also SGTM, adding!

@sipsma

sipsma commented Mar 14, 2024

Copy link
Copy Markdown
Contributor Author

@helderco Been updating based on your suggestions, the one thing I'm backing out on is switching name to be positional. It creates some awkward commands and hard-to-deal-with situations, e.g.

  • dagger config views set foo bar - it's really unobvious that foo is the name of the view and bar is a pattern
  • dagger config views my-view set bar baz - if the user accidentally puts the positional arg of the name of the module before the subcommand, cobra just invokes the top-level dagger config views command. There doesn't appear to be any way of dealing with this: Parameterised subcommand spf13/cobra#1378

So I'm going back to the flag, I think the shorthand flag -n minimizes the pain of extra typing and is worth it for avoiding the above problems.

@sipsma

sipsma commented Mar 14, 2024

Copy link
Copy Markdown
Contributor Author

Updated from feedback so far, just need to add a bunch of tests now. Updated PR description with latest examples cc @shykes

@sipsma

sipsma commented Mar 14, 2024 •

Copy link
Copy Markdown
Contributor Author

Just FYI going to split this PR out to one implementing the low-level changes that support views in dagger.json, keeping this one for CLI support. Goal being to increase the probability of getting at least the low-support in place for the next release since there's likely going to be some bikeshedding required for the CLI part.

EDIT: nevermind, just going to mark the commands as hidden for now and skip docs so we can merge it as is (pending approval) and modify the CLI UX in a follow-up.

@sipsma
sipsma marked this pull request as ready for review March 14, 2024 23:41
@sipsma
sipsma requested review from a team as code owners March 14, 2024 23:41
@sipsma sipsma changed the title WIP support for dagger config views and directory arg filtering Support for directory arg filtering via views Mar 14, 2024
Comment thread cmd/dagger/config.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add the experimental annotation so it's not included when generating the cli reference.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It looks like the cli reference didn't include the hidden subcommands already actually, but added the experimental annotation anyways in case I'm missing something

@helderco helderco Mar 15, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, right. Some of the hidden ones we wanted on the reference, but not others. So we removed the hidden flag and added the experimental in those. In this case, it should be just hidden, sorry about that.

EDIT: Still useful to have the experimental flag in case someone stumbles in the command, it'll show in the --help that it's in development.

@helderco

helderco commented Mar 16, 2024 •

Copy link
Copy Markdown
Contributor

By default every file is included right? Doesn't it make more sense to unify under an "exclude" key, and use "!" for things that shouldn't be excluded? It's also more consistent with .gitgignore and .dockerignore.

Before:

  "include": [
    "!**/.direnv",
    "!**/.envrc",
    "!**/.venv",
    "!**/sdk"
  ],

After:

  "exclude": [
    "**/.direnv",
    "**/.envrc",
    "**/.venv",
    "**/sdk"
  ],

@helderco

Copy link
Copy Markdown
Contributor

I think that even though the include patterns for loading the module are usually to exclude files, I find that for the views it's the opposite. The views I'm creating are primarily to include specific files, and exclude a few from those.

I much prefer the shift to "exclude", in line with git excludes for example, but even though the views are under "patterns", it can be confusing to use the opposite meaning from the ones for loading the module.

@helderco helderco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Works as advertised 🎉

Now I wish I could re-use the views from a dependency! Maybe another : value specifying the dependency, like PATH[:VIEW_NAME[:VIEW_ORIGIN]].

For example, sdk/python module has a few views. And our ci module depends on the python sdk's. Assuming the dependency is called "python":

dagger call python lint --src=docs:lint:python

Otherwise, we'll need to duplicate the patterns.

Could also be called with:

dagger call -m python lint --src=docs:lint

Not sure if that can replace everything since we may want to join everything together at the root (with an SDK interface).

Comment thread core/integration/module_config_test.go Outdated
Comment on lines 1433 to 1447

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are you not using require.JSONEq to validate the contents?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I felt like doing the unmarshal and require this way was a bit more readable since the json blob string in this case would be more complicated than in our other uses of JSONEq

@sipsma

sipsma commented Mar 19, 2024

Copy link
Copy Markdown
Contributor Author

By default every file is included right? Doesn't it make more sense to unify under an "exclude" key, and use "!" for things that shouldn't be excluded? It's also more consistent with .gitgignore and .dockerignore.

Totally agree, but as I was updating this I went down a rabbit hole and realized there's a ton of pre-existing problems with the way this global include/exclude is implemented. Details here #6902.

The consolidation of the global include/exclude was just an extra-credit thing that seemed simple enough to just clean up while I was here, but there's too many deeper issues intertwined so I ended up just backing out of that part of all this (including the dagger config include subcommand), so now this PR is just purely views. Global include/exclude are same as before. Will try to tackle all of that as part of the various follow-ups needed for this PR.

I think that even though the include patterns for loading the module are usually to exclude files, I find that for the views it's the opposite. The views I'm creating are primarily to include specific files, and exclude a few from those.
I much prefer the shift to "exclude", in line with git excludes for example, but even though the views are under "patterns", it can be confusing to use the opposite meaning from the ones for loading the module.

Agree with the general sentiment here, but just to clarify you are all good with the current approach? I think the name views and patterns are suggestive enough that they mean "the files I want to include", but do see how the discrepancy is not ideal.

Now I wish I could re-use the views from a dependency! Maybe another : value specifying the dependency, like PATH[:VIEW_NAME[:VIEW_ORIGIN]].

Totally, that makes sense. I'd need to think about the syntax a bit more but agree we should find a way to add something like this.

@sipsma sipsma modified the milestones: v0.10.x, v0.10.3 Mar 19, 2024
@sipsma

sipsma commented Mar 19, 2024

Copy link
Copy Markdown
Contributor Author

@helderco gonna merge as is, but let me know if there's any concerns, can fixup before the release next week

@sipsma
sipsma merged commit b7284f4 into dagger:main Mar 19, 2024
@helderco

Copy link
Copy Markdown
Contributor

just to clarify you are all good with the current approach? I think the name views and patterns are suggestive enough that they mean "the files I want to include", but do see how the discrepancy is not ideal.

Yep, all good. I agree, on their own, "views"+"patterns" and "exclude" make sense, it's just the discrepancy when they're together (same file), but I don't have a better suggestion. Good decision to keep split "include"+"exclude" for module load
for now. Also good that there's a automatic migration/deprecation path if/when we decide to change this. Some other changes (like not returning Void) don't have that "luxury". 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Need equiv of include / exclude for directories with dagger call CLI

4 participants