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

file inclusion improvement + docs #4768

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion core/src/graph/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ export const actionFromConfig = profileAsync(async function actionFromConfig({
}
}

const configPath = relative(garden.projectRoot, config.internal.configFilePath || config.internal.basePath)

if (!actionTypes[config.kind][config.type]) {
const configPath = relative(garden.projectRoot, config.internal.configFilePath || config.internal.basePath)
const availableKinds: ActionKind[] = []
actionKinds.forEach((actionKind) => {
if (actionTypes[actionKind][config.type]) {
Expand Down Expand Up @@ -282,6 +283,18 @@ export const actionFromConfig = profileAsync(async function actionFromConfig({

const dependencies = dependenciesFromActionConfig(log, config, configsByKey, definition, templateContext)

if (config.exclude?.includes("**/*")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we have equals here instead of includes, in case of something like src/test/**/*?

Copy link
Contributor

Choose a reason for hiding this comment

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

also, can we also add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

config.exclude is an array of strings. It's checking if the array includes the exact match of "**/*"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Writing tests was a good idea, found a bug in my original logic and removed some unwanted mutation
e4a9dce

if (config.include && config.include.length !== 0) {
throw new ConfigurationError({
message: deline`Action ${config.kind}.${config.name} (defined at ${configPath})
tries to include files but excludes all files via "**/*".
Read about including and excluding files and directories here:
https://docs.garden.io/using-garden/configuration-overview#including-excluding-files-and-directories`,
})
}
config.include = []
}

const treeVersion =
config.internal.treeVersion ||
(await garden.vcs.getTreeVersion({ log, projectName: garden.projectName, config, scanRoot }))
Expand Down
3 changes: 2 additions & 1 deletion core/src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ export class GitHandler extends VcsHandler {
if (!exclude) {
exclude = []
}
exclude.push("**/.garden/**/*")
// Make sure action config is not mutated
exclude = [...exclude, "**/.garden/**/*"]

const gitLog = log
.createLog({})
Expand Down
74 changes: 73 additions & 1 deletion core/test/unit/src/actions/action-configs-to-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
DEFAULT_BUILD_TIMEOUT_SEC,
DEFAULT_DEPLOY_TIMEOUT_SEC,
DEFAULT_RUN_TIMEOUT_SEC,
DEFAULT_TEST_TIMEOUT_SEC
DEFAULT_TEST_TIMEOUT_SEC,
} from "../../../../src/constants"

describe("actionConfigsToGraph", () => {
Expand Down Expand Up @@ -769,4 +769,76 @@ describe("actionConfigsToGraph", () => {
}
)
})

describe("file inclusion-exclusion", () => {
const getBaseParams = ({
include,
exclude,
}: {
include?: string[]
exclude?: string[]
}) => ({
garden,
log,
groupConfigs: [],
configs: [
{
kind: <"Build">"Build",
type: <"test">"test",
name: "foo",
timeout: DEFAULT_BUILD_TIMEOUT_SEC,
internal: {
basePath: tmpDir.path,
},
spec: {},

include,
exclude,
},
],
moduleGraph: new ModuleGraph([], {}),
actionModes: {},
linkedSources: {},
})

it("sets include and exclude", async () => {
const graph = await actionConfigsToGraph({
...getBaseParams({
include: ["include-file"],
exclude: ["exclude-file"],
}),
})
const action = graph.getBuild("foo")

expect(action.getConfig().include).to.eql(["include-file"])
expect(action.getConfig().exclude).to.eql(["exclude-file"])
})

it("sets include to [] if all is excluded", async () => {
const graph = await actionConfigsToGraph({
...getBaseParams({
include: undefined,
exclude: ["**/*", "some-thing-else"],
}),
})
const action = graph.getBuild("foo")

expect(action.getConfig().include).to.eql([])
})

it("throws if everything is excluded but an include is attempted", async () => {
await expectError(
() =>
actionConfigsToGraph({
...getBaseParams({
include: ["some-file"],
exclude: ["**/*"],
}),
}),
{
contains: ['tries to include files but excludes all files via "**/*"'],
}
)
})
})
})
6 changes: 6 additions & 0 deletions docs/using-garden/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ exclude:
...
```

{% hint style="info" %}
Generally, using `.gardenignore` files is far more performant than exclude config statements and will decrease
graph resolution time. Read more about `.gardenignore` files in the
[configuration-overview documentation](./configuration-overview.md#includingexcluding-files-and-directories)
{% endhint %}

Here we only include the `Dockerfile` and all the `.py` files under `my-sources/`, but exclude the `my-sources/tmp`
directory.

Expand Down
5 changes: 3 additions & 2 deletions docs/using-garden/configuration-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ The `scan.exclude` field is also used to limit the number of files and directori

### .ignore file

{% hint style="warning" %}
Prior to Garden 0.12.0, `.gitignore` files were also respected by default. The default is now to only respect `.gardenignore` files. See below how you can revert to the previous behavior.
{% hint style="info" %}
Generally, using .gardenignore files is far more performant than exclude config statements and will decrease
graph resolution time.
{% endhint %}

By default, Garden respects `.gardenignore` files and excludes any patterns matched in those files. You can place the ignore files anywhere in your repository, much like `.gitignore` files, and they will follow the same semantics.
Expand Down