Skip to content

Commit

Permalink
fix(config): throw error if multiple project configs are found
Browse files Browse the repository at this point in the history
throw error if multiple project configs are found across multiple
files in a dir (e.g. in garden.yml and project.garden.yml)
  • Loading branch information
shumailxyz committed Jul 25, 2023
1 parent d097a66 commit 86bb66f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
20 changes: 17 additions & 3 deletions core/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,26 +457,40 @@ export async function findProjectConfig({
}): Promise<ProjectResource | undefined> {
let sepCount = path.split(sep).length - 1

let allProjectSpecs: GardenResource[] = []

for (let i = 0; i < sepCount; i++) {
const configFiles = (await listDirectory(path, { recursive: false })).filter(isConfigFilename)

for (const configFile of configFiles) {
const resources = await loadConfigResources(log, path, join(path, configFile), allowInvalid)

const projectSpecs = resources.filter((s) => s.kind === "Project")
let projectSpecs = resources.filter((s) => s.kind === "Project")

if (projectSpecs.length > 1 && !allowInvalid) {
throw new ConfigurationError({
message: `Multiple project declarations found in ${path}`,
message: `Multiple project declarations found in ${path}/${configFile}`,
detail: {
projectSpecs,
},
})
} else if (projectSpecs.length > 0) {
return <ProjectResource>projectSpecs[0]
allProjectSpecs = allProjectSpecs.concat(projectSpecs)
}
}

if (allProjectSpecs.length > 1 && !allowInvalid) {
const configPaths = allProjectSpecs.map((i) => `- ${(i as ProjectConfig).configPath}`)
throw new ConfigurationError({
message: `Multiple project declarations found at paths:\n${configPaths.join("\n")}`,
detail: {
allProjectSpecs,
},
})
} else if (allProjectSpecs.length === 1) {
return <ProjectResource>allProjectSpecs[0]
}

if (!scan) {
break
}
Expand Down
13 changes: 13 additions & 0 deletions core/test/data/test-project-multiple-project-configs/garden.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: garden.io/v1
kind: Project
name: another-test-project
environments:
- name: local
- name: other
providers:
- name: test-plugin
environments: [local]
- name: test-plugin-b
environments: [local]
variables:
some: variable
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: garden.io/v1
kind: Project
name: test-project
environments:
- name: local
- name: other
providers:
- name: test-plugin
environments: [local]
- name: test-plugin-b
environments: [local]
variables:
some: variable
11 changes: 9 additions & 2 deletions core/test/unit/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const projectPathMultipleModules = getDataDir("test-projects", "multiple-module-
const modulePathAMultiple = resolve(projectPathMultipleModules, "module-a")

const projectPathDuplicateProjects = getDataDir("test-project-duplicate-project-config")
const projectPathMultipleProjects = getDataDir("test-project-multiple-project-configs")
const logger = getRootLogger()
const log = logger.createLog()

Expand Down Expand Up @@ -580,9 +581,15 @@ describe("findProjectConfig", async () => {
expect(project && project.path).to.eq(customConfigPath)
})

it("should throw an error if multiple projects are found", async () => {
it("should throw an error if multiple projects are found in same config file", async () => {
await expectError(async () => await findProjectConfig({ log, path: projectPathDuplicateProjects }), {
contains: "Multiple project declarations found",
contains: "Multiple project declarations found in",
})
})

it("should throw an error if multiple projects are found in multiple config files", async () => {
await expectError(async () => await findProjectConfig({ log, path: projectPathMultipleProjects }), {
contains: "Multiple project declarations found at paths",
})
})
})

0 comments on commit 86bb66f

Please sign in to comment.