Skip to content

Allow dagger.json include/exclude patterns to match context directory #8531

Closed as not planned

Description

@helderco

Summary

In a dagger.json's include/exclude patterns, make absolute paths (patterns starting with a /) relative to the module’s context directory (git repo root), while keeping relative paths relative to the dagger.json file.

Motivation

More control in monorepos

The proposed solution for importing code from outside a module in a monorepo is to add dagger.json files to target directories and use them as module dependencies:

However, I think the problem is the potential conflict of multiple modules requiring different patterns from the repo. For example, our own repo is daggerized with "source": ".dagger" at the root:

.
├── .git
├── .dagger
├── dagger.json
├── go.mod
└── go.sum

So if you have a sdk/go/dev module for example, that needs the go.mod file at the repo root, it’ll have to load all of these dependencies: https://github.com/dagger/dagger/blob/7ea7254fea1886831d3e3f211875cb86232a5945/dagger.json

The point is that two different modules could easily require different files from the same directory in the repo, so it’s harder to have fine-grained control using dependencies.

Using dagger.json include patterns that can match against the repo root, would make this much simpler to manage, and with greater control.

More control with .gitignore files

Suppose we move forward with parsing exclude patterns in .gitignore by default:

It’s possible a user may need to have something excluded from a git commit, but still want it locally in a Dagger module. For example, a .env file.

We could use the same technique to leverage module dependencies, and negate the exclude for it (!.env), but it has the same drawbacks too.

Using dagger.json exclude patterns that can match against the repo root, would make it easy to negate a pattern introduced by a .gitignore.

Specification

Allow include/exclude patterns in dagger.json to match against the root of the repo (context directory) if they begin in a slash (/).

Example

Suppose a my-module module in the following git repo needs to share the parent’s go.mod,cmd package, and the .git directory, but also exclude a .direnv file in the root directory:

.
├── .git
├── cmd
│  └── main.go
├── core
│  ├── backend.go
│  ├── consts.go
│  └── services.go
├── my-module
│  ├── .dagger
│  │  ├── internal
│  │  ├── dagger.gen.go
│  │  └── main.go
│  ├── .direnv
│  └── dagger.json
├── go.mod
└── go.sum

Then on my-module/dagger.json add:

{
    "name": "my-module",
    "sdk": "go",
    "source": ".dagger",
    "include": [
        "/go.mod",
        "/go.sum",
        "/cmd/*.go"
    ],
    "exclude": [
        "!/.git/**",
        ".direnv"
    ]
}

Rationale

With the recent Context directory feature, module authors are able to specify patterns for a Directory argument outside of the module’s “root directory” (where dagger.json is):

  • The context directory is the git repository containing the module. If there is not git repository, the context is simply the module.
  • Relative paths are relative to the [root directory]
  • Absolute paths are rooted in the context directory

Right now, the include/exclude patterns in dagger.json match only in relation to the directory with dagger.json, but there’s already a precedent for using an absolute path in order match from the context directory.

Activity

  1. helderco commented on Sep 23, 2024

    @helderco
    ContributorAuthor

    TIL: You can actually reach outside the module's root directory, with a relative path, however, I still think we should adjust the order of patterns when they're added because dagger.json includes are added before anything else, when it should be later in order to allow overrides.

    For example, for a foo/bar module with:

    {
        "name": "foobar",
        "sdk": "go",
        "source": ".dagger",
        "include": [
            "!../../**/vendor/",
            "!**/*",
            "src/"
        ]
    }

    The following include patterns are produced (diff is what I think it should be):

    -!**/vendor/
    -!foo/bar/**/*
    -foo/bar/src/
    -foo/bar/dagger.json
    -foo/bar/**/*
    **/go.mod
    **/go.sum
    **/go.work
    **/go.work.sum
    **/vendor/
    **/*.go
    +foo/bar/**/*
    +!**/vendor/
    +!foo/bar/**/*
    +foo/bar/src/
    +foo/bar/dagger.json

    Note

    The engine joins include patterns with:

    • <module root>/<dagger.json include...>
    • <module root>/dagger.json
    • <module root>/**/*
    • <sdk required paths>

    What I’m suggesting is:

    • <sdk required paths>
    • <module root>/**/*
    • <module root>/<dagger.json include...>
    • <module root>/dagger.json

    The exclude patterns are simpler and already correctly ordered:

    • .git/**
    • <module root>/<dagger.json exclude>...

    I’m aware that you can exclude something that was included, instead of negating the include, I’m just trying to prove a point on the ability to override.

  2. helderco commented on Sep 23, 2024

    @helderco
    ContributorAuthor

    After my last comment, I've broken this into two independent issues:

    The first one is the most important.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions