Skip to content

fix(deployments): preserve VCS source roots - #13574

Merged
HarshMN2345 merged 17 commits into
mainfrom
fix/root-directory-normalization
Sep 17, 2026
Merged

HarshMN2345 merged 17 commits into
mainfrom
fix/root-directory-normalization

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Redeploy builds from the wrong directory for functions and sites whose code lives in a subdirectory. Duplicate/Create.php passed the root directory stored on the source deployment, which deployments created from a git push never store, so the build ran against the repository root.

Redeploy now takes the root directory from the function or site, like every other VCS build path and like the entrypoint and build commands it already reads from there. Changing the root directory marks the function as not live, so a redeploy after that change now applies it. Deployments::createFromVcs() also records the root directory on the deployment, so APPWRITE_VCS_ROOT_DIRECTORY is no longer empty for push and redeploy builds.

The Gitea e2e test deploys a function from functions/api, changes the root directory to functions/web, redeploys, and checks that the execution comes from functions/web with that root directory in its environment.

Fixes #13587

… into

`providerRootDirectory` reached the jobs-service through
`trim($subdir, '/')`, which strips slashes but not dots. `docs`, `docs/`
and `/docs` all canonicalized to `docs`, while `./docs` survived verbatim
as an artifact subdir naming a path segment that does not exist in the
extracted tree. `.` and `./` were worse: they collapsed to a non-empty
`.`, so the repository-root case emitted a subdir instead of omitting one.

Canonicalize on the consume side, in `Deployments::rootDirectory()`. Every
remote-source build funnels through `submit()`, so one choke point covers
new writes, rows already holding `./docs`, webhooks, duplicates and
templates without a migration.

Drop whole `.` segments rather than trimming the character. The builds
worker's `ltrim($path, '.')` turned `.github` into `github` — a silent
wrong-directory build — and left `../etc` traversal intact. Both worker
call sites now share the canonicalizer, and the jobs-service hand-off
passes the canonicalized value instead of re-reading the raw attribute,
which also closes a mismatch where the template push committed at a
space-stripped path but sent the space-bearing one.

`..` segments are dropped rather than resolved, so a subdir cannot escape
the tree it indexes into.
The shared deployment document carried every other `provider*` field but
not `providerRootDirectory`, so a push-triggered deployment persisted an
empty root directory. The build itself was correct — the root directory
is passed separately — but duplicating such a deployment reads the value
back off the source document, so a redeploy silently built from the
repository root.

The trait is shared by GitHub, GitLab, Gitea, Bitbucket and Origin.
@greptile-apps

greptile-apps Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the current changes preserve VCS source coordinates and the end-to-end test verifies the intended redeployment behavior.

Fix All in Claude CodeFindings

  1. P2 Legacy fixture mirrors internals ▶
Fix with agent prompt
### Issue 1
tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php:185-211
This legacy-deployment scenario constructs production database, cache, and authorization services, directly updates the deployment record, purges its cache, and then asserts the stored attribute. That violates the repository directive to test observable behavior rather than mirror storage or configuration details, and it makes this regression test brittle against unrelated persistence refactors. This requirement must be satisfied before merging; use a sanctioned fixture or helper to represent a legacy deployment while retaining the duplicate-and-execute assertions.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR preserves the effective VCS source root through deployment creation and uses each function or site’s current source root when redeploying an existing commit.

  • Records providerRootDirectory on VCS-created deployments.
  • Aligns function and site redeploys with their current build configuration.
  • Adds an end-to-end Gitea test proving that changing the root redeploys and executes code from the new subdirectory.

Reviews (14) · Last reviewed commit: "Merge branch 'main' into fix/root-direct..."

Comment thread src/Appwrite/Deployment/Deployments.php Resolved
Dropping `..` segments made `docs/../x` build `docs/x`, which is neither
the directory the caller named nor the one POSIX would resolve — and if
both exist, the wrong application deploys with no warning. Resolving the
segment instead would let the subdir climb out of the tree it indexes
into.

Refuse the path instead. The failure is the owner's to fix, so it joins
the refused-variable-key case in carrying its reason into the build log
rather than a generic internal error.
@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/root-directory-normalization (after).

Metric Before After Change
🚀 Requests/sec 269.73 265.71 ⚪ -1.5%
⏱️ Latency P50 64.41 ms 65.59 ms ⚪ +1.8%
⏱️ Latency P95 152.87 ms 152.26 ms ⚪ -0.4%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 65.59 152.26 16,587 265.71 -0.61
Account 118.5 241.79 873 14.49 +9.4
TablesDB 62.17 118.94 9,021 147.38 +2.13
Storage 61.94 139.35 4,365 72.98 +4.27
Functions 93.89 176.7 2,328 39.41 -14.31

Top API waits (after)

API request Max wait (ms)
functions.create 412.06
account.name.update 365
functions.variables.update 315.94
account.prefs.update 314.88
storage.files.create 308.34

Builds a function from './docs/nested/' — the form the console's
directory picker writes — over the Gitea suite, the only live-VCS suite
CI boots.

Covers both halves observably: the push-created deployment must execute
the nested entrypoint, and duplicating it must reach 'ready'. A duplicate
reads the root directory back off the source deployment, so a regression
that stops persisting it builds from the repository root, where the
auto-init README is the only file and no entrypoint exists.
HarshMN2345 and others added 5 commits September 9, 2026 16:50
…uild

The deployment response model does not expose providerRootDirectory, so
reading it off the body asserted on a key that is never returned.

Duplicating the push-created deployment proves the same thing through
behavior: the duplicate reads the root directory back off the deployment
it copies, so it only reaches 'ready' if the push persisted one. Also
deploy explicitly before pushing, so the nested entrypoint is observed
executing on a deployment the test activated rather than one it assumed
was active.
Refusing `..` added a failure mode where there was none: `trim($subdir, '/')`
has always passed a parent segment through untouched, so `docs/../x` reaches
the extractor today and resolves to the directory it names. Rejecting it
before job submission turns a working deployment into a 400.

Canonicalize the forms that actually diverge — empty and `.` segments — and
leave the rest of the path as given. Dropping `..` instead would silently
build `docs/x`, which is the reason it was singled out in the first place.
@HarshMN2345 HarshMN2345 changed the title fix(deployments): canonicalize the root directory an artifact indexes into fix(deployments): preserve and normalize VCS source roots Sep 14, 2026
Keep main's build timeout plumbing and the Utopia\Command refactor, and
drop the root directory canonicalization this branch had added: the
orchestrator cleans an artifact subdir on every extraction path, and the
sparse-checkout pattern is the vcs adapter's to resolve.
@HarshMN2345 HarshMN2345 changed the title fix(deployments): preserve and normalize VCS source roots fix(deployments): preserve VCS source roots Sep 17, 2026
Comment on lines +185 to +211

\file_put_contents($workdir . '/docs/nested/index.js', "module.exports = async (context) => context.res.send('gitea-nested-v2:' + process.env.APPWRITE_VCS_ROOT_DIRECTORY);\n");
$this->gitHelper('git add docs && git commit -m "Update nested function"', $workdir);
$this->gitHelper('git push origin main', $workdir);

$webhookDeploymentId = $this->waitForNewDeploymentReadyHelper($functionId, $knownIds);
$this->assertEventually(fn () => $this->assertExecutionOutputHelper($functionId, 'gitea-nested-v2:./docs/nested/', $webhookDeploymentId), 30000, 1000);

// Upgrade fixture: deployments created before source roots were saved
// have NULL here. This metadata has no public update endpoint.
$seed = function (?string $root) use ($projectId, $webhookDeploymentId) {
global $register;
$pools = $register->get('pools');
$cache = new Cache(new Sharding(\array_map(
fn (string $name) => new CachePool($pools->get($name)),
Config::getParam('pools-cache', []),
)));
$authorization = new Authorization();
$factory = new Factory($pools, $cache, $authorization);

$authorization->skip(function () use ($factory, $projectId, $webhookDeploymentId, $root) {
$project = $factory->platform()->getDocument('projects', $projectId);
$database = $factory->project($project);
$database->updateDocument('deployments', $webhookDeploymentId, new Document([
'providerRootDirectory' => $root,
]));
$database->purgeCachedDocument('deployments', $webhookDeploymentId);

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.

P2 Legacy fixture mirrors internals

This legacy-deployment scenario constructs production database, cache, and authorization services, directly updates the deployment record, purges its cache, and then asserts the stored attribute. That violates the repository directive to test observable behavior rather than mirror storage or configuration details, and it makes this regression test brittle against unrelated persistence refactors. This requirement must be satisfied before merging; use a sanctioned fixture or helper to represent a legacy deployment while retaining the duplicate-and-execute assertions.

Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php
Line: 185-211

Comment:
**Legacy fixture mirrors internals**

This legacy-deployment scenario constructs production database, cache, and authorization services, directly updates the deployment record, purges its cache, and then asserts the stored attribute. That violates the repository directive to test observable behavior rather than mirror storage or configuration details, and it makes this regression test brittle against unrelated persistence refactors. This requirement must be satisfied before merging; use a sanctioned fixture or helper to represent a legacy deployment while retaining the duplicate-and-execute assertions.

**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That scenario is gone. The test no longer writes to the database; it only uses the public API.

@HarshMN2345
HarshMN2345 merged commit 7ae78b5 into main Sep 17, 2026
49 checks passed
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.

🐛 Bug Report: Redeploy always fails for monorepo functions — duplicate reads providerRootDirectory from the deployment, where nothing ever sets it

2 participants