Skip to content

Commit

Permalink
fix(build): remove deleted files/dirs during sync
Browse files Browse the repository at this point in the history
Before this fix, removing non-ignored files or folders from a module
during the execution of a watch-mode command would not result in those
files/folders being removed from the module's build directory.

This was fixed by adding the --delete option to the rsync call used by
BuildDir, and moving the .garden-version file from the module's build
directory to a new subdirectory of .garden,
build-metadata/[module-name].

In general, the build-metadata directory can be used by plugins to
persistently store build-related metadata.
  • Loading branch information
thsig committed Mar 15, 2019
1 parent f162e26 commit 9630192
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
19 changes: 16 additions & 3 deletions garden-service/src/build-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ import { ModuleConfig } from "./config/module"
// Lazily construct a directory of modules inside which all build steps are performed.

const buildDirRelPath = join(GARDEN_DIR_NAME, "build")
const buildMetadataDirRelPath = join(GARDEN_DIR_NAME, "build-metadata")

export class BuildDir {
constructor(private projectRoot: string, public buildDirPath: string) { }
constructor(private projectRoot: string, public buildDirPath: string, public buildMetadataDirPath) { }

static async factory(projectRoot: string) {
const buildDirPath = join(projectRoot, buildDirRelPath)
const buildMetadataDirPath = join(projectRoot, buildMetadataDirRelPath)
await ensureDir(buildDirPath)
return new BuildDir(projectRoot, buildDirPath)
await ensureDir(buildMetadataDirPath)
return new BuildDir(projectRoot, buildDirPath, buildMetadataDirPath)
}

async syncFromSrc(module: ModuleConfig) {
Expand Down Expand Up @@ -95,6 +98,16 @@ export class BuildDir {
return path
}

/**
* This directory can be used to store build-related metadata for a given module, for example the last built
* version for exec modules.
*/
async buildMetadataPath(moduleName: string): Promise<string> {
const path = resolve(this.buildMetadataDirPath, moduleName)
await ensureDir(path)
return path
}

private async sync(sourcePath: string, destinationPath: string): Promise<void> {
const destinationDir = parse(destinationPath).dir
await ensureDir(destinationDir)
Expand All @@ -110,7 +123,7 @@ export class BuildDir {
destinationPath = stripWildcard(destinationPath)

// --exclude is required for modules where the module and project are in the same directory
await execa("rsync", ["-rptgo", `--exclude=${GARDEN_DIR_NAME}`, sourcePath, destinationPath])
await execa("rsync", ["-rptgo", "--delete", `--exclude=${GARDEN_DIR_NAME}`, sourcePath, destinationPath])
}
}

Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const LOGS_DIR = join(GARDEN_DIR_NAME, "logs")
export const ERROR_LOG_FILENAME = "error.log"
export const PROJECT_SOURCES_DIR_NAME = join(GARDEN_DIR_NAME, "sources", "project")
export const MODULE_SOURCES_DIR_NAME = join(GARDEN_DIR_NAME, "sources", "module")
export const GARDEN_BUILD_VERSION_FILENAME = ".garden-build-version"
export const GARDEN_BUILD_VERSION_FILENAME = "garden-build-version"
export const GARDEN_VERSIONFILE_NAME = ".garden-version"
export const DEFAULT_NAMESPACE = "default"
export const DEFAULT_PORT_PROTOCOL = "TCP"
Expand Down
4 changes: 2 additions & 2 deletions garden-service/src/plugins/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function configureExecModule(
}

export async function getExecModuleBuildStatus({ module }: GetBuildStatusParams): Promise<BuildStatus> {
const buildVersionFilePath = join(module.buildPath, GARDEN_BUILD_VERSION_FILENAME)
const buildVersionFilePath = join(module.buildMetadataPath, GARDEN_BUILD_VERSION_FILENAME)
let builtVersion: ModuleVersion | null = null

try {
Expand Down Expand Up @@ -162,7 +162,7 @@ export async function buildExecModule({ module }: BuildModuleParams<ExecModule>)
}

// keep track of which version has been built
const buildVersionFilePath = join(buildPath, GARDEN_BUILD_VERSION_FILENAME)
const buildVersionFilePath = join(module.buildMetadataPath, GARDEN_BUILD_VERSION_FILENAME)
await writeModuleVersionFile(buildVersionFilePath, module.version)

return output
Expand Down
6 changes: 6 additions & 0 deletions garden-service/src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface Module<
W extends TaskSpec = any,
> extends ModuleConfig<M, S, T, W> {
buildPath: string
buildMetadataPath: string
version: ModuleVersion

buildDependencies: ModuleMap
Expand All @@ -51,6 +52,10 @@ export const moduleSchema = moduleConfigSchema
.required()
.uri(<any>{ relativeOnly: true })
.description("The path to the build staging directory for the module."),
buildMetadataPath: Joi.string()
.required()
.uri(<any>{ relativeOnly: true })
.description("The path to the build metadata directory for the module."),
version: moduleVersionSchema
.required(),
buildDependencies: joiIdentifierMap(Joi.lazy(() => moduleSchema))
Expand Down Expand Up @@ -83,6 +88,7 @@ export async function moduleFromConfig(garden: Garden, graph: ConfigGraph, confi
...cloneDeep(config),

buildPath: await garden.buildDir.buildPath(config.name),
buildMetadataPath: await garden.buildDir.buildMetadataPath(config.name),
version: await garden.resolveVersion(config.name, config.build.dependencies),

buildDependencies: {},
Expand Down
8 changes: 4 additions & 4 deletions garden-service/test/unit/src/plugins/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ describe("exec plugin", () => {
it("should read a build version file if it exists", async () => {
const module = await graph.getModule(moduleName)
const version = module.version
const buildPath = module.buildPath
const versionFilePath = join(buildPath, GARDEN_BUILD_VERSION_FILENAME)
const buildMetadataPath = module.buildMetadataPath
const versionFilePath = join(buildMetadataPath, GARDEN_BUILD_VERSION_FILENAME)

await writeModuleVersionFile(versionFilePath, version)

Expand All @@ -155,8 +155,8 @@ describe("exec plugin", () => {
it("should write a build version file after building", async () => {
const module = await graph.getModule(moduleName)
const version = module.version
const buildPath = module.buildPath
const versionFilePath = join(buildPath, GARDEN_BUILD_VERSION_FILENAME)
const buildMetadataPath = module.buildMetadataPath
const versionFilePath = join(buildMetadataPath, GARDEN_BUILD_VERSION_FILENAME)

await garden.actions.build({ log, module })

Expand Down

0 comments on commit 9630192

Please sign in to comment.