Skip to content

[net11] Keep elevated Appium npm install out of the user's npm cache - #40391

Merged
kubaflo merged 1 commit into
net11from
fix/appium-elevated-npm-cache
Nov 18, 2026
Merged

kubaflo merged 1 commit into
net11from
fix/appium-elevated-npm-cache

Conversation

@kubaflo

@kubaflo kubaflo commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Target Branch

This PR is for Initial base
New features, API changes, or .NET 12 work (including .NET 12 fixes) main
.NET 11 servicing fixes net11, for review
Older servicing, release integration, infrastructure, or documentation The branch designated for that work

For .NET 11 fixes, only a maintainer retargets the approved PR to the active
inflight lane and merges it after CI validates that final base. Do not merge
individual fixes directly into net11; agents must not retarget or merge.

Description of Change

CI hardening only. No product, test, or pipeline YAML changes.

Symptom. Candidate UI build 1687815 (#40116), job Controls Lifecycle,ManualReview,Maps on Mac, failed in Install Appium before any test ran. All 5 RetryHelper attempts failed. Log 4334 shows this chain:

  1. npm list -g appium → not installed.
  2. The normal npm i --location=global appium@2.17.1 fails with a transient ERR_SOCKET_TIMEOUT.
  3. InstallAppium falls back to sudo -E npm i --location=global appium@2.17.1, which succeeds.
  4. appium driver install uiautomator2@4.2.1 (runs as the agent user) fails with EACCES: mkdir '/Users/cloudtest/.npm/_cacache/index-v5/…'. npm explains: "Your cache folder contains root-owned files … sudo chown -R 501:20 /Users/cloudtest/.npm".
  5. Retries 2–5 fail on the same root-owned cache (EACCES/EEXIST), then on ENOTEMPTY debris in .appium/node_modules/appium.

Root cause. sudo -E preserves HOME, so the elevated npm uses and populates the user's ~/.npm/_cacache as root. Every later non-root npm call on the agent then fails. A single network blip becomes a job failure that retries cannot fix.

Same signature elsewhere (UI pipeline 313, builds since 11-13):

Build Context Agent / log Notes
1687815 candidate Mac, AcesShared 107, log 4334 This failure.
1686824 PR #40351 Mac, AcesShared 90, log 3817 Identical chain.
1685341 main 13 Linux jobs Network outage; the sudo fallback then hit EACCES on /home/cloudtest/.npm/_cacache.

The next job on AcesShared 107 (log 5398) passed, so the damage is per job, not per machine.

Fix. In src/Provisioning/Provisioning.csproj, the macOS/Linux ElevatedNpmCommand now passes --cache "<temp>/maui-elevated-npm-cache". npm keeps its logs and update-notifier state under the cache directory too, so the elevated install no longer writes anything into ~/.npm. The normal (non-elevated) path, the Windows elevation command, versions, timeouts, and retry budgets are unchanged.

Validation. Real root was unavailable locally, so I emulated it:

  • Node/npm matched CI: Node 20.3.1 / npm 9.6.7, the UseNode version in ui-tests-steps.yml.
  • HOME, APPIUM_HOME, and the npm global prefix were isolated.
  • A sudo shim runs the command, then removes write permission from every path that command created. This matches what a non-root user can do with root-owned 755/644 entries.
  • An npm shim fails the first non-elevated npm i --location=global appium@… with ERR_SOCKET_TIMEOUT.

Each state ran the real target, dotnet build ./src/Provisioning/Provisioning.csproj -t:ProvisionAppium -p:SkipAppiumDoctor=true, repeated up to CI's 5 attempts:

State Result
Baseline (origin/net11) Red 4/4: appium driver install uiautomator2@4.2.1 fails with npm's "cache folder contains root-owned files" EACCES; 3,612 non-writable entries left in ~/.npm.
Fix Green on attempt 1: uiautomator2 4.2.1, mac2 1.20.3, and xcuitest 7.32.0 installed; 0 root-created paths under HOME or APPIUM_HOME.
Fix reverted Red 5/5, same EACCES.
Fix restored Green on attempt 1.
Control (no network failure) Green; the elevated command is not invoked.

In the emulation, the elevated install created nothing under APPIUM_HOME, so isolating the npm cache is enough. Other checks: xmllint --noout on the csproj and git diff --check both pass. Real CI on this PR exercises the normal path; the elevated path only runs after a network failure.

I found no open or recently merged PR that changes the elevated npm install. Provisioning.csproj is byte-identical on net11, main, inflight/current, and candidate/11.0.1xx-sr2.

Issues Fixed

No tracking issue. Found while analysing candidate PR #40116 CI (UI build 1687815).

When the normal global Appium install fails (for example ERR_SOCKET_TIMEOUT),
InstallAppium retries with `sudo -E npm i`. sudo -E preserves HOME, so root npm
writes root-owned entries into the agent user's ~/.npm cache. The following
non-root `appium driver install` then fails with npm's "Your cache folder
contains root-owned files" EACCES on every RetryHelper attempt, turning a
transient network error into a failed UI-test job.

Give the elevated install its own cache under the temp directory so it never
touches ~/.npm.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@kubaflo
kubaflo temporarily deployed to copilot-pat-pool November 18, 2026 16:14 — with GitHub Actions Inactive
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40391

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40391"

@azure-pipelines

This comment has been minimized.

@kubaflo
kubaflo temporarily deployed to copilot-pat-pool November 18, 2026 16:14 — with GitHub Actions Inactive
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026
@kubaflo

kubaflo commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

/rebase

@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@MauiBot

MauiBot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@kubaflo — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026

@github-actions github-actions Bot left a comment

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.

Expert Review — 1 finding

Reviewed commit 79ba029.

See inline comments for details.

<!-- macOS/Linux privilege elevation. sudo -E keeps HOME, so root npm would otherwise leave
root-owned files in the user's ~/.npm cache and every later non-root npm call
(such as 'appium driver install') would fail with EACCES. -->
<_ElevatedNpmCache>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), 'maui-elevated-npm-cache'))</_ElevatedNpmCache>

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.

[moderate] Regression Prevention and Test Coverage - The cache-isolation fix has no checked-in regression test. The existing UI setup never exercises the elevated command when the normal npm install succeeds, so removing --cache can go undetected until an initial install failure causes the fallback to leave root-owned user-cache entries and the subsequent non-root driver install fails with EACCES. Preserve the described isolated reproduction as a lightweight provisioning test: force the initial install to fail, assert the elevated invocation uses a separate cache and subsequent driver installation retains a writable user cache, and include a successful-first-install control that never invokes sudo. No device/UI automation is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I'm intentionally not adding a checked-in test. This change only touches CI provisioning (InstallAppium in Provisioning.csproj), and the policy for this work is to validate pipeline/CI configuration with configuration checks and real CI, not with tests of the configuration.

Validation evidence for the elevated path (isolated local emulation of the InstallAppium sequence, forcing the first install to fail):

  • Baseline (elevated npm shares ~/.npm): red 4/4. A root-owned _cacache was left behind, and the following non-root appium driver install hit EACCES, matching candidate UI 1687815 log 4334.
  • With --cache "$(_ElevatedNpmCache)": green. The user cache stayed user-owned and the driver install succeeded.
  • Fix reverted: red 5/5. Fix restored: green.
  • Control (successful first install): sudo never invoked; green.

The normal (non-elevated) path is exercised by every maui-pr-uitests job on this PR (1688279). I'll confirm Install Appium stays green there.

Auto-replied by the GitHub Copilot app

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026
@MauiBot

MauiBot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@kubaflo — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 18, 2026

@github-actions github-actions Bot left a comment

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.

Expert Review — 2 findings

Reviewed commit 79ba029.

See inline comments for details.

<!-- macOS/Linux privilege elevation. sudo -E keeps HOME, so root npm would otherwise leave
root-owned files in the user's ~/.npm cache and every later non-root npm call
(such as 'appium driver install') would fail with EACCES. -->
<_ElevatedNpmCache>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), 'maui-elevated-npm-cache'))</_ElevatedNpmCache>

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.

[major] Input and Path Correctness - TMPDIR flows through GetTempPath() and Path.Combine into _ElevatedNpmCache, then into the shell-parsed ElevatedNpmCommand/Exec without literal escaping. Double quotes protect spaces but still allow shell expansion: if the selected temporary directory has the literal name /tmp/npm-$USER, a failed normal install reaches this fallback and the shell expands $USER in the cache argument, directing npm outside the selected directory. Pass the cache path as data rather than interpolated command text, or escape it for the Unix shell.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I checked this empirically, and the shell can't change this path while the elevated Exec still runs, so I've left the code unchanged.

On Unix, MSBuild's Exec writes each command to $TMPDIR/MSBuildTemp<random>/tmp<guid>.exec.cmd and runs it as /bin/sh -c ". <path>", with that path unquoted. Every Exec in the target, including the elevated fallback, therefore depends on TMPDIR passing through the shell unchanged before the command runs. An unquoted path goes through every transformation that applies inside double quotes, plus word splitting, so any TMPDIR that would change the double-quoted --cache value already prevents every Exec from starting.

Minimal project. An Exec prints "$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), 'maui-elevated-npm-cache'))", the same expression and quoting as the PR. Results by TMPDIR directory name:

TMPDIR name Result
plain OK
npm-$USER /bin/sh: …/npm-kubaflo/MSBuildTemp…/tmp….exec.cmd: No such file or directory, MSB3073
with space split path, not found, MSB3073
it's unexpected EOF while looking for matching ', MSB3073
back`tick same EOF error, MSB3073
a$ (a $ the shell leaves literal) OK, cache = …/a$/maui-elevated-npm-cache, unchanged

Real ProvisionAppium target on this PR's head (79ba029). I used the local harness: shimmed npm whose first global install fails with ERR_SOCKET_TIMEOUT, and a shimmed sudo that marks new files non-writable to emulate root ownership.

  • TMPDIR=…/npm-$USER/: the build exits 1 at the JsonPeek after npm list (Provisioning.csproj:241), because that Exec's output is the shell error above, not JSON. There were 0 npm invocations: neither the normal nor the elevated install ran, and no cache directory was created anywhere.
  • Control, TMPDIR=…/npm-a$/: build exit 0. The elevated call ran with --cache /tmp/appiumrepro/run-tmpdir-literal-dollar/tmp/npm-a$/maui-elevated-npm-cache, and that directory exists inside the selected TMPDIR. No cache directory was created outside TMPDIR, the user npm cache has 0 non-writable entries, and uiautomator2, mac2 and xcuitest all installed.

So a TMPDIR the shell would alter fails the provisioning build loudly before the fallback, rather than redirecting the root cache, and a TMPDIR the shell keeps literal round-trips exactly. Changing the quoting here would protect against a state the target can't reach. I've left this thread open for the reviewer.

Auto-replied by the GitHub Copilot app

root-owned files in the user's ~/.npm cache and every later non-root npm call
(such as 'appium driver install') would fail with EACCES. -->
<_ElevatedNpmCache>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), 'maui-elevated-npm-cache'))</_ElevatedNpmCache>
<ElevatedNpmCommand>sudo -E npm i --location=global appium@$(AppiumVersion) --cache "$(_ElevatedNpmCache)"</ElevatedNpmCommand>

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.

[moderate] Regression Prevention and Test Coverage - No checked-in regression exercises cache isolation after the initial non-root npm install fails. Successful-first-install provisioning skips this elevated command entirely, so removing --cache can go undetected until the fallback leaves root-owned user-cache entries and subsequent non-root driver installation fails with EACCES. Preserve the described isolated reproduction as a repeatable provisioning test: force the initial install to fail, verify the elevated invocation uses a separate cache and subsequent driver installation retains a writable user cache, and include a successful-first-install control that never invokes sudo. No device/UI automation is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same answer as the earlier thread (4617808542). This PR changes only CI provisioning MSBuild logic, and our policy for this work is not to add checked-in tests for pipeline or provisioning configuration. These changes are validated by real execution and CI instead.

I validated it by running the real ProvisionAppium target locally in a reproducible harness: shimmed npm whose first global install fails with ERR_SOCKET_TIMEOUT, and a shimmed sudo that makes new files non-writable to emulate root ownership. Results:

  • Baseline (fix reverted), red: the elevated install leaves non-writable entries in ~/.npm, and the next driver install fails with EACCES.
  • Fix, green: the elevated install uses the separate temp cache, ~/.npm has 0 non-writable entries, and all three drivers install.
  • Fix reverted again, red. Fix restored, green.
  • Control (no network failure), green.
  • TMPDIR edge cases: covered in the other thread.

The next check is this PR's own CI, where the Mac "Install Appium" step must stay green.

Auto-replied by the GitHub Copilot app

@kubaflo
kubaflo merged commit ef97824 into net11 Nov 18, 2026
72 of 192 checks passed
@kubaflo
kubaflo deleted the fix/appium-elevated-npm-cache branch November 18, 2026 19:36

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 79ba0292 Deployed Nov 18, 2026 by kubaflo via conclusion #3092
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.

2 participants